* powerpc.cc (Powerpc_relocate_functions): Upcase enum values,
[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
63 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
64 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
65 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
66 special_(0), opd_ent_shndx_(), opd_ent_off_()
67 { }
68
69 ~Powerpc_relobj()
70 { }
71
72 // The .got2 section shndx.
73 unsigned int
74 got2_shndx() const
75 {
76 if (size == 32)
77 return this->special_;
78 else
79 return 0;
80 }
81
82 // The .opd section shndx.
83 unsigned int
84 opd_shndx() const
85 {
86 if (size == 32)
87 return 0;
88 else
89 return this->special_;
90 }
91
92 // Init OPD entry arrays.
93 void
94 init_opd(size_t opd_size)
95 {
96 size_t count = this->opd_ent_ndx(opd_size);
97 this->opd_ent_shndx_.resize(count);
98 this->opd_ent_off_.reserve(count);
99 }
100
101 // Return section and offset of function entry for .opd + R_OFF.
102 void
103 get_opd_ent(Address r_off, unsigned int* shndx, Address* value) const
104 {
105 size_t ndx = this->opd_ent_ndx(r_off);
106 gold_assert(ndx < this->opd_ent_shndx_.size());
107 gold_assert(this->opd_ent_shndx_[ndx] != 0);
108 *shndx = this->opd_ent_shndx_[ndx];
109 *value = this->opd_ent_off_[ndx];
110 }
111
112 // Set section and offset of function entry for .opd + R_OFF.
113 void
114 set_opd_ent(Address r_off, unsigned int shndx, Address value)
115 {
116 size_t ndx = this->opd_ent_ndx(r_off);
117 gold_assert(ndx < this->opd_ent_shndx_.size());
118 this->opd_ent_shndx_[ndx] = shndx;
119 this->opd_ent_off_[ndx] = value;
120 }
121
122 // Examine .rela.opd to build info about function entry points.
123 void
124 scan_opd_relocs(size_t reloc_count,
125 const unsigned char* prelocs,
126 const unsigned char* plocal_syms);
127
128 void
129 do_read_relocs(Read_relocs_data*);
130
131 bool
132 do_find_special_sections(Read_symbols_data* sd);
133
134 // Return offset in output GOT section that this object will use
135 // as a TOC pointer. Won't be just a constant with multi-toc support.
136 Address
137 toc_base_offset() const
138 { return 0x8000; }
139
140 private:
141 // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
142 // at OFF. .opd entries are 24 bytes long, but they can be spaced
143 // 16 bytes apart when the language doesn't use the last 8-byte
144 // word, the environment pointer. Thus dividing the entry section
145 // offset by 16 will give an index into opd_ent_shndx_ and
146 // opd_ent_off_ that works for either layout of .opd. (It leaves
147 // some elements of the vectors unused when .opd entries are spaced
148 // 24 bytes apart, but we don't know the spacing until relocations
149 // are processed, and in any case it is possible for an object to
150 // have some entries spaced 16 bytes apart and others 24 bytes apart.)
151 size_t
152 opd_ent_ndx(size_t off) const
153 { return off >> 4;}
154
155 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
156 unsigned int special_;
157 // The first 8-byte word of an OPD entry gives the address of the
158 // entry point of the function. Relocatable object files have a
159 // relocation on this word. The following two vectors record the
160 // section and offset specified by these relocations.
161 std::vector<unsigned int> opd_ent_shndx_;
162 std::vector<Offset> opd_ent_off_;
163 };
164
165 template<int size, bool big_endian>
166 class Target_powerpc : public Sized_target<size, big_endian>
167 {
168 public:
169 typedef
170 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
171 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
172 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
173 static const Address invalid_address = static_cast<Address>(0) - 1;
174 // Offset of tp and dtp pointers from start of TLS block.
175 static const Address tp_offset = 0x7000;
176 static const Address dtp_offset = 0x8000;
177
178 Target_powerpc()
179 : Sized_target<size, big_endian>(&powerpc_info),
180 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
181 copy_relocs_(elfcpp::R_POWERPC_COPY),
182 dynbss_(NULL), tlsld_got_offset_(-1U)
183 {
184 }
185
186 // Process the relocations to determine unreferenced sections for
187 // garbage collection.
188 void
189 gc_process_relocs(Symbol_table* symtab,
190 Layout* layout,
191 Sized_relobj_file<size, big_endian>* object,
192 unsigned int data_shndx,
193 unsigned int sh_type,
194 const unsigned char* prelocs,
195 size_t reloc_count,
196 Output_section* output_section,
197 bool needs_special_offset_handling,
198 size_t local_symbol_count,
199 const unsigned char* plocal_symbols);
200
201 // Scan the relocations to look for symbol adjustments.
202 void
203 scan_relocs(Symbol_table* symtab,
204 Layout* layout,
205 Sized_relobj_file<size, big_endian>* object,
206 unsigned int data_shndx,
207 unsigned int sh_type,
208 const unsigned char* prelocs,
209 size_t reloc_count,
210 Output_section* output_section,
211 bool needs_special_offset_handling,
212 size_t local_symbol_count,
213 const unsigned char* plocal_symbols);
214
215 // Map input .toc section to output .got section.
216 const char*
217 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
218 {
219 if (size == 64 && strcmp(name, ".toc") == 0)
220 {
221 *plen = 4;
222 return ".got";
223 }
224 return NULL;
225 }
226
227 // Finalize the sections.
228 void
229 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
230
231 // Return the value to use for a dynamic which requires special
232 // treatment.
233 uint64_t
234 do_dynsym_value(const Symbol*) const;
235
236 // Relocate a section.
237 void
238 relocate_section(const Relocate_info<size, big_endian>*,
239 unsigned int sh_type,
240 const unsigned char* prelocs,
241 size_t reloc_count,
242 Output_section* output_section,
243 bool needs_special_offset_handling,
244 unsigned char* view,
245 Address view_address,
246 section_size_type view_size,
247 const Reloc_symbol_changes*);
248
249 // Scan the relocs during a relocatable link.
250 void
251 scan_relocatable_relocs(Symbol_table* symtab,
252 Layout* layout,
253 Sized_relobj_file<size, big_endian>* object,
254 unsigned int data_shndx,
255 unsigned int sh_type,
256 const unsigned char* prelocs,
257 size_t reloc_count,
258 Output_section* output_section,
259 bool needs_special_offset_handling,
260 size_t local_symbol_count,
261 const unsigned char* plocal_symbols,
262 Relocatable_relocs*);
263
264 // Emit relocations for a section.
265 void
266 relocate_relocs(const Relocate_info<size, big_endian>*,
267 unsigned int sh_type,
268 const unsigned char* prelocs,
269 size_t reloc_count,
270 Output_section* output_section,
271 off_t offset_in_output_section,
272 const Relocatable_relocs*,
273 unsigned char*,
274 Address view_address,
275 section_size_type,
276 unsigned char* reloc_view,
277 section_size_type reloc_view_size);
278
279 // Return whether SYM is defined by the ABI.
280 bool
281 do_is_defined_by_abi(const Symbol* sym) const
282 {
283 return strcmp(sym->name(), "__tls_get_addr") == 0;
284 }
285
286 // Return the size of the GOT section.
287 section_size_type
288 got_size() const
289 {
290 gold_assert(this->got_ != NULL);
291 return this->got_->data_size();
292 }
293
294 // Get the PLT section.
295 const Output_data_plt_powerpc<size, big_endian>*
296 plt_section() const
297 {
298 gold_assert(this->plt_ != NULL);
299 return this->plt_;
300 }
301
302 // Get the .glink section.
303 const Output_data_glink<size, big_endian>*
304 glink_section() const
305 {
306 gold_assert(this->glink_ != NULL);
307 return this->glink_;
308 }
309
310 // Get the GOT section.
311 const Output_data_got_powerpc<size, big_endian>*
312 got_section() const
313 {
314 gold_assert(this->got_ != NULL);
315 return this->got_;
316 }
317
318 Object*
319 do_make_elf_object(const std::string&, Input_file*, off_t,
320 const elfcpp::Ehdr<size, big_endian>&);
321
322 // Return the number of entries in the GOT.
323 unsigned int
324 got_entry_count() const
325 {
326 if (this->got_ == NULL)
327 return 0;
328 return this->got_size() / (size / 8);
329 }
330
331 // Return the number of entries in the PLT.
332 unsigned int
333 plt_entry_count() const;
334
335 // Return the offset of the first non-reserved PLT entry.
336 unsigned int
337 first_plt_entry_offset() const;
338
339 // Return the size of each PLT entry.
340 unsigned int
341 plt_entry_size() const;
342
343 private:
344
345 // The class which scans relocations.
346 class Scan
347 {
348 public:
349 Scan()
350 : issued_non_pic_error_(false)
351 { }
352
353 static inline int
354 get_reference_flags(unsigned int r_type);
355
356 inline void
357 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
358 Sized_relobj_file<size, big_endian>* object,
359 unsigned int data_shndx,
360 Output_section* output_section,
361 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
362 const elfcpp::Sym<size, big_endian>& lsym);
363
364 inline void
365 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
366 Sized_relobj_file<size, big_endian>* object,
367 unsigned int data_shndx,
368 Output_section* output_section,
369 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
370 Symbol* gsym);
371
372 inline bool
373 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
374 Target_powerpc* ,
375 Sized_relobj_file<size, big_endian>* ,
376 unsigned int ,
377 Output_section* ,
378 const elfcpp::Rela<size, big_endian>& ,
379 unsigned int ,
380 const elfcpp::Sym<size, big_endian>&)
381 { return false; }
382
383 inline bool
384 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
385 Target_powerpc* ,
386 Sized_relobj_file<size, big_endian>* ,
387 unsigned int ,
388 Output_section* ,
389 const elfcpp::Rela<size,
390 big_endian>& ,
391 unsigned int , Symbol*)
392 { return false; }
393
394 private:
395 static void
396 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
397 unsigned int r_type);
398
399 static void
400 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
401 unsigned int r_type, Symbol*);
402
403 static void
404 generate_tls_call(Symbol_table* symtab, Layout* layout,
405 Target_powerpc* target);
406
407 void
408 check_non_pic(Relobj*, unsigned int r_type);
409
410 // Whether we have issued an error about a non-PIC compilation.
411 bool issued_non_pic_error_;
412 };
413
414 Address
415 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
416 Powerpc_relobj<size, big_endian>* object,
417 unsigned int *dest_shndx);
418
419 // The class which implements relocation.
420 class Relocate
421 {
422 public:
423 // Use 'at' branch hints when true, 'y' when false.
424 // FIXME maybe: set this with an option.
425 static const bool is_isa_v2 = true;
426
427 enum skip_tls
428 {
429 CALL_NOT_EXPECTED = 0,
430 CALL_EXPECTED = 1,
431 CALL_SKIP = 2
432 };
433
434 Relocate()
435 : call_tls_get_addr_(CALL_NOT_EXPECTED)
436 { }
437
438 ~Relocate()
439 {
440 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
441 {
442 // FIXME: This needs to specify the location somehow.
443 gold_error(_("missing expected __tls_get_addr call"));
444 }
445 }
446
447 // Do a relocation. Return false if the caller should not issue
448 // any warnings about this relocation.
449 inline bool
450 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
451 Output_section*, size_t relnum,
452 const elfcpp::Rela<size, big_endian>&,
453 unsigned int r_type, const Sized_symbol<size>*,
454 const Symbol_value<size>*,
455 unsigned char*,
456 typename elfcpp::Elf_types<size>::Elf_Addr,
457 section_size_type);
458
459 // This is set if we should skip the next reloc, which should be a
460 // call to __tls_get_addr.
461 enum skip_tls call_tls_get_addr_;
462 };
463
464 // A class which returns the size required for a relocation type,
465 // used while scanning relocs during a relocatable link.
466 class Relocatable_size_for_reloc
467 {
468 public:
469 unsigned int
470 get_size_for_reloc(unsigned int, Relobj*)
471 {
472 gold_unreachable();
473 return 0;
474 }
475 };
476
477 // Optimize the TLS relocation type based on what we know about the
478 // symbol. IS_FINAL is true if the final address of this symbol is
479 // known at link time.
480
481 tls::Tls_optimization
482 optimize_tls_gd(bool is_final)
483 {
484 // If we are generating a shared library, then we can't do anything
485 // in the linker.
486 if (parameters->options().shared())
487 return tls::TLSOPT_NONE;
488
489 if (!is_final)
490 return tls::TLSOPT_TO_IE;
491 return tls::TLSOPT_TO_LE;
492 }
493
494 tls::Tls_optimization
495 optimize_tls_ld()
496 {
497 if (parameters->options().shared())
498 return tls::TLSOPT_NONE;
499
500 return tls::TLSOPT_TO_LE;
501 }
502
503 tls::Tls_optimization
504 optimize_tls_ie(bool is_final)
505 {
506 if (!is_final || parameters->options().shared())
507 return tls::TLSOPT_NONE;
508
509 return tls::TLSOPT_TO_LE;
510 }
511
512 // Get the GOT section, creating it if necessary.
513 Output_data_got_powerpc<size, big_endian>*
514 got_section(Symbol_table*, Layout*);
515
516 // Create glink.
517 void
518 make_glink_section(Layout*);
519
520 // Create the PLT section.
521 void
522 make_plt_section(Layout*);
523
524 // Create a PLT entry for a global symbol.
525 void
526 make_plt_entry(Layout*, Symbol*,
527 const elfcpp::Rela<size, big_endian>&,
528 const Sized_relobj<size, big_endian>* object);
529
530 // Create a GOT entry for local dynamic __tls_get_addr.
531 unsigned int
532 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
533 Sized_relobj_file<size, big_endian>* object);
534
535 unsigned int
536 tlsld_got_offset() const
537 {
538 return this->tlsld_got_offset_;
539 }
540
541 // Get the dynamic reloc section, creating it if necessary.
542 Reloc_section*
543 rela_dyn_section(Layout*);
544
545 // Copy a relocation against a global symbol.
546 void
547 copy_reloc(Symbol_table* symtab, Layout* layout,
548 Sized_relobj_file<size, big_endian>* object,
549 unsigned int shndx, Output_section* output_section,
550 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
551 {
552 this->copy_relocs_.copy_reloc(symtab, layout,
553 symtab->get_sized_symbol<size>(sym),
554 object, shndx, output_section,
555 reloc, this->rela_dyn_section(layout));
556 }
557
558 // Information about this specific target which we pass to the
559 // general Target structure.
560 static Target::Target_info powerpc_info;
561
562 // The types of GOT entries needed for this platform.
563 // These values are exposed to the ABI in an incremental link.
564 // Do not renumber existing values without changing the version
565 // number of the .gnu_incremental_inputs section.
566 enum Got_type
567 {
568 GOT_TYPE_STANDARD,
569 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
570 GOT_TYPE_DTPREL, // entry for @got@dtprel
571 GOT_TYPE_TPREL // entry for @got@tprel
572 };
573
574 // The GOT output section.
575 Output_data_got_powerpc<size, big_endian>* got_;
576 // The PLT output section.
577 Output_data_plt_powerpc<size, big_endian>* plt_;
578 // The .glink output section.
579 Output_data_glink<size, big_endian>* glink_;
580 // The dynamic reloc output section.
581 Reloc_section* rela_dyn_;
582 // Relocs saved to avoid a COPY reloc.
583 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
584 // Space for variables copied with a COPY reloc.
585 Output_data_space* dynbss_;
586 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
587 unsigned int tlsld_got_offset_;
588 };
589
590 template<>
591 Target::Target_info Target_powerpc<32, true>::powerpc_info =
592 {
593 32, // size
594 true, // is_big_endian
595 elfcpp::EM_PPC, // machine_code
596 false, // has_make_symbol
597 false, // has_resolve
598 false, // has_code_fill
599 true, // is_default_stack_executable
600 false, // can_icf_inline_merge_sections
601 '\0', // wrap_char
602 "/usr/lib/ld.so.1", // dynamic_linker
603 0x10000000, // default_text_segment_address
604 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
605 4 * 1024, // common_pagesize (overridable by -z common-page-size)
606 false, // isolate_execinstr
607 0, // rosegment_gap
608 elfcpp::SHN_UNDEF, // small_common_shndx
609 elfcpp::SHN_UNDEF, // large_common_shndx
610 0, // small_common_section_flags
611 0, // large_common_section_flags
612 NULL, // attributes_section
613 NULL // attributes_vendor
614 };
615
616 template<>
617 Target::Target_info Target_powerpc<32, false>::powerpc_info =
618 {
619 32, // size
620 false, // is_big_endian
621 elfcpp::EM_PPC, // machine_code
622 false, // has_make_symbol
623 false, // has_resolve
624 false, // has_code_fill
625 true, // is_default_stack_executable
626 false, // can_icf_inline_merge_sections
627 '\0', // wrap_char
628 "/usr/lib/ld.so.1", // dynamic_linker
629 0x10000000, // default_text_segment_address
630 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
631 4 * 1024, // common_pagesize (overridable by -z common-page-size)
632 false, // isolate_execinstr
633 0, // rosegment_gap
634 elfcpp::SHN_UNDEF, // small_common_shndx
635 elfcpp::SHN_UNDEF, // large_common_shndx
636 0, // small_common_section_flags
637 0, // large_common_section_flags
638 NULL, // attributes_section
639 NULL // attributes_vendor
640 };
641
642 template<>
643 Target::Target_info Target_powerpc<64, true>::powerpc_info =
644 {
645 64, // size
646 true, // is_big_endian
647 elfcpp::EM_PPC64, // machine_code
648 false, // has_make_symbol
649 false, // has_resolve
650 false, // has_code_fill
651 true, // is_default_stack_executable
652 false, // can_icf_inline_merge_sections
653 '\0', // wrap_char
654 "/usr/lib/ld.so.1", // dynamic_linker
655 0x10000000, // default_text_segment_address
656 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
657 4 * 1024, // common_pagesize (overridable by -z common-page-size)
658 false, // isolate_execinstr
659 0, // rosegment_gap
660 elfcpp::SHN_UNDEF, // small_common_shndx
661 elfcpp::SHN_UNDEF, // large_common_shndx
662 0, // small_common_section_flags
663 0, // large_common_section_flags
664 NULL, // attributes_section
665 NULL // attributes_vendor
666 };
667
668 template<>
669 Target::Target_info Target_powerpc<64, false>::powerpc_info =
670 {
671 64, // size
672 false, // is_big_endian
673 elfcpp::EM_PPC64, // machine_code
674 false, // has_make_symbol
675 false, // has_resolve
676 false, // has_code_fill
677 true, // is_default_stack_executable
678 false, // can_icf_inline_merge_sections
679 '\0', // wrap_char
680 "/usr/lib/ld.so.1", // dynamic_linker
681 0x10000000, // default_text_segment_address
682 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
683 4 * 1024, // common_pagesize (overridable by -z common-page-size)
684 false, // isolate_execinstr
685 0, // rosegment_gap
686 elfcpp::SHN_UNDEF, // small_common_shndx
687 elfcpp::SHN_UNDEF, // large_common_shndx
688 0, // small_common_section_flags
689 0, // large_common_section_flags
690 NULL, // attributes_section
691 NULL // attributes_vendor
692 };
693
694 inline bool
695 is_branch_reloc(unsigned int r_type)
696 {
697 return (r_type == elfcpp::R_POWERPC_REL24
698 || r_type == elfcpp::R_PPC_PLTREL24
699 || r_type == elfcpp::R_PPC_LOCAL24PC
700 || r_type == elfcpp::R_POWERPC_REL14
701 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
702 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
703 || r_type == elfcpp::R_POWERPC_ADDR24
704 || r_type == elfcpp::R_POWERPC_ADDR14
705 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
706 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
707 }
708
709 // If INSN is an opcode that may be used with an @tls operand, return
710 // the transformed insn for TLS optimisation, otherwise return 0. If
711 // REG is non-zero only match an insn with RB or RA equal to REG.
712 uint32_t
713 at_tls_transform(uint32_t insn, unsigned int reg)
714 {
715 if ((insn & (0x3f << 26)) != 31 << 26)
716 return 0;
717
718 unsigned int rtra;
719 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
720 rtra = insn & ((1 << 26) - (1 << 16));
721 else if (((insn >> 16) & 0x1f) == reg)
722 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
723 else
724 return 0;
725
726 if ((insn & (0x3ff << 1)) == 266 << 1)
727 // add -> addi
728 insn = 14 << 26;
729 else if ((insn & (0x1f << 1)) == 23 << 1
730 && ((insn & (0x1f << 6)) < 14 << 6
731 || ((insn & (0x1f << 6)) >= 16 << 6
732 && (insn & (0x1f << 6)) < 24 << 6)))
733 // load and store indexed -> dform
734 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
735 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
736 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
737 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
738 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
739 // lwax -> lwa
740 insn = (58 << 26) | 2;
741 else
742 return 0;
743 insn |= rtra;
744 return insn;
745 }
746
747 // Modified version of symtab.h class Symbol member
748 // Given a direct absolute or pc-relative static relocation against
749 // the global symbol, this function returns whether a dynamic relocation
750 // is needed.
751
752 template<int size>
753 bool
754 needs_dynamic_reloc(const Symbol* gsym, int flags)
755 {
756 // No dynamic relocations in a static link!
757 if (parameters->doing_static_link())
758 return false;
759
760 // A reference to an undefined symbol from an executable should be
761 // statically resolved to 0, and does not need a dynamic relocation.
762 // This matches gnu ld behavior.
763 if (gsym->is_undefined() && !parameters->options().shared())
764 return false;
765
766 // A reference to an absolute symbol does not need a dynamic relocation.
767 if (gsym->is_absolute())
768 return false;
769
770 // An absolute reference within a position-independent output file
771 // will need a dynamic relocation.
772 if ((flags & Symbol::ABSOLUTE_REF)
773 && parameters->options().output_is_position_independent())
774 return true;
775
776 // A function call that can branch to a local PLT entry does not need
777 // a dynamic relocation.
778 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
779 return false;
780
781 // A reference to any PLT entry in a non-position-independent executable
782 // does not need a dynamic relocation.
783 // Except due to having function descriptors on powerpc64 we don't define
784 // functions to their plt code in an executable, so this doesn't apply.
785 if (size == 32
786 && !parameters->options().output_is_position_independent()
787 && gsym->has_plt_offset())
788 return false;
789
790 // A reference to a symbol defined in a dynamic object or to a
791 // symbol that is preemptible will need a dynamic relocation.
792 if (gsym->is_from_dynobj()
793 || gsym->is_undefined()
794 || gsym->is_preemptible())
795 return true;
796
797 // For all other cases, return FALSE.
798 return false;
799 }
800
801 // Modified version of symtab.h class Symbol member
802 // Whether we should use the PLT offset associated with a symbol for
803 // a relocation. FLAGS is a set of Reference_flags.
804
805 template<int size>
806 bool
807 use_plt_offset(const Symbol* gsym, int flags)
808 {
809 // If the symbol doesn't have a PLT offset, then naturally we
810 // don't want to use it.
811 if (!gsym->has_plt_offset())
812 return false;
813
814 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
815 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
816 return true;
817
818 // If we are going to generate a dynamic relocation, then we will
819 // wind up using that, so no need to use the PLT entry.
820 if (needs_dynamic_reloc<size>(gsym, flags))
821 return false;
822
823 // If the symbol is from a dynamic object, we need to use the PLT
824 // entry.
825 if (gsym->is_from_dynobj())
826 return true;
827
828 // If we are generating a shared object, and gsym symbol is
829 // undefined or preemptible, we need to use the PLT entry.
830 if (parameters->options().shared()
831 && (gsym->is_undefined() || gsym->is_preemptible()))
832 return true;
833
834 // If gsym is a call to a weak undefined symbol, we need to use
835 // the PLT entry; the symbol may be defined by a library loaded
836 // at runtime.
837 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
838 return true;
839
840 // Otherwise we can use the regular definition.
841 return false;
842 }
843
844 template<int size, bool big_endian>
845 class Powerpc_relocate_functions
846 {
847 public:
848 enum Overflow_check
849 {
850 CHECK_NONE,
851 CHECK_SIGNED,
852 CHECK_BITFIELD
853 };
854
855 enum Status
856 {
857 STATUS_OK,
858 STATUS_OVERFLOW
859 };
860
861 private:
862 typedef Powerpc_relocate_functions<size, big_endian> This;
863 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
864
865 template<int valsize>
866 static inline bool
867 has_overflow_signed(Address value)
868 {
869 // limit = 1 << (valsize - 1) without shift count exceeding size of type
870 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
871 limit <<= ((valsize - 1) >> 1);
872 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
873 return value + limit > (limit << 1) - 1;
874 }
875
876 template<int valsize>
877 static inline bool
878 has_overflow_bitfield(Address value)
879 {
880 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
881 limit <<= ((valsize - 1) >> 1);
882 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
883 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
884 }
885
886 template<int valsize>
887 static inline Status
888 overflowed(Address value, Overflow_check overflow)
889 {
890 if (overflow == CHECK_SIGNED)
891 {
892 if (has_overflow_signed<valsize>(value))
893 return STATUS_OVERFLOW;
894 }
895 else if (overflow == CHECK_BITFIELD)
896 {
897 if (has_overflow_bitfield<valsize>(value))
898 return STATUS_OVERFLOW;
899 }
900 return STATUS_OK;
901 }
902
903 // Do a simple RELA relocation
904 template<int valsize>
905 static inline Status
906 rela(unsigned char* view, Address value, Overflow_check overflow)
907 {
908 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
909 Valtype* wv = reinterpret_cast<Valtype*>(view);
910 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
911 return overflowed<valsize>(value, overflow);
912 }
913
914 template<int valsize>
915 static inline Status
916 rela(unsigned char* view,
917 unsigned int right_shift,
918 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
919 Address value,
920 Overflow_check overflow)
921 {
922 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
923 Valtype* wv = reinterpret_cast<Valtype*>(view);
924 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
925 Valtype reloc = value >> right_shift;
926 val &= ~dst_mask;
927 reloc &= dst_mask;
928 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
929 return overflowed<valsize>(value >> right_shift, overflow);
930 }
931
932 // Do a simple RELA relocation, unaligned.
933 template<int valsize>
934 static inline Status
935 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
936 {
937 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
938 return overflowed<valsize>(value, overflow);
939 }
940
941 template<int valsize>
942 static inline Status
943 rela_ua(unsigned char* view,
944 unsigned int right_shift,
945 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
946 Address value,
947 Overflow_check overflow)
948 {
949 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
950 Valtype;
951 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
952 Valtype reloc = value >> right_shift;
953 val &= ~dst_mask;
954 reloc &= dst_mask;
955 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
956 return overflowed<valsize>(value >> right_shift, overflow);
957 }
958
959 public:
960 // R_PPC64_ADDR64: (Symbol + Addend)
961 static inline void
962 addr64(unsigned char* view, Address value)
963 { This::template rela<64>(view, value, CHECK_NONE); }
964
965 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
966 static inline void
967 addr64_u(unsigned char* view, Address value)
968 { This::template rela_ua<64>(view, value, CHECK_NONE); }
969
970 // R_POWERPC_ADDR32: (Symbol + Addend)
971 static inline Status
972 addr32(unsigned char* view, Address value, Overflow_check overflow)
973 { return This::template rela<32>(view, value, overflow); }
974
975 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
976 static inline Status
977 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
978 { return This::template rela_ua<32>(view, value, overflow); }
979
980 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
981 static inline Status
982 addr24(unsigned char* view, Address value, Overflow_check overflow)
983 {
984 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
985 if (overflow != CHECK_NONE && (value & 3) != 0)
986 stat = STATUS_OVERFLOW;
987 return stat;
988 }
989
990 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
991 static inline Status
992 addr16(unsigned char* view, Address value, Overflow_check overflow)
993 { return This::template rela<16>(view, value, overflow); }
994
995 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
996 static inline Status
997 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
998 { return This::template rela_ua<16>(view, value, overflow); }
999
1000 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1001 static inline Status
1002 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1003 {
1004 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1005 if (overflow != CHECK_NONE && (value & 3) != 0)
1006 stat = STATUS_OVERFLOW;
1007 return stat;
1008 }
1009
1010 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1011 static inline void
1012 addr16_hi(unsigned char* view, Address value)
1013 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1014
1015 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1016 static inline void
1017 addr16_ha(unsigned char* view, Address value)
1018 { This::addr16_hi(view, value + 0x8000); }
1019
1020 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1021 static inline void
1022 addr16_hi2(unsigned char* view, Address value)
1023 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1024
1025 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1026 static inline void
1027 addr16_ha2(unsigned char* view, Address value)
1028 { This::addr16_hi2(view, value + 0x8000); }
1029
1030 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1031 static inline void
1032 addr16_hi3(unsigned char* view, Address value)
1033 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1034
1035 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1036 static inline void
1037 addr16_ha3(unsigned char* view, Address value)
1038 { This::addr16_hi3(view, value + 0x8000); }
1039
1040 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1041 static inline Status
1042 addr14(unsigned char* view, Address value, Overflow_check overflow)
1043 {
1044 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1045 if (overflow != CHECK_NONE && (value & 3) != 0)
1046 stat = STATUS_OVERFLOW;
1047 return stat;
1048 }
1049 };
1050
1051 // Stash away the index of .got2 or .opd in a relocatable object, if
1052 // such a section exists.
1053
1054 template<int size, bool big_endian>
1055 bool
1056 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1057 Read_symbols_data* sd)
1058 {
1059 const unsigned char* const pshdrs = sd->section_headers->data();
1060 const unsigned char* namesu = sd->section_names->data();
1061 const char* names = reinterpret_cast<const char*>(namesu);
1062 section_size_type names_size = sd->section_names_size;
1063 const unsigned char* s;
1064
1065 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1066 names, names_size, NULL);
1067 if (s != NULL)
1068 {
1069 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1070 this->special_ = ndx;
1071 }
1072 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1073 }
1074
1075 // Examine .rela.opd to build info about function entry points.
1076
1077 template<int size, bool big_endian>
1078 void
1079 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1080 size_t reloc_count,
1081 const unsigned char* prelocs,
1082 const unsigned char* plocal_syms)
1083 {
1084 if (size == 64)
1085 {
1086 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1087 Reltype;
1088 const int reloc_size
1089 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1090 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1091
1092 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1093 {
1094 Reltype reloc(prelocs);
1095 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1096 = reloc.get_r_info();
1097 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1098 if (r_type == elfcpp::R_PPC64_ADDR64)
1099 {
1100 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1101 typename elfcpp::Elf_types<size>::Elf_Addr value;
1102 bool is_ordinary;
1103 unsigned int shndx;
1104 if (r_sym < this->local_symbol_count())
1105 {
1106 typename elfcpp::Sym<size, big_endian>
1107 lsym(plocal_syms + r_sym * sym_size);
1108 shndx = lsym.get_st_shndx();
1109 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1110 value = lsym.get_st_value();
1111 }
1112 else
1113 shndx = this->symbol_section_and_value(r_sym, &value,
1114 &is_ordinary);
1115 this->set_opd_ent(reloc.get_r_offset(), shndx,
1116 value + reloc.get_r_addend());
1117 }
1118 }
1119 }
1120 }
1121
1122 template<int size, bool big_endian>
1123 void
1124 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1125 {
1126 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1127 if (size == 64)
1128 {
1129 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1130 p != rd->relocs.end();
1131 ++p)
1132 {
1133 if (p->data_shndx == this->opd_shndx())
1134 {
1135 this->init_opd(this->section_size(this->opd_shndx()));
1136 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1137 rd->local_symbols->data());
1138 break;
1139 }
1140 }
1141 }
1142 }
1143
1144 // Set up PowerPC target specific relobj.
1145
1146 template<int size, bool big_endian>
1147 Object*
1148 Target_powerpc<size, big_endian>::do_make_elf_object(
1149 const std::string& name,
1150 Input_file* input_file,
1151 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1152 {
1153 int et = ehdr.get_e_type();
1154 // ET_EXEC files are valid input for --just-symbols/-R,
1155 // and we treat them as relocatable objects.
1156 if (et == elfcpp::ET_REL
1157 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1158 {
1159 Powerpc_relobj<size, big_endian>* obj =
1160 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1161 obj->setup();
1162 return obj;
1163 }
1164 else if (et == elfcpp::ET_DYN)
1165 {
1166 Sized_dynobj<size, big_endian>* obj =
1167 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1168 obj->setup();
1169 return obj;
1170 }
1171 else
1172 {
1173 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1174 return NULL;
1175 }
1176 }
1177
1178 template<int size, bool big_endian>
1179 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1180 {
1181 public:
1182 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1183 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1184
1185 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1186 : Output_data_got<size, big_endian>(),
1187 symtab_(symtab), layout_(layout),
1188 header_ent_cnt_(size == 32 ? 3 : 1),
1189 header_index_(size == 32 ? 0x2000 : 0)
1190 {}
1191
1192 class Got_entry;
1193
1194 // Create a new GOT entry and return its offset.
1195 unsigned int
1196 add_got_entry(Got_entry got_entry)
1197 {
1198 this->reserve_ent();
1199 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1200 }
1201
1202 // Create a pair of new GOT entries and return the offset of the first.
1203 unsigned int
1204 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1205 {
1206 this->reserve_ent(2);
1207 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1208 got_entry_2);
1209 }
1210
1211 unsigned int
1212 add_constant_pair(Valtype c1, Valtype c2)
1213 {
1214 this->reserve_ent(2);
1215 unsigned int got_offset = this->add_constant(c1);
1216 this->add_constant(c2);
1217 return got_offset;
1218 }
1219
1220 // Offset of _GLOBAL_OFFSET_TABLE_.
1221 unsigned int
1222 g_o_t() const
1223 {
1224 return this->got_offset(this->header_index_);
1225 }
1226
1227 // Offset of base used to access the GOT/TOC.
1228 // The got/toc pointer reg will be set to this value.
1229 typename elfcpp::Elf_types<size>::Elf_Off
1230 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1231 {
1232 if (size == 32)
1233 return this->g_o_t();
1234 else
1235 return (this->output_section()->address()
1236 + object->toc_base_offset()
1237 - this->address());
1238 }
1239
1240 // Ensure our GOT has a header.
1241 void
1242 set_final_data_size()
1243 {
1244 if (this->header_ent_cnt_ != 0)
1245 this->make_header();
1246 Output_data_got<size, big_endian>::set_final_data_size();
1247 }
1248
1249 // First word of GOT header needs some values that are not
1250 // handled by Output_data_got so poke them in here.
1251 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1252 void
1253 do_write(Output_file* of)
1254 {
1255 this->replace_constant(this->header_index_,
1256 (size == 32
1257 ? this->layout_->dynamic_section()->address()
1258 : this->output_section()->address() + 0x8000));
1259
1260 Output_data_got<size, big_endian>::do_write(of);
1261 }
1262
1263 private:
1264 void
1265 reserve_ent(unsigned int cnt = 1)
1266 {
1267 if (this->header_ent_cnt_ == 0)
1268 return;
1269 if (this->num_entries() + cnt > this->header_index_)
1270 this->make_header();
1271 }
1272
1273 void
1274 make_header()
1275 {
1276 this->header_ent_cnt_ = 0;
1277 this->header_index_ = this->num_entries();
1278 if (size == 32)
1279 {
1280 Output_data_got<size, big_endian>::add_constant(0);
1281 Output_data_got<size, big_endian>::add_constant(0);
1282 Output_data_got<size, big_endian>::add_constant(0);
1283
1284 // Define _GLOBAL_OFFSET_TABLE_ at the header
1285 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1286 Symbol_table::PREDEFINED,
1287 this, this->g_o_t(), 0,
1288 elfcpp::STT_OBJECT,
1289 elfcpp::STB_LOCAL,
1290 elfcpp::STV_HIDDEN,
1291 0, false, false);
1292 }
1293 else
1294 Output_data_got<size, big_endian>::add_constant(0);
1295 }
1296
1297 // Stashed pointers.
1298 Symbol_table* symtab_;
1299 Layout* layout_;
1300
1301 // GOT header size.
1302 unsigned int header_ent_cnt_;
1303 // GOT header index.
1304 unsigned int header_index_;
1305 };
1306
1307 // Get the GOT section, creating it if necessary.
1308
1309 template<int size, bool big_endian>
1310 Output_data_got_powerpc<size, big_endian>*
1311 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1312 Layout* layout)
1313 {
1314 if (this->got_ == NULL)
1315 {
1316 gold_assert(symtab != NULL && layout != NULL);
1317
1318 this->got_
1319 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1320
1321 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1322 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1323 this->got_, ORDER_DATA, false);
1324 }
1325
1326 return this->got_;
1327 }
1328
1329 // Get the dynamic reloc section, creating it if necessary.
1330
1331 template<int size, bool big_endian>
1332 typename Target_powerpc<size, big_endian>::Reloc_section*
1333 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1334 {
1335 if (this->rela_dyn_ == NULL)
1336 {
1337 gold_assert(layout != NULL);
1338 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1339 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1340 elfcpp::SHF_ALLOC, this->rela_dyn_,
1341 ORDER_DYNAMIC_RELOCS, false);
1342 }
1343 return this->rela_dyn_;
1344 }
1345
1346 // A class to handle the PLT data.
1347
1348 template<int size, bool big_endian>
1349 class Output_data_plt_powerpc : public Output_section_data_build
1350 {
1351 public:
1352 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1353 size, big_endian> Reloc_section;
1354
1355 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1356
1357 // Add an entry to the PLT.
1358 void
1359 add_entry(Symbol*);
1360
1361 // Return the .rela.plt section data.
1362 const Reloc_section*
1363 rel_plt() const
1364 {
1365 return this->rel_;
1366 }
1367
1368 // Return the number of PLT entries.
1369 unsigned int
1370 entry_count() const
1371 {
1372 return ((this->current_data_size() - initial_plt_entry_size)
1373 / plt_entry_size);
1374 }
1375
1376 // Return the offset of the first non-reserved PLT entry.
1377 static unsigned int
1378 first_plt_entry_offset()
1379 { return initial_plt_entry_size; }
1380
1381 // Return the size of a PLT entry.
1382 static unsigned int
1383 get_plt_entry_size()
1384 { return plt_entry_size; }
1385
1386 protected:
1387 void
1388 do_adjust_output_section(Output_section* os)
1389 {
1390 os->set_entsize(0);
1391 }
1392
1393 // Write to a map file.
1394 void
1395 do_print_to_mapfile(Mapfile* mapfile) const
1396 { mapfile->print_output_data(this, _("** PLT")); }
1397
1398 private:
1399 // The size of an entry in the PLT.
1400 static const int plt_entry_size = size == 32 ? 4 : 24;
1401 // The size of the first reserved entry.
1402 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1403
1404 // Write out the PLT data.
1405 void
1406 do_write(Output_file*);
1407
1408 // The reloc section.
1409 Reloc_section* rel_;
1410 // Allows access to .glink for do_write.
1411 Target_powerpc<size, big_endian>* targ_;
1412 };
1413
1414 // Create the PLT section.
1415
1416 template<int size, bool big_endian>
1417 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1418 Layout* layout,
1419 Target_powerpc<size, big_endian>* targ)
1420 : Output_section_data_build(size == 32 ? 4 : 8),
1421 targ_(targ)
1422 {
1423 this->rel_ = new Reloc_section(false);
1424 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1425 elfcpp::SHF_ALLOC, this->rel_,
1426 ORDER_DYNAMIC_PLT_RELOCS, false);
1427 }
1428
1429 // Add an entry to the PLT.
1430
1431 template<int size, bool big_endian>
1432 void
1433 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1434 {
1435 if (!gsym->has_plt_offset())
1436 {
1437 off_t off = this->current_data_size();
1438
1439 if (off == 0)
1440 off += initial_plt_entry_size;
1441 gsym->set_plt_offset(off);
1442 gsym->set_needs_dynsym_entry();
1443 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1444 off += plt_entry_size;
1445 this->set_current_data_size(off);
1446 }
1447 }
1448
1449 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1450 static const uint32_t add_3_3_2 = 0x7c631214;
1451 static const uint32_t add_3_3_13 = 0x7c636a14;
1452 static const uint32_t add_11_0_11 = 0x7d605a14;
1453 static const uint32_t add_12_2_11 = 0x7d825a14;
1454 static const uint32_t addi_11_11 = 0x396b0000;
1455 static const uint32_t addi_12_12 = 0x398c0000;
1456 static const uint32_t addi_2_2 = 0x38420000;
1457 static const uint32_t addi_3_2 = 0x38620000;
1458 static const uint32_t addi_3_3 = 0x38630000;
1459 static const uint32_t addis_0_2 = 0x3c020000;
1460 static const uint32_t addis_0_13 = 0x3c0d0000;
1461 static const uint32_t addis_11_11 = 0x3d6b0000;
1462 static const uint32_t addis_11_30 = 0x3d7e0000;
1463 static const uint32_t addis_12_12 = 0x3d8c0000;
1464 static const uint32_t addis_12_2 = 0x3d820000;
1465 static const uint32_t addis_3_2 = 0x3c620000;
1466 static const uint32_t addis_3_13 = 0x3c6d0000;
1467 static const uint32_t b = 0x48000000;
1468 static const uint32_t bcl_20_31 = 0x429f0005;
1469 static const uint32_t bctr = 0x4e800420;
1470 static const uint32_t blrl = 0x4e800021;
1471 static const uint32_t cror_15_15_15 = 0x4def7b82;
1472 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1473 static const uint32_t ld_11_12 = 0xe96c0000;
1474 static const uint32_t ld_11_2 = 0xe9620000;
1475 static const uint32_t ld_2_1 = 0xe8410000;
1476 static const uint32_t ld_2_11 = 0xe84b0000;
1477 static const uint32_t ld_2_12 = 0xe84c0000;
1478 static const uint32_t ld_2_2 = 0xe8420000;
1479 static const uint32_t li_0_0 = 0x38000000;
1480 static const uint32_t lis_0_0 = 0x3c000000;
1481 static const uint32_t lis_11 = 0x3d600000;
1482 static const uint32_t lis_12 = 0x3d800000;
1483 static const uint32_t lwz_0_12 = 0x800c0000;
1484 static const uint32_t lwz_11_11 = 0x816b0000;
1485 static const uint32_t lwz_11_30 = 0x817e0000;
1486 static const uint32_t lwz_12_12 = 0x818c0000;
1487 static const uint32_t lwzu_0_12 = 0x840c0000;
1488 static const uint32_t mflr_0 = 0x7c0802a6;
1489 static const uint32_t mflr_11 = 0x7d6802a6;
1490 static const uint32_t mflr_12 = 0x7d8802a6;
1491 static const uint32_t mtctr_0 = 0x7c0903a6;
1492 static const uint32_t mtctr_11 = 0x7d6903a6;
1493 static const uint32_t mtlr_0 = 0x7c0803a6;
1494 static const uint32_t mtlr_12 = 0x7d8803a6;
1495 static const uint32_t nop = 0x60000000;
1496 static const uint32_t ori_0_0_0 = 0x60000000;
1497 static const uint32_t std_2_1 = 0xf8410000;
1498 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1499
1500 // Write out the PLT.
1501
1502 template<int size, bool big_endian>
1503 void
1504 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1505 {
1506 if (size == 32)
1507 {
1508 const off_t offset = this->offset();
1509 const section_size_type oview_size
1510 = convert_to_section_size_type(this->data_size());
1511 unsigned char* const oview = of->get_output_view(offset, oview_size);
1512 unsigned char* pov = oview;
1513 unsigned char* endpov = oview + oview_size;
1514
1515 // The address the .glink branch table
1516 const Output_data_glink<size, big_endian>* glink
1517 = this->targ_->glink_section();
1518 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1519 = glink->address() + glink->pltresolve();
1520
1521 while (pov < endpov)
1522 {
1523 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1524 pov += 4;
1525 branch_tab += 4;
1526 }
1527
1528 of->write_output_view(offset, oview_size, oview);
1529 }
1530 }
1531
1532 // Create the PLT section.
1533
1534 template<int size, bool big_endian>
1535 void
1536 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1537 {
1538 if (this->plt_ == NULL)
1539 {
1540 if (this->glink_ == NULL)
1541 make_glink_section(layout);
1542
1543 // Ensure that .rela.dyn always appears before .rela.plt This is
1544 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1545 // needs to include .rela.plt in it's range.
1546 this->rela_dyn_section(layout);
1547
1548 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1549 layout->add_output_section_data(".plt",
1550 (size == 32
1551 ? elfcpp::SHT_PROGBITS
1552 : elfcpp::SHT_NOBITS),
1553 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1554 this->plt_,
1555 (size == 32
1556 ? ORDER_SMALL_DATA
1557 : ORDER_SMALL_BSS),
1558 false);
1559 }
1560 }
1561
1562 // A class to handle .glink.
1563
1564 template<int size, bool big_endian>
1565 class Output_data_glink : public Output_section_data
1566 {
1567 public:
1568 static const int pltresolve_size = 16*4;
1569
1570 Output_data_glink(Target_powerpc<size, big_endian>*);
1571
1572 // Add an entry
1573 void
1574 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1575 const Sized_relobj<size, big_endian>*);
1576
1577 unsigned int
1578 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1579 const Sized_relobj<size, big_endian>*) const;
1580
1581 unsigned int
1582 glink_entry_size() const
1583 {
1584 if (size == 32)
1585 return 4 * 4;
1586 else
1587 // FIXME: We should be using multiple glink sections for
1588 // stubs to support > 33M applications.
1589 return 8 * 4;
1590 }
1591
1592 off_t
1593 pltresolve() const
1594 {
1595 return this->pltresolve_;
1596 }
1597
1598 protected:
1599 // Write to a map file.
1600 void
1601 do_print_to_mapfile(Mapfile* mapfile) const
1602 { mapfile->print_output_data(this, _("** glink")); }
1603
1604 private:
1605 void
1606 set_final_data_size();
1607
1608 // Write out .glink
1609 void
1610 do_write(Output_file*);
1611
1612 class Glink_sym_ent
1613 {
1614 public:
1615 Glink_sym_ent(const Symbol* sym,
1616 const elfcpp::Rela<size, big_endian>& reloc,
1617 const Sized_relobj<size, big_endian>* object)
1618 : sym_(sym), addend_(0), object_(0)
1619 {
1620 if (size != 32)
1621 this->addend_ = reloc.get_r_addend();
1622 else if (parameters->options().output_is_position_independent()
1623 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1624 == elfcpp::R_PPC_PLTREL24))
1625 {
1626 this->addend_ = reloc.get_r_addend();
1627 if (this->addend_ != 0)
1628 this->object_ = object;
1629 }
1630 }
1631
1632 bool operator==(const Glink_sym_ent& that) const
1633 {
1634 return (this->sym_ == that.sym_
1635 && this->object_ == that.object_
1636 && this->addend_ == that.addend_);
1637 }
1638
1639 const Symbol* sym_;
1640 unsigned int addend_;
1641 const Sized_relobj<size, big_endian>* object_;
1642 };
1643
1644 class Glink_sym_ent_hash
1645 {
1646 public:
1647 size_t operator()(const Glink_sym_ent& ent) const
1648 {
1649 return (reinterpret_cast<uintptr_t>(ent.sym_)
1650 ^ reinterpret_cast<uintptr_t>(ent.object_)
1651 ^ ent.addend_);
1652 }
1653 };
1654
1655 // Map sym/object/addend to index.
1656 typedef Unordered_map<Glink_sym_ent, unsigned int,
1657 Glink_sym_ent_hash> Glink_entries;
1658 Glink_entries glink_entries_;
1659
1660 // Offset of pltresolve stub (actually, branch table for 32-bit)
1661 off_t pltresolve_;
1662
1663 // Allows access to .got and .plt for do_write.
1664 Target_powerpc<size, big_endian>* targ_;
1665 };
1666
1667 // Create the glink section.
1668
1669 template<int size, bool big_endian>
1670 Output_data_glink<size, big_endian>::Output_data_glink(
1671 Target_powerpc<size, big_endian>* targ)
1672 : Output_section_data(16),
1673 pltresolve_(0), targ_(targ)
1674 {
1675 }
1676
1677 // Add an entry to glink, if we do not already have one for this
1678 // sym/object/addend combo.
1679
1680 template<int size, bool big_endian>
1681 void
1682 Output_data_glink<size, big_endian>::add_entry(
1683 const Symbol* gsym,
1684 const elfcpp::Rela<size, big_endian>& reloc,
1685 const Sized_relobj<size, big_endian>* object)
1686 {
1687 Glink_sym_ent ent(gsym, reloc, object);
1688 unsigned int indx = this->glink_entries_.size();
1689 this->glink_entries_.insert(std::make_pair(ent, indx));
1690 }
1691
1692 template<int size, bool big_endian>
1693 unsigned int
1694 Output_data_glink<size, big_endian>::find_entry(
1695 const Symbol* gsym,
1696 const elfcpp::Rela<size, big_endian>& reloc,
1697 const Sized_relobj<size, big_endian>* object) const
1698 {
1699 Glink_sym_ent ent(gsym, reloc, object);
1700 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1701 gold_assert(p != this->glink_entries_.end());
1702 return p->second;
1703 }
1704
1705 template<int size, bool big_endian>
1706 void
1707 Output_data_glink<size, big_endian>::set_final_data_size()
1708 {
1709 unsigned int count = this->glink_entries_.size();
1710 off_t total = count;
1711
1712 if (count != 0)
1713 {
1714 if (size == 32)
1715 {
1716 total *= 16;
1717 this->pltresolve_ = total;
1718
1719 // space for branch table
1720 total += 4 * (count - 1);
1721
1722 total += -total & 15;
1723 total += this->pltresolve_size;
1724 }
1725 else
1726 {
1727 total *= 32;
1728 this->pltresolve_ = total;
1729 total += this->pltresolve_size;
1730
1731 // space for branch table
1732 total += 8 * count;
1733 if (count > 0x8000)
1734 total += 4 * (count - 0x8000);
1735 }
1736 }
1737
1738 this->set_data_size(total);
1739 }
1740
1741 static inline uint32_t
1742 l(uint32_t a)
1743 {
1744 return a & 0xffff;
1745 }
1746
1747 static inline uint32_t
1748 hi(uint32_t a)
1749 {
1750 return l(a >> 16);
1751 }
1752
1753 static inline uint32_t
1754 ha(uint32_t a)
1755 {
1756 return hi(a + 0x8000);
1757 }
1758
1759 template<bool big_endian>
1760 static inline void
1761 write_insn(unsigned char* p, uint32_t v)
1762 {
1763 elfcpp::Swap<32, big_endian>::writeval(p, v);
1764 }
1765
1766 // Write out .glink.
1767
1768 template<int size, bool big_endian>
1769 void
1770 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1771 {
1772 const off_t off = this->offset();
1773 const section_size_type oview_size =
1774 convert_to_section_size_type(this->data_size());
1775 unsigned char* const oview = of->get_output_view(off, oview_size);
1776 unsigned char* p;
1777
1778 // The base address of the .plt section.
1779 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1780 Address plt_base = this->targ_->plt_section()->address();
1781
1782 const Output_data_got_powerpc<size, big_endian>* got
1783 = this->targ_->got_section();
1784
1785 if (size == 64)
1786 {
1787 Address got_os_addr = got->output_section()->address();
1788
1789 // Write out call stubs.
1790 typename Glink_entries::const_iterator g;
1791 for (g = this->glink_entries_.begin();
1792 g != this->glink_entries_.end();
1793 ++g)
1794 {
1795 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1796 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1797 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1798 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1799 Address pltoff = plt_addr - got_addr;
1800
1801 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1802 gold_error(_("%s: linkage table error against `%s'"),
1803 g->first.object_->name().c_str(),
1804 g->first.sym_->demangled_name().c_str());
1805
1806 p = oview + g->second * this->glink_entry_size();
1807 if (ha(pltoff) != 0)
1808 {
1809 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1810 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1811 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1812 if (ha(pltoff + 16) != ha(pltoff))
1813 {
1814 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1815 pltoff = 0;
1816 }
1817 write_insn<big_endian>(p, mtctr_11), p += 4;
1818 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1819 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1820 write_insn<big_endian>(p, bctr), p += 4;
1821 }
1822 else
1823 {
1824 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1825 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1826 if (ha(pltoff + 16) != ha(pltoff))
1827 {
1828 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1829 pltoff = 0;
1830 }
1831 write_insn<big_endian>(p, mtctr_11), p += 4;
1832 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1833 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1834 write_insn<big_endian>(p, bctr), p += 4;
1835 }
1836 }
1837
1838 // Write pltresolve stub.
1839 p = oview + this->pltresolve_;
1840 Address after_bcl = this->address() + this->pltresolve_ + 16;
1841 Address pltoff = plt_base - after_bcl;
1842
1843 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1844
1845 write_insn<big_endian>(p, mflr_12), p += 4;
1846 write_insn<big_endian>(p, bcl_20_31), p += 4;
1847 write_insn<big_endian>(p, mflr_11), p += 4;
1848 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1849 write_insn<big_endian>(p, mtlr_12), p += 4;
1850 write_insn<big_endian>(p, add_12_2_11), p += 4;
1851 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1852 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1853 write_insn<big_endian>(p, mtctr_11), p += 4;
1854 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1855 write_insn<big_endian>(p, bctr), p += 4;
1856 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1857 write_insn<big_endian>(p, nop), p += 4;
1858
1859 // Write lazy link call stubs.
1860 uint32_t indx = 0;
1861 while (p < oview + oview_size)
1862 {
1863 if (indx < 0x8000)
1864 {
1865 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1866 }
1867 else
1868 {
1869 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1870 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1871 }
1872 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1873 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1874 indx++;
1875 }
1876 }
1877 else
1878 {
1879 // The address of _GLOBAL_OFFSET_TABLE_.
1880 Address g_o_t = got->address() + got->g_o_t();
1881
1882 // Write out call stubs.
1883 typename Glink_entries::const_iterator g;
1884 for (g = this->glink_entries_.begin();
1885 g != this->glink_entries_.end();
1886 ++g)
1887 {
1888 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1889 Address got_addr;
1890 const Address invalid_address = static_cast<Address>(-1);
1891
1892 p = oview + g->second * this->glink_entry_size();
1893 if (parameters->options().output_is_position_independent())
1894 {
1895 const Powerpc_relobj<size, big_endian>* object = static_cast
1896 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1897 if (object != NULL)
1898 {
1899 unsigned int got2 = object->got2_shndx();
1900 got_addr = g->first.object_->get_output_section_offset(got2);
1901 gold_assert(got_addr != invalid_address);
1902 got_addr += (g->first.object_->output_section(got2)->address()
1903 + g->first.addend_);
1904 }
1905 else
1906 got_addr = g_o_t;
1907
1908 Address pltoff = plt_addr - got_addr;
1909 if (ha(pltoff) == 0)
1910 {
1911 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1912 write_insn<big_endian>(p + 4, mtctr_11);
1913 write_insn<big_endian>(p + 8, bctr);
1914 }
1915 else
1916 {
1917 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1918 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1919 write_insn<big_endian>(p + 8, mtctr_11);
1920 write_insn<big_endian>(p + 12, bctr);
1921 }
1922 }
1923 else
1924 {
1925 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1926 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1927 write_insn<big_endian>(p + 8, mtctr_11);
1928 write_insn<big_endian>(p + 12, bctr);
1929 }
1930 }
1931
1932 // Write out pltresolve branch table.
1933 p = oview + this->pltresolve_;
1934 unsigned int the_end = oview_size - this->pltresolve_size;
1935 unsigned char* end_p = oview + the_end;
1936 while (p < end_p - 8 * 4)
1937 write_insn<big_endian>(p, b + end_p - p), p += 4;
1938 while (p < end_p)
1939 write_insn<big_endian>(p, nop), p += 4;
1940
1941 // Write out pltresolve call stub.
1942 if (parameters->options().output_is_position_independent())
1943 {
1944 Address res0_off = this->pltresolve_;
1945 Address after_bcl_off = the_end + 12;
1946 Address bcl_res0 = after_bcl_off - res0_off;
1947
1948 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1949 write_insn<big_endian>(p + 4, mflr_0);
1950 write_insn<big_endian>(p + 8, bcl_20_31);
1951 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1952 write_insn<big_endian>(p + 16, mflr_12);
1953 write_insn<big_endian>(p + 20, mtlr_0);
1954 write_insn<big_endian>(p + 24, sub_11_11_12);
1955
1956 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1957
1958 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1959 if (ha(got_bcl) == ha(got_bcl + 4))
1960 {
1961 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1962 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1963 }
1964 else
1965 {
1966 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1967 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1968 }
1969 write_insn<big_endian>(p + 40, mtctr_0);
1970 write_insn<big_endian>(p + 44, add_0_11_11);
1971 write_insn<big_endian>(p + 48, add_11_0_11);
1972 write_insn<big_endian>(p + 52, bctr);
1973 write_insn<big_endian>(p + 56, nop);
1974 write_insn<big_endian>(p + 60, nop);
1975 }
1976 else
1977 {
1978 Address res0 = this->pltresolve_ + this->address();
1979
1980 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1981 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1982 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1983 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1984 else
1985 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1986 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1987 write_insn<big_endian>(p + 16, mtctr_0);
1988 write_insn<big_endian>(p + 20, add_0_11_11);
1989 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1990 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1991 else
1992 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1993 write_insn<big_endian>(p + 28, add_11_0_11);
1994 write_insn<big_endian>(p + 32, bctr);
1995 write_insn<big_endian>(p + 36, nop);
1996 write_insn<big_endian>(p + 40, nop);
1997 write_insn<big_endian>(p + 44, nop);
1998 write_insn<big_endian>(p + 48, nop);
1999 write_insn<big_endian>(p + 52, nop);
2000 write_insn<big_endian>(p + 56, nop);
2001 write_insn<big_endian>(p + 60, nop);
2002 }
2003 p += 64;
2004 }
2005
2006 of->write_output_view(off, oview_size, oview);
2007 }
2008
2009 // Create the glink section.
2010
2011 template<int size, bool big_endian>
2012 void
2013 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2014 {
2015 if (this->glink_ == NULL)
2016 {
2017 this->glink_ = new Output_data_glink<size, big_endian>(this);
2018 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2019 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2020 this->glink_, ORDER_TEXT, false);
2021 }
2022 }
2023
2024 // Create a PLT entry for a global symbol.
2025
2026 template<int size, bool big_endian>
2027 void
2028 Target_powerpc<size, big_endian>::make_plt_entry(
2029 Layout* layout,
2030 Symbol* gsym,
2031 const elfcpp::Rela<size, big_endian>& reloc,
2032 const Sized_relobj<size, big_endian>* object)
2033 {
2034 if (this->plt_ == NULL)
2035 this->make_plt_section(layout);
2036
2037 this->plt_->add_entry(gsym);
2038
2039 this->glink_->add_entry(gsym, reloc, object);
2040 }
2041
2042 // Return the number of entries in the PLT.
2043
2044 template<int size, bool big_endian>
2045 unsigned int
2046 Target_powerpc<size, big_endian>::plt_entry_count() const
2047 {
2048 if (this->plt_ == NULL)
2049 return 0;
2050 return this->plt_->entry_count();
2051 }
2052
2053 // Return the offset of the first non-reserved PLT entry.
2054
2055 template<int size, bool big_endian>
2056 unsigned int
2057 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2058 {
2059 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2060 }
2061
2062 // Return the size of each PLT entry.
2063
2064 template<int size, bool big_endian>
2065 unsigned int
2066 Target_powerpc<size, big_endian>::plt_entry_size() const
2067 {
2068 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2069 }
2070
2071 // Create a GOT entry for local dynamic __tls_get_addr calls.
2072
2073 template<int size, bool big_endian>
2074 unsigned int
2075 Target_powerpc<size, big_endian>::tlsld_got_offset(
2076 Symbol_table* symtab,
2077 Layout* layout,
2078 Sized_relobj_file<size, big_endian>* object)
2079 {
2080 if (this->tlsld_got_offset_ == -1U)
2081 {
2082 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2083 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2084 Output_data_got_powerpc<size, big_endian>* got
2085 = this->got_section(symtab, layout);
2086 unsigned int got_offset = got->add_constant_pair(0, 0);
2087 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2088 got_offset, 0);
2089 this->tlsld_got_offset_ = got_offset;
2090 }
2091 return this->tlsld_got_offset_;
2092 }
2093
2094 // Get the Reference_flags for a particular relocation.
2095
2096 template<int size, bool big_endian>
2097 int
2098 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2099 {
2100 switch (r_type)
2101 {
2102 case elfcpp::R_POWERPC_NONE:
2103 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2104 case elfcpp::R_POWERPC_GNU_VTENTRY:
2105 case elfcpp::R_PPC64_TOC:
2106 // No symbol reference.
2107 return 0;
2108
2109 case elfcpp::R_PPC64_ADDR64:
2110 case elfcpp::R_PPC64_UADDR64:
2111 case elfcpp::R_POWERPC_ADDR32:
2112 case elfcpp::R_POWERPC_UADDR32:
2113 case elfcpp::R_POWERPC_ADDR16:
2114 case elfcpp::R_POWERPC_UADDR16:
2115 case elfcpp::R_POWERPC_ADDR16_LO:
2116 case elfcpp::R_POWERPC_ADDR16_HI:
2117 case elfcpp::R_POWERPC_ADDR16_HA:
2118 return Symbol::ABSOLUTE_REF;
2119
2120 case elfcpp::R_POWERPC_ADDR24:
2121 case elfcpp::R_POWERPC_ADDR14:
2122 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2123 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2124 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2125
2126 case elfcpp::R_POWERPC_REL32:
2127 case elfcpp::R_PPC_LOCAL24PC:
2128 case elfcpp::R_POWERPC_REL16:
2129 case elfcpp::R_POWERPC_REL16_LO:
2130 case elfcpp::R_POWERPC_REL16_HI:
2131 case elfcpp::R_POWERPC_REL16_HA:
2132 return Symbol::RELATIVE_REF;
2133
2134 case elfcpp::R_POWERPC_REL24:
2135 case elfcpp::R_PPC_PLTREL24:
2136 case elfcpp::R_POWERPC_REL14:
2137 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2138 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2139 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2140
2141 case elfcpp::R_POWERPC_GOT16:
2142 case elfcpp::R_POWERPC_GOT16_LO:
2143 case elfcpp::R_POWERPC_GOT16_HI:
2144 case elfcpp::R_POWERPC_GOT16_HA:
2145 case elfcpp::R_PPC64_TOC16:
2146 case elfcpp::R_PPC64_TOC16_LO:
2147 case elfcpp::R_PPC64_TOC16_HI:
2148 case elfcpp::R_PPC64_TOC16_HA:
2149 case elfcpp::R_PPC64_TOC16_DS:
2150 case elfcpp::R_PPC64_TOC16_LO_DS:
2151 // Absolute in GOT.
2152 return Symbol::ABSOLUTE_REF;
2153
2154 case elfcpp::R_POWERPC_GOT_TPREL16:
2155 case elfcpp::R_POWERPC_TLS:
2156 return Symbol::TLS_REF;
2157
2158 case elfcpp::R_POWERPC_COPY:
2159 case elfcpp::R_POWERPC_GLOB_DAT:
2160 case elfcpp::R_POWERPC_JMP_SLOT:
2161 case elfcpp::R_POWERPC_RELATIVE:
2162 case elfcpp::R_POWERPC_DTPMOD:
2163 default:
2164 // Not expected. We will give an error later.
2165 return 0;
2166 }
2167 }
2168
2169 // Report an unsupported relocation against a local symbol.
2170
2171 template<int size, bool big_endian>
2172 void
2173 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2174 Sized_relobj_file<size, big_endian>* object,
2175 unsigned int r_type)
2176 {
2177 gold_error(_("%s: unsupported reloc %u against local symbol"),
2178 object->name().c_str(), r_type);
2179 }
2180
2181 // We are about to emit a dynamic relocation of type R_TYPE. If the
2182 // dynamic linker does not support it, issue an error.
2183
2184 template<int size, bool big_endian>
2185 void
2186 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2187 unsigned int r_type)
2188 {
2189 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2190
2191 // These are the relocation types supported by glibc for both 32-bit
2192 // and 64-bit powerpc.
2193 switch (r_type)
2194 {
2195 case elfcpp::R_POWERPC_NONE:
2196 case elfcpp::R_POWERPC_RELATIVE:
2197 case elfcpp::R_POWERPC_GLOB_DAT:
2198 case elfcpp::R_POWERPC_DTPMOD:
2199 case elfcpp::R_POWERPC_DTPREL:
2200 case elfcpp::R_POWERPC_TPREL:
2201 case elfcpp::R_POWERPC_JMP_SLOT:
2202 case elfcpp::R_POWERPC_COPY:
2203 case elfcpp::R_POWERPC_IRELATIVE:
2204 case elfcpp::R_POWERPC_ADDR32:
2205 case elfcpp::R_POWERPC_UADDR32:
2206 case elfcpp::R_POWERPC_ADDR24:
2207 case elfcpp::R_POWERPC_ADDR16:
2208 case elfcpp::R_POWERPC_UADDR16:
2209 case elfcpp::R_POWERPC_ADDR16_LO:
2210 case elfcpp::R_POWERPC_ADDR16_HI:
2211 case elfcpp::R_POWERPC_ADDR16_HA:
2212 case elfcpp::R_POWERPC_ADDR14:
2213 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2214 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2215 case elfcpp::R_POWERPC_REL32:
2216 case elfcpp::R_POWERPC_REL24:
2217 case elfcpp::R_POWERPC_TPREL16:
2218 case elfcpp::R_POWERPC_TPREL16_LO:
2219 case elfcpp::R_POWERPC_TPREL16_HI:
2220 case elfcpp::R_POWERPC_TPREL16_HA:
2221 return;
2222
2223 default:
2224 break;
2225 }
2226
2227 if (size == 64)
2228 {
2229 switch (r_type)
2230 {
2231 // These are the relocation types supported only on 64-bit.
2232 case elfcpp::R_PPC64_ADDR64:
2233 case elfcpp::R_PPC64_UADDR64:
2234 case elfcpp::R_PPC64_JMP_IREL:
2235 case elfcpp::R_PPC64_ADDR16_DS:
2236 case elfcpp::R_PPC64_ADDR16_LO_DS:
2237 case elfcpp::R_PPC64_ADDR16_HIGHER:
2238 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2239 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2240 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2241 case elfcpp::R_PPC64_REL64:
2242 case elfcpp::R_POWERPC_ADDR30:
2243 case elfcpp::R_PPC64_TPREL16_DS:
2244 case elfcpp::R_PPC64_TPREL16_LO_DS:
2245 case elfcpp::R_PPC64_TPREL16_HIGHER:
2246 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2247 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2248 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2249 return;
2250
2251 default:
2252 break;
2253 }
2254 }
2255 else
2256 {
2257 switch (r_type)
2258 {
2259 // These are the relocation types supported only on 32-bit.
2260 // ??? glibc ld.so doesn't need to support these.
2261 case elfcpp::R_POWERPC_DTPREL16:
2262 case elfcpp::R_POWERPC_DTPREL16_LO:
2263 case elfcpp::R_POWERPC_DTPREL16_HI:
2264 case elfcpp::R_POWERPC_DTPREL16_HA:
2265 return;
2266
2267 default:
2268 break;
2269 }
2270 }
2271
2272 // This prevents us from issuing more than one error per reloc
2273 // section. But we can still wind up issuing more than one
2274 // error per object file.
2275 if (this->issued_non_pic_error_)
2276 return;
2277 gold_assert(parameters->options().output_is_position_independent());
2278 object->error(_("requires unsupported dynamic reloc; "
2279 "recompile with -fPIC"));
2280 this->issued_non_pic_error_ = true;
2281 return;
2282 }
2283
2284 // Scan a relocation for a local symbol.
2285
2286 template<int size, bool big_endian>
2287 inline void
2288 Target_powerpc<size, big_endian>::Scan::local(
2289 Symbol_table* symtab,
2290 Layout* layout,
2291 Target_powerpc<size, big_endian>* target,
2292 Sized_relobj_file<size, big_endian>* object,
2293 unsigned int data_shndx,
2294 Output_section* output_section,
2295 const elfcpp::Rela<size, big_endian>& reloc,
2296 unsigned int r_type,
2297 const elfcpp::Sym<size, big_endian>& lsym)
2298 {
2299 Powerpc_relobj<size, big_endian>* ppc_object
2300 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2301
2302 switch (r_type)
2303 {
2304 case elfcpp::R_POWERPC_NONE:
2305 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2306 case elfcpp::R_POWERPC_GNU_VTENTRY:
2307 case elfcpp::R_PPC64_TOCSAVE:
2308 case elfcpp::R_PPC_EMB_MRKREF:
2309 case elfcpp::R_POWERPC_TLS:
2310 break;
2311
2312 case elfcpp::R_PPC64_TOC:
2313 {
2314 Output_data_got_powerpc<size, big_endian>* got
2315 = target->got_section(symtab, layout);
2316 if (parameters->options().output_is_position_independent())
2317 {
2318 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2319 rela_dyn->add_output_section_relative(got->output_section(),
2320 elfcpp::R_POWERPC_RELATIVE,
2321 output_section,
2322 object, data_shndx,
2323 reloc.get_r_offset(),
2324 ppc_object->toc_base_offset());
2325 }
2326 }
2327 break;
2328
2329 case elfcpp::R_PPC64_ADDR64:
2330 case elfcpp::R_PPC64_UADDR64:
2331 case elfcpp::R_POWERPC_ADDR32:
2332 case elfcpp::R_POWERPC_UADDR32:
2333 case elfcpp::R_POWERPC_ADDR24:
2334 case elfcpp::R_POWERPC_ADDR16:
2335 case elfcpp::R_POWERPC_ADDR16_LO:
2336 case elfcpp::R_POWERPC_ADDR16_HI:
2337 case elfcpp::R_POWERPC_ADDR16_HA:
2338 case elfcpp::R_POWERPC_UADDR16:
2339 case elfcpp::R_PPC64_ADDR16_HIGHER:
2340 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2341 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2342 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2343 case elfcpp::R_PPC64_ADDR16_DS:
2344 case elfcpp::R_PPC64_ADDR16_LO_DS:
2345 case elfcpp::R_POWERPC_ADDR14:
2346 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2347 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2348 // If building a shared library (or a position-independent
2349 // executable), we need to create a dynamic relocation for
2350 // this location.
2351 if (parameters->options().output_is_position_independent())
2352 {
2353 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2354
2355 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2356 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2357 {
2358 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2359 rela_dyn->add_local_relative(object, r_sym,
2360 elfcpp::R_POWERPC_RELATIVE,
2361 output_section, data_shndx,
2362 reloc.get_r_offset(),
2363 reloc.get_r_addend(), false);
2364 }
2365 else
2366 {
2367 check_non_pic(object, r_type);
2368 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2369 rela_dyn->add_local(object, r_sym, r_type, output_section,
2370 data_shndx, reloc.get_r_offset(),
2371 reloc.get_r_addend());
2372 }
2373 }
2374 break;
2375
2376 case elfcpp::R_PPC64_REL64:
2377 case elfcpp::R_POWERPC_REL32:
2378 case elfcpp::R_POWERPC_REL24:
2379 case elfcpp::R_PPC_LOCAL24PC:
2380 case elfcpp::R_POWERPC_REL16:
2381 case elfcpp::R_POWERPC_REL16_LO:
2382 case elfcpp::R_POWERPC_REL16_HI:
2383 case elfcpp::R_POWERPC_REL16_HA:
2384 case elfcpp::R_POWERPC_REL14:
2385 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2386 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2387 case elfcpp::R_POWERPC_SECTOFF:
2388 case elfcpp::R_POWERPC_TPREL16:
2389 case elfcpp::R_POWERPC_DTPREL16:
2390 case elfcpp::R_POWERPC_SECTOFF_LO:
2391 case elfcpp::R_POWERPC_TPREL16_LO:
2392 case elfcpp::R_POWERPC_DTPREL16_LO:
2393 case elfcpp::R_POWERPC_SECTOFF_HI:
2394 case elfcpp::R_POWERPC_TPREL16_HI:
2395 case elfcpp::R_POWERPC_DTPREL16_HI:
2396 case elfcpp::R_POWERPC_SECTOFF_HA:
2397 case elfcpp::R_POWERPC_TPREL16_HA:
2398 case elfcpp::R_POWERPC_DTPREL16_HA:
2399 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2400 case elfcpp::R_PPC64_TPREL16_HIGHER:
2401 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2402 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2403 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2404 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2405 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2406 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2407 case elfcpp::R_PPC64_TPREL16_DS:
2408 case elfcpp::R_PPC64_TPREL16_LO_DS:
2409 case elfcpp::R_PPC64_DTPREL16_DS:
2410 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2411 case elfcpp::R_PPC64_SECTOFF_DS:
2412 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2413 case elfcpp::R_PPC64_TLSGD:
2414 case elfcpp::R_PPC64_TLSLD:
2415 break;
2416
2417 case elfcpp::R_POWERPC_GOT16:
2418 case elfcpp::R_POWERPC_GOT16_LO:
2419 case elfcpp::R_POWERPC_GOT16_HI:
2420 case elfcpp::R_POWERPC_GOT16_HA:
2421 case elfcpp::R_PPC64_GOT16_DS:
2422 case elfcpp::R_PPC64_GOT16_LO_DS:
2423 {
2424 // The symbol requires a GOT entry.
2425 Output_data_got_powerpc<size, big_endian>* got
2426 = target->got_section(symtab, layout);
2427 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2428
2429 // If we are generating a shared object, we need to add a
2430 // dynamic relocation for this symbol's GOT entry.
2431 if (parameters->options().output_is_position_independent())
2432 {
2433 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2434 {
2435 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2436 unsigned int off;
2437
2438 off = got->add_constant(0);
2439 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2440 rela_dyn->add_local_relative(object, r_sym,
2441 elfcpp::R_POWERPC_RELATIVE,
2442 got, off, 0, false);
2443 }
2444 }
2445 else
2446 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2447 }
2448 break;
2449
2450 case elfcpp::R_PPC64_TOC16:
2451 case elfcpp::R_PPC64_TOC16_LO:
2452 case elfcpp::R_PPC64_TOC16_HI:
2453 case elfcpp::R_PPC64_TOC16_HA:
2454 case elfcpp::R_PPC64_TOC16_DS:
2455 case elfcpp::R_PPC64_TOC16_LO_DS:
2456 // We need a GOT section.
2457 target->got_section(symtab, layout);
2458 break;
2459
2460 case elfcpp::R_POWERPC_GOT_TLSGD16:
2461 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2462 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2463 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2464 {
2465 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2466 if (tls_type == tls::TLSOPT_NONE)
2467 {
2468 Output_data_got_powerpc<size, big_endian>* got
2469 = target->got_section(symtab, layout);
2470 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2471 unsigned int shndx = lsym.get_st_shndx();
2472 bool is_ordinary;
2473 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2474 gold_assert(is_ordinary);
2475 got->add_local_pair_with_rel(object, r_sym,
2476 shndx,
2477 GOT_TYPE_TLSGD,
2478 target->rela_dyn_section(layout),
2479 elfcpp::R_POWERPC_DTPMOD,
2480 elfcpp::R_POWERPC_DTPREL);
2481 }
2482 else if (tls_type == tls::TLSOPT_TO_LE)
2483 {
2484 // no GOT relocs needed for Local Exec.
2485 }
2486 else
2487 gold_unreachable();
2488 }
2489 break;
2490
2491 case elfcpp::R_POWERPC_GOT_TLSLD16:
2492 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2493 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2494 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2495 {
2496 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2497 if (tls_type == tls::TLSOPT_NONE)
2498 target->tlsld_got_offset(symtab, layout, object);
2499 else if (tls_type == tls::TLSOPT_TO_LE)
2500 {
2501 // no GOT relocs needed for Local Exec.
2502 if (parameters->options().emit_relocs())
2503 {
2504 Output_section* os = layout->tls_segment()->first_section();
2505 gold_assert(os != NULL);
2506 os->set_needs_symtab_index();
2507 }
2508 }
2509 else
2510 gold_unreachable();
2511 }
2512 break;
2513
2514 case elfcpp::R_POWERPC_GOT_DTPREL16:
2515 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2516 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2517 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2518 {
2519 Output_data_got_powerpc<size, big_endian>* got
2520 = target->got_section(symtab, layout);
2521 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2522 got->add_local_with_rel(object, r_sym, GOT_TYPE_DTPREL,
2523 target->rela_dyn_section(layout),
2524 elfcpp::R_POWERPC_DTPREL);
2525 }
2526 break;
2527
2528 case elfcpp::R_POWERPC_GOT_TPREL16:
2529 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2530 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2531 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2532 {
2533 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2534 if (tls_type == tls::TLSOPT_NONE)
2535 {
2536 Output_data_got_powerpc<size, big_endian>* got
2537 = target->got_section(symtab, layout);
2538 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2539 got->add_local_with_rel(object, r_sym, GOT_TYPE_TPREL,
2540 target->rela_dyn_section(layout),
2541 elfcpp::R_POWERPC_TPREL);
2542 }
2543 else if (tls_type == tls::TLSOPT_TO_LE)
2544 {
2545 // no GOT relocs needed for Local Exec.
2546 }
2547 else
2548 gold_unreachable();
2549 }
2550 break;
2551
2552 default:
2553 unsupported_reloc_local(object, r_type);
2554 break;
2555 }
2556 }
2557
2558 // Report an unsupported relocation against a global symbol.
2559
2560 template<int size, bool big_endian>
2561 void
2562 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2563 Sized_relobj_file<size, big_endian>* object,
2564 unsigned int r_type,
2565 Symbol* gsym)
2566 {
2567 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2568 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2569 }
2570
2571 // Scan a relocation for a global symbol.
2572
2573 template<int size, bool big_endian>
2574 inline void
2575 Target_powerpc<size, big_endian>::Scan::global(
2576 Symbol_table* symtab,
2577 Layout* layout,
2578 Target_powerpc<size, big_endian>* target,
2579 Sized_relobj_file<size, big_endian>* object,
2580 unsigned int data_shndx,
2581 Output_section* output_section,
2582 const elfcpp::Rela<size, big_endian>& reloc,
2583 unsigned int r_type,
2584 Symbol* gsym)
2585 {
2586 Powerpc_relobj<size, big_endian>* ppc_object
2587 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2588
2589 switch (r_type)
2590 {
2591 case elfcpp::R_POWERPC_NONE:
2592 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2593 case elfcpp::R_POWERPC_GNU_VTENTRY:
2594 case elfcpp::R_PPC_LOCAL24PC:
2595 case elfcpp::R_PPC_EMB_MRKREF:
2596 case elfcpp::R_POWERPC_TLS:
2597 break;
2598
2599 case elfcpp::R_PPC64_TOC:
2600 {
2601 Output_data_got_powerpc<size, big_endian>* got
2602 = target->got_section(symtab, layout);
2603 if (parameters->options().output_is_position_independent())
2604 {
2605 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2606 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2607 if (data_shndx != ppc_object->opd_shndx())
2608 symobj = static_cast
2609 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2610 rela_dyn->add_output_section_relative(got->output_section(),
2611 elfcpp::R_POWERPC_RELATIVE,
2612 output_section,
2613 object, data_shndx,
2614 reloc.get_r_offset(),
2615 symobj->toc_base_offset());
2616 }
2617 }
2618 break;
2619
2620 case elfcpp::R_PPC64_ADDR64:
2621 case elfcpp::R_PPC64_UADDR64:
2622 case elfcpp::R_POWERPC_ADDR32:
2623 case elfcpp::R_POWERPC_UADDR32:
2624 case elfcpp::R_POWERPC_ADDR24:
2625 case elfcpp::R_POWERPC_ADDR16:
2626 case elfcpp::R_POWERPC_ADDR16_LO:
2627 case elfcpp::R_POWERPC_ADDR16_HI:
2628 case elfcpp::R_POWERPC_ADDR16_HA:
2629 case elfcpp::R_POWERPC_UADDR16:
2630 case elfcpp::R_PPC64_ADDR16_HIGHER:
2631 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2632 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2633 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2634 case elfcpp::R_PPC64_ADDR16_DS:
2635 case elfcpp::R_PPC64_ADDR16_LO_DS:
2636 case elfcpp::R_POWERPC_ADDR14:
2637 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2638 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2639 {
2640 // Make a PLT entry if necessary.
2641 if (gsym->needs_plt_entry())
2642 {
2643 target->make_plt_entry(layout, gsym, reloc, 0);
2644 // Since this is not a PC-relative relocation, we may be
2645 // taking the address of a function. In that case we need to
2646 // set the entry in the dynamic symbol table to the address of
2647 // the PLT entry.
2648 if (size == 32
2649 && gsym->is_from_dynobj() && !parameters->options().shared())
2650 gsym->set_needs_dynsym_value();
2651 }
2652 // Make a dynamic relocation if necessary.
2653 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2654 {
2655 if (gsym->may_need_copy_reloc())
2656 {
2657 target->copy_reloc(symtab, layout, object,
2658 data_shndx, output_section, gsym, reloc);
2659 }
2660 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2661 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2662 && (gsym->can_use_relative_reloc(false)
2663 || data_shndx == ppc_object->opd_shndx()))
2664 {
2665 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2666 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2667 output_section, object,
2668 data_shndx, reloc.get_r_offset(),
2669 reloc.get_r_addend(), false);
2670 }
2671 else
2672 {
2673 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2674 check_non_pic(object, r_type);
2675 rela_dyn->add_global(gsym, r_type, output_section,
2676 object, data_shndx,
2677 reloc.get_r_offset(),
2678 reloc.get_r_addend());
2679 }
2680 }
2681 }
2682 break;
2683
2684 case elfcpp::R_PPC_PLTREL24:
2685 case elfcpp::R_POWERPC_REL24:
2686 if (gsym->needs_plt_entry()
2687 || (!gsym->final_value_is_known()
2688 && (gsym->is_undefined()
2689 || gsym->is_from_dynobj()
2690 || gsym->is_preemptible())))
2691 target->make_plt_entry(layout, gsym, reloc, object);
2692 // Fall thru
2693
2694 case elfcpp::R_PPC64_REL64:
2695 case elfcpp::R_POWERPC_REL32:
2696 // Make a dynamic relocation if necessary.
2697 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2698 {
2699 if (gsym->may_need_copy_reloc())
2700 {
2701 target->copy_reloc(symtab, layout, object,
2702 data_shndx, output_section, gsym,
2703 reloc);
2704 }
2705 else
2706 {
2707 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2708 check_non_pic(object, r_type);
2709 rela_dyn->add_global(gsym, r_type, output_section, object,
2710 data_shndx, reloc.get_r_offset(),
2711 reloc.get_r_addend());
2712 }
2713 }
2714 break;
2715
2716 case elfcpp::R_POWERPC_REL16:
2717 case elfcpp::R_POWERPC_REL16_LO:
2718 case elfcpp::R_POWERPC_REL16_HI:
2719 case elfcpp::R_POWERPC_REL16_HA:
2720 case elfcpp::R_POWERPC_REL14:
2721 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2722 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2723 case elfcpp::R_POWERPC_SECTOFF:
2724 case elfcpp::R_POWERPC_TPREL16:
2725 case elfcpp::R_POWERPC_DTPREL16:
2726 case elfcpp::R_POWERPC_SECTOFF_LO:
2727 case elfcpp::R_POWERPC_TPREL16_LO:
2728 case elfcpp::R_POWERPC_DTPREL16_LO:
2729 case elfcpp::R_POWERPC_SECTOFF_HI:
2730 case elfcpp::R_POWERPC_TPREL16_HI:
2731 case elfcpp::R_POWERPC_DTPREL16_HI:
2732 case elfcpp::R_POWERPC_SECTOFF_HA:
2733 case elfcpp::R_POWERPC_TPREL16_HA:
2734 case elfcpp::R_POWERPC_DTPREL16_HA:
2735 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2736 case elfcpp::R_PPC64_TPREL16_HIGHER:
2737 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2738 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2739 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2740 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2741 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2742 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2743 case elfcpp::R_PPC64_TPREL16_DS:
2744 case elfcpp::R_PPC64_TPREL16_LO_DS:
2745 case elfcpp::R_PPC64_DTPREL16_DS:
2746 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2747 case elfcpp::R_PPC64_SECTOFF_DS:
2748 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2749 case elfcpp::R_PPC64_TLSGD:
2750 case elfcpp::R_PPC64_TLSLD:
2751 break;
2752
2753 case elfcpp::R_POWERPC_GOT16:
2754 case elfcpp::R_POWERPC_GOT16_LO:
2755 case elfcpp::R_POWERPC_GOT16_HI:
2756 case elfcpp::R_POWERPC_GOT16_HA:
2757 case elfcpp::R_PPC64_GOT16_DS:
2758 case elfcpp::R_PPC64_GOT16_LO_DS:
2759 {
2760 // The symbol requires a GOT entry.
2761 Output_data_got_powerpc<size, big_endian>* got;
2762
2763 got = target->got_section(symtab, layout);
2764 if (gsym->final_value_is_known())
2765 got->add_global(gsym, GOT_TYPE_STANDARD);
2766 else
2767 {
2768 // If this symbol is not fully resolved, we need to add a
2769 // dynamic relocation for it.
2770 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2771 if (gsym->is_from_dynobj()
2772 || gsym->is_undefined()
2773 || gsym->is_preemptible())
2774 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2775 elfcpp::R_POWERPC_GLOB_DAT);
2776 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2777 {
2778 unsigned int off = got->add_constant(0);
2779
2780 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2781 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2782 got, off, 0, false);
2783 }
2784 }
2785 }
2786 break;
2787
2788 case elfcpp::R_PPC64_TOC16:
2789 case elfcpp::R_PPC64_TOC16_LO:
2790 case elfcpp::R_PPC64_TOC16_HI:
2791 case elfcpp::R_PPC64_TOC16_HA:
2792 case elfcpp::R_PPC64_TOC16_DS:
2793 case elfcpp::R_PPC64_TOC16_LO_DS:
2794 // We need a GOT section.
2795 target->got_section(symtab, layout);
2796 break;
2797
2798 case elfcpp::R_POWERPC_GOT_TLSGD16:
2799 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2800 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2801 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2802 {
2803 const bool final = gsym->final_value_is_known();
2804 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2805 if (tls_type == tls::TLSOPT_NONE)
2806 {
2807 Output_data_got_powerpc<size, big_endian>* got
2808 = target->got_section(symtab, layout);
2809 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2810 target->rela_dyn_section(layout),
2811 elfcpp::R_POWERPC_DTPMOD,
2812 elfcpp::R_POWERPC_DTPREL);
2813 }
2814 else if (tls_type == tls::TLSOPT_TO_IE)
2815 {
2816 Output_data_got_powerpc<size, big_endian>* got
2817 = target->got_section(symtab, layout);
2818 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2819 target->rela_dyn_section(layout),
2820 elfcpp::R_POWERPC_TPREL);
2821 }
2822 else if (tls_type == tls::TLSOPT_TO_LE)
2823 {
2824 // no GOT relocs needed for Local Exec.
2825 }
2826 else
2827 gold_unreachable();
2828 }
2829 break;
2830
2831 case elfcpp::R_POWERPC_GOT_TLSLD16:
2832 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2833 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2834 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2835 {
2836 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2837 if (tls_type == tls::TLSOPT_NONE)
2838 target->tlsld_got_offset(symtab, layout, object);
2839 else if (tls_type == tls::TLSOPT_TO_LE)
2840 {
2841 // no GOT relocs needed for Local Exec.
2842 if (parameters->options().emit_relocs())
2843 {
2844 Output_section* os = layout->tls_segment()->first_section();
2845 gold_assert(os != NULL);
2846 os->set_needs_symtab_index();
2847 }
2848 }
2849 else
2850 gold_unreachable();
2851 }
2852 break;
2853
2854 case elfcpp::R_POWERPC_GOT_DTPREL16:
2855 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2856 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2857 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2858 {
2859 Output_data_got_powerpc<size, big_endian>* got
2860 = target->got_section(symtab, layout);
2861 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2862 target->rela_dyn_section(layout),
2863 elfcpp::R_POWERPC_DTPREL);
2864 }
2865 break;
2866
2867 case elfcpp::R_POWERPC_GOT_TPREL16:
2868 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2869 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2870 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2871 {
2872 const bool final = gsym->final_value_is_known();
2873 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2874 if (tls_type == tls::TLSOPT_NONE)
2875 {
2876 Output_data_got_powerpc<size, big_endian>* got
2877 = target->got_section(symtab, layout);
2878 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2879 target->rela_dyn_section(layout),
2880 elfcpp::R_POWERPC_TPREL);
2881 }
2882 else if (tls_type == tls::TLSOPT_TO_LE)
2883 {
2884 // no GOT relocs needed for Local Exec.
2885 }
2886 else
2887 gold_unreachable();
2888 }
2889 break;
2890
2891 default:
2892 unsupported_reloc_global(object, r_type, gsym);
2893 break;
2894 }
2895 }
2896
2897 // Process relocations for gc.
2898
2899 template<int size, bool big_endian>
2900 void
2901 Target_powerpc<size, big_endian>::gc_process_relocs(
2902 Symbol_table* symtab,
2903 Layout* layout,
2904 Sized_relobj_file<size, big_endian>* object,
2905 unsigned int data_shndx,
2906 unsigned int,
2907 const unsigned char* prelocs,
2908 size_t reloc_count,
2909 Output_section* output_section,
2910 bool needs_special_offset_handling,
2911 size_t local_symbol_count,
2912 const unsigned char* plocal_symbols)
2913 {
2914 typedef Target_powerpc<size, big_endian> Powerpc;
2915 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2916
2917 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2918 typename Target_powerpc::Relocatable_size_for_reloc>(
2919 symtab,
2920 layout,
2921 this,
2922 object,
2923 data_shndx,
2924 prelocs,
2925 reloc_count,
2926 output_section,
2927 needs_special_offset_handling,
2928 local_symbol_count,
2929 plocal_symbols);
2930 }
2931
2932 // Scan relocations for a section.
2933
2934 template<int size, bool big_endian>
2935 void
2936 Target_powerpc<size, big_endian>::scan_relocs(
2937 Symbol_table* symtab,
2938 Layout* layout,
2939 Sized_relobj_file<size, big_endian>* object,
2940 unsigned int data_shndx,
2941 unsigned int sh_type,
2942 const unsigned char* prelocs,
2943 size_t reloc_count,
2944 Output_section* output_section,
2945 bool needs_special_offset_handling,
2946 size_t local_symbol_count,
2947 const unsigned char* plocal_symbols)
2948 {
2949 typedef Target_powerpc<size, big_endian> Powerpc;
2950 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2951
2952 if (sh_type == elfcpp::SHT_REL)
2953 {
2954 gold_error(_("%s: unsupported REL reloc section"),
2955 object->name().c_str());
2956 return;
2957 }
2958
2959 if (size == 32)
2960 {
2961 static Output_data_space* sdata;
2962
2963 // Define _SDA_BASE_ at the start of the .sdata section.
2964 if (sdata == NULL)
2965 {
2966 // layout->find_output_section(".sdata") == NULL
2967 sdata = new Output_data_space(4, "** sdata");
2968 Output_section* os
2969 = layout->add_output_section_data(".sdata", 0,
2970 elfcpp::SHF_ALLOC
2971 | elfcpp::SHF_WRITE,
2972 sdata, ORDER_SMALL_DATA, false);
2973 symtab->define_in_output_data("_SDA_BASE_", NULL,
2974 Symbol_table::PREDEFINED,
2975 os, 32768, 0, elfcpp::STT_OBJECT,
2976 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2977 0, false, false);
2978 }
2979 }
2980
2981 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2982 symtab,
2983 layout,
2984 this,
2985 object,
2986 data_shndx,
2987 prelocs,
2988 reloc_count,
2989 output_section,
2990 needs_special_offset_handling,
2991 local_symbol_count,
2992 plocal_symbols);
2993 }
2994
2995 // Finalize the sections.
2996
2997 template<int size, bool big_endian>
2998 void
2999 Target_powerpc<size, big_endian>::do_finalize_sections(
3000 Layout* layout,
3001 const Input_objects*,
3002 Symbol_table*)
3003 {
3004 // Fill in some more dynamic tags.
3005 const Reloc_section* rel_plt = (this->plt_ == NULL
3006 ? NULL
3007 : this->plt_->rel_plt());
3008 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3009 this->rela_dyn_, true, size == 32);
3010
3011 Output_data_dynamic* odyn = layout->dynamic_data();
3012 if (size == 32)
3013 {
3014 if (this->got_ != NULL)
3015 {
3016 this->got_->finalize_data_size();
3017 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3018 this->got_, this->got_->g_o_t());
3019 }
3020 }
3021 else
3022 {
3023 if (this->glink_ != NULL)
3024 {
3025 this->glink_->finalize_data_size();
3026 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3027 this->glink_,
3028 (this->glink_->pltresolve()
3029 + this->glink_->pltresolve_size - 32));
3030 }
3031 }
3032
3033 // Emit any relocs we saved in an attempt to avoid generating COPY
3034 // relocs.
3035 if (this->copy_relocs_.any_saved_relocs())
3036 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3037 }
3038
3039 // Return the value to use for a branch relocation.
3040
3041 template<int size, bool big_endian>
3042 typename elfcpp::Elf_types<size>::Elf_Addr
3043 Target_powerpc<size, big_endian>::symval_for_branch(
3044 Address value,
3045 const Sized_symbol<size>* gsym,
3046 Powerpc_relobj<size, big_endian>* object,
3047 unsigned int *dest_shndx)
3048 {
3049 *dest_shndx = 0;
3050 if (size == 32)
3051 return value;
3052
3053 // If the symbol is defined in an opd section, ie. is a function
3054 // descriptor, use the function descriptor code entry address
3055 Powerpc_relobj<size, big_endian>* symobj = object;
3056 if (gsym != NULL)
3057 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3058 unsigned int shndx = symobj->opd_shndx();
3059 if (shndx == 0)
3060 return value;
3061 Address opd_addr = symobj->get_output_section_offset(shndx);
3062 gold_assert(opd_addr != invalid_address);
3063 opd_addr += symobj->output_section(shndx)->address();
3064 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3065 {
3066 Address sec_off;
3067 symobj->get_opd_ent(value - opd_addr, dest_shndx, &sec_off);
3068 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3069 gold_assert(sec_addr != invalid_address);
3070 sec_addr += symobj->output_section(*dest_shndx)->address();
3071 value = sec_addr + sec_off;
3072 }
3073 return value;
3074 }
3075
3076 // Perform a relocation.
3077
3078 template<int size, bool big_endian>
3079 inline bool
3080 Target_powerpc<size, big_endian>::Relocate::relocate(
3081 const Relocate_info<size, big_endian>* relinfo,
3082 Target_powerpc* target,
3083 Output_section* os,
3084 size_t relnum,
3085 const elfcpp::Rela<size, big_endian>& rela,
3086 unsigned int r_type,
3087 const Sized_symbol<size>* gsym,
3088 const Symbol_value<size>* psymval,
3089 unsigned char* view,
3090 Address address,
3091 section_size_type view_size)
3092 {
3093
3094 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3095 || r_type == elfcpp::R_PPC_PLTREL24)
3096 && gsym != NULL
3097 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3098 enum skip_tls last_tls = this->call_tls_get_addr_;
3099 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3100 if (is_tls_call)
3101 {
3102 if (last_tls == CALL_NOT_EXPECTED)
3103 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3104 _("__tls_get_addr call lacks marker reloc"));
3105 else if (last_tls == CALL_SKIP)
3106 return false;
3107 }
3108 else if (last_tls != CALL_NOT_EXPECTED)
3109 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3110 _("missing expected __tls_get_addr call"));
3111
3112 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3113 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3114 Powerpc_relobj<size, big_endian>* const object
3115 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3116 Address value = 0;
3117 bool has_plt_value = false;
3118 if (gsym != NULL
3119 && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3120 {
3121 const Output_data_glink<size, big_endian>* glink
3122 = target->glink_section();
3123 unsigned int glink_index = glink->find_entry(gsym, rela, object);
3124 value = glink->address() + glink_index * glink->glink_entry_size();
3125 has_plt_value = true;
3126 }
3127
3128 if (r_type == elfcpp::R_POWERPC_GOT16
3129 || r_type == elfcpp::R_POWERPC_GOT16_LO
3130 || r_type == elfcpp::R_POWERPC_GOT16_HI
3131 || r_type == elfcpp::R_POWERPC_GOT16_HA
3132 || r_type == elfcpp::R_PPC64_GOT16_DS
3133 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3134 {
3135 if (gsym != NULL)
3136 {
3137 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3138 value = gsym->got_offset(GOT_TYPE_STANDARD);
3139 }
3140 else
3141 {
3142 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3143 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3144 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3145 }
3146 value -= target->got_section()->got_base_offset(object);
3147 }
3148 else if (r_type == elfcpp::R_PPC64_TOC)
3149 {
3150 value = (target->got_section()->output_section()->address()
3151 + object->toc_base_offset());
3152 }
3153 else if (gsym != NULL
3154 && (r_type == elfcpp::R_POWERPC_REL24
3155 || r_type == elfcpp::R_PPC_PLTREL24)
3156 && has_plt_value)
3157 {
3158 if (size == 64)
3159 {
3160 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3161 Valtype* wv = reinterpret_cast<Valtype*>(view);
3162 bool can_plt_call = false;
3163 if (rela.get_r_offset() + 8 <= view_size)
3164 {
3165 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3166 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3167 if ((insn & 1) != 0
3168 && (insn2 == nop
3169 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
3170 {
3171 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3172 can_plt_call = true;
3173 }
3174 }
3175 if (!can_plt_call)
3176 {
3177 // If we don't have a branch and link followed by a nop,
3178 // we can't go via the plt because there is no place to
3179 // put a toc restoring instruction.
3180 // Unless we know we won't be returning.
3181 if (strcmp(gsym->name(), "__libc_start_main") == 0)
3182 can_plt_call = true;
3183 }
3184 if (!can_plt_call)
3185 {
3186 // This is not an error in one special case: A self
3187 // call. It isn't possible to cheaply verify we have
3188 // such a call so just check for a call to the same
3189 // section.
3190 bool ok = false;
3191 if (gsym->source() == Symbol::FROM_OBJECT
3192 && gsym->object() == object)
3193 {
3194 Address addend = rela.get_r_addend();
3195 unsigned int dest_shndx;
3196 value = psymval->value(object, addend);
3197 value = target->symval_for_branch(value, gsym, object,
3198 &dest_shndx);
3199 bool is_ordinary;
3200 if (dest_shndx == 0)
3201 dest_shndx = gsym->shndx(&is_ordinary);
3202 ok = dest_shndx == relinfo->data_shndx;
3203 }
3204 if (!ok)
3205 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3206 _("call lacks nop, can't restore toc; "
3207 "recompile with -fPIC"));
3208 }
3209 }
3210 }
3211 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3212 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3213 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3214 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3215 {
3216 // First instruction of a global dynamic sequence, arg setup insn.
3217 const bool final = gsym == NULL || gsym->final_value_is_known();
3218 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3219 enum Got_type got_type = GOT_TYPE_STANDARD;
3220 if (tls_type == tls::TLSOPT_NONE)
3221 got_type = GOT_TYPE_TLSGD;
3222 else if (tls_type == tls::TLSOPT_TO_IE)
3223 got_type = GOT_TYPE_TPREL;
3224 if (got_type != GOT_TYPE_STANDARD)
3225 {
3226 if (gsym != NULL)
3227 {
3228 gold_assert(gsym->has_got_offset(got_type));
3229 value = gsym->got_offset(got_type);
3230 }
3231 else
3232 {
3233 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3234 gold_assert(object->local_has_got_offset(r_sym, got_type));
3235 value = object->local_got_offset(r_sym, got_type);
3236 }
3237 value -= target->got_section()->got_base_offset(object);
3238 }
3239 if (tls_type == tls::TLSOPT_TO_IE)
3240 {
3241 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3242 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3243 {
3244 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3245 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3246 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3247 if (size == 32)
3248 insn |= 32 << 26; // lwz
3249 else
3250 insn |= 58 << 26; // ld
3251 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3252 }
3253 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3254 - elfcpp::R_POWERPC_GOT_TLSGD16);
3255 }
3256 else if (tls_type == tls::TLSOPT_TO_LE)
3257 {
3258 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3259 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3260 {
3261 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3262 Insn insn = addis_3_13;
3263 if (size == 32)
3264 insn = addis_3_2;
3265 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3266 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3267 value = psymval->value(object, rela.get_r_addend());
3268 }
3269 else
3270 {
3271 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3272 Insn insn = nop;
3273 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3274 r_type = elfcpp::R_POWERPC_NONE;
3275 }
3276 }
3277 }
3278 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3279 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3280 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3281 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3282 {
3283 // First instruction of a local dynamic sequence, arg setup insn.
3284 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3285 if (tls_type == tls::TLSOPT_NONE)
3286 {
3287 value = target->tlsld_got_offset();
3288 value -= target->got_section()->got_base_offset(object);
3289 }
3290 else
3291 {
3292 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3293 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3294 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3295 {
3296 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3297 Insn insn = addis_3_13;
3298 if (size == 32)
3299 insn = addis_3_2;
3300 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3301 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3302 value = dtp_offset;
3303 }
3304 else
3305 {
3306 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3307 Insn insn = nop;
3308 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3309 r_type = elfcpp::R_POWERPC_NONE;
3310 }
3311 }
3312 }
3313 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3314 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3315 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3316 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3317 {
3318 // Accesses relative to a local dynamic sequence address,
3319 // no optimisation here.
3320 if (gsym != NULL)
3321 {
3322 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3323 value = gsym->got_offset(GOT_TYPE_DTPREL);
3324 }
3325 else
3326 {
3327 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3328 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3329 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3330 }
3331 value -= target->got_section()->got_base_offset(object);
3332 }
3333 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3334 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3335 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3336 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3337 {
3338 // First instruction of initial exec sequence.
3339 const bool final = gsym == NULL || gsym->final_value_is_known();
3340 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3341 if (tls_type == tls::TLSOPT_NONE)
3342 {
3343 if (gsym != NULL)
3344 {
3345 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3346 value = gsym->got_offset(GOT_TYPE_TPREL);
3347 }
3348 else
3349 {
3350 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3351 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3352 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3353 }
3354 value -= target->got_section()->got_base_offset(object);
3355 }
3356 else
3357 {
3358 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3359 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3360 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3361 {
3362 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3363 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3364 insn &= (1 << 26) - (1 << 21); // extract rt from ld
3365 if (size == 32)
3366 insn |= addis_0_2;
3367 else
3368 insn |= addis_0_13;
3369 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3370 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3371 value = psymval->value(object, rela.get_r_addend());
3372 }
3373 else
3374 {
3375 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3376 Insn insn = nop;
3377 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3378 r_type = elfcpp::R_POWERPC_NONE;
3379 }
3380 }
3381 }
3382 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3383 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3384 {
3385 // Second instruction of a global dynamic sequence,
3386 // the __tls_get_addr call
3387 this->call_tls_get_addr_ = CALL_EXPECTED;
3388 const bool final = gsym == NULL || gsym->final_value_is_known();
3389 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3390 if (tls_type != tls::TLSOPT_NONE)
3391 {
3392 if (tls_type == tls::TLSOPT_TO_IE)
3393 {
3394 Insn* iview = reinterpret_cast<Insn*>(view);
3395 Insn insn = add_3_3_13;
3396 if (size == 32)
3397 insn = add_3_3_2;
3398 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3399 r_type = elfcpp::R_POWERPC_NONE;
3400 }
3401 else
3402 {
3403 Insn* iview = reinterpret_cast<Insn*>(view);
3404 Insn insn = addi_3_3;
3405 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3406 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3407 view += 2 * big_endian;
3408 value = psymval->value(object, rela.get_r_addend());
3409 }
3410 this->call_tls_get_addr_ = CALL_SKIP;
3411 }
3412 }
3413 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3414 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3415 {
3416 // Second instruction of a local dynamic sequence,
3417 // the __tls_get_addr call
3418 this->call_tls_get_addr_ = CALL_EXPECTED;
3419 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3420 if (tls_type == tls::TLSOPT_TO_LE)
3421 {
3422 Insn* iview = reinterpret_cast<Insn*>(view);
3423 Insn insn = addi_3_3;
3424 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3425 this->call_tls_get_addr_ = CALL_SKIP;
3426 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3427 view += 2 * big_endian;
3428 value = dtp_offset;
3429 }
3430 }
3431 else if (r_type == elfcpp::R_POWERPC_TLS)
3432 {
3433 // Second instruction of an initial exec sequence
3434 const bool final = gsym == NULL || gsym->final_value_is_known();
3435 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3436 if (tls_type == tls::TLSOPT_TO_LE)
3437 {
3438 Insn* iview = reinterpret_cast<Insn*>(view);
3439 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3440 unsigned int reg = size == 32 ? 2 : 13;
3441 insn = at_tls_transform(insn, reg);
3442 gold_assert(insn != 0);
3443 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3444 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3445 view += 2 * big_endian;
3446 value = psymval->value(object, rela.get_r_addend());
3447 }
3448 }
3449 else
3450 {
3451 Address addend = 0;
3452 unsigned int dest_shndx;
3453 if (r_type != elfcpp::R_PPC_PLTREL24)
3454 addend = rela.get_r_addend();
3455 if (size == 64 || !has_plt_value)
3456 value = psymval->value(object, addend);
3457 if (size == 64 && is_branch_reloc(r_type))
3458 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
3459 }
3460
3461 switch (r_type)
3462 {
3463 case elfcpp::R_PPC64_REL64:
3464 case elfcpp::R_POWERPC_REL32:
3465 case elfcpp::R_POWERPC_REL24:
3466 case elfcpp::R_PPC_PLTREL24:
3467 case elfcpp::R_PPC_LOCAL24PC:
3468 case elfcpp::R_POWERPC_REL16:
3469 case elfcpp::R_POWERPC_REL16_LO:
3470 case elfcpp::R_POWERPC_REL16_HI:
3471 case elfcpp::R_POWERPC_REL16_HA:
3472 case elfcpp::R_POWERPC_REL14:
3473 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3474 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3475 value -= address;
3476 break;
3477
3478 case elfcpp::R_PPC64_TOC16:
3479 case elfcpp::R_PPC64_TOC16_LO:
3480 case elfcpp::R_PPC64_TOC16_HI:
3481 case elfcpp::R_PPC64_TOC16_HA:
3482 case elfcpp::R_PPC64_TOC16_DS:
3483 case elfcpp::R_PPC64_TOC16_LO_DS:
3484 // Subtract the TOC base address.
3485 value -= (target->got_section()->output_section()->address()
3486 + object->toc_base_offset());
3487 break;
3488
3489 case elfcpp::R_POWERPC_SECTOFF:
3490 case elfcpp::R_POWERPC_SECTOFF_LO:
3491 case elfcpp::R_POWERPC_SECTOFF_HI:
3492 case elfcpp::R_POWERPC_SECTOFF_HA:
3493 case elfcpp::R_PPC64_SECTOFF_DS:
3494 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3495 if (os != NULL)
3496 value -= os->address();
3497 break;
3498
3499 case elfcpp::R_PPC64_TPREL16_DS:
3500 case elfcpp::R_PPC64_TPREL16_LO_DS:
3501 if (size != 64)
3502 // R_PPC_TLSGD and R_PPC_TLSLD
3503 break;
3504 case elfcpp::R_POWERPC_TPREL16:
3505 case elfcpp::R_POWERPC_TPREL16_LO:
3506 case elfcpp::R_POWERPC_TPREL16_HI:
3507 case elfcpp::R_POWERPC_TPREL16_HA:
3508 case elfcpp::R_POWERPC_TPREL:
3509 case elfcpp::R_PPC64_TPREL16_HIGHER:
3510 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3511 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3512 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3513 // tls symbol values are relative to tls_segment()->vaddr()
3514 value -= tp_offset;
3515 break;
3516
3517 case elfcpp::R_PPC64_DTPREL16_DS:
3518 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3519 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3520 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3521 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3522 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3523 if (size != 64)
3524 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3525 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3526 break;
3527 case elfcpp::R_POWERPC_DTPREL16:
3528 case elfcpp::R_POWERPC_DTPREL16_LO:
3529 case elfcpp::R_POWERPC_DTPREL16_HI:
3530 case elfcpp::R_POWERPC_DTPREL16_HA:
3531 case elfcpp::R_POWERPC_DTPREL:
3532 // tls symbol values are relative to tls_segment()->vaddr()
3533 value -= dtp_offset;
3534 break;
3535
3536 default:
3537 break;
3538 }
3539
3540 Insn branch_bit = 0;
3541 switch (r_type)
3542 {
3543 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3544 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3545 branch_bit = 1 << 21;
3546 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3547 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3548 {
3549 Insn* iview = reinterpret_cast<Insn*>(view);
3550 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3551 insn &= ~(1 << 21);
3552 insn |= branch_bit;
3553 if (this->is_isa_v2)
3554 {
3555 // Set 'a' bit. This is 0b00010 in BO field for branch
3556 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3557 // for branch on CTR insns (BO == 1a00t or 1a01t).
3558 if ((insn & (0x14 << 21)) == (0x04 << 21))
3559 insn |= 0x02 << 21;
3560 else if ((insn & (0x14 << 21)) == (0x10 << 21))
3561 insn |= 0x08 << 21;
3562 else
3563 break;
3564 }
3565 else
3566 {
3567 // Invert 'y' bit if not the default.
3568 if (static_cast<Signed_address>(value) < 0)
3569 insn ^= 1 << 21;
3570 }
3571 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3572 }
3573 break;
3574
3575 default:
3576 break;
3577 }
3578
3579 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
3580 switch (r_type)
3581 {
3582 case elfcpp::R_POWERPC_ADDR32:
3583 case elfcpp::R_POWERPC_UADDR32:
3584 if (size == 64)
3585 overflow = Reloc::CHECK_BITFIELD;
3586 break;
3587
3588 case elfcpp::R_POWERPC_REL32:
3589 if (size == 64)
3590 overflow = Reloc::CHECK_SIGNED;
3591 break;
3592
3593 case elfcpp::R_POWERPC_ADDR24:
3594 case elfcpp::R_POWERPC_ADDR16:
3595 case elfcpp::R_POWERPC_UADDR16:
3596 case elfcpp::R_PPC64_ADDR16_DS:
3597 case elfcpp::R_POWERPC_ADDR14:
3598 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3599 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3600 overflow = Reloc::CHECK_BITFIELD;
3601 break;
3602
3603 case elfcpp::R_POWERPC_REL24:
3604 case elfcpp::R_PPC_PLTREL24:
3605 case elfcpp::R_PPC_LOCAL24PC:
3606 case elfcpp::R_POWERPC_REL16:
3607 case elfcpp::R_PPC64_TOC16:
3608 case elfcpp::R_POWERPC_GOT16:
3609 case elfcpp::R_POWERPC_SECTOFF:
3610 case elfcpp::R_POWERPC_TPREL16:
3611 case elfcpp::R_POWERPC_DTPREL16:
3612 case elfcpp::R_PPC64_TPREL16_DS:
3613 case elfcpp::R_PPC64_DTPREL16_DS:
3614 case elfcpp::R_PPC64_TOC16_DS:
3615 case elfcpp::R_PPC64_GOT16_DS:
3616 case elfcpp::R_PPC64_SECTOFF_DS:
3617 case elfcpp::R_POWERPC_REL14:
3618 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3619 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3620 case elfcpp::R_POWERPC_GOT_TLSGD16:
3621 case elfcpp::R_POWERPC_GOT_TLSLD16:
3622 case elfcpp::R_POWERPC_GOT_TPREL16:
3623 case elfcpp::R_POWERPC_GOT_DTPREL16:
3624 overflow = Reloc::CHECK_SIGNED;
3625 break;
3626 }
3627
3628 typename Powerpc_relocate_functions<size, big_endian>::Status status
3629 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
3630 switch (r_type)
3631 {
3632 case elfcpp::R_POWERPC_NONE:
3633 case elfcpp::R_POWERPC_TLS:
3634 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3635 case elfcpp::R_POWERPC_GNU_VTENTRY:
3636 case elfcpp::R_PPC_EMB_MRKREF:
3637 break;
3638
3639 case elfcpp::R_PPC64_ADDR64:
3640 case elfcpp::R_PPC64_REL64:
3641 case elfcpp::R_PPC64_TOC:
3642 Reloc::addr64(view, value);
3643 break;
3644
3645 case elfcpp::R_POWERPC_TPREL:
3646 case elfcpp::R_POWERPC_DTPREL:
3647 if (size == 64)
3648 Reloc::addr64(view, value);
3649 else
3650 status = Reloc::addr32(view, value, overflow);
3651 break;
3652
3653 case elfcpp::R_PPC64_UADDR64:
3654 Reloc::addr64_u(view, value);
3655 break;
3656
3657 case elfcpp::R_POWERPC_ADDR32:
3658 case elfcpp::R_POWERPC_REL32:
3659 status = Reloc::addr32(view, value, overflow);
3660 break;
3661
3662 case elfcpp::R_POWERPC_UADDR32:
3663 status = Reloc::addr32_u(view, value, overflow);
3664 break;
3665
3666 case elfcpp::R_POWERPC_ADDR24:
3667 case elfcpp::R_POWERPC_REL24:
3668 case elfcpp::R_PPC_PLTREL24:
3669 case elfcpp::R_PPC_LOCAL24PC:
3670 status = Reloc::addr24(view, value, overflow);
3671 break;
3672
3673 case elfcpp::R_POWERPC_GOT_DTPREL16:
3674 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3675 if (size == 64)
3676 {
3677 status = Reloc::addr16_ds(view, value, overflow);
3678 break;
3679 }
3680 case elfcpp::R_POWERPC_ADDR16:
3681 case elfcpp::R_POWERPC_REL16:
3682 case elfcpp::R_PPC64_TOC16:
3683 case elfcpp::R_POWERPC_GOT16:
3684 case elfcpp::R_POWERPC_SECTOFF:
3685 case elfcpp::R_POWERPC_TPREL16:
3686 case elfcpp::R_POWERPC_DTPREL16:
3687 case elfcpp::R_POWERPC_GOT_TLSGD16:
3688 case elfcpp::R_POWERPC_GOT_TLSLD16:
3689 case elfcpp::R_POWERPC_GOT_TPREL16:
3690 case elfcpp::R_POWERPC_ADDR16_LO:
3691 case elfcpp::R_POWERPC_REL16_LO:
3692 case elfcpp::R_PPC64_TOC16_LO:
3693 case elfcpp::R_POWERPC_GOT16_LO:
3694 case elfcpp::R_POWERPC_SECTOFF_LO:
3695 case elfcpp::R_POWERPC_TPREL16_LO:
3696 case elfcpp::R_POWERPC_DTPREL16_LO:
3697 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3698 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3699 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3700 status = Reloc::addr16(view, value, overflow);
3701 break;
3702
3703 case elfcpp::R_POWERPC_UADDR16:
3704 status = Reloc::addr16_u(view, value, overflow);
3705 break;
3706
3707 case elfcpp::R_POWERPC_ADDR16_HI:
3708 case elfcpp::R_POWERPC_REL16_HI:
3709 case elfcpp::R_PPC64_TOC16_HI:
3710 case elfcpp::R_POWERPC_GOT16_HI:
3711 case elfcpp::R_POWERPC_SECTOFF_HI:
3712 case elfcpp::R_POWERPC_TPREL16_HI:
3713 case elfcpp::R_POWERPC_DTPREL16_HI:
3714 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3715 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3716 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3717 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3718 Reloc::addr16_hi(view, value);
3719 break;
3720
3721 case elfcpp::R_POWERPC_ADDR16_HA:
3722 case elfcpp::R_POWERPC_REL16_HA:
3723 case elfcpp::R_PPC64_TOC16_HA:
3724 case elfcpp::R_POWERPC_GOT16_HA:
3725 case elfcpp::R_POWERPC_SECTOFF_HA:
3726 case elfcpp::R_POWERPC_TPREL16_HA:
3727 case elfcpp::R_POWERPC_DTPREL16_HA:
3728 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3729 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3730 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3731 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3732 Reloc::addr16_ha(view, value);
3733 break;
3734
3735 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3736 if (size == 32)
3737 // R_PPC_EMB_NADDR16_LO
3738 goto unsupp;
3739 case elfcpp::R_PPC64_ADDR16_HIGHER:
3740 case elfcpp::R_PPC64_TPREL16_HIGHER:
3741 Reloc::addr16_hi2(view, value);
3742 break;
3743
3744 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3745 if (size == 32)
3746 // R_PPC_EMB_NADDR16_HI
3747 goto unsupp;
3748 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3749 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3750 Reloc::addr16_ha2(view, value);
3751 break;
3752
3753 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3754 if (size == 32)
3755 // R_PPC_EMB_NADDR16_HA
3756 goto unsupp;
3757 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3758 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3759 Reloc::addr16_hi3(view, value);
3760 break;
3761
3762 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3763 if (size == 32)
3764 // R_PPC_EMB_SDAI16
3765 goto unsupp;
3766 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3767 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3768 Reloc::addr16_ha3(view, value);
3769 break;
3770
3771 case elfcpp::R_PPC64_DTPREL16_DS:
3772 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3773 if (size == 32)
3774 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3775 goto unsupp;
3776 case elfcpp::R_PPC64_TPREL16_DS:
3777 case elfcpp::R_PPC64_TPREL16_LO_DS:
3778 if (size == 32)
3779 // R_PPC_TLSGD, R_PPC_TLSLD
3780 break;
3781 case elfcpp::R_PPC64_ADDR16_DS:
3782 case elfcpp::R_PPC64_ADDR16_LO_DS:
3783 case elfcpp::R_PPC64_TOC16_DS:
3784 case elfcpp::R_PPC64_TOC16_LO_DS:
3785 case elfcpp::R_PPC64_GOT16_DS:
3786 case elfcpp::R_PPC64_GOT16_LO_DS:
3787 case elfcpp::R_PPC64_SECTOFF_DS:
3788 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3789 status = Reloc::addr16_ds(view, value, overflow);
3790 break;
3791
3792 case elfcpp::R_POWERPC_ADDR14:
3793 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3794 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3795 case elfcpp::R_POWERPC_REL14:
3796 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3797 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3798 status = Reloc::addr14(view, value, overflow);
3799 break;
3800
3801 case elfcpp::R_POWERPC_COPY:
3802 case elfcpp::R_POWERPC_GLOB_DAT:
3803 case elfcpp::R_POWERPC_JMP_SLOT:
3804 case elfcpp::R_POWERPC_RELATIVE:
3805 case elfcpp::R_POWERPC_DTPMOD:
3806 case elfcpp::R_PPC64_JMP_IREL:
3807 case elfcpp::R_POWERPC_IRELATIVE:
3808 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3809 _("unexpected reloc %u in object file"),
3810 r_type);
3811 break;
3812
3813 case elfcpp::R_PPC_EMB_SDA21:
3814 if (size == 32)
3815 goto unsupp;
3816 else
3817 {
3818 // R_PPC64_TOCSAVE. For the time being this can be ignored.
3819 }
3820 break;
3821
3822 case elfcpp::R_PPC_EMB_SDA2I16:
3823 case elfcpp::R_PPC_EMB_SDA2REL:
3824 if (size == 32)
3825 goto unsupp;
3826 // R_PPC64_TLSGD, R_PPC64_TLSLD
3827 break;
3828
3829 case elfcpp::R_POWERPC_PLT32:
3830 case elfcpp::R_POWERPC_PLTREL32:
3831 case elfcpp::R_POWERPC_PLT16_LO:
3832 case elfcpp::R_POWERPC_PLT16_HI:
3833 case elfcpp::R_POWERPC_PLT16_HA:
3834 case elfcpp::R_PPC_SDAREL16:
3835 case elfcpp::R_POWERPC_ADDR30:
3836 case elfcpp::R_PPC64_PLT64:
3837 case elfcpp::R_PPC64_PLTREL64:
3838 case elfcpp::R_PPC64_PLTGOT16:
3839 case elfcpp::R_PPC64_PLTGOT16_LO:
3840 case elfcpp::R_PPC64_PLTGOT16_HI:
3841 case elfcpp::R_PPC64_PLTGOT16_HA:
3842 case elfcpp::R_PPC64_PLT16_LO_DS:
3843 case elfcpp::R_PPC64_PLTGOT16_DS:
3844 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3845 case elfcpp::R_PPC_EMB_RELSEC16:
3846 case elfcpp::R_PPC_EMB_RELST_LO:
3847 case elfcpp::R_PPC_EMB_RELST_HI:
3848 case elfcpp::R_PPC_EMB_RELST_HA:
3849 case elfcpp::R_PPC_EMB_BIT_FLD:
3850 case elfcpp::R_PPC_EMB_RELSDA:
3851 case elfcpp::R_PPC_TOC16:
3852 default:
3853 unsupp:
3854 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3855 _("unsupported reloc %u"),
3856 r_type);
3857 break;
3858 }
3859 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
3860 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3861 _("relocation overflow"));
3862
3863 return true;
3864 }
3865
3866 // Relocate section data.
3867
3868 template<int size, bool big_endian>
3869 void
3870 Target_powerpc<size, big_endian>::relocate_section(
3871 const Relocate_info<size, big_endian>* relinfo,
3872 unsigned int sh_type,
3873 const unsigned char* prelocs,
3874 size_t reloc_count,
3875 Output_section* output_section,
3876 bool needs_special_offset_handling,
3877 unsigned char* view,
3878 Address address,
3879 section_size_type view_size,
3880 const Reloc_symbol_changes* reloc_symbol_changes)
3881 {
3882 typedef Target_powerpc<size, big_endian> Powerpc;
3883 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
3884
3885 gold_assert(sh_type == elfcpp::SHT_RELA);
3886
3887 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
3888 Powerpc_relocate>(
3889 relinfo,
3890 this,
3891 prelocs,
3892 reloc_count,
3893 output_section,
3894 needs_special_offset_handling,
3895 view,
3896 address,
3897 view_size,
3898 reloc_symbol_changes);
3899 }
3900
3901 class Powerpc_scan_relocatable_reloc
3902 {
3903 public:
3904 // Return the strategy to use for a local symbol which is not a
3905 // section symbol, given the relocation type.
3906 inline Relocatable_relocs::Reloc_strategy
3907 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
3908 {
3909 if (r_type == 0 && r_sym == 0)
3910 return Relocatable_relocs::RELOC_DISCARD;
3911 return Relocatable_relocs::RELOC_COPY;
3912 }
3913
3914 // Return the strategy to use for a local symbol which is a section
3915 // symbol, given the relocation type.
3916 inline Relocatable_relocs::Reloc_strategy
3917 local_section_strategy(unsigned int, Relobj*)
3918 {
3919 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
3920 }
3921
3922 // Return the strategy to use for a global symbol, given the
3923 // relocation type, the object, and the symbol index.
3924 inline Relocatable_relocs::Reloc_strategy
3925 global_strategy(unsigned int r_type, Relobj*, unsigned int)
3926 {
3927 if (r_type == elfcpp::R_PPC_PLTREL24)
3928 return Relocatable_relocs::RELOC_SPECIAL;
3929 return Relocatable_relocs::RELOC_COPY;
3930 }
3931 };
3932
3933 // Scan the relocs during a relocatable link.
3934
3935 template<int size, bool big_endian>
3936 void
3937 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
3938 Symbol_table* symtab,
3939 Layout* layout,
3940 Sized_relobj_file<size, big_endian>* object,
3941 unsigned int data_shndx,
3942 unsigned int sh_type,
3943 const unsigned char* prelocs,
3944 size_t reloc_count,
3945 Output_section* output_section,
3946 bool needs_special_offset_handling,
3947 size_t local_symbol_count,
3948 const unsigned char* plocal_symbols,
3949 Relocatable_relocs* rr)
3950 {
3951 gold_assert(sh_type == elfcpp::SHT_RELA);
3952
3953 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3954 Powerpc_scan_relocatable_reloc>(
3955 symtab,
3956 layout,
3957 object,
3958 data_shndx,
3959 prelocs,
3960 reloc_count,
3961 output_section,
3962 needs_special_offset_handling,
3963 local_symbol_count,
3964 plocal_symbols,
3965 rr);
3966 }
3967
3968 // Emit relocations for a section.
3969 // This is a modified version of the function by the same name in
3970 // target-reloc.h. Using relocate_special_relocatable for
3971 // R_PPC_PLTREL24 would require duplication of the entire body of the
3972 // loop, so we may as well duplicate the whole thing.
3973
3974 template<int size, bool big_endian>
3975 void
3976 Target_powerpc<size, big_endian>::relocate_relocs(
3977 const Relocate_info<size, big_endian>* relinfo,
3978 unsigned int sh_type,
3979 const unsigned char* prelocs,
3980 size_t reloc_count,
3981 Output_section* output_section,
3982 off_t offset_in_output_section,
3983 const Relocatable_relocs* rr,
3984 unsigned char*,
3985 Address view_address,
3986 section_size_type,
3987 unsigned char* reloc_view,
3988 section_size_type reloc_view_size)
3989 {
3990 gold_assert(sh_type == elfcpp::SHT_RELA);
3991
3992 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3993 Reltype;
3994 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3995 Reltype_write;
3996 const int reloc_size
3997 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3998
3999 Powerpc_relobj<size, big_endian>* const object
4000 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4001 const unsigned int local_count = object->local_symbol_count();
4002 unsigned int got2_shndx = object->got2_shndx();
4003 Address got2_addend = 0;
4004 if (got2_shndx != 0)
4005 {
4006 got2_addend = object->get_output_section_offset(got2_shndx);
4007 gold_assert(got2_addend != invalid_address);
4008 }
4009
4010 unsigned char* pwrite = reloc_view;
4011 bool zap_next = false;
4012 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4013 {
4014 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4015 if (strategy == Relocatable_relocs::RELOC_DISCARD)
4016 continue;
4017
4018 Reltype reloc(prelocs);
4019 Reltype_write reloc_write(pwrite);
4020
4021 Address offset = reloc.get_r_offset();
4022 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
4023 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4024 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4025 const unsigned int orig_r_sym = r_sym;
4026 typename elfcpp::Elf_types<size>::Elf_Swxword addend
4027 = reloc.get_r_addend();
4028 const Symbol* gsym = NULL;
4029
4030 if (zap_next)
4031 {
4032 // We could arrange to discard these and other relocs for
4033 // tls optimised sequences in the strategy methods, but for
4034 // now do as BFD ld does.
4035 r_type = elfcpp::R_POWERPC_NONE;
4036 zap_next = false;
4037 }
4038
4039 // Get the new symbol index.
4040 if (r_sym < local_count)
4041 {
4042 switch (strategy)
4043 {
4044 case Relocatable_relocs::RELOC_COPY:
4045 case Relocatable_relocs::RELOC_SPECIAL:
4046 if (r_sym != 0)
4047 {
4048 r_sym = object->symtab_index(r_sym);
4049 gold_assert(r_sym != -1U);
4050 }
4051 break;
4052
4053 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4054 {
4055 // We are adjusting a section symbol. We need to find
4056 // the symbol table index of the section symbol for
4057 // the output section corresponding to input section
4058 // in which this symbol is defined.
4059 gold_assert(r_sym < local_count);
4060 bool is_ordinary;
4061 unsigned int shndx =
4062 object->local_symbol_input_shndx(r_sym, &is_ordinary);
4063 gold_assert(is_ordinary);
4064 Output_section* os = object->output_section(shndx);
4065 gold_assert(os != NULL);
4066 gold_assert(os->needs_symtab_index());
4067 r_sym = os->symtab_index();
4068 }
4069 break;
4070
4071 default:
4072 gold_unreachable();
4073 }
4074 }
4075 else
4076 {
4077 gsym = object->global_symbol(r_sym);
4078 gold_assert(gsym != NULL);
4079 if (gsym->is_forwarder())
4080 gsym = relinfo->symtab->resolve_forwards(gsym);
4081
4082 gold_assert(gsym->has_symtab_index());
4083 r_sym = gsym->symtab_index();
4084 }
4085
4086 // Get the new offset--the location in the output section where
4087 // this relocation should be applied.
4088 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4089 offset += offset_in_output_section;
4090 else
4091 {
4092 section_offset_type sot_offset =
4093 convert_types<section_offset_type, Address>(offset);
4094 section_offset_type new_sot_offset =
4095 output_section->output_offset(object, relinfo->data_shndx,
4096 sot_offset);
4097 gold_assert(new_sot_offset != -1);
4098 offset = new_sot_offset;
4099 }
4100
4101 // In an object file, r_offset is an offset within the section.
4102 // In an executable or dynamic object, generated by
4103 // --emit-relocs, r_offset is an absolute address.
4104 if (!parameters->options().relocatable())
4105 {
4106 offset += view_address;
4107 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4108 offset -= offset_in_output_section;
4109 }
4110
4111 // Handle the reloc addend based on the strategy.
4112 if (strategy == Relocatable_relocs::RELOC_COPY)
4113 ;
4114 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4115 {
4116 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
4117 addend = psymval->value(object, addend);
4118 }
4119 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4120 {
4121 if (addend >= 32768)
4122 addend += got2_addend;
4123 }
4124 else
4125 gold_unreachable();
4126
4127 if (!parameters->options().relocatable())
4128 {
4129 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4130 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4131 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4132 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4133 {
4134 // First instruction of a global dynamic sequence,
4135 // arg setup insn.
4136 const bool final = gsym == NULL || gsym->final_value_is_known();
4137 switch (this->optimize_tls_gd(final))
4138 {
4139 case tls::TLSOPT_TO_IE:
4140 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4141 - elfcpp::R_POWERPC_GOT_TLSGD16);
4142 break;
4143 case tls::TLSOPT_TO_LE:
4144 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4145 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4146 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4147 else
4148 {
4149 r_type = elfcpp::R_POWERPC_NONE;
4150 offset -= 2 * big_endian;
4151 }
4152 break;
4153 default:
4154 break;
4155 }
4156 }
4157 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4158 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4159 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4160 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4161 {
4162 // First instruction of a local dynamic sequence,
4163 // arg setup insn.
4164 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4165 {
4166 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4167 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4168 {
4169 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4170 const Output_section* os = relinfo->layout->tls_segment()
4171 ->first_section();
4172 gold_assert(os != NULL);
4173 gold_assert(os->needs_symtab_index());
4174 r_sym = os->symtab_index();
4175 addend = dtp_offset;
4176 }
4177 else
4178 {
4179 r_type = elfcpp::R_POWERPC_NONE;
4180 offset -= 2 * big_endian;
4181 }
4182 }
4183 }
4184 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4185 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4186 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4187 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4188 {
4189 // First instruction of initial exec sequence.
4190 const bool final = gsym == NULL || gsym->final_value_is_known();
4191 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4192 {
4193 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4194 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4195 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4196 else
4197 {
4198 r_type = elfcpp::R_POWERPC_NONE;
4199 offset -= 2 * big_endian;
4200 }
4201 }
4202 }
4203 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4204 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4205 {
4206 // Second instruction of a global dynamic sequence,
4207 // the __tls_get_addr call
4208 const bool final = gsym == NULL || gsym->final_value_is_known();
4209 switch (this->optimize_tls_gd(final))
4210 {
4211 case tls::TLSOPT_TO_IE:
4212 r_type = elfcpp::R_POWERPC_NONE;
4213 zap_next = true;
4214 break;
4215 case tls::TLSOPT_TO_LE:
4216 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4217 offset += 2 * big_endian;
4218 zap_next = true;
4219 break;
4220 default:
4221 break;
4222 }
4223 }
4224 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4225 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4226 {
4227 // Second instruction of a local dynamic sequence,
4228 // the __tls_get_addr call
4229 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4230 {
4231 const Output_section* os = relinfo->layout->tls_segment()
4232 ->first_section();
4233 gold_assert(os != NULL);
4234 gold_assert(os->needs_symtab_index());
4235 r_sym = os->symtab_index();
4236 addend = dtp_offset;
4237 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4238 offset += 2 * big_endian;
4239 zap_next = true;
4240 }
4241 }
4242 else if (r_type == elfcpp::R_POWERPC_TLS)
4243 {
4244 // Second instruction of an initial exec sequence
4245 const bool final = gsym == NULL || gsym->final_value_is_known();
4246 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4247 {
4248 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4249 offset += 2 * big_endian;
4250 }
4251 }
4252 }
4253
4254 reloc_write.put_r_offset(offset);
4255 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4256 reloc_write.put_r_addend(addend);
4257
4258 pwrite += reloc_size;
4259 }
4260
4261 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4262 == reloc_view_size);
4263 }
4264
4265 // Return the value to use for a dynamic which requires special
4266 // treatment. This is how we support equality comparisons of function
4267 // pointers across shared library boundaries, as described in the
4268 // processor specific ABI supplement.
4269
4270 template<int size, bool big_endian>
4271 uint64_t
4272 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4273 {
4274 if (size == 32)
4275 {
4276 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4277 return this->plt_section()->address() + gsym->plt_offset();
4278 }
4279 else
4280 gold_unreachable();
4281 }
4282
4283 // The selector for powerpc object files.
4284
4285 template<int size, bool big_endian>
4286 class Target_selector_powerpc : public Target_selector
4287 {
4288 public:
4289 Target_selector_powerpc()
4290 : Target_selector(elfcpp::EM_NONE, size, big_endian,
4291 (size == 64
4292 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4293 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4294 (size == 64
4295 ? (big_endian ? "elf64ppc" : "elf64lppc")
4296 : (big_endian ? "elf32ppc" : "elf32lppc")))
4297 { }
4298
4299 virtual Target*
4300 do_recognize(Input_file*, off_t, int machine, int, int)
4301 {
4302 switch (size)
4303 {
4304 case 64:
4305 if (machine != elfcpp::EM_PPC64)
4306 return NULL;
4307 break;
4308
4309 case 32:
4310 if (machine != elfcpp::EM_PPC)
4311 return NULL;
4312 break;
4313
4314 default:
4315 return NULL;
4316 }
4317
4318 return this->instantiate_target();
4319 }
4320
4321 virtual Target*
4322 do_instantiate_target()
4323 { return new Target_powerpc<size, big_endian>(); }
4324 };
4325
4326 Target_selector_powerpc<32, true> target_selector_ppc32;
4327 Target_selector_powerpc<32, false> target_selector_ppc32le;
4328 Target_selector_powerpc<64, true> target_selector_ppc64;
4329 Target_selector_powerpc<64, false> target_selector_ppc64le;
4330
4331 } // End anonymous namespace.