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