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