* powerpc.cc: Formatting and white space.
[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_Off Offset;
61
62 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
63 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
64 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
65 special_(0), opd_ent_shndx_(), opd_ent_off_()
66 { }
67
68 ~Powerpc_relobj()
69 { }
70
71 // The .got2 section shndx.
72 unsigned int
73 got2_shndx() const
74 {
75 if (size == 32)
76 return this->special_;
77 else
78 return 0;
79 }
80
81 // The .opd section shndx.
82 unsigned int
83 opd_shndx() const
84 {
85 if (size == 32)
86 return 0;
87 else
88 return this->special_;
89 }
90
91 // Init OPD entry arrays.
92 void
93 init_opd(size_t opd_size)
94 {
95 size_t count = this->opd_ent_ndx(opd_size);
96 this->opd_ent_shndx_.resize(count);
97 this->opd_ent_off_.reserve(count);
98 }
99
100 // Return section and offset of function entry for .opd + R_OFF.
101 void
102 get_opd_ent(typename elfcpp::Elf_types<size>::Elf_Addr r_off,
103 unsigned int* shndx,
104 typename elfcpp::Elf_types<size>::Elf_Addr* value)
105 {
106 size_t ndx = this->opd_ent_ndx(r_off);
107 gold_assert(ndx < this->opd_ent_shndx_.size());
108 gold_assert(this->opd_ent_shndx_[ndx] != 0);
109 *shndx = this->opd_ent_shndx_[ndx];
110 *value = this->opd_ent_off_[ndx];
111 }
112
113 // Set section and offset of function entry for .opd + R_OFF.
114 void
115 set_opd_ent(typename elfcpp::Elf_types<size>::Elf_Addr r_off,
116 unsigned int shndx,
117 typename elfcpp::Elf_types<size>::Elf_Addr value)
118 {
119 size_t ndx = this->opd_ent_ndx(r_off);
120 gold_assert(ndx < this->opd_ent_shndx_.size());
121 this->opd_ent_shndx_[ndx] = shndx;
122 this->opd_ent_off_[ndx] = value;
123 }
124
125 // Examine .rela.opd to build info about function entry points.
126 void
127 scan_opd_relocs(size_t reloc_count,
128 const unsigned char* prelocs,
129 const unsigned char* plocal_syms);
130
131 void
132 do_read_relocs(Read_relocs_data*);
133
134 bool
135 do_find_special_sections(Read_symbols_data* sd);
136
137 private:
138 // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
139 // at OFF. .opd entries are 24 bytes long, but they can be spaced
140 // 16 bytes apart when the language doesn't use the last 8-byte
141 // word, the environment pointer. Thus dividing the entry section
142 // offset by 16 will give an index into opd_ent_shndx_ and
143 // opd_ent_off_ that works for either layout of .opd. (It leaves
144 // some elements of the vectors unused when .opd entries are spaced
145 // 24 bytes apart, but we don't know the spacing until relocations
146 // are processed, and in any case it is possible for an object to
147 // have some entries spaced 16 bytes apart and others 24 bytes apart.)
148 size_t
149 opd_ent_ndx(size_t off) const
150 { return off >> 4;}
151
152 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
153 unsigned int special_;
154 // The first 8-byte word of an OPD entry gives the address of the
155 // entry point of the function. Relocatable object files have a
156 // relocation on this word. The following two vectors record the
157 // section and offset specified by these relocations.
158 std::vector<unsigned int> opd_ent_shndx_;
159 std::vector<Offset> opd_ent_off_;
160 };
161
162 template<int size, bool big_endian>
163 class Target_powerpc : public Sized_target<size, big_endian>
164 {
165 public:
166 typedef
167 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
168 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
169 static const Address invalid_address = static_cast<Address>(0) - 1;
170
171 Target_powerpc()
172 : Sized_target<size, big_endian>(&powerpc_info),
173 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
174 copy_relocs_(elfcpp::R_POWERPC_COPY),
175 dynbss_(NULL), got_mod_index_offset_(-1U)
176 {
177 }
178
179 // Process the relocations to determine unreferenced sections for
180 // garbage collection.
181 void
182 gc_process_relocs(Symbol_table* symtab,
183 Layout* layout,
184 Sized_relobj_file<size, big_endian>* object,
185 unsigned int data_shndx,
186 unsigned int sh_type,
187 const unsigned char* prelocs,
188 size_t reloc_count,
189 Output_section* output_section,
190 bool needs_special_offset_handling,
191 size_t local_symbol_count,
192 const unsigned char* plocal_symbols);
193
194 // Scan the relocations to look for symbol adjustments.
195 void
196 scan_relocs(Symbol_table* symtab,
197 Layout* layout,
198 Sized_relobj_file<size, big_endian>* object,
199 unsigned int data_shndx,
200 unsigned int sh_type,
201 const unsigned char* prelocs,
202 size_t reloc_count,
203 Output_section* output_section,
204 bool needs_special_offset_handling,
205 size_t local_symbol_count,
206 const unsigned char* plocal_symbols);
207
208 // Map input .toc section to output .got section.
209 const char*
210 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
211 {
212 if (size == 64 && strcmp(name, ".toc") == 0)
213 {
214 *plen = 4;
215 return ".got";
216 }
217 return NULL;
218 }
219
220 // Finalize the sections.
221 void
222 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
223
224 // Return the value to use for a dynamic which requires special
225 // treatment.
226 uint64_t
227 do_dynsym_value(const Symbol*) const;
228
229 // Relocate a section.
230 void
231 relocate_section(const Relocate_info<size, big_endian>*,
232 unsigned int sh_type,
233 const unsigned char* prelocs,
234 size_t reloc_count,
235 Output_section* output_section,
236 bool needs_special_offset_handling,
237 unsigned char* view,
238 Address view_address,
239 section_size_type view_size,
240 const Reloc_symbol_changes*);
241
242 // Scan the relocs during a relocatable link.
243 void
244 scan_relocatable_relocs(Symbol_table* symtab,
245 Layout* layout,
246 Sized_relobj_file<size, big_endian>* object,
247 unsigned int data_shndx,
248 unsigned int sh_type,
249 const unsigned char* prelocs,
250 size_t reloc_count,
251 Output_section* output_section,
252 bool needs_special_offset_handling,
253 size_t local_symbol_count,
254 const unsigned char* plocal_symbols,
255 Relocatable_relocs*);
256
257 // Relocate a section during a relocatable link.
258 void
259 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
260 unsigned int sh_type,
261 const unsigned char* prelocs,
262 size_t reloc_count,
263 Output_section* output_section,
264 off_t offset_in_output_section,
265 const Relocatable_relocs*,
266 unsigned char*,
267 Address,
268 section_size_type,
269 unsigned char* reloc_view,
270 section_size_type reloc_view_size);
271
272 // Return whether SYM is defined by the ABI.
273 bool
274 do_is_defined_by_abi(const Symbol* sym) const
275 {
276 return strcmp(sym->name(), "__tls_get_addr") == 0;
277 }
278
279 // Return the size of the GOT section.
280 section_size_type
281 got_size() const
282 {
283 gold_assert(this->got_ != NULL);
284 return this->got_->data_size();
285 }
286
287 // Get the PLT section.
288 const Output_data_plt_powerpc<size, big_endian>*
289 plt_section() const
290 {
291 gold_assert(this->plt_ != NULL);
292 return this->plt_;
293 }
294
295 // Get the .glink section.
296 const Output_data_glink<size, big_endian>*
297 glink_section() const
298 {
299 gold_assert(this->glink_ != NULL);
300 return this->glink_;
301 }
302
303 // Get the GOT section.
304 const Output_data_got_powerpc<size, big_endian>*
305 got_section() const
306 {
307 gold_assert(this->got_ != NULL);
308 return this->got_;
309 }
310
311 protected:
312 Object*
313 do_make_elf_object(const std::string&, Input_file*, off_t,
314 const elfcpp::Ehdr<size, big_endian>&);
315
316 // Return the number of entries in the GOT.
317 unsigned int
318 got_entry_count() const
319 {
320 if (this->got_ == NULL)
321 return 0;
322 return this->got_size() / (size / 8);
323 }
324
325 // Return the number of entries in the PLT.
326 unsigned int
327 plt_entry_count() const;
328
329 // Return the offset of the first non-reserved PLT entry.
330 unsigned int
331 first_plt_entry_offset() const;
332
333 // Return the size of each PLT entry.
334 unsigned int
335 plt_entry_size() const;
336
337 private:
338
339 // The class which scans relocations.
340 class Scan
341 {
342 public:
343 Scan()
344 : issued_non_pic_error_(false)
345 { }
346
347 static inline int
348 get_reference_flags(unsigned int r_type);
349
350 inline void
351 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
352 Sized_relobj_file<size, big_endian>* object,
353 unsigned int data_shndx,
354 Output_section* output_section,
355 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
356 const elfcpp::Sym<size, big_endian>& lsym);
357
358 inline void
359 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
360 Sized_relobj_file<size, big_endian>* object,
361 unsigned int data_shndx,
362 Output_section* output_section,
363 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
364 Symbol* gsym);
365
366 inline bool
367 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
368 Target_powerpc* ,
369 Sized_relobj_file<size, big_endian>* ,
370 unsigned int ,
371 Output_section* ,
372 const elfcpp::Rela<size, big_endian>& ,
373 unsigned int ,
374 const elfcpp::Sym<size, big_endian>&)
375 { return false; }
376
377 inline bool
378 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
379 Target_powerpc* ,
380 Sized_relobj_file<size, big_endian>* ,
381 unsigned int ,
382 Output_section* ,
383 const elfcpp::Rela<size,
384 big_endian>& ,
385 unsigned int , Symbol*)
386 { return false; }
387
388 private:
389 static void
390 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
391 unsigned int r_type);
392
393 static void
394 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
395 unsigned int r_type, Symbol*);
396
397 static void
398 generate_tls_call(Symbol_table* symtab, Layout* layout,
399 Target_powerpc* target);
400
401 void
402 check_non_pic(Relobj*, unsigned int r_type);
403
404 // Whether we have issued an error about a non-PIC compilation.
405 bool issued_non_pic_error_;
406 };
407
408 // The class which implements relocation.
409 class Relocate
410 {
411 public:
412 // Do a relocation. Return false if the caller should not issue
413 // any warnings about this relocation.
414 inline bool
415 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
416 Output_section*, size_t relnum,
417 const elfcpp::Rela<size, big_endian>&,
418 unsigned int r_type, const Sized_symbol<size>*,
419 const Symbol_value<size>*,
420 unsigned char*,
421 typename elfcpp::Elf_types<size>::Elf_Addr,
422 section_size_type);
423
424 inline bool
425 is_branch_reloc(unsigned int r_type) const
426 {
427 return (r_type == elfcpp::R_POWERPC_REL24
428 || r_type == elfcpp::R_POWERPC_REL14
429 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
430 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
431 || r_type == elfcpp::R_POWERPC_ADDR24
432 || r_type == elfcpp::R_POWERPC_ADDR14
433 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
434 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
435 }
436
437 private:
438 // Do a TLS relocation.
439 inline void
440 relocate_tls(const Relocate_info<size, big_endian>*,
441 Target_powerpc* target,
442 size_t relnum, const elfcpp::Rela<size, big_endian>&,
443 unsigned int r_type, const Sized_symbol<size>*,
444 const Symbol_value<size>*,
445 unsigned char*,
446 typename elfcpp::Elf_types<size>::Elf_Addr,
447 section_size_type);
448 };
449
450 // A class which returns the size required for a relocation type,
451 // used while scanning relocs during a relocatable link.
452 class Relocatable_size_for_reloc
453 {
454 public:
455 unsigned int
456 get_size_for_reloc(unsigned int, Relobj*)
457 {
458 gold_unreachable();
459 return 0;
460 }
461 };
462
463 // Adjust TLS relocation type based on the options and whether this
464 // is a local symbol.
465 static tls::Tls_optimization
466 optimize_tls_reloc(bool is_final, int r_type);
467
468 // Get the GOT section, creating it if necessary.
469 Output_data_got_powerpc<size, big_endian>*
470 got_section(Symbol_table*, Layout*);
471
472 // Create glink.
473 void
474 make_glink_section(Layout*);
475
476 // Create the PLT section.
477 void
478 make_plt_section(Layout*);
479
480 // Create a PLT entry for a global symbol.
481 void
482 make_plt_entry(Layout*, Symbol*,
483 const elfcpp::Rela<size, big_endian>&,
484 const Sized_relobj<size, big_endian>* object);
485
486 // Create a GOT entry for the TLS module index.
487 unsigned int
488 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
489 Sized_relobj_file<size, big_endian>* object);
490
491 // Get the dynamic reloc section, creating it if necessary.
492 Reloc_section*
493 rela_dyn_section(Layout*);
494
495 // Copy a relocation against a global symbol.
496 void
497 copy_reloc(Symbol_table* symtab, Layout* layout,
498 Sized_relobj_file<size, big_endian>* object,
499 unsigned int shndx, Output_section* output_section,
500 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
501 {
502 this->copy_relocs_.copy_reloc(symtab, layout,
503 symtab->get_sized_symbol<size>(sym),
504 object, shndx, output_section,
505 reloc, this->rela_dyn_section(layout));
506 }
507
508 // Information about this specific target which we pass to the
509 // general Target structure.
510 static Target::Target_info powerpc_info;
511
512 // The types of GOT entries needed for this platform.
513 // These values are exposed to the ABI in an incremental link.
514 // Do not renumber existing values without changing the version
515 // number of the .gnu_incremental_inputs section.
516 enum Got_type
517 {
518 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
519 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
520 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
521 };
522
523 // The GOT output section.
524 Output_data_got_powerpc<size, big_endian>* got_;
525 // The PLT output section.
526 Output_data_plt_powerpc<size, big_endian>* plt_;
527 // The .glink output section.
528 Output_data_glink<size, big_endian>* glink_;
529 // The dynamic reloc output section.
530 Reloc_section* rela_dyn_;
531 // Relocs saved to avoid a COPY reloc.
532 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
533 // Space for variables copied with a COPY reloc.
534 Output_data_space* dynbss_;
535 // Offset of the GOT entry for the TLS module index;
536 unsigned int got_mod_index_offset_;
537 };
538
539 template<>
540 Target::Target_info Target_powerpc<32, true>::powerpc_info =
541 {
542 32, // size
543 true, // is_big_endian
544 elfcpp::EM_PPC, // machine_code
545 false, // has_make_symbol
546 false, // has_resolve
547 false, // has_code_fill
548 true, // is_default_stack_executable
549 false, // can_icf_inline_merge_sections
550 '\0', // wrap_char
551 "/usr/lib/ld.so.1", // dynamic_linker
552 0x10000000, // default_text_segment_address
553 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
554 4 * 1024, // common_pagesize (overridable by -z common-page-size)
555 false, // isolate_execinstr
556 0, // rosegment_gap
557 elfcpp::SHN_UNDEF, // small_common_shndx
558 elfcpp::SHN_UNDEF, // large_common_shndx
559 0, // small_common_section_flags
560 0, // large_common_section_flags
561 NULL, // attributes_section
562 NULL // attributes_vendor
563 };
564
565 template<>
566 Target::Target_info Target_powerpc<32, false>::powerpc_info =
567 {
568 32, // size
569 false, // is_big_endian
570 elfcpp::EM_PPC, // machine_code
571 false, // has_make_symbol
572 false, // has_resolve
573 false, // has_code_fill
574 true, // is_default_stack_executable
575 false, // can_icf_inline_merge_sections
576 '\0', // wrap_char
577 "/usr/lib/ld.so.1", // dynamic_linker
578 0x10000000, // default_text_segment_address
579 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
580 4 * 1024, // common_pagesize (overridable by -z common-page-size)
581 false, // isolate_execinstr
582 0, // rosegment_gap
583 elfcpp::SHN_UNDEF, // small_common_shndx
584 elfcpp::SHN_UNDEF, // large_common_shndx
585 0, // small_common_section_flags
586 0, // large_common_section_flags
587 NULL, // attributes_section
588 NULL // attributes_vendor
589 };
590
591 template<>
592 Target::Target_info Target_powerpc<64, true>::powerpc_info =
593 {
594 64, // size
595 true, // is_big_endian
596 elfcpp::EM_PPC64, // machine_code
597 false, // has_make_symbol
598 false, // has_resolve
599 false, // has_code_fill
600 true, // is_default_stack_executable
601 false, // can_icf_inline_merge_sections
602 '\0', // wrap_char
603 "/usr/lib/ld.so.1", // dynamic_linker
604 0x10000000, // default_text_segment_address
605 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
606 8 * 1024, // common_pagesize (overridable by -z common-page-size)
607 false, // isolate_execinstr
608 0, // rosegment_gap
609 elfcpp::SHN_UNDEF, // small_common_shndx
610 elfcpp::SHN_UNDEF, // large_common_shndx
611 0, // small_common_section_flags
612 0, // large_common_section_flags
613 NULL, // attributes_section
614 NULL // attributes_vendor
615 };
616
617 template<>
618 Target::Target_info Target_powerpc<64, false>::powerpc_info =
619 {
620 64, // size
621 false, // is_big_endian
622 elfcpp::EM_PPC64, // machine_code
623 false, // has_make_symbol
624 false, // has_resolve
625 false, // has_code_fill
626 true, // is_default_stack_executable
627 false, // can_icf_inline_merge_sections
628 '\0', // wrap_char
629 "/usr/lib/ld.so.1", // dynamic_linker
630 0x10000000, // default_text_segment_address
631 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
632 8 * 1024, // common_pagesize (overridable by -z common-page-size)
633 false, // isolate_execinstr
634 0, // rosegment_gap
635 elfcpp::SHN_UNDEF, // small_common_shndx
636 elfcpp::SHN_UNDEF, // large_common_shndx
637 0, // small_common_section_flags
638 0, // large_common_section_flags
639 NULL, // attributes_section
640 NULL // attributes_vendor
641 };
642
643 template<int size, bool big_endian>
644 class Powerpc_relocate_functions
645 {
646 private:
647 typedef Powerpc_relocate_functions<size, big_endian> This;
648 typedef Relocate_functions<size, big_endian> This_reloc;
649 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
650
651 // Do a simple RELA relocation
652 template<int valsize>
653 static inline void
654 rela(unsigned char* view,
655 unsigned int right_shift,
656 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
657 Address value,
658 Address addend)
659 {
660 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
661 Valtype* wv = reinterpret_cast<Valtype*>(view);
662 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
663 Valtype reloc = (value + addend) >> right_shift;
664 val &= ~dst_mask;
665 reloc &= dst_mask;
666 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
667 }
668
669 // Do a simple RELA relocation, unaligned.
670 template<int valsize>
671 static inline void
672 rela_ua(unsigned char* view,
673 unsigned int right_shift,
674 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
675 Address value,
676 Address addend)
677 {
678 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
679 Valtype;
680 Valtype* wv = reinterpret_cast<Valtype*>(view);
681 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
682 Valtype reloc = (value + addend) >> right_shift;
683 val &= ~dst_mask;
684 reloc &= dst_mask;
685 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
686 }
687
688 public:
689 // R_POWERPC_REL32: (Symbol + Addend - Address)
690 static inline void
691 rel32(unsigned char* view, Address value, Address addend, Address address)
692 { This_reloc::pcrela32(view, value, addend, address); }
693
694 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
695 static inline void
696 rel24(unsigned char* view, Address value, Address addend, Address address)
697 { This::template rela<32>(view, 0, 0x03fffffc, value - address, addend); }
698
699 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
700 static inline void
701 rel14(unsigned char* view, Address value, Address addend, Address address)
702 { This::template rela<32>(view, 0, 0xfffc, value - address, addend); }
703
704 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
705 static inline void
706 addr16(unsigned char* view, Address value, Address addend)
707 { This_reloc::rela16(view, value, addend); }
708
709 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
710 static inline void
711 addr16_ds(unsigned char* view, Address value, Address addend)
712 { This::template rela<16>(view, 0, 0xfffc, value, addend); }
713
714 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
715 static inline void
716 addr16_lo(unsigned char* view, Address value, Address addend)
717 { This_reloc::rela16(view, value, addend); }
718
719 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
720 static inline void
721 addr16_hi(unsigned char* view, Address value, Address addend)
722 { This::template rela<16>(view, 16, 0xffff, value, addend); }
723
724 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
725 static inline void
726 addr16_ha(unsigned char* view, Address value, Address addend)
727 { This::addr16_hi(view, value + 0x8000, addend); }
728
729 // R_POWERPC_REL16: (Symbol + Addend - Address) & 0xffff
730 static inline void
731 rel16(unsigned char* view, Address value, Address addend, Address address)
732 { This_reloc::pcrela16(view, value, addend, address); }
733
734 // R_POWERPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
735 static inline void
736 rel16_lo(unsigned char* view, Address value, Address addend, Address address)
737 { This_reloc::pcrela16(view, value, addend, address); }
738
739 // R_POWERPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
740 static inline void
741 rel16_hi(unsigned char* view, Address value, Address addend, Address address)
742 { This::template rela<16>(view, 16, 0xffff, value - address, addend); }
743
744 // R_POWERPC_REL16_HA: ((Symbol + Addend + 0x8000 - Address) >> 16) & 0xffff
745 static inline void
746 rel16_ha(unsigned char* view, Address value, Address addend, Address address)
747 { This::rel16_hi(view, value + 0x8000, addend, address); }
748 };
749
750 // Stash away the index of .got2 or .opd in a relocatable object, if
751 // such a section exists.
752
753 template<int size, bool big_endian>
754 bool
755 Powerpc_relobj<size, big_endian>::do_find_special_sections(
756 Read_symbols_data* sd)
757 {
758 const unsigned char* const pshdrs = sd->section_headers->data();
759 const unsigned char* namesu = sd->section_names->data();
760 const char* names = reinterpret_cast<const char*>(namesu);
761 section_size_type names_size = sd->section_names_size;
762 const unsigned char* s;
763
764 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
765 names, names_size, NULL);
766 if (s != NULL)
767 {
768 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
769 this->special_ = ndx;
770 }
771 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
772 }
773
774 // Examine .rela.opd to build info about function entry points.
775
776 template<int size, bool big_endian>
777 void
778 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
779 size_t reloc_count,
780 const unsigned char* prelocs,
781 const unsigned char* plocal_syms)
782 {
783 if (size == 64)
784 {
785 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
786 Reltype;
787 const int reloc_size
788 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
789 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
790
791 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
792 {
793 Reltype reloc(prelocs);
794 typename elfcpp::Elf_types<size>::Elf_WXword r_info
795 = reloc.get_r_info();
796 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
797 if (r_type == elfcpp::R_PPC64_ADDR64)
798 {
799 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
800 typename elfcpp::Elf_types<size>::Elf_Addr value;
801 bool is_ordinary;
802 unsigned int shndx;
803 if (r_sym < this->local_symbol_count())
804 {
805 typename elfcpp::Sym<size, big_endian>
806 lsym(plocal_syms + r_sym * sym_size);
807 shndx = lsym.get_st_shndx();
808 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
809 value = lsym.get_st_value();
810 }
811 else
812 shndx = this->symbol_section_and_value(r_sym, &value,
813 &is_ordinary);
814 this->set_opd_ent(reloc.get_r_offset(), shndx,
815 value + reloc.get_r_addend());
816 }
817 }
818 }
819 }
820
821 template<int size, bool big_endian>
822 void
823 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
824 {
825 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
826 if (size == 64)
827 {
828 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
829 p != rd->relocs.end();
830 ++p)
831 {
832 if (p->data_shndx == this->opd_shndx())
833 {
834 this->init_opd(this->section_size(this->opd_shndx()));
835 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
836 rd->local_symbols->data());
837 break;
838 }
839 }
840 }
841 }
842
843 // Set up PowerPC target specific relobj.
844
845 template<int size, bool big_endian>
846 Object*
847 Target_powerpc<size, big_endian>::do_make_elf_object(
848 const std::string& name,
849 Input_file* input_file,
850 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
851 {
852 int et = ehdr.get_e_type();
853 if (et == elfcpp::ET_REL)
854 {
855 Powerpc_relobj<size, big_endian>* obj =
856 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
857 obj->setup();
858 return obj;
859 }
860 else if (et == elfcpp::ET_DYN)
861 {
862 Sized_dynobj<size, big_endian>* obj =
863 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
864 obj->setup();
865 return obj;
866 }
867 else
868 {
869 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
870 return NULL;
871 }
872 }
873
874 template<int size, bool big_endian>
875 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
876 {
877 public:
878 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
879 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
880
881 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
882 : Output_data_got<size, big_endian>(),
883 symtab_(symtab), layout_(layout),
884 header_ent_cnt_(size == 32 ? 3 : 1),
885 header_index_(size == 32 ? 0x2000 : 0)
886 {}
887
888 class Got_entry;
889
890 // Create a new GOT entry and return its offset.
891 unsigned int
892 add_got_entry(Got_entry got_entry)
893 {
894 this->reserve_ent();
895 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
896 }
897
898 // Create a pair of new GOT entries and return the offset of the first.
899 unsigned int
900 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
901 {
902 this->reserve_ent(2);
903 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
904 got_entry_2);
905 }
906
907 // Value of _GLOBAL_OFFSET_TABLE_
908 unsigned int
909 g_o_t() const
910 {
911 return this->got_offset(this->header_index_);
912 }
913
914 // Ensure our GOT has a header.
915 void
916 set_final_data_size()
917 {
918 if (this->header_ent_cnt_ != 0)
919 this->make_header();
920 Output_data_got<size, big_endian>::set_final_data_size();
921 }
922
923 // First word of GOT header needs some values that are not
924 // handled by Output_data_got so poke them in here.
925 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
926 void
927 do_write(Output_file* of)
928 {
929 this->replace_constant(this->header_index_,
930 (size == 32
931 ? this->layout_->dynamic_section()->address()
932 : this->address() + 0x8000));
933
934 Output_data_got<size, big_endian>::do_write(of);
935 }
936
937 private:
938 void
939 reserve_ent(unsigned int cnt = 1)
940 {
941 if (this->header_ent_cnt_ == 0)
942 return;
943 if (this->num_entries() + cnt > this->header_index_)
944 this->make_header();
945 }
946
947 void
948 make_header()
949 {
950 this->header_ent_cnt_ = 0;
951 this->header_index_ = this->num_entries();
952 if (size == 32)
953 {
954 Output_data_got<size, big_endian>::add_constant(0);
955 Output_data_got<size, big_endian>::add_constant(0);
956 Output_data_got<size, big_endian>::add_constant(0);
957
958 // Define _GLOBAL_OFFSET_TABLE_ at the header
959 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
960 Symbol_table::PREDEFINED,
961 this, this->g_o_t(), 0,
962 elfcpp::STT_OBJECT,
963 elfcpp::STB_LOCAL,
964 elfcpp::STV_HIDDEN,
965 0, false, false);
966 }
967 else
968 Output_data_got<size, big_endian>::add_constant(0);
969 }
970
971 // Stashed pointers.
972 Symbol_table* symtab_;
973 Layout* layout_;
974
975 // GOT header size.
976 unsigned int header_ent_cnt_;
977 // GOT header index.
978 unsigned int header_index_;
979 };
980
981 // Get the GOT section, creating it if necessary.
982
983 template<int size, bool big_endian>
984 Output_data_got_powerpc<size, big_endian>*
985 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
986 Layout* layout)
987 {
988 if (this->got_ == NULL)
989 {
990 gold_assert(symtab != NULL && layout != NULL);
991
992 this->got_
993 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
994
995 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
996 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
997 this->got_, ORDER_DATA, false);
998 }
999
1000 return this->got_;
1001 }
1002
1003 // Get the dynamic reloc section, creating it if necessary.
1004
1005 template<int size, bool big_endian>
1006 typename Target_powerpc<size, big_endian>::Reloc_section*
1007 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1008 {
1009 if (this->rela_dyn_ == NULL)
1010 {
1011 gold_assert(layout != NULL);
1012 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1013 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1014 elfcpp::SHF_ALLOC, this->rela_dyn_,
1015 ORDER_DYNAMIC_RELOCS, false);
1016 }
1017 return this->rela_dyn_;
1018 }
1019
1020 // A class to handle the PLT data.
1021
1022 template<int size, bool big_endian>
1023 class Output_data_plt_powerpc : public Output_section_data_build
1024 {
1025 public:
1026 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1027 size, big_endian> Reloc_section;
1028
1029 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1030
1031 // Add an entry to the PLT.
1032 void
1033 add_entry(Symbol*);
1034
1035 // Return the .rela.plt section data.
1036 const Reloc_section*
1037 rel_plt() const
1038 {
1039 return this->rel_;
1040 }
1041
1042 // Return the number of PLT entries.
1043 unsigned int
1044 entry_count() const
1045 {
1046 return ((this->current_data_size() - initial_plt_entry_size)
1047 / plt_entry_size);
1048 }
1049
1050 // Return the offset of the first non-reserved PLT entry.
1051 static unsigned int
1052 first_plt_entry_offset()
1053 { return initial_plt_entry_size; }
1054
1055 // Return the size of a PLT entry.
1056 static unsigned int
1057 get_plt_entry_size()
1058 { return plt_entry_size; }
1059
1060 protected:
1061 void
1062 do_adjust_output_section(Output_section* os)
1063 {
1064 os->set_entsize(0);
1065 }
1066
1067 // Write to a map file.
1068 void
1069 do_print_to_mapfile(Mapfile* mapfile) const
1070 { mapfile->print_output_data(this, _("** PLT")); }
1071
1072 private:
1073 // The size of an entry in the PLT.
1074 static const int plt_entry_size = size == 32 ? 4 : 24;
1075 // The size of the first reserved entry.
1076 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1077
1078 // Write out the PLT data.
1079 void
1080 do_write(Output_file*);
1081
1082 // The reloc section.
1083 Reloc_section* rel_;
1084 // Allows access to .glink for do_write.
1085 Target_powerpc<size, big_endian>* targ_;
1086 };
1087
1088 // Create the PLT section.
1089
1090 template<int size, bool big_endian>
1091 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1092 Layout* layout,
1093 Target_powerpc<size, big_endian>* targ)
1094 : Output_section_data_build(size == 32 ? 4 : 8),
1095 targ_(targ)
1096 {
1097 this->rel_ = new Reloc_section(false);
1098 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1099 elfcpp::SHF_ALLOC, this->rel_,
1100 ORDER_DYNAMIC_PLT_RELOCS, false);
1101 }
1102
1103 // Add an entry to the PLT.
1104
1105 template<int size, bool big_endian>
1106 void
1107 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1108 {
1109 if (!gsym->has_plt_offset())
1110 {
1111 off_t off = this->current_data_size();
1112
1113 if (off == 0)
1114 off += initial_plt_entry_size;
1115 gsym->set_plt_offset(off);
1116 gsym->set_needs_dynsym_entry();
1117 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1118 off += plt_entry_size;
1119 this->set_current_data_size(off);
1120 }
1121 }
1122
1123 static const uint32_t addis_11_11 = 0x3d6b0000;
1124 static const uint32_t addis_11_30 = 0x3d7e0000;
1125 static const uint32_t addis_12_12 = 0x3d8c0000;
1126 static const uint32_t addi_11_11 = 0x396b0000;
1127 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1128 static const uint32_t add_11_0_11 = 0x7d605a14;
1129 static const uint32_t b = 0x48000000;
1130 static const uint32_t bcl_20_31 = 0x429f0005;
1131 static const uint32_t bctr = 0x4e800420;
1132 static const uint32_t blrl = 0x4e800021;
1133 static const uint32_t lis_11 = 0x3d600000;
1134 static const uint32_t lis_12 = 0x3d800000;
1135 static const uint32_t lwzu_0_12 = 0x840c0000;
1136 static const uint32_t lwz_0_12 = 0x800c0000;
1137 static const uint32_t lwz_11_11 = 0x816b0000;
1138 static const uint32_t lwz_11_30 = 0x817e0000;
1139 static const uint32_t lwz_12_12 = 0x818c0000;
1140 static const uint32_t mflr_0 = 0x7c0802a6;
1141 static const uint32_t mflr_12 = 0x7d8802a6;
1142 static const uint32_t mtctr_0 = 0x7c0903a6;
1143 static const uint32_t mtctr_11 = 0x7d6903a6;
1144 static const uint32_t mtlr_0 = 0x7c0803a6;
1145 static const uint32_t nop = 0x60000000;
1146 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1147 static const uint32_t addis_12_2 = 0x3d820000;
1148 static const uint32_t std_2_1 = 0xf8410000;
1149 static const uint32_t ld_2_1 = 0xe8410000;
1150 static const uint32_t ld_11_12 = 0xe96c0000;
1151 static const uint32_t ld_2_12 = 0xe84c0000;
1152 static const uint32_t addi_12_12 = 0x398c0000;
1153 static const uint32_t ld_11_2 = 0xe9620000;
1154 static const uint32_t addi_2_2 = 0x38420000;
1155 static const uint32_t ld_2_2 = 0xe8420000;
1156 static const uint32_t mflr_11 = 0x7d6802a6;
1157 static const uint32_t ld_2_11 = 0xe84b0000;
1158 static const uint32_t mtlr_12 = 0x7d8803a6;
1159 static const uint32_t add_12_2_11 = 0x7d825a14;
1160 static const uint32_t li_0_0 = 0x38000000;
1161 static const uint32_t lis_0_0 = 0x3c000000;
1162 static const uint32_t ori_0_0_0 = 0x60000000;
1163 static const uint32_t cror_15_15_15 = 0x4def7b82;
1164 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1165
1166 // Write out the PLT.
1167
1168 template<int size, bool big_endian>
1169 void
1170 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1171 {
1172 if (size == 32)
1173 {
1174 const off_t offset = this->offset();
1175 const section_size_type oview_size
1176 = convert_to_section_size_type(this->data_size());
1177 unsigned char* const oview = of->get_output_view(offset, oview_size);
1178 unsigned char* pov = oview;
1179 unsigned char* endpov = oview + oview_size;
1180
1181 // The address the .glink branch table
1182 const Output_data_glink<size, big_endian>* glink
1183 = this->targ_->glink_section();
1184 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1185 = glink->address() + glink->pltresolve();
1186
1187 while (pov < endpov)
1188 {
1189 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1190 pov += 4;
1191 branch_tab += 4;
1192 }
1193
1194 of->write_output_view(offset, oview_size, oview);
1195 }
1196 }
1197
1198 // Create the PLT section.
1199
1200 template<int size, bool big_endian>
1201 void
1202 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1203 {
1204 if (this->plt_ == NULL)
1205 {
1206 if (this->glink_ == NULL)
1207 make_glink_section(layout);
1208
1209 // Ensure that .rela.dyn always appears before .rela.plt This is
1210 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1211 // needs to include .rela.plt in it's range.
1212 this->rela_dyn_section(layout);
1213
1214 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1215 layout->add_output_section_data(".plt",
1216 (size == 32
1217 ? elfcpp::SHT_PROGBITS
1218 : elfcpp::SHT_NOBITS),
1219 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1220 this->plt_,
1221 (size == 32
1222 ? ORDER_SMALL_DATA
1223 : ORDER_SMALL_BSS),
1224 false);
1225 }
1226 }
1227
1228 // A class to handle .glink.
1229
1230 template<int size, bool big_endian>
1231 class Output_data_glink : public Output_section_data
1232 {
1233 public:
1234 static const int pltresolve_size = 16*4;
1235
1236 Output_data_glink(Target_powerpc<size, big_endian>*);
1237
1238 // Add an entry
1239 void
1240 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1241 const Sized_relobj<size, big_endian>*);
1242
1243 unsigned int
1244 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1245 const Sized_relobj<size, big_endian>*) const;
1246
1247 unsigned int
1248 glink_entry_size() const
1249 {
1250 if (size == 32)
1251 return 4 * 4;
1252 else
1253 // FIXME: We should be using multiple glink sections for
1254 // stubs to support > 33M applications.
1255 return 8 * 4;
1256 }
1257
1258 off_t
1259 pltresolve() const
1260 {
1261 return this->pltresolve_;
1262 }
1263
1264 protected:
1265 // Write to a map file.
1266 void
1267 do_print_to_mapfile(Mapfile* mapfile) const
1268 { mapfile->print_output_data(this, _("** glink")); }
1269
1270 private:
1271 void
1272 set_final_data_size();
1273
1274 // Write out .glink
1275 void
1276 do_write(Output_file*);
1277
1278 class Glink_sym_ent
1279 {
1280 public:
1281 Glink_sym_ent(const Symbol* sym,
1282 const elfcpp::Rela<size, big_endian>& reloc,
1283 const Sized_relobj<size, big_endian>* object)
1284 : sym_(sym), addend_(0), object_(0)
1285 {
1286 if (size != 32)
1287 this->addend_ = reloc.get_r_addend();
1288 else if (parameters->options().output_is_position_independent()
1289 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1290 == elfcpp::R_PPC_PLTREL24))
1291 {
1292 this->addend_ = reloc.get_r_addend();
1293 if (this->addend_ != 0)
1294 this->object_ = object;
1295 }
1296 }
1297
1298 bool operator==(const Glink_sym_ent& that) const
1299 {
1300 return (this->sym_ == that.sym_
1301 && this->object_ == that.object_
1302 && this->addend_ == that.addend_);
1303 }
1304
1305 const Symbol* sym_;
1306 unsigned int addend_;
1307 const Sized_relobj<size, big_endian>* object_;
1308 };
1309
1310 class Glink_sym_ent_hash
1311 {
1312 public:
1313 size_t operator()(const Glink_sym_ent& ent) const
1314 {
1315 return (reinterpret_cast<uintptr_t>(ent.sym_)
1316 ^ reinterpret_cast<uintptr_t>(ent.object_)
1317 ^ ent.addend_);
1318 }
1319 };
1320
1321 // Map sym/object/addend to index.
1322 typedef Unordered_map<Glink_sym_ent, unsigned int,
1323 Glink_sym_ent_hash> Glink_entries;
1324 Glink_entries glink_entries_;
1325
1326 // Offset of pltresolve stub (actually, branch table for 32-bit)
1327 off_t pltresolve_;
1328
1329 // Allows access to .got and .plt for do_write.
1330 Target_powerpc<size, big_endian>* targ_;
1331 };
1332
1333 // Create the glink section.
1334
1335 template<int size, bool big_endian>
1336 Output_data_glink<size, big_endian>::Output_data_glink(
1337 Target_powerpc<size, big_endian>* targ)
1338 : Output_section_data(16),
1339 pltresolve_(0), targ_(targ)
1340 {
1341 }
1342
1343 // Add an entry to glink, if we do not already have one for this
1344 // sym/object/addend combo.
1345
1346 template<int size, bool big_endian>
1347 void
1348 Output_data_glink<size, big_endian>::add_entry(
1349 const Symbol* gsym,
1350 const elfcpp::Rela<size, big_endian>& reloc,
1351 const Sized_relobj<size, big_endian>* object)
1352 {
1353 Glink_sym_ent ent(gsym, reloc, object);
1354 unsigned int indx = this->glink_entries_.size();
1355 this->glink_entries_.insert(std::make_pair(ent, indx));
1356 }
1357
1358 template<int size, bool big_endian>
1359 unsigned int
1360 Output_data_glink<size, big_endian>::find_entry(
1361 const Symbol* gsym,
1362 const elfcpp::Rela<size, big_endian>& reloc,
1363 const Sized_relobj<size, big_endian>* object) const
1364 {
1365 Glink_sym_ent ent(gsym, reloc, object);
1366 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1367 gold_assert(p != this->glink_entries_.end());
1368 return p->second;
1369 }
1370
1371 template<int size, bool big_endian>
1372 void
1373 Output_data_glink<size, big_endian>::set_final_data_size()
1374 {
1375 unsigned int count = this->glink_entries_.size();
1376 off_t total = count;
1377
1378 if (count != 0)
1379 {
1380 if (size == 32)
1381 {
1382 total *= 16;
1383 this->pltresolve_ = total;
1384
1385 // space for branch table
1386 total += 4 * (count - 1);
1387
1388 total += -total & 15;
1389 total += this->pltresolve_size;
1390 }
1391 else
1392 {
1393 total *= 32;
1394 this->pltresolve_ = total;
1395 total += this->pltresolve_size;
1396
1397 // space for branch table
1398 total += 8 * count;
1399 if (count > 0x8000)
1400 total += 4 * (count - 0x8000);
1401 }
1402 }
1403
1404 this->set_data_size(total);
1405 }
1406
1407 static inline uint32_t
1408 l(uint32_t a)
1409 {
1410 return a & 0xffff;
1411 }
1412
1413 static inline uint32_t
1414 hi(uint32_t a)
1415 {
1416 return l(a >> 16);
1417 }
1418
1419 static inline uint32_t
1420 ha(uint32_t a)
1421 {
1422 return hi(a + 0x8000);
1423 }
1424
1425 template<bool big_endian>
1426 static inline void
1427 write_insn(unsigned char* p, uint32_t v)
1428 {
1429 elfcpp::Swap<32, big_endian>::writeval(p, v);
1430 }
1431
1432 // Write out .glink.
1433
1434 template<int size, bool big_endian>
1435 void
1436 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1437 {
1438 const off_t off = this->offset();
1439 const section_size_type oview_size =
1440 convert_to_section_size_type(this->data_size());
1441 unsigned char* const oview = of->get_output_view(off, oview_size);
1442 unsigned char* p;
1443
1444 // The base address of the .plt section.
1445 uint32_t plt_base = this->targ_->plt_section()->address();
1446
1447 // The address of _GLOBAL_OFFSET_TABLE_.
1448 const Output_data_got_powerpc<size, big_endian>* got;
1449 typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1450 got = this->targ_->got_section();
1451
1452 if (size == 64)
1453 {
1454 const unsigned int toc_base_offset = 0x8000;
1455 g_o_t = got->output_section()->address() + toc_base_offset;
1456
1457 // Write out call stubs.
1458 typename Glink_entries::const_iterator g;
1459 for (g = this->glink_entries_.begin();
1460 g != this->glink_entries_.end();
1461 ++g)
1462 {
1463 uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1464 uint64_t got_addr = g_o_t;
1465 uint64_t pltoff = plt_addr - got_addr;
1466
1467 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1468 gold_error(_("%s: linkage table error against `%s'"),
1469 g->first.object_->name().c_str(),
1470 g->first.sym_->demangled_name().c_str());
1471
1472 p = oview + g->second * this->glink_entry_size();
1473 if (ha(pltoff) != 0)
1474 {
1475 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1476 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1477 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1478 if (ha(pltoff + 16) != ha(pltoff))
1479 {
1480 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1481 pltoff = 0;
1482 }
1483 write_insn<big_endian>(p, mtctr_11), p += 4;
1484 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1485 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1486 write_insn<big_endian>(p, bctr), p += 4;
1487 }
1488 else
1489 {
1490 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1491 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1492 if (ha(pltoff + 16) != ha(pltoff))
1493 {
1494 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1495 pltoff = 0;
1496 }
1497 write_insn<big_endian>(p, mtctr_11), p += 4;
1498 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1499 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1500 write_insn<big_endian>(p, bctr), p += 4;
1501 }
1502 }
1503
1504 // Write pltresolve stub.
1505 p = oview + this->pltresolve_;
1506 uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1507 uint64_t pltoff = plt_base - after_bcl;
1508
1509 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1510
1511 write_insn<big_endian>(p, mflr_12), p += 4;
1512 write_insn<big_endian>(p, bcl_20_31), p += 4;
1513 write_insn<big_endian>(p, mflr_11), p += 4;
1514 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1515 write_insn<big_endian>(p, mtlr_12), p += 4;
1516 write_insn<big_endian>(p, add_12_2_11), p += 4;
1517 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1518 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1519 write_insn<big_endian>(p, mtctr_11), p += 4;
1520 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1521 write_insn<big_endian>(p, bctr), p += 4;
1522 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1523 write_insn<big_endian>(p, nop), p += 4;
1524
1525 // Write lazy link call stubs.
1526 uint32_t indx = 0;
1527 while (p < oview + oview_size)
1528 {
1529 if (indx < 0x8000)
1530 {
1531 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1532 }
1533 else
1534 {
1535 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1536 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1537 }
1538 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1539 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1540 indx++;
1541 }
1542 }
1543 else
1544 {
1545 g_o_t = got->address() + got->g_o_t();
1546
1547 // Write out call stubs.
1548 typename Glink_entries::const_iterator g;
1549 for (g = this->glink_entries_.begin();
1550 g != this->glink_entries_.end();
1551 ++g)
1552 {
1553 uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1554 uint32_t got_addr;
1555 const uint32_t invalid_address = static_cast<uint32_t>(-1);
1556
1557 p = oview + g->second * this->glink_entry_size();
1558 if (parameters->options().output_is_position_independent())
1559 {
1560 const Powerpc_relobj<size, big_endian>* object = static_cast
1561 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1562 if (object != NULL)
1563 {
1564 unsigned int got2 = object->got2_shndx();
1565 got_addr = g->first.object_->get_output_section_offset(got2);
1566 gold_assert(got_addr != invalid_address);
1567 got_addr += (g->first.object_->output_section(got2)->address()
1568 + g->first.addend_);
1569 }
1570 else
1571 got_addr = g_o_t;
1572
1573 uint32_t pltoff = plt_addr - got_addr;
1574 if (ha(pltoff) == 0)
1575 {
1576 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1577 write_insn<big_endian>(p + 4, mtctr_11);
1578 write_insn<big_endian>(p + 8, bctr);
1579 }
1580 else
1581 {
1582 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1583 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1584 write_insn<big_endian>(p + 8, mtctr_11);
1585 write_insn<big_endian>(p + 12, bctr);
1586 }
1587 }
1588 else
1589 {
1590 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1591 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1592 write_insn<big_endian>(p + 8, mtctr_11);
1593 write_insn<big_endian>(p + 12, bctr);
1594 }
1595 }
1596
1597 // Write out pltresolve branch table.
1598 p = oview + this->pltresolve_;
1599 unsigned int the_end = oview_size - this->pltresolve_size;
1600 unsigned char* end_p = oview + the_end;
1601 while (p < end_p - 8 * 4)
1602 write_insn<big_endian>(p, b + end_p - p), p += 4;
1603 while (p < end_p)
1604 write_insn<big_endian>(p, nop), p += 4;
1605
1606 // Write out pltresolve call stub.
1607 if (parameters->options().output_is_position_independent())
1608 {
1609 uint32_t res0_off = this->pltresolve_;
1610 uint32_t after_bcl_off = the_end + 12;
1611 uint32_t bcl_res0 = after_bcl_off - res0_off;
1612
1613 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1614 write_insn<big_endian>(p + 4, mflr_0);
1615 write_insn<big_endian>(p + 8, bcl_20_31);
1616 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1617 write_insn<big_endian>(p + 16, mflr_12);
1618 write_insn<big_endian>(p + 20, mtlr_0);
1619 write_insn<big_endian>(p + 24, sub_11_11_12);
1620
1621 uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1622
1623 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1624 if (ha(got_bcl) == ha(got_bcl + 4))
1625 {
1626 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1627 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1628 }
1629 else
1630 {
1631 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1632 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1633 }
1634 write_insn<big_endian>(p + 40, mtctr_0);
1635 write_insn<big_endian>(p + 44, add_0_11_11);
1636 write_insn<big_endian>(p + 48, add_11_0_11);
1637 write_insn<big_endian>(p + 52, bctr);
1638 write_insn<big_endian>(p + 56, nop);
1639 write_insn<big_endian>(p + 60, nop);
1640 }
1641 else
1642 {
1643 uint32_t res0 = this->pltresolve_ + this->address();
1644
1645 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1646 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1647 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1648 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1649 else
1650 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1651 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1652 write_insn<big_endian>(p + 16, mtctr_0);
1653 write_insn<big_endian>(p + 20, add_0_11_11);
1654 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1655 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1656 else
1657 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1658 write_insn<big_endian>(p + 28, add_11_0_11);
1659 write_insn<big_endian>(p + 32, bctr);
1660 write_insn<big_endian>(p + 36, nop);
1661 write_insn<big_endian>(p + 40, nop);
1662 write_insn<big_endian>(p + 44, nop);
1663 write_insn<big_endian>(p + 48, nop);
1664 write_insn<big_endian>(p + 52, nop);
1665 write_insn<big_endian>(p + 56, nop);
1666 write_insn<big_endian>(p + 60, nop);
1667 }
1668 p += 64;
1669 }
1670
1671 of->write_output_view(off, oview_size, oview);
1672 }
1673
1674 // Create the glink section.
1675
1676 template<int size, bool big_endian>
1677 void
1678 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1679 {
1680 if (this->glink_ == NULL)
1681 {
1682 this->glink_ = new Output_data_glink<size, big_endian>(this);
1683 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1684 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1685 this->glink_, ORDER_TEXT, false);
1686 }
1687 }
1688
1689 // Create a PLT entry for a global symbol.
1690
1691 template<int size, bool big_endian>
1692 void
1693 Target_powerpc<size, big_endian>::make_plt_entry(
1694 Layout* layout,
1695 Symbol* gsym,
1696 const elfcpp::Rela<size, big_endian>& reloc,
1697 const Sized_relobj<size, big_endian>* object)
1698 {
1699 if (this->plt_ == NULL)
1700 this->make_plt_section(layout);
1701
1702 this->plt_->add_entry(gsym);
1703
1704 this->glink_->add_entry(gsym, reloc, object);
1705 }
1706
1707 // Return the number of entries in the PLT.
1708
1709 template<int size, bool big_endian>
1710 unsigned int
1711 Target_powerpc<size, big_endian>::plt_entry_count() const
1712 {
1713 if (this->plt_ == NULL)
1714 return 0;
1715 return this->plt_->entry_count();
1716 }
1717
1718 // Return the offset of the first non-reserved PLT entry.
1719
1720 template<int size, bool big_endian>
1721 unsigned int
1722 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1723 {
1724 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1725 }
1726
1727 // Return the size of each PLT entry.
1728
1729 template<int size, bool big_endian>
1730 unsigned int
1731 Target_powerpc<size, big_endian>::plt_entry_size() const
1732 {
1733 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1734 }
1735
1736 // Create a GOT entry for the TLS module index.
1737
1738 template<int size, bool big_endian>
1739 unsigned int
1740 Target_powerpc<size, big_endian>::got_mod_index_entry(
1741 Symbol_table* symtab,
1742 Layout* layout,
1743 Sized_relobj_file<size, big_endian>* object)
1744 {
1745 if (this->got_mod_index_offset_ == -1U)
1746 {
1747 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1748 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1749 Output_data_got_powerpc<size, big_endian>* got;
1750 unsigned int got_offset;
1751
1752 got = this->got_section(symtab, layout);
1753 got->reserve_ent(2);
1754 got_offset = got->add_constant(0);
1755 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1756 got_offset, 0);
1757 got->add_constant(0);
1758 this->got_mod_index_offset_ = got_offset;
1759 }
1760 return this->got_mod_index_offset_;
1761 }
1762
1763 // Optimize the TLS relocation type based on what we know about the
1764 // symbol. IS_FINAL is true if the final address of this symbol is
1765 // known at link time.
1766
1767 template<int size, bool big_endian>
1768 tls::Tls_optimization
1769 Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
1770 {
1771 // If we are generating a shared library, then we can't do anything
1772 // in the linker.
1773 if (parameters->options().shared())
1774 return tls::TLSOPT_NONE;
1775 // FIXME
1776 return tls::TLSOPT_NONE;
1777 }
1778
1779 // Get the Reference_flags for a particular relocation.
1780
1781 template<int size, bool big_endian>
1782 int
1783 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1784 {
1785 switch (r_type)
1786 {
1787 case elfcpp::R_POWERPC_NONE:
1788 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1789 case elfcpp::R_POWERPC_GNU_VTENTRY:
1790 case elfcpp::R_PPC64_TOC:
1791 // No symbol reference.
1792 return 0;
1793
1794 case elfcpp::R_POWERPC_ADDR16:
1795 case elfcpp::R_POWERPC_ADDR16_LO:
1796 case elfcpp::R_POWERPC_ADDR16_HI:
1797 case elfcpp::R_POWERPC_ADDR16_HA:
1798 case elfcpp::R_POWERPC_ADDR32:
1799 case elfcpp::R_PPC64_ADDR64:
1800 return Symbol::ABSOLUTE_REF;
1801
1802 case elfcpp::R_POWERPC_REL24:
1803 case elfcpp::R_PPC_LOCAL24PC:
1804 case elfcpp::R_POWERPC_REL16:
1805 case elfcpp::R_POWERPC_REL16_LO:
1806 case elfcpp::R_POWERPC_REL16_HI:
1807 case elfcpp::R_POWERPC_REL16_HA:
1808 return Symbol::RELATIVE_REF;
1809
1810 case elfcpp::R_PPC_PLTREL24:
1811 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1812
1813 case elfcpp::R_POWERPC_GOT16:
1814 case elfcpp::R_POWERPC_GOT16_LO:
1815 case elfcpp::R_POWERPC_GOT16_HI:
1816 case elfcpp::R_POWERPC_GOT16_HA:
1817 case elfcpp::R_PPC64_TOC16:
1818 case elfcpp::R_PPC64_TOC16_LO:
1819 case elfcpp::R_PPC64_TOC16_HI:
1820 case elfcpp::R_PPC64_TOC16_HA:
1821 case elfcpp::R_PPC64_TOC16_DS:
1822 case elfcpp::R_PPC64_TOC16_LO_DS:
1823 // Absolute in GOT.
1824 return Symbol::ABSOLUTE_REF;
1825
1826 case elfcpp::R_POWERPC_GOT_TPREL16:
1827 case elfcpp::R_POWERPC_TLS:
1828 return Symbol::TLS_REF;
1829
1830 case elfcpp::R_POWERPC_COPY:
1831 case elfcpp::R_POWERPC_GLOB_DAT:
1832 case elfcpp::R_POWERPC_JMP_SLOT:
1833 case elfcpp::R_POWERPC_RELATIVE:
1834 case elfcpp::R_POWERPC_DTPMOD:
1835 default:
1836 // Not expected. We will give an error later.
1837 return 0;
1838 }
1839 }
1840
1841 // Report an unsupported relocation against a local symbol.
1842
1843 template<int size, bool big_endian>
1844 void
1845 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1846 Sized_relobj_file<size, big_endian>* object,
1847 unsigned int r_type)
1848 {
1849 gold_error(_("%s: unsupported reloc %u against local symbol"),
1850 object->name().c_str(), r_type);
1851 }
1852
1853 // We are about to emit a dynamic relocation of type R_TYPE. If the
1854 // dynamic linker does not support it, issue an error.
1855
1856 template<int size, bool big_endian>
1857 void
1858 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1859 unsigned int r_type)
1860 {
1861 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1862
1863 // These are the relocation types supported by glibc for both 32-bit
1864 // and 64-bit powerpc.
1865 switch (r_type)
1866 {
1867 case elfcpp::R_POWERPC_RELATIVE:
1868 case elfcpp::R_POWERPC_GLOB_DAT:
1869 case elfcpp::R_POWERPC_DTPMOD:
1870 case elfcpp::R_POWERPC_DTPREL:
1871 case elfcpp::R_POWERPC_TPREL:
1872 case elfcpp::R_POWERPC_JMP_SLOT:
1873 case elfcpp::R_POWERPC_COPY:
1874 case elfcpp::R_POWERPC_ADDR32:
1875 case elfcpp::R_POWERPC_ADDR24:
1876 case elfcpp::R_POWERPC_REL24:
1877 return;
1878
1879 default:
1880 break;
1881 }
1882
1883 if (size == 64)
1884 {
1885 switch (r_type)
1886 {
1887 // These are the relocation types supported only on 64-bit.
1888 case elfcpp::R_PPC64_ADDR64:
1889 case elfcpp::R_PPC64_TPREL16_LO_DS:
1890 case elfcpp::R_PPC64_TPREL16_DS:
1891 case elfcpp::R_POWERPC_TPREL16:
1892 case elfcpp::R_POWERPC_TPREL16_LO:
1893 case elfcpp::R_POWERPC_TPREL16_HI:
1894 case elfcpp::R_POWERPC_TPREL16_HA:
1895 case elfcpp::R_PPC64_TPREL16_HIGHER:
1896 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1897 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1898 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1899 case elfcpp::R_PPC64_ADDR16_LO_DS:
1900 case elfcpp::R_POWERPC_ADDR16_LO:
1901 case elfcpp::R_POWERPC_ADDR16_HI:
1902 case elfcpp::R_POWERPC_ADDR16_HA:
1903 case elfcpp::R_POWERPC_ADDR30:
1904 case elfcpp::R_PPC64_UADDR64:
1905 case elfcpp::R_POWERPC_UADDR32:
1906 case elfcpp::R_POWERPC_ADDR16:
1907 case elfcpp::R_POWERPC_UADDR16:
1908 case elfcpp::R_PPC64_ADDR16_DS:
1909 case elfcpp::R_PPC64_ADDR16_HIGHER:
1910 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1911 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1912 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1913 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1914 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1915 case elfcpp::R_POWERPC_REL32:
1916 case elfcpp::R_PPC64_REL64:
1917 return;
1918
1919 default:
1920 break;
1921 }
1922 }
1923 else
1924 {
1925 switch (r_type)
1926 {
1927 // These are the relocation types supported only on 32-bit.
1928
1929 default:
1930 break;
1931 }
1932 }
1933
1934 // This prevents us from issuing more than one error per reloc
1935 // section. But we can still wind up issuing more than one
1936 // error per object file.
1937 if (this->issued_non_pic_error_)
1938 return;
1939 gold_assert(parameters->options().output_is_position_independent());
1940 object->error(_("requires unsupported dynamic reloc; "
1941 "recompile with -fPIC"));
1942 this->issued_non_pic_error_ = true;
1943 return;
1944 }
1945
1946 // Scan a relocation for a local symbol.
1947
1948 template<int size, bool big_endian>
1949 inline void
1950 Target_powerpc<size, big_endian>::Scan::local(
1951 Symbol_table* symtab,
1952 Layout* layout,
1953 Target_powerpc<size, big_endian>* target,
1954 Sized_relobj_file<size, big_endian>* object,
1955 unsigned int data_shndx,
1956 Output_section* output_section,
1957 const elfcpp::Rela<size, big_endian>& reloc,
1958 unsigned int r_type,
1959 const elfcpp::Sym<size, big_endian>& lsym)
1960 {
1961 switch (r_type)
1962 {
1963 case elfcpp::R_POWERPC_NONE:
1964 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1965 case elfcpp::R_POWERPC_GNU_VTENTRY:
1966 case elfcpp::R_PPC64_TOCSAVE:
1967 break;
1968
1969 case elfcpp::R_PPC64_ADDR64:
1970 case elfcpp::R_POWERPC_ADDR32:
1971 case elfcpp::R_POWERPC_ADDR16:
1972 case elfcpp::R_POWERPC_ADDR16_LO:
1973 case elfcpp::R_POWERPC_ADDR16_HI:
1974 case elfcpp::R_POWERPC_ADDR16_HA:
1975 // If building a shared library (or a position-independent
1976 // executable), we need to create a dynamic relocation for
1977 // this location.
1978 if (parameters->options().output_is_position_independent())
1979 {
1980 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1981
1982 check_non_pic(object, r_type);
1983 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1984 {
1985 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1986 rela_dyn->add_local(object, r_sym, r_type, output_section,
1987 data_shndx, reloc.get_r_offset(),
1988 reloc.get_r_addend());
1989 }
1990 else
1991 {
1992 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1993 gold_assert(lsym.get_st_value() == 0);
1994 rela_dyn->add_local_relative(object, r_sym, r_type,
1995 output_section, data_shndx,
1996 reloc.get_r_offset(),
1997 reloc.get_r_addend(), false);
1998 }
1999 }
2000 break;
2001
2002 case elfcpp::R_POWERPC_REL24:
2003 case elfcpp::R_PPC_LOCAL24PC:
2004 case elfcpp::R_POWERPC_REL32:
2005 case elfcpp::R_POWERPC_REL16_LO:
2006 case elfcpp::R_POWERPC_REL16_HA:
2007 break;
2008
2009 case elfcpp::R_POWERPC_GOT16:
2010 case elfcpp::R_POWERPC_GOT16_LO:
2011 case elfcpp::R_POWERPC_GOT16_HI:
2012 case elfcpp::R_POWERPC_GOT16_HA:
2013 {
2014 // The symbol requires a GOT entry.
2015 Output_data_got_powerpc<size, big_endian>* got;
2016 unsigned int r_sym;
2017
2018 got = target->got_section(symtab, layout);
2019 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2020
2021 // If we are generating a shared object, we need to add a
2022 // dynamic relocation for this symbol's GOT entry.
2023 if (parameters->options().output_is_position_independent())
2024 {
2025 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2026 {
2027 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2028 unsigned int off;
2029
2030 off = got->add_constant(0);
2031 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2032 rela_dyn->add_local_relative(object, r_sym,
2033 elfcpp::R_POWERPC_RELATIVE,
2034 got, off, 0, false);
2035 }
2036 }
2037 else
2038 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2039 }
2040 break;
2041
2042 case elfcpp::R_PPC64_TOC16:
2043 case elfcpp::R_PPC64_TOC16_LO:
2044 case elfcpp::R_PPC64_TOC16_HI:
2045 case elfcpp::R_PPC64_TOC16_HA:
2046 case elfcpp::R_PPC64_TOC16_DS:
2047 case elfcpp::R_PPC64_TOC16_LO_DS:
2048 case elfcpp::R_PPC64_TOC:
2049 // We need a GOT section.
2050 target->got_section(symtab, layout);
2051 break;
2052
2053 // These are relocations which should only be seen by the
2054 // dynamic linker, and should never be seen here.
2055 case elfcpp::R_POWERPC_COPY:
2056 case elfcpp::R_POWERPC_GLOB_DAT:
2057 case elfcpp::R_POWERPC_JMP_SLOT:
2058 case elfcpp::R_POWERPC_RELATIVE:
2059 case elfcpp::R_POWERPC_DTPMOD:
2060 gold_error(_("%s: unexpected reloc %u in object file"),
2061 object->name().c_str(), r_type);
2062 break;
2063
2064 default:
2065 unsupported_reloc_local(object, r_type);
2066 break;
2067 }
2068 }
2069
2070 // Report an unsupported relocation against a global symbol.
2071
2072 template<int size, bool big_endian>
2073 void
2074 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2075 Sized_relobj_file<size, big_endian>* object,
2076 unsigned int r_type,
2077 Symbol* gsym)
2078 {
2079 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2080 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2081 }
2082
2083 // Scan a relocation for a global symbol.
2084
2085 template<int size, bool big_endian>
2086 inline void
2087 Target_powerpc<size, big_endian>::Scan::global(
2088 Symbol_table* symtab,
2089 Layout* layout,
2090 Target_powerpc<size, big_endian>* target,
2091 Sized_relobj_file<size, big_endian>* object,
2092 unsigned int data_shndx,
2093 Output_section* output_section,
2094 const elfcpp::Rela<size, big_endian>& reloc,
2095 unsigned int r_type,
2096 Symbol* gsym)
2097 {
2098 switch (r_type)
2099 {
2100 case elfcpp::R_POWERPC_NONE:
2101 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2102 case elfcpp::R_POWERPC_GNU_VTENTRY:
2103 case elfcpp::R_PPC_LOCAL24PC:
2104 break;
2105
2106 case elfcpp::R_PPC64_ADDR64:
2107 case elfcpp::R_POWERPC_ADDR32:
2108 case elfcpp::R_POWERPC_ADDR16:
2109 case elfcpp::R_POWERPC_ADDR16_LO:
2110 case elfcpp::R_POWERPC_ADDR16_HI:
2111 case elfcpp::R_POWERPC_ADDR16_HA:
2112 {
2113 // Make a PLT entry if necessary.
2114 if (gsym->needs_plt_entry())
2115 {
2116 target->make_plt_entry(layout, gsym, reloc, 0);
2117 // Since this is not a PC-relative relocation, we may be
2118 // taking the address of a function. In that case we need to
2119 // set the entry in the dynamic symbol table to the address of
2120 // the PLT entry.
2121 if (size == 32
2122 && gsym->is_from_dynobj() && !parameters->options().shared())
2123 gsym->set_needs_dynsym_value();
2124 }
2125 // Make a dynamic relocation if necessary.
2126 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2127 {
2128 if (gsym->may_need_copy_reloc())
2129 {
2130 target->copy_reloc(symtab, layout, object,
2131 data_shndx, output_section, gsym, reloc);
2132 }
2133 else if ((r_type == elfcpp::R_POWERPC_ADDR32
2134 || r_type == elfcpp::R_PPC64_ADDR64)
2135 && gsym->can_use_relative_reloc(false))
2136 {
2137 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2138 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2139 output_section, object,
2140 data_shndx, reloc.get_r_offset(),
2141 reloc.get_r_addend(), false);
2142 }
2143 else
2144 {
2145 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2146
2147 check_non_pic(object, r_type);
2148 if (gsym->is_from_dynobj()
2149 || gsym->is_undefined()
2150 || gsym->is_preemptible())
2151 rela_dyn->add_global(gsym, r_type, output_section,
2152 object, data_shndx,
2153 reloc.get_r_offset(),
2154 reloc.get_r_addend());
2155 else
2156 rela_dyn->add_global_relative(gsym, r_type,
2157 output_section, object,
2158 data_shndx,
2159 reloc.get_r_offset(),
2160 reloc.get_r_addend(), false);
2161 }
2162 }
2163 }
2164 break;
2165
2166 case elfcpp::R_PPC_PLTREL24:
2167 case elfcpp::R_POWERPC_REL24:
2168 {
2169 if (gsym->needs_plt_entry()
2170 || (!gsym->final_value_is_known()
2171 && !(gsym->is_defined()
2172 && !gsym->is_from_dynobj()
2173 && !gsym->is_preemptible())))
2174 target->make_plt_entry(layout, gsym, reloc, object);
2175 // Make a dynamic relocation if necessary.
2176 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2177 {
2178 if (gsym->may_need_copy_reloc())
2179 {
2180 target->copy_reloc(symtab, layout, object,
2181 data_shndx, output_section, gsym,
2182 reloc);
2183 }
2184 else
2185 {
2186 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2187 check_non_pic(object, r_type);
2188 rela_dyn->add_global(gsym, r_type, output_section, object,
2189 data_shndx, reloc.get_r_offset(),
2190 reloc.get_r_addend());
2191 }
2192 }
2193 }
2194 break;
2195
2196 case elfcpp::R_POWERPC_REL16:
2197 case elfcpp::R_POWERPC_REL16_LO:
2198 case elfcpp::R_POWERPC_REL16_HI:
2199 case elfcpp::R_POWERPC_REL16_HA:
2200 break;
2201
2202 case elfcpp::R_POWERPC_GOT16:
2203 case elfcpp::R_POWERPC_GOT16_LO:
2204 case elfcpp::R_POWERPC_GOT16_HI:
2205 case elfcpp::R_POWERPC_GOT16_HA:
2206 {
2207 // The symbol requires a GOT entry.
2208 Output_data_got_powerpc<size, big_endian>* got;
2209
2210 got = target->got_section(symtab, layout);
2211 if (gsym->final_value_is_known())
2212 got->add_global(gsym, GOT_TYPE_STANDARD);
2213 else
2214 {
2215 // If this symbol is not fully resolved, we need to add a
2216 // dynamic relocation for it.
2217 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2218 if (gsym->is_from_dynobj()
2219 || gsym->is_undefined()
2220 || gsym->is_preemptible())
2221 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2222 elfcpp::R_POWERPC_GLOB_DAT);
2223 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2224 {
2225 unsigned int off = got->add_constant(0);
2226
2227 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2228 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2229 got, off, 0, false);
2230 }
2231 }
2232 }
2233 break;
2234
2235 case elfcpp::R_PPC64_TOC:
2236 case elfcpp::R_PPC64_TOC16:
2237 case elfcpp::R_PPC64_TOC16_LO:
2238 case elfcpp::R_PPC64_TOC16_HI:
2239 case elfcpp::R_PPC64_TOC16_HA:
2240 case elfcpp::R_PPC64_TOC16_DS:
2241 case elfcpp::R_PPC64_TOC16_LO_DS:
2242 // We need a GOT section.
2243 target->got_section(symtab, layout);
2244 break;
2245
2246 case elfcpp::R_POWERPC_GOT_TPREL16:
2247 case elfcpp::R_POWERPC_TLS:
2248 // XXX TLS
2249 break;
2250
2251 // These are relocations which should only be seen by the
2252 // dynamic linker, and should never be seen here.
2253 case elfcpp::R_POWERPC_COPY:
2254 case elfcpp::R_POWERPC_GLOB_DAT:
2255 case elfcpp::R_POWERPC_JMP_SLOT:
2256 case elfcpp::R_POWERPC_RELATIVE:
2257 case elfcpp::R_POWERPC_DTPMOD:
2258 gold_error(_("%s: unexpected reloc %u in object file"),
2259 object->name().c_str(), r_type);
2260 break;
2261
2262 default:
2263 unsupported_reloc_global(object, r_type, gsym);
2264 break;
2265 }
2266 }
2267
2268 // Process relocations for gc.
2269
2270 template<int size, bool big_endian>
2271 void
2272 Target_powerpc<size, big_endian>::gc_process_relocs(
2273 Symbol_table* symtab,
2274 Layout* layout,
2275 Sized_relobj_file<size, big_endian>* object,
2276 unsigned int data_shndx,
2277 unsigned int,
2278 const unsigned char* prelocs,
2279 size_t reloc_count,
2280 Output_section* output_section,
2281 bool needs_special_offset_handling,
2282 size_t local_symbol_count,
2283 const unsigned char* plocal_symbols)
2284 {
2285 typedef Target_powerpc<size, big_endian> Powerpc;
2286 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2287
2288 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2289 typename Target_powerpc::Relocatable_size_for_reloc>(
2290 symtab,
2291 layout,
2292 this,
2293 object,
2294 data_shndx,
2295 prelocs,
2296 reloc_count,
2297 output_section,
2298 needs_special_offset_handling,
2299 local_symbol_count,
2300 plocal_symbols);
2301 }
2302
2303 // Scan relocations for a section.
2304
2305 template<int size, bool big_endian>
2306 void
2307 Target_powerpc<size, big_endian>::scan_relocs(
2308 Symbol_table* symtab,
2309 Layout* layout,
2310 Sized_relobj_file<size, big_endian>* object,
2311 unsigned int data_shndx,
2312 unsigned int sh_type,
2313 const unsigned char* prelocs,
2314 size_t reloc_count,
2315 Output_section* output_section,
2316 bool needs_special_offset_handling,
2317 size_t local_symbol_count,
2318 const unsigned char* plocal_symbols)
2319 {
2320 typedef Target_powerpc<size, big_endian> Powerpc;
2321 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2322
2323 if (sh_type == elfcpp::SHT_REL)
2324 {
2325 gold_error(_("%s: unsupported REL reloc section"),
2326 object->name().c_str());
2327 return;
2328 }
2329
2330 if (size == 32)
2331 {
2332 static Output_data_space* sdata;
2333
2334 // Define _SDA_BASE_ at the start of the .sdata section.
2335 if (sdata == NULL)
2336 {
2337 // layout->find_output_section(".sdata") == NULL
2338 sdata = new Output_data_space(4, "** sdata");
2339 Output_section* os
2340 = layout->add_output_section_data(".sdata", 0,
2341 elfcpp::SHF_ALLOC
2342 | elfcpp::SHF_WRITE,
2343 sdata, ORDER_SMALL_DATA, false);
2344 symtab->define_in_output_data("_SDA_BASE_", NULL,
2345 Symbol_table::PREDEFINED,
2346 os, 32768, 0, elfcpp::STT_OBJECT,
2347 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2348 0, false, false);
2349 }
2350 }
2351
2352 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2353 symtab,
2354 layout,
2355 this,
2356 object,
2357 data_shndx,
2358 prelocs,
2359 reloc_count,
2360 output_section,
2361 needs_special_offset_handling,
2362 local_symbol_count,
2363 plocal_symbols);
2364 }
2365
2366 // Finalize the sections.
2367
2368 template<int size, bool big_endian>
2369 void
2370 Target_powerpc<size, big_endian>::do_finalize_sections(
2371 Layout* layout,
2372 const Input_objects*,
2373 Symbol_table*)
2374 {
2375 // Fill in some more dynamic tags.
2376 const Reloc_section* rel_plt = (this->plt_ == NULL
2377 ? NULL
2378 : this->plt_->rel_plt());
2379 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2380 this->rela_dyn_, true, size == 32);
2381
2382 Output_data_dynamic* odyn = layout->dynamic_data();
2383 if (size == 32)
2384 {
2385 this->got_->finalize_data_size();
2386 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2387 this->got_, this->got_->g_o_t());
2388 }
2389 else
2390 {
2391 this->glink_->finalize_data_size();
2392 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
2393 this->glink_,
2394 (this->glink_->pltresolve()
2395 + this->glink_->pltresolve_size - 32));
2396 }
2397
2398 // Emit any relocs we saved in an attempt to avoid generating COPY
2399 // relocs.
2400 if (this->copy_relocs_.any_saved_relocs())
2401 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2402 }
2403
2404 // Perform a relocation.
2405
2406 template<int size, bool big_endian>
2407 inline bool
2408 Target_powerpc<size, big_endian>::Relocate::relocate(
2409 const Relocate_info<size, big_endian>* relinfo,
2410 Target_powerpc* target,
2411 Output_section* os,
2412 size_t relnum,
2413 const elfcpp::Rela<size, big_endian>& rela,
2414 unsigned int r_type,
2415 const Sized_symbol<size>* gsym,
2416 const Symbol_value<size>* psymval,
2417 unsigned char* view,
2418 Address address,
2419 section_size_type view_size)
2420 {
2421 const unsigned int toc_base_offset = 0x8000;
2422 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
2423 const Powerpc_relobj<size, big_endian>* const object
2424 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2425 Address value;
2426
2427 if (r_type == elfcpp::R_POWERPC_GOT16
2428 || r_type == elfcpp::R_POWERPC_GOT16_LO
2429 || r_type == elfcpp::R_POWERPC_GOT16_HI
2430 || r_type == elfcpp::R_POWERPC_GOT16_HA
2431 || r_type == elfcpp::R_PPC64_GOT16_DS
2432 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
2433 {
2434 if (gsym != NULL)
2435 {
2436 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2437 value = gsym->got_offset(GOT_TYPE_STANDARD);
2438 }
2439 else
2440 {
2441 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2442 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2443 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2444 }
2445 value -= target->got_section()->g_o_t();
2446 }
2447 else if (r_type == elfcpp::R_PPC64_TOC)
2448 {
2449 value = (target->got_section()->output_section()->address()
2450 + toc_base_offset);
2451 }
2452 else if (gsym != NULL
2453 && (r_type == elfcpp::R_POWERPC_REL24
2454 || r_type == elfcpp::R_PPC_PLTREL24)
2455 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2456 {
2457 const Output_data_glink<size, big_endian>* glink;
2458
2459 glink = target->glink_section();
2460 unsigned int glink_index = glink->find_entry(gsym, rela, object);
2461 value = glink->address() + glink_index * glink->glink_entry_size();
2462 if (size == 64)
2463 {
2464 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2465 Valtype* wv = reinterpret_cast<Valtype*>(view);
2466 bool can_plt_call = false;
2467 if (rela.get_r_offset() + 8 <= view_size)
2468 {
2469 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
2470 if (insn2 == nop
2471 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31)
2472 {
2473 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
2474 can_plt_call = true;
2475 }
2476 }
2477 if (!can_plt_call)
2478 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2479 _("call lacks nop, can't restore toc"));
2480 }
2481 }
2482 else
2483 {
2484 typename elfcpp::Elf_types<size>::Elf_Swxword addend = 0;
2485 if (r_type != elfcpp::R_PPC_PLTREL24)
2486 addend = rela.get_r_addend();
2487 value = psymval->value(object, addend);
2488 if (size == 64 && this->is_branch_reloc(r_type))
2489 {
2490 Powerpc_relobj<size, big_endian>* symobj = const_cast
2491 <Powerpc_relobj<size, big_endian>*>(object);
2492 if (gsym != NULL)
2493 symobj = static_cast
2494 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2495 unsigned int shndx = symobj->opd_shndx();
2496 Address opd_addr = symobj->get_output_section_offset(shndx);
2497 gold_assert(opd_addr != invalid_address);
2498 opd_addr += symobj->output_section(shndx)->address();
2499 if (value >= opd_addr
2500 && value < opd_addr + symobj->section_size(shndx))
2501 {
2502 Address sec_off;
2503 symobj->get_opd_ent(value - opd_addr, &shndx, &sec_off);
2504 Address sec_addr = symobj->get_output_section_offset(shndx);
2505 gold_assert(sec_addr != invalid_address);
2506 sec_addr += symobj->output_section(shndx)->address();
2507 value = sec_addr + sec_off;
2508 }
2509 }
2510 }
2511
2512 switch (r_type)
2513 {
2514 case elfcpp::R_PPC64_TOC16:
2515 case elfcpp::R_PPC64_TOC16_LO:
2516 case elfcpp::R_PPC64_TOC16_HI:
2517 case elfcpp::R_PPC64_TOC16_HA:
2518 case elfcpp::R_PPC64_TOC16_DS:
2519 case elfcpp::R_PPC64_TOC16_LO_DS:
2520 // Subtract the TOC base address.
2521 value -= (target->got_section()->output_section()->address()
2522 + toc_base_offset);
2523 break;
2524
2525 case elfcpp::R_POWERPC_SECTOFF:
2526 case elfcpp::R_POWERPC_SECTOFF_LO:
2527 case elfcpp::R_POWERPC_SECTOFF_HI:
2528 case elfcpp::R_POWERPC_SECTOFF_HA:
2529 case elfcpp::R_PPC64_SECTOFF_DS:
2530 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2531 if (os != NULL)
2532 value -= os->address();
2533 break;
2534
2535 default:
2536 break;
2537 }
2538
2539 switch (r_type)
2540 {
2541 case elfcpp::R_POWERPC_NONE:
2542 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2543 case elfcpp::R_POWERPC_GNU_VTENTRY:
2544 break;
2545
2546 case elfcpp::R_POWERPC_REL32:
2547 Reloc::rel32(view, value, 0, address);
2548 break;
2549
2550 case elfcpp::R_POWERPC_REL24:
2551 case elfcpp::R_PPC_PLTREL24:
2552 case elfcpp::R_PPC_LOCAL24PC:
2553 Reloc::rel24(view, value, 0, address);
2554 break;
2555
2556 case elfcpp::R_POWERPC_REL14:
2557 Reloc::rel14(view, value, 0, address);
2558 break;
2559
2560 case elfcpp::R_PPC64_ADDR64:
2561 case elfcpp::R_PPC64_TOC:
2562 Relocate_functions<size, big_endian>::rela64(view, value, 0);
2563 break;
2564
2565 case elfcpp::R_POWERPC_ADDR32:
2566 Relocate_functions<size, big_endian>::rela32(view, value, 0);
2567 break;
2568
2569 case elfcpp::R_POWERPC_ADDR16:
2570 case elfcpp::R_PPC64_TOC16:
2571 case elfcpp::R_POWERPC_GOT16:
2572 case elfcpp::R_POWERPC_SECTOFF:
2573 Reloc::addr16(view, value, 0);
2574 break;
2575
2576 case elfcpp::R_POWERPC_ADDR16_LO:
2577 case elfcpp::R_PPC64_TOC16_LO:
2578 case elfcpp::R_POWERPC_GOT16_LO:
2579 case elfcpp::R_POWERPC_SECTOFF_LO:
2580 Reloc::addr16_lo(view, value, 0);
2581 break;
2582
2583 case elfcpp::R_POWERPC_ADDR16_HI:
2584 case elfcpp::R_PPC64_TOC16_HI:
2585 case elfcpp::R_POWERPC_GOT16_HI:
2586 case elfcpp::R_POWERPC_SECTOFF_HI:
2587 Reloc::addr16_hi(view, value, 0);
2588 break;
2589
2590 case elfcpp::R_POWERPC_ADDR16_HA:
2591 case elfcpp::R_PPC64_TOC16_HA:
2592 case elfcpp::R_POWERPC_GOT16_HA:
2593 case elfcpp::R_POWERPC_SECTOFF_HA:
2594 Reloc::addr16_ha(view, value, 0);
2595 break;
2596
2597 case elfcpp::R_POWERPC_REL16_LO:
2598 Reloc::rel16_lo(view, value, 0, address);
2599 break;
2600
2601 case elfcpp::R_POWERPC_REL16_HI:
2602 Reloc::rel16_hi(view, value, 0, address);
2603 break;
2604
2605 case elfcpp::R_POWERPC_REL16_HA:
2606 Reloc::rel16_ha(view, value, 0, address);
2607 break;
2608
2609 case elfcpp::R_PPC64_ADDR16_DS:
2610 case elfcpp::R_PPC64_ADDR16_LO_DS:
2611 case elfcpp::R_PPC64_TOC16_DS:
2612 case elfcpp::R_PPC64_TOC16_LO_DS:
2613 case elfcpp::R_PPC64_GOT16_DS:
2614 case elfcpp::R_PPC64_GOT16_LO_DS:
2615 case elfcpp::R_PPC64_SECTOFF_DS:
2616 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2617 Reloc::addr16_ds(view, value, 0);
2618 break;
2619
2620 case elfcpp::R_POWERPC_COPY:
2621 case elfcpp::R_POWERPC_GLOB_DAT:
2622 case elfcpp::R_POWERPC_JMP_SLOT:
2623 case elfcpp::R_POWERPC_RELATIVE:
2624 // This is an outstanding tls reloc, which is unexpected when
2625 // linking.
2626 case elfcpp::R_POWERPC_DTPMOD:
2627 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2628 _("unexpected reloc %u in object file"),
2629 r_type);
2630 break;
2631
2632 case elfcpp::R_PPC64_TOCSAVE:
2633 // For the time being this can be ignored.
2634 break;
2635
2636 default:
2637 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2638 _("unsupported reloc %u"),
2639 r_type);
2640 break;
2641 }
2642
2643 return true;
2644 }
2645
2646 // Perform a TLS relocation.
2647
2648 template<int size, bool big_endian>
2649 inline void
2650 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
2651 const Relocate_info<size, big_endian>* relinfo,
2652 Target_powerpc<size, big_endian>* target,
2653 size_t relnum,
2654 const elfcpp::Rela<size, big_endian>& rela,
2655 unsigned int r_type,
2656 const Sized_symbol<size>* gsym,
2657 const Symbol_value<size>* psymval,
2658 unsigned char* view,
2659 Address address,
2660 section_size_type)
2661 {
2662 Output_segment* tls_segment = relinfo->layout->tls_segment();
2663 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2664
2665 const Address addend = rela.get_r_addend();
2666 Address value = psymval->value(object, 0);
2667
2668 const bool is_final =
2669 (gsym == NULL
2670 ? !parameters->options().output_is_position_independent()
2671 : gsym->final_value_is_known());
2672
2673 switch (r_type)
2674 {
2675 // XXX
2676 }
2677 }
2678
2679 // Relocate section data.
2680
2681 template<int size, bool big_endian>
2682 void
2683 Target_powerpc<size, big_endian>::relocate_section(
2684 const Relocate_info<size, big_endian>* relinfo,
2685 unsigned int sh_type,
2686 const unsigned char* prelocs,
2687 size_t reloc_count,
2688 Output_section* output_section,
2689 bool needs_special_offset_handling,
2690 unsigned char* view,
2691 Address address,
2692 section_size_type view_size,
2693 const Reloc_symbol_changes* reloc_symbol_changes)
2694 {
2695 typedef Target_powerpc<size, big_endian> Powerpc;
2696 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2697
2698 gold_assert(sh_type == elfcpp::SHT_RELA);
2699
2700 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2701 Powerpc_relocate>(
2702 relinfo,
2703 this,
2704 prelocs,
2705 reloc_count,
2706 output_section,
2707 needs_special_offset_handling,
2708 view,
2709 address,
2710 view_size,
2711 reloc_symbol_changes);
2712 }
2713
2714 class Powerpc_scan_relocatable_reloc
2715 {
2716 public:
2717 // Return the strategy to use for a local symbol which is not a
2718 // section symbol, given the relocation type.
2719 inline Relocatable_relocs::Reloc_strategy
2720 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2721 {
2722 if (r_type == 0 && r_sym == 0)
2723 return Relocatable_relocs::RELOC_DISCARD;
2724 return Relocatable_relocs::RELOC_COPY;
2725 }
2726
2727 // Return the strategy to use for a local symbol which is a section
2728 // symbol, given the relocation type.
2729 inline Relocatable_relocs::Reloc_strategy
2730 local_section_strategy(unsigned int, Relobj*)
2731 {
2732 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2733 }
2734
2735 // Return the strategy to use for a global symbol, given the
2736 // relocation type, the object, and the symbol index.
2737 inline Relocatable_relocs::Reloc_strategy
2738 global_strategy(unsigned int r_type, Relobj*, unsigned int)
2739 {
2740 if (r_type == elfcpp::R_PPC_PLTREL24)
2741 return Relocatable_relocs::RELOC_SPECIAL;
2742 return Relocatable_relocs::RELOC_COPY;
2743 }
2744 };
2745
2746 // Scan the relocs during a relocatable link.
2747
2748 template<int size, bool big_endian>
2749 void
2750 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2751 Symbol_table* symtab,
2752 Layout* layout,
2753 Sized_relobj_file<size, big_endian>* object,
2754 unsigned int data_shndx,
2755 unsigned int sh_type,
2756 const unsigned char* prelocs,
2757 size_t reloc_count,
2758 Output_section* output_section,
2759 bool needs_special_offset_handling,
2760 size_t local_symbol_count,
2761 const unsigned char* plocal_symbols,
2762 Relocatable_relocs* rr)
2763 {
2764 gold_assert(sh_type == elfcpp::SHT_RELA);
2765
2766 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2767 Powerpc_scan_relocatable_reloc>(
2768 symtab,
2769 layout,
2770 object,
2771 data_shndx,
2772 prelocs,
2773 reloc_count,
2774 output_section,
2775 needs_special_offset_handling,
2776 local_symbol_count,
2777 plocal_symbols,
2778 rr);
2779 }
2780
2781 // Relocate a section during a relocatable link.
2782
2783 template<int size, bool big_endian>
2784 void
2785 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2786 const Relocate_info<size, big_endian>* relinfo,
2787 unsigned int sh_type,
2788 const unsigned char* prelocs,
2789 size_t reloc_count,
2790 Output_section* output_section,
2791 off_t offset_in_output_section,
2792 const Relocatable_relocs* rr,
2793 unsigned char*,
2794 Address,
2795 section_size_type,
2796 unsigned char* reloc_view,
2797 section_size_type reloc_view_size)
2798 {
2799 gold_assert(sh_type == elfcpp::SHT_RELA);
2800
2801 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2802 Reltype;
2803 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2804 Reltype_write;
2805 const int reloc_size
2806 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2807
2808 Powerpc_relobj<size, big_endian>* const object
2809 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2810 const unsigned int local_count = object->local_symbol_count();
2811 unsigned int got2_shndx = object->got2_shndx();
2812 Address got2_addend = 0;
2813 if (got2_shndx != 0)
2814 {
2815 got2_addend = object->get_output_section_offset(got2_shndx);
2816 gold_assert(got2_addend != invalid_address);
2817 }
2818
2819 unsigned char* pwrite = reloc_view;
2820
2821 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2822 {
2823 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2824 if (strategy == Relocatable_relocs::RELOC_DISCARD)
2825 continue;
2826
2827 Reltype reloc(prelocs);
2828 Reltype_write reloc_write(pwrite);
2829
2830 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2831 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2832 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2833
2834 // Get the new symbol index.
2835
2836 unsigned int new_symndx;
2837 if (r_sym < local_count)
2838 {
2839 switch (strategy)
2840 {
2841 case Relocatable_relocs::RELOC_COPY:
2842 case Relocatable_relocs::RELOC_SPECIAL:
2843 new_symndx = object->symtab_index(r_sym);
2844 gold_assert(new_symndx != -1U);
2845 break;
2846
2847 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2848 {
2849 // We are adjusting a section symbol. We need to find
2850 // the symbol table index of the section symbol for
2851 // the output section corresponding to input section
2852 // in which this symbol is defined.
2853 gold_assert(r_sym < local_count);
2854 bool is_ordinary;
2855 unsigned int shndx =
2856 object->local_symbol_input_shndx(r_sym, &is_ordinary);
2857 gold_assert(is_ordinary);
2858 Output_section* os = object->output_section(shndx);
2859 gold_assert(os != NULL);
2860 gold_assert(os->needs_symtab_index());
2861 new_symndx = os->symtab_index();
2862 }
2863 break;
2864
2865 default:
2866 gold_unreachable();
2867 }
2868 }
2869 else
2870 {
2871 const Symbol* gsym = object->global_symbol(r_sym);
2872 gold_assert(gsym != NULL);
2873 if (gsym->is_forwarder())
2874 gsym = relinfo->symtab->resolve_forwards(gsym);
2875
2876 gold_assert(gsym->has_symtab_index());
2877 new_symndx = gsym->symtab_index();
2878 }
2879
2880 // Get the new offset--the location in the output section where
2881 // this relocation should be applied.
2882
2883 Address offset = reloc.get_r_offset();
2884 Address new_offset;
2885 if (static_cast<Address>(offset_in_output_section) != invalid_address)
2886 new_offset = offset + offset_in_output_section;
2887 else
2888 {
2889 section_offset_type sot_offset =
2890 convert_types<section_offset_type, Address>(offset);
2891 section_offset_type new_sot_offset =
2892 output_section->output_offset(object, relinfo->data_shndx,
2893 sot_offset);
2894 gold_assert(new_sot_offset != -1);
2895 new_offset = new_sot_offset;
2896 }
2897
2898 reloc_write.put_r_offset(new_offset);
2899 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2900
2901 // Handle the reloc addend based on the strategy.
2902 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2903 addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2904 get_reloc_addend(&reloc);
2905
2906 if (strategy == Relocatable_relocs::RELOC_COPY)
2907 ;
2908 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2909 {
2910 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2911
2912 addend = psymval->value(object, addend);
2913 }
2914 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2915 {
2916 if (addend >= 32768)
2917 addend += got2_addend;
2918 }
2919 else
2920 gold_unreachable();
2921
2922 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2923 set_reloc_addend(&reloc_write, addend);
2924
2925 pwrite += reloc_size;
2926 }
2927
2928 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2929 == reloc_view_size);
2930 }
2931
2932 // Return the value to use for a dynamic which requires special
2933 // treatment. This is how we support equality comparisons of function
2934 // pointers across shared library boundaries, as described in the
2935 // processor specific ABI supplement.
2936
2937 template<int size, bool big_endian>
2938 uint64_t
2939 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2940 {
2941 if (size == 32)
2942 {
2943 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2944 return this->plt_section()->address() + gsym->plt_offset();
2945 }
2946 else
2947 gold_unreachable();
2948 }
2949
2950 // The selector for powerpc object files.
2951
2952 template<int size, bool big_endian>
2953 class Target_selector_powerpc : public Target_selector
2954 {
2955 public:
2956 Target_selector_powerpc()
2957 : Target_selector(elfcpp::EM_NONE, size, big_endian,
2958 (size == 64
2959 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2960 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2961 (size == 64
2962 ? (big_endian ? "elf64ppc" : "elf64lppc")
2963 : (big_endian ? "elf32ppc" : "elf32lppc")))
2964 { }
2965
2966 virtual Target*
2967 do_recognize(Input_file*, off_t, int machine, int, int)
2968 {
2969 switch (size)
2970 {
2971 case 64:
2972 if (machine != elfcpp::EM_PPC64)
2973 return NULL;
2974 break;
2975
2976 case 32:
2977 if (machine != elfcpp::EM_PPC)
2978 return NULL;
2979 break;
2980
2981 default:
2982 return NULL;
2983 }
2984
2985 return this->instantiate_target();
2986 }
2987
2988 virtual Target*
2989 do_instantiate_target()
2990 { return new Target_powerpc<size, big_endian>(); }
2991 };
2992
2993 Target_selector_powerpc<32, true> target_selector_ppc32;
2994 Target_selector_powerpc<32, false> target_selector_ppc32le;
2995 Target_selector_powerpc<64, true> target_selector_ppc64;
2996 Target_selector_powerpc<64, false> target_selector_ppc64le;
2997
2998 } // End anonymous namespace.