ChangeLog fix.
[deliverable/binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 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 <set>
27 #include <algorithm>
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "powerpc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "errors.h"
43 #include "gc.h"
44
45 namespace
46 {
47
48 using namespace gold;
49
50 template<int size, bool big_endian>
51 class Output_data_plt_powerpc;
52
53 template<int size, bool big_endian>
54 class Output_data_brlt_powerpc;
55
56 template<int size, bool big_endian>
57 class Output_data_got_powerpc;
58
59 template<int size, bool big_endian>
60 class Output_data_glink;
61
62 template<int size, bool big_endian>
63 class Stub_table;
64
65 inline bool
66 is_branch_reloc(unsigned int r_type);
67
68 template<int size, bool big_endian>
69 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
70 {
71 public:
72 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
73 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
74 typedef Unordered_map<Address, Section_refs> Access_from;
75
76 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
77 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
78 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
79 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
80 opd_ent_(), access_from_map_(), has14_(), stub_table_(),
81 e_flags_(ehdr.get_e_flags()), st_other_()
82 {
83 this->set_abiversion(0);
84 }
85
86 ~Powerpc_relobj()
87 { }
88
89 // Read the symbols then set up st_other vector.
90 void
91 do_read_symbols(Read_symbols_data*);
92
93 // The .got2 section shndx.
94 unsigned int
95 got2_shndx() const
96 {
97 if (size == 32)
98 return this->special_;
99 else
100 return 0;
101 }
102
103 // The .opd section shndx.
104 unsigned int
105 opd_shndx() const
106 {
107 if (size == 32)
108 return 0;
109 else
110 return this->special_;
111 }
112
113 // Init OPD entry arrays.
114 void
115 init_opd(size_t opd_size)
116 {
117 size_t count = this->opd_ent_ndx(opd_size);
118 this->opd_ent_.resize(count);
119 }
120
121 // Return section and offset of function entry for .opd + R_OFF.
122 unsigned int
123 get_opd_ent(Address r_off, Address* value = NULL) const
124 {
125 size_t ndx = this->opd_ent_ndx(r_off);
126 gold_assert(ndx < this->opd_ent_.size());
127 gold_assert(this->opd_ent_[ndx].shndx != 0);
128 if (value != NULL)
129 *value = this->opd_ent_[ndx].off;
130 return this->opd_ent_[ndx].shndx;
131 }
132
133 // Set section and offset of function entry for .opd + R_OFF.
134 void
135 set_opd_ent(Address r_off, unsigned int shndx, Address value)
136 {
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 this->opd_ent_[ndx].shndx = shndx;
140 this->opd_ent_[ndx].off = value;
141 }
142
143 // Return discard flag for .opd + R_OFF.
144 bool
145 get_opd_discard(Address r_off) const
146 {
147 size_t ndx = this->opd_ent_ndx(r_off);
148 gold_assert(ndx < this->opd_ent_.size());
149 return this->opd_ent_[ndx].discard;
150 }
151
152 // Set discard flag for .opd + R_OFF.
153 void
154 set_opd_discard(Address r_off)
155 {
156 size_t ndx = this->opd_ent_ndx(r_off);
157 gold_assert(ndx < this->opd_ent_.size());
158 this->opd_ent_[ndx].discard = true;
159 }
160
161 bool
162 opd_valid() const
163 { return this->opd_valid_; }
164
165 void
166 set_opd_valid()
167 { this->opd_valid_ = true; }
168
169 // Examine .rela.opd to build info about function entry points.
170 void
171 scan_opd_relocs(size_t reloc_count,
172 const unsigned char* prelocs,
173 const unsigned char* plocal_syms);
174
175 // Perform the Sized_relobj_file method, then set up opd info from
176 // .opd relocs.
177 void
178 do_read_relocs(Read_relocs_data*);
179
180 bool
181 do_find_special_sections(Read_symbols_data* sd);
182
183 // Adjust this local symbol value. Return false if the symbol
184 // should be discarded from the output file.
185 bool
186 do_adjust_local_symbol(Symbol_value<size>* lv) const
187 {
188 if (size == 64 && this->opd_shndx() != 0)
189 {
190 bool is_ordinary;
191 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
192 return true;
193 if (this->get_opd_discard(lv->input_value()))
194 return false;
195 }
196 return true;
197 }
198
199 Access_from*
200 access_from_map()
201 { return &this->access_from_map_; }
202
203 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
204 // section at DST_OFF.
205 void
206 add_reference(Object* src_obj,
207 unsigned int src_indx,
208 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
209 {
210 Section_id src_id(src_obj, src_indx);
211 this->access_from_map_[dst_off].insert(src_id);
212 }
213
214 // Add a reference to the code section specified by the .opd entry
215 // at DST_OFF
216 void
217 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
218 {
219 size_t ndx = this->opd_ent_ndx(dst_off);
220 if (ndx >= this->opd_ent_.size())
221 this->opd_ent_.resize(ndx + 1);
222 this->opd_ent_[ndx].gc_mark = true;
223 }
224
225 void
226 process_gc_mark(Symbol_table* symtab)
227 {
228 for (size_t i = 0; i < this->opd_ent_.size(); i++)
229 if (this->opd_ent_[i].gc_mark)
230 {
231 unsigned int shndx = this->opd_ent_[i].shndx;
232 symtab->gc()->worklist().push(Section_id(this, shndx));
233 }
234 }
235
236 // Return offset in output GOT section that this object will use
237 // as a TOC pointer. Won't be just a constant with multi-toc support.
238 Address
239 toc_base_offset() const
240 { return 0x8000; }
241
242 void
243 set_has_small_toc_reloc()
244 { has_small_toc_reloc_ = true; }
245
246 bool
247 has_small_toc_reloc() const
248 { return has_small_toc_reloc_; }
249
250 void
251 set_has_14bit_branch(unsigned int shndx)
252 {
253 if (shndx >= this->has14_.size())
254 this->has14_.resize(shndx + 1);
255 this->has14_[shndx] = true;
256 }
257
258 bool
259 has_14bit_branch(unsigned int shndx) const
260 { return shndx < this->has14_.size() && this->has14_[shndx]; }
261
262 void
263 set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
264 {
265 if (shndx >= this->stub_table_.size())
266 this->stub_table_.resize(shndx + 1);
267 this->stub_table_[shndx] = stub_table;
268 }
269
270 Stub_table<size, big_endian>*
271 stub_table(unsigned int shndx)
272 {
273 if (shndx < this->stub_table_.size())
274 return this->stub_table_[shndx];
275 return NULL;
276 }
277
278 int
279 abiversion() const
280 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
281
282 // Set ABI version for input and output
283 void
284 set_abiversion(int ver);
285
286 unsigned int
287 ppc64_local_entry_offset(const Symbol* sym) const
288 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
289
290 unsigned int
291 ppc64_local_entry_offset(unsigned int symndx) const
292 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
293
294 private:
295 struct Opd_ent
296 {
297 unsigned int shndx;
298 bool discard : 1;
299 bool gc_mark : 1;
300 Address off;
301 };
302
303 // Return index into opd_ent_ array for .opd entry at OFF.
304 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
305 // apart when the language doesn't use the last 8-byte word, the
306 // environment pointer. Thus dividing the entry section offset by
307 // 16 will give an index into opd_ent_ that works for either layout
308 // of .opd. (It leaves some elements of the vector unused when .opd
309 // entries are spaced 24 bytes apart, but we don't know the spacing
310 // until relocations are processed, and in any case it is possible
311 // for an object to have some entries spaced 16 bytes apart and
312 // others 24 bytes apart.)
313 size_t
314 opd_ent_ndx(size_t off) const
315 { return off >> 4;}
316
317 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
318 unsigned int special_;
319
320 // For 64-bit, whether this object uses small model relocs to access
321 // the toc.
322 bool has_small_toc_reloc_;
323
324 // Set at the start of gc_process_relocs, when we know opd_ent_
325 // vector is valid. The flag could be made atomic and set in
326 // do_read_relocs with memory_order_release and then tested with
327 // memory_order_acquire, potentially resulting in fewer entries in
328 // access_from_map_.
329 bool opd_valid_;
330
331 // The first 8-byte word of an OPD entry gives the address of the
332 // entry point of the function. Relocatable object files have a
333 // relocation on this word. The following vector records the
334 // section and offset specified by these relocations.
335 std::vector<Opd_ent> opd_ent_;
336
337 // References made to this object's .opd section when running
338 // gc_process_relocs for another object, before the opd_ent_ vector
339 // is valid for this object.
340 Access_from access_from_map_;
341
342 // Whether input section has a 14-bit branch reloc.
343 std::vector<bool> has14_;
344
345 // The stub table to use for a given input section.
346 std::vector<Stub_table<size, big_endian>*> stub_table_;
347
348 // Header e_flags
349 elfcpp::Elf_Word e_flags_;
350
351 // ELF st_other field for local symbols.
352 std::vector<unsigned char> st_other_;
353 };
354
355 template<int size, bool big_endian>
356 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
357 {
358 public:
359 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
360
361 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
362 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
363 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
364 opd_shndx_(0), opd_ent_(), e_flags_(ehdr.get_e_flags())
365 {
366 this->set_abiversion(0);
367 }
368
369 ~Powerpc_dynobj()
370 { }
371
372 // Call Sized_dynobj::do_read_symbols to read the symbols then
373 // read .opd from a dynamic object, filling in opd_ent_ vector,
374 void
375 do_read_symbols(Read_symbols_data*);
376
377 // The .opd section shndx.
378 unsigned int
379 opd_shndx() const
380 {
381 return this->opd_shndx_;
382 }
383
384 // The .opd section address.
385 Address
386 opd_address() const
387 {
388 return this->opd_address_;
389 }
390
391 // Init OPD entry arrays.
392 void
393 init_opd(size_t opd_size)
394 {
395 size_t count = this->opd_ent_ndx(opd_size);
396 this->opd_ent_.resize(count);
397 }
398
399 // Return section and offset of function entry for .opd + R_OFF.
400 unsigned int
401 get_opd_ent(Address r_off, Address* value = NULL) const
402 {
403 size_t ndx = this->opd_ent_ndx(r_off);
404 gold_assert(ndx < this->opd_ent_.size());
405 gold_assert(this->opd_ent_[ndx].shndx != 0);
406 if (value != NULL)
407 *value = this->opd_ent_[ndx].off;
408 return this->opd_ent_[ndx].shndx;
409 }
410
411 // Set section and offset of function entry for .opd + R_OFF.
412 void
413 set_opd_ent(Address r_off, unsigned int shndx, Address value)
414 {
415 size_t ndx = this->opd_ent_ndx(r_off);
416 gold_assert(ndx < this->opd_ent_.size());
417 this->opd_ent_[ndx].shndx = shndx;
418 this->opd_ent_[ndx].off = value;
419 }
420
421 int
422 abiversion() const
423 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
424
425 // Set ABI version for input and output.
426 void
427 set_abiversion(int ver);
428
429 private:
430 // Used to specify extent of executable sections.
431 struct Sec_info
432 {
433 Sec_info(Address start_, Address len_, unsigned int shndx_)
434 : start(start_), len(len_), shndx(shndx_)
435 { }
436
437 bool
438 operator<(const Sec_info& that) const
439 { return this->start < that.start; }
440
441 Address start;
442 Address len;
443 unsigned int shndx;
444 };
445
446 struct Opd_ent
447 {
448 unsigned int shndx;
449 Address off;
450 };
451
452 // Return index into opd_ent_ array for .opd entry at OFF.
453 size_t
454 opd_ent_ndx(size_t off) const
455 { return off >> 4;}
456
457 // For 64-bit the .opd section shndx and address.
458 unsigned int opd_shndx_;
459 Address opd_address_;
460
461 // The first 8-byte word of an OPD entry gives the address of the
462 // entry point of the function. Records the section and offset
463 // corresponding to the address. Note that in dynamic objects,
464 // offset is *not* relative to the section.
465 std::vector<Opd_ent> opd_ent_;
466
467 // Header e_flags
468 elfcpp::Elf_Word e_flags_;
469 };
470
471 template<int size, bool big_endian>
472 class Target_powerpc : public Sized_target<size, big_endian>
473 {
474 public:
475 typedef
476 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
477 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
478 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
479 static const Address invalid_address = static_cast<Address>(0) - 1;
480 // Offset of tp and dtp pointers from start of TLS block.
481 static const Address tp_offset = 0x7000;
482 static const Address dtp_offset = 0x8000;
483
484 Target_powerpc()
485 : Sized_target<size, big_endian>(&powerpc_info),
486 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
487 glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
488 tlsld_got_offset_(-1U),
489 stub_tables_(), branch_lookup_table_(), branch_info_(),
490 plt_thread_safe_(false)
491 {
492 }
493
494 // Process the relocations to determine unreferenced sections for
495 // garbage collection.
496 void
497 gc_process_relocs(Symbol_table* symtab,
498 Layout* layout,
499 Sized_relobj_file<size, big_endian>* object,
500 unsigned int data_shndx,
501 unsigned int sh_type,
502 const unsigned char* prelocs,
503 size_t reloc_count,
504 Output_section* output_section,
505 bool needs_special_offset_handling,
506 size_t local_symbol_count,
507 const unsigned char* plocal_symbols);
508
509 // Scan the relocations to look for symbol adjustments.
510 void
511 scan_relocs(Symbol_table* symtab,
512 Layout* layout,
513 Sized_relobj_file<size, big_endian>* object,
514 unsigned int data_shndx,
515 unsigned int sh_type,
516 const unsigned char* prelocs,
517 size_t reloc_count,
518 Output_section* output_section,
519 bool needs_special_offset_handling,
520 size_t local_symbol_count,
521 const unsigned char* plocal_symbols);
522
523 // Map input .toc section to output .got section.
524 const char*
525 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
526 {
527 if (size == 64 && strcmp(name, ".toc") == 0)
528 {
529 *plen = 4;
530 return ".got";
531 }
532 return NULL;
533 }
534
535 // Provide linker defined save/restore functions.
536 void
537 define_save_restore_funcs(Layout*, Symbol_table*);
538
539 // No stubs unless a final link.
540 bool
541 do_may_relax() const
542 { return !parameters->options().relocatable(); }
543
544 bool
545 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
546
547 void
548 do_plt_fde_location(const Output_data*, unsigned char*,
549 uint64_t*, off_t*) const;
550
551 // Stash info about branches, for stub generation.
552 void
553 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
554 unsigned int data_shndx, Address r_offset,
555 unsigned int r_type, unsigned int r_sym, Address addend)
556 {
557 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
558 this->branch_info_.push_back(info);
559 if (r_type == elfcpp::R_POWERPC_REL14
560 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
561 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
562 ppc_object->set_has_14bit_branch(data_shndx);
563 }
564
565 Stub_table<size, big_endian>*
566 new_stub_table();
567
568 void
569 do_define_standard_symbols(Symbol_table*, Layout*);
570
571 // Finalize the sections.
572 void
573 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
574
575 // Return the value to use for a dynamic which requires special
576 // treatment.
577 uint64_t
578 do_dynsym_value(const Symbol*) const;
579
580 // Return the PLT address to use for a local symbol.
581 uint64_t
582 do_plt_address_for_local(const Relobj*, unsigned int) const;
583
584 // Return the PLT address to use for a global symbol.
585 uint64_t
586 do_plt_address_for_global(const Symbol*) const;
587
588 // Return the offset to use for the GOT_INDX'th got entry which is
589 // for a local tls symbol specified by OBJECT, SYMNDX.
590 int64_t
591 do_tls_offset_for_local(const Relobj* object,
592 unsigned int symndx,
593 unsigned int got_indx) const;
594
595 // Return the offset to use for the GOT_INDX'th got entry which is
596 // for global tls symbol GSYM.
597 int64_t
598 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
599
600 void
601 do_function_location(Symbol_location*) const;
602
603 bool
604 do_can_check_for_function_pointers() const
605 { return true; }
606
607 // Relocate a section.
608 void
609 relocate_section(const Relocate_info<size, big_endian>*,
610 unsigned int sh_type,
611 const unsigned char* prelocs,
612 size_t reloc_count,
613 Output_section* output_section,
614 bool needs_special_offset_handling,
615 unsigned char* view,
616 Address view_address,
617 section_size_type view_size,
618 const Reloc_symbol_changes*);
619
620 // Scan the relocs during a relocatable link.
621 void
622 scan_relocatable_relocs(Symbol_table* symtab,
623 Layout* layout,
624 Sized_relobj_file<size, big_endian>* object,
625 unsigned int data_shndx,
626 unsigned int sh_type,
627 const unsigned char* prelocs,
628 size_t reloc_count,
629 Output_section* output_section,
630 bool needs_special_offset_handling,
631 size_t local_symbol_count,
632 const unsigned char* plocal_symbols,
633 Relocatable_relocs*);
634
635 // Emit relocations for a section.
636 void
637 relocate_relocs(const Relocate_info<size, big_endian>*,
638 unsigned int sh_type,
639 const unsigned char* prelocs,
640 size_t reloc_count,
641 Output_section* output_section,
642 typename elfcpp::Elf_types<size>::Elf_Off
643 offset_in_output_section,
644 const Relocatable_relocs*,
645 unsigned char*,
646 Address view_address,
647 section_size_type,
648 unsigned char* reloc_view,
649 section_size_type reloc_view_size);
650
651 // Return whether SYM is defined by the ABI.
652 bool
653 do_is_defined_by_abi(const Symbol* sym) const
654 {
655 return strcmp(sym->name(), "__tls_get_addr") == 0;
656 }
657
658 // Return the size of the GOT section.
659 section_size_type
660 got_size() const
661 {
662 gold_assert(this->got_ != NULL);
663 return this->got_->data_size();
664 }
665
666 // Get the PLT section.
667 const Output_data_plt_powerpc<size, big_endian>*
668 plt_section() const
669 {
670 gold_assert(this->plt_ != NULL);
671 return this->plt_;
672 }
673
674 // Get the IPLT section.
675 const Output_data_plt_powerpc<size, big_endian>*
676 iplt_section() const
677 {
678 gold_assert(this->iplt_ != NULL);
679 return this->iplt_;
680 }
681
682 // Get the .glink section.
683 const Output_data_glink<size, big_endian>*
684 glink_section() const
685 {
686 gold_assert(this->glink_ != NULL);
687 return this->glink_;
688 }
689
690 bool has_glink() const
691 { return this->glink_ != NULL; }
692
693 // Get the GOT section.
694 const Output_data_got_powerpc<size, big_endian>*
695 got_section() const
696 {
697 gold_assert(this->got_ != NULL);
698 return this->got_;
699 }
700
701 // Get the GOT section, creating it if necessary.
702 Output_data_got_powerpc<size, big_endian>*
703 got_section(Symbol_table*, Layout*);
704
705 Object*
706 do_make_elf_object(const std::string&, Input_file*, off_t,
707 const elfcpp::Ehdr<size, big_endian>&);
708
709 // Return the number of entries in the GOT.
710 unsigned int
711 got_entry_count() const
712 {
713 if (this->got_ == NULL)
714 return 0;
715 return this->got_size() / (size / 8);
716 }
717
718 // Return the number of entries in the PLT.
719 unsigned int
720 plt_entry_count() const;
721
722 // Return the offset of the first non-reserved PLT entry.
723 unsigned int
724 first_plt_entry_offset() const
725 {
726 if (size == 32)
727 return 0;
728 if (this->abiversion() >= 2)
729 return 16;
730 return 24;
731 }
732
733 // Return the size of each PLT entry.
734 unsigned int
735 plt_entry_size() const
736 {
737 if (size == 32)
738 return 4;
739 if (this->abiversion() >= 2)
740 return 8;
741 return 24;
742 }
743
744 // Add any special sections for this symbol to the gc work list.
745 // For powerpc64, this adds the code section of a function
746 // descriptor.
747 void
748 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
749
750 // Handle target specific gc actions when adding a gc reference from
751 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
752 // and DST_OFF. For powerpc64, this adds a referenc to the code
753 // section of a function descriptor.
754 void
755 do_gc_add_reference(Symbol_table* symtab,
756 Object* src_obj,
757 unsigned int src_shndx,
758 Object* dst_obj,
759 unsigned int dst_shndx,
760 Address dst_off) const;
761
762 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
763 const Stub_tables&
764 stub_tables() const
765 { return this->stub_tables_; }
766
767 const Output_data_brlt_powerpc<size, big_endian>*
768 brlt_section() const
769 { return this->brlt_section_; }
770
771 void
772 add_branch_lookup_table(Address to)
773 {
774 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
775 this->branch_lookup_table_.insert(std::make_pair(to, off));
776 }
777
778 Address
779 find_branch_lookup_table(Address to)
780 {
781 typename Branch_lookup_table::const_iterator p
782 = this->branch_lookup_table_.find(to);
783 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
784 }
785
786 void
787 write_branch_lookup_table(unsigned char *oview)
788 {
789 for (typename Branch_lookup_table::const_iterator p
790 = this->branch_lookup_table_.begin();
791 p != this->branch_lookup_table_.end();
792 ++p)
793 {
794 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
795 }
796 }
797
798 bool
799 plt_thread_safe() const
800 { return this->plt_thread_safe_; }
801
802 int
803 abiversion () const
804 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
805
806 void
807 set_abiversion (int ver)
808 {
809 elfcpp::Elf_Word flags = this->processor_specific_flags();
810 flags &= ~elfcpp::EF_PPC64_ABI;
811 flags |= ver & elfcpp::EF_PPC64_ABI;
812 this->set_processor_specific_flags(flags);
813 }
814
815 // Offset to to save stack slot
816 int
817 stk_toc () const
818 { return this->abiversion() < 2 ? 40 : 24; }
819
820 private:
821
822 class Track_tls
823 {
824 public:
825 enum Tls_get_addr
826 {
827 NOT_EXPECTED = 0,
828 EXPECTED = 1,
829 SKIP = 2,
830 NORMAL = 3
831 };
832
833 Track_tls()
834 : tls_get_addr_(NOT_EXPECTED),
835 relinfo_(NULL), relnum_(0), r_offset_(0)
836 { }
837
838 ~Track_tls()
839 {
840 if (this->tls_get_addr_ != NOT_EXPECTED)
841 this->missing();
842 }
843
844 void
845 missing(void)
846 {
847 if (this->relinfo_ != NULL)
848 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
849 _("missing expected __tls_get_addr call"));
850 }
851
852 void
853 expect_tls_get_addr_call(
854 const Relocate_info<size, big_endian>* relinfo,
855 size_t relnum,
856 Address r_offset)
857 {
858 this->tls_get_addr_ = EXPECTED;
859 this->relinfo_ = relinfo;
860 this->relnum_ = relnum;
861 this->r_offset_ = r_offset;
862 }
863
864 void
865 expect_tls_get_addr_call()
866 { this->tls_get_addr_ = EXPECTED; }
867
868 void
869 skip_next_tls_get_addr_call()
870 {this->tls_get_addr_ = SKIP; }
871
872 Tls_get_addr
873 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
874 {
875 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
876 || r_type == elfcpp::R_PPC_PLTREL24)
877 && gsym != NULL
878 && strcmp(gsym->name(), "__tls_get_addr") == 0);
879 Tls_get_addr last_tls = this->tls_get_addr_;
880 this->tls_get_addr_ = NOT_EXPECTED;
881 if (is_tls_call && last_tls != EXPECTED)
882 return last_tls;
883 else if (!is_tls_call && last_tls != NOT_EXPECTED)
884 {
885 this->missing();
886 return EXPECTED;
887 }
888 return NORMAL;
889 }
890
891 private:
892 // What we're up to regarding calls to __tls_get_addr.
893 // On powerpc, the branch and link insn making a call to
894 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
895 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
896 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
897 // The marker relocation always comes first, and has the same
898 // symbol as the reloc on the insn setting up the __tls_get_addr
899 // argument. This ties the arg setup insn with the call insn,
900 // allowing ld to safely optimize away the call. We check that
901 // every call to __tls_get_addr has a marker relocation, and that
902 // every marker relocation is on a call to __tls_get_addr.
903 Tls_get_addr tls_get_addr_;
904 // Info about the last reloc for error message.
905 const Relocate_info<size, big_endian>* relinfo_;
906 size_t relnum_;
907 Address r_offset_;
908 };
909
910 // The class which scans relocations.
911 class Scan : protected Track_tls
912 {
913 public:
914 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
915
916 Scan()
917 : Track_tls(), issued_non_pic_error_(false)
918 { }
919
920 static inline int
921 get_reference_flags(unsigned int r_type);
922
923 inline void
924 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
925 Sized_relobj_file<size, big_endian>* object,
926 unsigned int data_shndx,
927 Output_section* output_section,
928 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
929 const elfcpp::Sym<size, big_endian>& lsym,
930 bool is_discarded);
931
932 inline void
933 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
934 Sized_relobj_file<size, big_endian>* object,
935 unsigned int data_shndx,
936 Output_section* output_section,
937 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
938 Symbol* gsym);
939
940 inline bool
941 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
942 Target_powerpc* ,
943 Sized_relobj_file<size, big_endian>* ,
944 unsigned int ,
945 Output_section* ,
946 const elfcpp::Rela<size, big_endian>& ,
947 unsigned int r_type,
948 const elfcpp::Sym<size, big_endian>&)
949 {
950 // PowerPC64 .opd is not folded, so any identical function text
951 // may be folded and we'll still keep function addresses distinct.
952 // That means no reloc is of concern here.
953 if (size == 64)
954 return false;
955 // For 32-bit, conservatively assume anything but calls to
956 // function code might be taking the address of the function.
957 return !is_branch_reloc(r_type);
958 }
959
960 inline bool
961 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
962 Target_powerpc* ,
963 Sized_relobj_file<size, big_endian>* ,
964 unsigned int ,
965 Output_section* ,
966 const elfcpp::Rela<size, big_endian>& ,
967 unsigned int r_type,
968 Symbol*)
969 {
970 // As above.
971 if (size == 64)
972 return false;
973 return !is_branch_reloc(r_type);
974 }
975
976 static bool
977 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
978 unsigned int r_type, bool report_err);
979
980 private:
981 static void
982 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
983 unsigned int r_type);
984
985 static void
986 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
987 unsigned int r_type, Symbol*);
988
989 static void
990 generate_tls_call(Symbol_table* symtab, Layout* layout,
991 Target_powerpc* target);
992
993 void
994 check_non_pic(Relobj*, unsigned int r_type);
995
996 // Whether we have issued an error about a non-PIC compilation.
997 bool issued_non_pic_error_;
998 };
999
1000 Address
1001 symval_for_branch(const Symbol_table* symtab, Address value,
1002 const Sized_symbol<size>* gsym,
1003 Powerpc_relobj<size, big_endian>* object,
1004 unsigned int *dest_shndx);
1005
1006 // The class which implements relocation.
1007 class Relocate : protected Track_tls
1008 {
1009 public:
1010 // Use 'at' branch hints when true, 'y' when false.
1011 // FIXME maybe: set this with an option.
1012 static const bool is_isa_v2 = true;
1013
1014 Relocate()
1015 : Track_tls()
1016 { }
1017
1018 // Do a relocation. Return false if the caller should not issue
1019 // any warnings about this relocation.
1020 inline bool
1021 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
1022 Output_section*, size_t relnum,
1023 const elfcpp::Rela<size, big_endian>&,
1024 unsigned int r_type, const Sized_symbol<size>*,
1025 const Symbol_value<size>*,
1026 unsigned char*,
1027 typename elfcpp::Elf_types<size>::Elf_Addr,
1028 section_size_type);
1029 };
1030
1031 class Relocate_comdat_behavior
1032 {
1033 public:
1034 // Decide what the linker should do for relocations that refer to
1035 // discarded comdat sections.
1036 inline Comdat_behavior
1037 get(const char* name)
1038 {
1039 gold::Default_comdat_behavior default_behavior;
1040 Comdat_behavior ret = default_behavior.get(name);
1041 if (ret == CB_WARNING)
1042 {
1043 if (size == 32
1044 && (strcmp(name, ".fixup") == 0
1045 || strcmp(name, ".got2") == 0))
1046 ret = CB_IGNORE;
1047 if (size == 64
1048 && (strcmp(name, ".opd") == 0
1049 || strcmp(name, ".toc") == 0
1050 || strcmp(name, ".toc1") == 0))
1051 ret = CB_IGNORE;
1052 }
1053 return ret;
1054 }
1055 };
1056
1057 // A class which returns the size required for a relocation type,
1058 // used while scanning relocs during a relocatable link.
1059 class Relocatable_size_for_reloc
1060 {
1061 public:
1062 unsigned int
1063 get_size_for_reloc(unsigned int, Relobj*)
1064 {
1065 gold_unreachable();
1066 return 0;
1067 }
1068 };
1069
1070 // Optimize the TLS relocation type based on what we know about the
1071 // symbol. IS_FINAL is true if the final address of this symbol is
1072 // known at link time.
1073
1074 tls::Tls_optimization
1075 optimize_tls_gd(bool is_final)
1076 {
1077 // If we are generating a shared library, then we can't do anything
1078 // in the linker.
1079 if (parameters->options().shared())
1080 return tls::TLSOPT_NONE;
1081
1082 if (!is_final)
1083 return tls::TLSOPT_TO_IE;
1084 return tls::TLSOPT_TO_LE;
1085 }
1086
1087 tls::Tls_optimization
1088 optimize_tls_ld()
1089 {
1090 if (parameters->options().shared())
1091 return tls::TLSOPT_NONE;
1092
1093 return tls::TLSOPT_TO_LE;
1094 }
1095
1096 tls::Tls_optimization
1097 optimize_tls_ie(bool is_final)
1098 {
1099 if (!is_final || parameters->options().shared())
1100 return tls::TLSOPT_NONE;
1101
1102 return tls::TLSOPT_TO_LE;
1103 }
1104
1105 // Create glink.
1106 void
1107 make_glink_section(Layout*);
1108
1109 // Create the PLT section.
1110 void
1111 make_plt_section(Symbol_table*, Layout*);
1112
1113 void
1114 make_iplt_section(Symbol_table*, Layout*);
1115
1116 void
1117 make_brlt_section(Layout*);
1118
1119 // Create a PLT entry for a global symbol.
1120 void
1121 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1122
1123 // Create a PLT entry for a local IFUNC symbol.
1124 void
1125 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1126 Sized_relobj_file<size, big_endian>*,
1127 unsigned int);
1128
1129
1130 // Create a GOT entry for local dynamic __tls_get_addr.
1131 unsigned int
1132 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1133 Sized_relobj_file<size, big_endian>* object);
1134
1135 unsigned int
1136 tlsld_got_offset() const
1137 {
1138 return this->tlsld_got_offset_;
1139 }
1140
1141 // Get the dynamic reloc section, creating it if necessary.
1142 Reloc_section*
1143 rela_dyn_section(Layout*);
1144
1145 // Similarly, but for ifunc symbols get the one for ifunc.
1146 Reloc_section*
1147 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1148
1149 // Copy a relocation against a global symbol.
1150 void
1151 copy_reloc(Symbol_table* symtab, Layout* layout,
1152 Sized_relobj_file<size, big_endian>* object,
1153 unsigned int shndx, Output_section* output_section,
1154 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1155 {
1156 this->copy_relocs_.copy_reloc(symtab, layout,
1157 symtab->get_sized_symbol<size>(sym),
1158 object, shndx, output_section,
1159 reloc, this->rela_dyn_section(layout));
1160 }
1161
1162 // Look over all the input sections, deciding where to place stubs.
1163 void
1164 group_sections(Layout*, const Task*);
1165
1166 // Sort output sections by address.
1167 struct Sort_sections
1168 {
1169 bool
1170 operator()(const Output_section* sec1, const Output_section* sec2)
1171 { return sec1->address() < sec2->address(); }
1172 };
1173
1174 class Branch_info
1175 {
1176 public:
1177 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1178 unsigned int data_shndx,
1179 Address r_offset,
1180 unsigned int r_type,
1181 unsigned int r_sym,
1182 Address addend)
1183 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1184 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1185 { }
1186
1187 ~Branch_info()
1188 { }
1189
1190 // If this branch needs a plt call stub, or a long branch stub, make one.
1191 void
1192 make_stub(Stub_table<size, big_endian>*,
1193 Stub_table<size, big_endian>*,
1194 Symbol_table*) const;
1195
1196 private:
1197 // The branch location..
1198 Powerpc_relobj<size, big_endian>* object_;
1199 unsigned int shndx_;
1200 Address offset_;
1201 // ..and the branch type and destination.
1202 unsigned int r_type_;
1203 unsigned int r_sym_;
1204 Address addend_;
1205 };
1206
1207 // Information about this specific target which we pass to the
1208 // general Target structure.
1209 static Target::Target_info powerpc_info;
1210
1211 // The types of GOT entries needed for this platform.
1212 // These values are exposed to the ABI in an incremental link.
1213 // Do not renumber existing values without changing the version
1214 // number of the .gnu_incremental_inputs section.
1215 enum Got_type
1216 {
1217 GOT_TYPE_STANDARD,
1218 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1219 GOT_TYPE_DTPREL, // entry for @got@dtprel
1220 GOT_TYPE_TPREL // entry for @got@tprel
1221 };
1222
1223 // The GOT section.
1224 Output_data_got_powerpc<size, big_endian>* got_;
1225 // The PLT section. This is a container for a table of addresses,
1226 // and their relocations. Each address in the PLT has a dynamic
1227 // relocation (R_*_JMP_SLOT) and each address will have a
1228 // corresponding entry in .glink for lazy resolution of the PLT.
1229 // ppc32 initialises the PLT to point at the .glink entry, while
1230 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1231 // linker adds a stub that loads the PLT entry into ctr then
1232 // branches to ctr. There may be more than one stub for each PLT
1233 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1234 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1235 Output_data_plt_powerpc<size, big_endian>* plt_;
1236 // The IPLT section. Like plt_, this is a container for a table of
1237 // addresses and their relocations, specifically for STT_GNU_IFUNC
1238 // functions that resolve locally (STT_GNU_IFUNC functions that
1239 // don't resolve locally go in PLT). Unlike plt_, these have no
1240 // entry in .glink for lazy resolution, and the relocation section
1241 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1242 // the relocation section may contain relocations against
1243 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1244 // relocation section will appear at the end of other dynamic
1245 // relocations, so that ld.so applies these relocations after other
1246 // dynamic relocations. In a static executable, the relocation
1247 // section is emitted and marked with __rela_iplt_start and
1248 // __rela_iplt_end symbols.
1249 Output_data_plt_powerpc<size, big_endian>* iplt_;
1250 // Section holding long branch destinations.
1251 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1252 // The .glink section.
1253 Output_data_glink<size, big_endian>* glink_;
1254 // The dynamic reloc section.
1255 Reloc_section* rela_dyn_;
1256 // Relocs saved to avoid a COPY reloc.
1257 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1258 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1259 unsigned int tlsld_got_offset_;
1260
1261 Stub_tables stub_tables_;
1262 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1263 Branch_lookup_table branch_lookup_table_;
1264
1265 typedef std::vector<Branch_info> Branches;
1266 Branches branch_info_;
1267
1268 bool plt_thread_safe_;
1269 };
1270
1271 template<>
1272 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1273 {
1274 32, // size
1275 true, // is_big_endian
1276 elfcpp::EM_PPC, // machine_code
1277 false, // has_make_symbol
1278 false, // has_resolve
1279 false, // has_code_fill
1280 true, // is_default_stack_executable
1281 false, // can_icf_inline_merge_sections
1282 '\0', // wrap_char
1283 "/usr/lib/ld.so.1", // dynamic_linker
1284 0x10000000, // default_text_segment_address
1285 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1286 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1287 false, // isolate_execinstr
1288 0, // rosegment_gap
1289 elfcpp::SHN_UNDEF, // small_common_shndx
1290 elfcpp::SHN_UNDEF, // large_common_shndx
1291 0, // small_common_section_flags
1292 0, // large_common_section_flags
1293 NULL, // attributes_section
1294 NULL, // attributes_vendor
1295 "_start" // entry_symbol_name
1296 };
1297
1298 template<>
1299 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1300 {
1301 32, // size
1302 false, // is_big_endian
1303 elfcpp::EM_PPC, // machine_code
1304 false, // has_make_symbol
1305 false, // has_resolve
1306 false, // has_code_fill
1307 true, // is_default_stack_executable
1308 false, // can_icf_inline_merge_sections
1309 '\0', // wrap_char
1310 "/usr/lib/ld.so.1", // dynamic_linker
1311 0x10000000, // default_text_segment_address
1312 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1313 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1314 false, // isolate_execinstr
1315 0, // rosegment_gap
1316 elfcpp::SHN_UNDEF, // small_common_shndx
1317 elfcpp::SHN_UNDEF, // large_common_shndx
1318 0, // small_common_section_flags
1319 0, // large_common_section_flags
1320 NULL, // attributes_section
1321 NULL, // attributes_vendor
1322 "_start" // entry_symbol_name
1323 };
1324
1325 template<>
1326 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1327 {
1328 64, // size
1329 true, // is_big_endian
1330 elfcpp::EM_PPC64, // machine_code
1331 false, // has_make_symbol
1332 false, // has_resolve
1333 false, // has_code_fill
1334 true, // is_default_stack_executable
1335 false, // can_icf_inline_merge_sections
1336 '\0', // wrap_char
1337 "/usr/lib/ld.so.1", // dynamic_linker
1338 0x10000000, // default_text_segment_address
1339 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1340 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1341 false, // isolate_execinstr
1342 0, // rosegment_gap
1343 elfcpp::SHN_UNDEF, // small_common_shndx
1344 elfcpp::SHN_UNDEF, // large_common_shndx
1345 0, // small_common_section_flags
1346 0, // large_common_section_flags
1347 NULL, // attributes_section
1348 NULL, // attributes_vendor
1349 "_start" // entry_symbol_name
1350 };
1351
1352 template<>
1353 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1354 {
1355 64, // size
1356 false, // is_big_endian
1357 elfcpp::EM_PPC64, // machine_code
1358 false, // has_make_symbol
1359 false, // has_resolve
1360 false, // has_code_fill
1361 true, // is_default_stack_executable
1362 false, // can_icf_inline_merge_sections
1363 '\0', // wrap_char
1364 "/usr/lib/ld.so.1", // dynamic_linker
1365 0x10000000, // default_text_segment_address
1366 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1367 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1368 false, // isolate_execinstr
1369 0, // rosegment_gap
1370 elfcpp::SHN_UNDEF, // small_common_shndx
1371 elfcpp::SHN_UNDEF, // large_common_shndx
1372 0, // small_common_section_flags
1373 0, // large_common_section_flags
1374 NULL, // attributes_section
1375 NULL, // attributes_vendor
1376 "_start" // entry_symbol_name
1377 };
1378
1379 inline bool
1380 is_branch_reloc(unsigned int r_type)
1381 {
1382 return (r_type == elfcpp::R_POWERPC_REL24
1383 || r_type == elfcpp::R_PPC_PLTREL24
1384 || r_type == elfcpp::R_PPC_LOCAL24PC
1385 || r_type == elfcpp::R_POWERPC_REL14
1386 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1387 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1388 || r_type == elfcpp::R_POWERPC_ADDR24
1389 || r_type == elfcpp::R_POWERPC_ADDR14
1390 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1391 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1392 }
1393
1394 // If INSN is an opcode that may be used with an @tls operand, return
1395 // the transformed insn for TLS optimisation, otherwise return 0. If
1396 // REG is non-zero only match an insn with RB or RA equal to REG.
1397 uint32_t
1398 at_tls_transform(uint32_t insn, unsigned int reg)
1399 {
1400 if ((insn & (0x3f << 26)) != 31 << 26)
1401 return 0;
1402
1403 unsigned int rtra;
1404 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1405 rtra = insn & ((1 << 26) - (1 << 16));
1406 else if (((insn >> 16) & 0x1f) == reg)
1407 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1408 else
1409 return 0;
1410
1411 if ((insn & (0x3ff << 1)) == 266 << 1)
1412 // add -> addi
1413 insn = 14 << 26;
1414 else if ((insn & (0x1f << 1)) == 23 << 1
1415 && ((insn & (0x1f << 6)) < 14 << 6
1416 || ((insn & (0x1f << 6)) >= 16 << 6
1417 && (insn & (0x1f << 6)) < 24 << 6)))
1418 // load and store indexed -> dform
1419 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1420 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1421 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1422 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1423 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1424 // lwax -> lwa
1425 insn = (58 << 26) | 2;
1426 else
1427 return 0;
1428 insn |= rtra;
1429 return insn;
1430 }
1431
1432 // Modified version of symtab.h class Symbol member
1433 // Given a direct absolute or pc-relative static relocation against
1434 // the global symbol, this function returns whether a dynamic relocation
1435 // is needed.
1436
1437 template<int size>
1438 bool
1439 needs_dynamic_reloc(const Symbol* gsym, int flags)
1440 {
1441 // No dynamic relocations in a static link!
1442 if (parameters->doing_static_link())
1443 return false;
1444
1445 // A reference to an undefined symbol from an executable should be
1446 // statically resolved to 0, and does not need a dynamic relocation.
1447 // This matches gnu ld behavior.
1448 if (gsym->is_undefined() && !parameters->options().shared())
1449 return false;
1450
1451 // A reference to an absolute symbol does not need a dynamic relocation.
1452 if (gsym->is_absolute())
1453 return false;
1454
1455 // An absolute reference within a position-independent output file
1456 // will need a dynamic relocation.
1457 if ((flags & Symbol::ABSOLUTE_REF)
1458 && parameters->options().output_is_position_independent())
1459 return true;
1460
1461 // A function call that can branch to a local PLT entry does not need
1462 // a dynamic relocation.
1463 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
1464 return false;
1465
1466 // A reference to any PLT entry in a non-position-independent executable
1467 // does not need a dynamic relocation.
1468 // Except due to having function descriptors on powerpc64 we don't define
1469 // functions to their plt code in an executable, so this doesn't apply.
1470 if (size == 32
1471 && !parameters->options().output_is_position_independent()
1472 && gsym->has_plt_offset())
1473 return false;
1474
1475 // A reference to a symbol defined in a dynamic object or to a
1476 // symbol that is preemptible will need a dynamic relocation.
1477 if (gsym->is_from_dynobj()
1478 || gsym->is_undefined()
1479 || gsym->is_preemptible())
1480 return true;
1481
1482 // For all other cases, return FALSE.
1483 return false;
1484 }
1485
1486 // Modified version of symtab.h class Symbol member
1487 // Whether we should use the PLT offset associated with a symbol for
1488 // a relocation. FLAGS is a set of Reference_flags.
1489
1490 template<int size>
1491 bool
1492 use_plt_offset(const Symbol* gsym, int flags)
1493 {
1494 // If the symbol doesn't have a PLT offset, then naturally we
1495 // don't want to use it.
1496 if (!gsym->has_plt_offset())
1497 return false;
1498
1499 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1500 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1501 return true;
1502
1503 // If we are going to generate a dynamic relocation, then we will
1504 // wind up using that, so no need to use the PLT entry.
1505 if (needs_dynamic_reloc<size>(gsym, flags))
1506 return false;
1507
1508 // If the symbol is from a dynamic object, we need to use the PLT
1509 // entry.
1510 if (gsym->is_from_dynobj())
1511 return true;
1512
1513 // If we are generating a shared object, and this symbol is
1514 // undefined or preemptible, we need to use the PLT entry.
1515 if (parameters->options().shared()
1516 && (gsym->is_undefined() || gsym->is_preemptible()))
1517 return true;
1518
1519 // If this is a call to a weak undefined symbol, we need to use
1520 // the PLT entry; the symbol may be defined by a library loaded
1521 // at runtime.
1522 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1523 return true;
1524
1525 // Otherwise we can use the regular definition.
1526 return false;
1527 }
1528
1529 template<int size, bool big_endian>
1530 class Powerpc_relocate_functions
1531 {
1532 public:
1533 enum Overflow_check
1534 {
1535 CHECK_NONE,
1536 CHECK_SIGNED,
1537 CHECK_BITFIELD
1538 };
1539
1540 enum Status
1541 {
1542 STATUS_OK,
1543 STATUS_OVERFLOW
1544 };
1545
1546 private:
1547 typedef Powerpc_relocate_functions<size, big_endian> This;
1548 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1549
1550 template<int valsize>
1551 static inline bool
1552 has_overflow_signed(Address value)
1553 {
1554 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1555 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1556 limit <<= ((valsize - 1) >> 1);
1557 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1558 return value + limit > (limit << 1) - 1;
1559 }
1560
1561 template<int valsize>
1562 static inline bool
1563 has_overflow_bitfield(Address value)
1564 {
1565 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1566 limit <<= ((valsize - 1) >> 1);
1567 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1568 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1569 }
1570
1571 template<int valsize>
1572 static inline Status
1573 overflowed(Address value, Overflow_check overflow)
1574 {
1575 if (overflow == CHECK_SIGNED)
1576 {
1577 if (has_overflow_signed<valsize>(value))
1578 return STATUS_OVERFLOW;
1579 }
1580 else if (overflow == CHECK_BITFIELD)
1581 {
1582 if (has_overflow_bitfield<valsize>(value))
1583 return STATUS_OVERFLOW;
1584 }
1585 return STATUS_OK;
1586 }
1587
1588 // Do a simple RELA relocation
1589 template<int valsize>
1590 static inline Status
1591 rela(unsigned char* view, Address value, Overflow_check overflow)
1592 {
1593 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1594 Valtype* wv = reinterpret_cast<Valtype*>(view);
1595 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1596 return overflowed<valsize>(value, overflow);
1597 }
1598
1599 template<int valsize>
1600 static inline Status
1601 rela(unsigned char* view,
1602 unsigned int right_shift,
1603 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1604 Address value,
1605 Overflow_check overflow)
1606 {
1607 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1608 Valtype* wv = reinterpret_cast<Valtype*>(view);
1609 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1610 Valtype reloc = value >> right_shift;
1611 val &= ~dst_mask;
1612 reloc &= dst_mask;
1613 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1614 return overflowed<valsize>(value >> right_shift, overflow);
1615 }
1616
1617 // Do a simple RELA relocation, unaligned.
1618 template<int valsize>
1619 static inline Status
1620 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1621 {
1622 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1623 return overflowed<valsize>(value, overflow);
1624 }
1625
1626 template<int valsize>
1627 static inline Status
1628 rela_ua(unsigned char* view,
1629 unsigned int right_shift,
1630 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1631 Address value,
1632 Overflow_check overflow)
1633 {
1634 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1635 Valtype;
1636 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1637 Valtype reloc = value >> right_shift;
1638 val &= ~dst_mask;
1639 reloc &= dst_mask;
1640 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1641 return overflowed<valsize>(value >> right_shift, overflow);
1642 }
1643
1644 public:
1645 // R_PPC64_ADDR64: (Symbol + Addend)
1646 static inline void
1647 addr64(unsigned char* view, Address value)
1648 { This::template rela<64>(view, value, CHECK_NONE); }
1649
1650 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1651 static inline void
1652 addr64_u(unsigned char* view, Address value)
1653 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1654
1655 // R_POWERPC_ADDR32: (Symbol + Addend)
1656 static inline Status
1657 addr32(unsigned char* view, Address value, Overflow_check overflow)
1658 { return This::template rela<32>(view, value, overflow); }
1659
1660 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1661 static inline Status
1662 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1663 { return This::template rela_ua<32>(view, value, overflow); }
1664
1665 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1666 static inline Status
1667 addr24(unsigned char* view, Address value, Overflow_check overflow)
1668 {
1669 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1670 if (overflow != CHECK_NONE && (value & 3) != 0)
1671 stat = STATUS_OVERFLOW;
1672 return stat;
1673 }
1674
1675 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1676 static inline Status
1677 addr16(unsigned char* view, Address value, Overflow_check overflow)
1678 { return This::template rela<16>(view, value, overflow); }
1679
1680 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1681 static inline Status
1682 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1683 { return This::template rela_ua<16>(view, value, overflow); }
1684
1685 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1686 static inline Status
1687 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1688 {
1689 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1690 if (overflow != CHECK_NONE && (value & 3) != 0)
1691 stat = STATUS_OVERFLOW;
1692 return stat;
1693 }
1694
1695 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1696 static inline void
1697 addr16_hi(unsigned char* view, Address value)
1698 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1699
1700 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1701 static inline void
1702 addr16_ha(unsigned char* view, Address value)
1703 { This::addr16_hi(view, value + 0x8000); }
1704
1705 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1706 static inline void
1707 addr16_hi2(unsigned char* view, Address value)
1708 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1709
1710 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1711 static inline void
1712 addr16_ha2(unsigned char* view, Address value)
1713 { This::addr16_hi2(view, value + 0x8000); }
1714
1715 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1716 static inline void
1717 addr16_hi3(unsigned char* view, Address value)
1718 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1719
1720 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1721 static inline void
1722 addr16_ha3(unsigned char* view, Address value)
1723 { This::addr16_hi3(view, value + 0x8000); }
1724
1725 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1726 static inline Status
1727 addr14(unsigned char* view, Address value, Overflow_check overflow)
1728 {
1729 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1730 if (overflow != CHECK_NONE && (value & 3) != 0)
1731 stat = STATUS_OVERFLOW;
1732 return stat;
1733 }
1734 };
1735
1736 // Set ABI version for input and output.
1737
1738 template<int size, bool big_endian>
1739 void
1740 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1741 {
1742 this->e_flags_ |= ver;
1743 if (this->abiversion() != 0)
1744 {
1745 Target_powerpc<size, big_endian>* target =
1746 static_cast<Target_powerpc<size, big_endian>*>(
1747 parameters->sized_target<size, big_endian>());
1748 if (target->abiversion() == 0)
1749 target->set_abiversion(this->abiversion());
1750 else if (target->abiversion() != this->abiversion())
1751 gold_error(_("%s: ABI version %d is not compatible "
1752 "with ABI version %d output"),
1753 this->name().c_str(),
1754 this->abiversion(), target->abiversion());
1755
1756 }
1757 }
1758
1759 // Stash away the index of .got2 or .opd in a relocatable object, if
1760 // such a section exists.
1761
1762 template<int size, bool big_endian>
1763 bool
1764 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1765 Read_symbols_data* sd)
1766 {
1767 const unsigned char* const pshdrs = sd->section_headers->data();
1768 const unsigned char* namesu = sd->section_names->data();
1769 const char* names = reinterpret_cast<const char*>(namesu);
1770 section_size_type names_size = sd->section_names_size;
1771 const unsigned char* s;
1772
1773 s = this->template find_shdr<size, big_endian>(pshdrs,
1774 size == 32 ? ".got2" : ".opd",
1775 names, names_size, NULL);
1776 if (s != NULL)
1777 {
1778 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1779 this->special_ = ndx;
1780 if (size == 64)
1781 {
1782 if (this->abiversion() == 0)
1783 this->set_abiversion(1);
1784 else if (this->abiversion() > 1)
1785 gold_error(_("%s: .opd invalid in abiv%d"),
1786 this->name().c_str(), this->abiversion());
1787 }
1788 }
1789 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1790 }
1791
1792 // Examine .rela.opd to build info about function entry points.
1793
1794 template<int size, bool big_endian>
1795 void
1796 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1797 size_t reloc_count,
1798 const unsigned char* prelocs,
1799 const unsigned char* plocal_syms)
1800 {
1801 if (size == 64)
1802 {
1803 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1804 Reltype;
1805 const int reloc_size
1806 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1807 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1808 Address expected_off = 0;
1809 bool regular = true;
1810 unsigned int opd_ent_size = 0;
1811
1812 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1813 {
1814 Reltype reloc(prelocs);
1815 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1816 = reloc.get_r_info();
1817 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1818 if (r_type == elfcpp::R_PPC64_ADDR64)
1819 {
1820 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1821 typename elfcpp::Elf_types<size>::Elf_Addr value;
1822 bool is_ordinary;
1823 unsigned int shndx;
1824 if (r_sym < this->local_symbol_count())
1825 {
1826 typename elfcpp::Sym<size, big_endian>
1827 lsym(plocal_syms + r_sym * sym_size);
1828 shndx = lsym.get_st_shndx();
1829 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1830 value = lsym.get_st_value();
1831 }
1832 else
1833 shndx = this->symbol_section_and_value(r_sym, &value,
1834 &is_ordinary);
1835 this->set_opd_ent(reloc.get_r_offset(), shndx,
1836 value + reloc.get_r_addend());
1837 if (i == 2)
1838 {
1839 expected_off = reloc.get_r_offset();
1840 opd_ent_size = expected_off;
1841 }
1842 else if (expected_off != reloc.get_r_offset())
1843 regular = false;
1844 expected_off += opd_ent_size;
1845 }
1846 else if (r_type == elfcpp::R_PPC64_TOC)
1847 {
1848 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1849 regular = false;
1850 }
1851 else
1852 {
1853 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1854 this->name().c_str(), r_type);
1855 regular = false;
1856 }
1857 }
1858 if (reloc_count <= 2)
1859 opd_ent_size = this->section_size(this->opd_shndx());
1860 if (opd_ent_size != 24 && opd_ent_size != 16)
1861 regular = false;
1862 if (!regular)
1863 {
1864 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1865 this->name().c_str());
1866 opd_ent_size = 0;
1867 }
1868 }
1869 }
1870
1871 template<int size, bool big_endian>
1872 void
1873 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1874 {
1875 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1876 if (size == 64)
1877 {
1878 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1879 p != rd->relocs.end();
1880 ++p)
1881 {
1882 if (p->data_shndx == this->opd_shndx())
1883 {
1884 uint64_t opd_size = this->section_size(this->opd_shndx());
1885 gold_assert(opd_size == static_cast<size_t>(opd_size));
1886 if (opd_size != 0)
1887 {
1888 this->init_opd(opd_size);
1889 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1890 rd->local_symbols->data());
1891 }
1892 break;
1893 }
1894 }
1895 }
1896 }
1897
1898 // Read the symbols then set up st_other vector.
1899
1900 template<int size, bool big_endian>
1901 void
1902 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1903 {
1904 Sized_relobj_file<size, big_endian>::do_read_symbols(sd);
1905 if (size == 64)
1906 {
1907 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1908 const unsigned char* const pshdrs = sd->section_headers->data();
1909 const unsigned int loccount = this->do_local_symbol_count();
1910 if (loccount != 0)
1911 {
1912 this->st_other_.resize(loccount);
1913 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1914 off_t locsize = loccount * sym_size;
1915 const unsigned int symtab_shndx = this->symtab_shndx();
1916 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
1917 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
1918 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
1919 locsize, true, false);
1920 psyms += sym_size;
1921 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1922 {
1923 elfcpp::Sym<size, big_endian> sym(psyms);
1924 unsigned char st_other = sym.get_st_other();
1925 this->st_other_[i] = st_other;
1926 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1927 {
1928 if (this->abiversion() == 0)
1929 this->set_abiversion(2);
1930 else if (this->abiversion() < 2)
1931 gold_error(_("%s: local symbol %d has invalid st_other"
1932 " for ABI version 1"),
1933 this->name().c_str(), i);
1934 }
1935 }
1936 }
1937 }
1938 }
1939
1940 template<int size, bool big_endian>
1941 void
1942 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
1943 {
1944 this->e_flags_ |= ver;
1945 if (this->abiversion() != 0)
1946 {
1947 Target_powerpc<size, big_endian>* target =
1948 static_cast<Target_powerpc<size, big_endian>*>(
1949 parameters->sized_target<size, big_endian>());
1950 if (target->abiversion() == 0)
1951 target->set_abiversion(this->abiversion());
1952 else if (target->abiversion() != this->abiversion())
1953 gold_error(_("%s: ABI version %d is not compatible "
1954 "with ABI version %d output"),
1955 this->name().c_str(),
1956 this->abiversion(), target->abiversion());
1957
1958 }
1959 }
1960
1961 // Call Sized_dynobj::do_read_symbols to read the symbols then
1962 // read .opd from a dynamic object, filling in opd_ent_ vector,
1963
1964 template<int size, bool big_endian>
1965 void
1966 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1967 {
1968 Sized_dynobj<size, big_endian>::do_read_symbols(sd);
1969 if (size == 64)
1970 {
1971 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1972 const unsigned char* const pshdrs = sd->section_headers->data();
1973 const unsigned char* namesu = sd->section_names->data();
1974 const char* names = reinterpret_cast<const char*>(namesu);
1975 const unsigned char* s = NULL;
1976 const unsigned char* opd;
1977 section_size_type opd_size;
1978
1979 // Find and read .opd section.
1980 while (1)
1981 {
1982 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
1983 sd->section_names_size,
1984 s);
1985 if (s == NULL)
1986 return;
1987
1988 typename elfcpp::Shdr<size, big_endian> shdr(s);
1989 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1990 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1991 {
1992 if (this->abiversion() == 0)
1993 this->set_abiversion(1);
1994 else if (this->abiversion() > 1)
1995 gold_error(_("%s: .opd invalid in abiv%d"),
1996 this->name().c_str(), this->abiversion());
1997
1998 this->opd_shndx_ = (s - pshdrs) / shdr_size;
1999 this->opd_address_ = shdr.get_sh_addr();
2000 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2001 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2002 true, false);
2003 break;
2004 }
2005 }
2006
2007 // Build set of executable sections.
2008 // Using a set is probably overkill. There is likely to be only
2009 // a few executable sections, typically .init, .text and .fini,
2010 // and they are generally grouped together.
2011 typedef std::set<Sec_info> Exec_sections;
2012 Exec_sections exec_sections;
2013 s = pshdrs;
2014 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2015 {
2016 typename elfcpp::Shdr<size, big_endian> shdr(s);
2017 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2018 && ((shdr.get_sh_flags()
2019 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2020 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2021 && shdr.get_sh_size() != 0)
2022 {
2023 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2024 shdr.get_sh_size(), i));
2025 }
2026 }
2027 if (exec_sections.empty())
2028 return;
2029
2030 // Look over the OPD entries. This is complicated by the fact
2031 // that some binaries will use two-word entries while others
2032 // will use the standard three-word entries. In most cases
2033 // the third word (the environment pointer for languages like
2034 // Pascal) is unused and will be zero. If the third word is
2035 // used it should not be pointing into executable sections,
2036 // I think.
2037 this->init_opd(opd_size);
2038 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2039 {
2040 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2041 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2042 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2043 if (val == 0)
2044 // Chances are that this is the third word of an OPD entry.
2045 continue;
2046 typename Exec_sections::const_iterator e
2047 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2048 if (e != exec_sections.begin())
2049 {
2050 --e;
2051 if (e->start <= val && val < e->start + e->len)
2052 {
2053 // We have an address in an executable section.
2054 // VAL ought to be the function entry, set it up.
2055 this->set_opd_ent(p - opd, e->shndx, val);
2056 // Skip second word of OPD entry, the TOC pointer.
2057 p += 8;
2058 }
2059 }
2060 // If we didn't match any executable sections, we likely
2061 // have a non-zero third word in the OPD entry.
2062 }
2063 }
2064 }
2065
2066 // Set up some symbols.
2067
2068 template<int size, bool big_endian>
2069 void
2070 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2071 Symbol_table* symtab,
2072 Layout* layout)
2073 {
2074 if (size == 32)
2075 {
2076 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2077 // undefined when scanning relocs (and thus requires
2078 // non-relative dynamic relocs). The proper value will be
2079 // updated later.
2080 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2081 if (gotsym != NULL && gotsym->is_undefined())
2082 {
2083 Target_powerpc<size, big_endian>* target =
2084 static_cast<Target_powerpc<size, big_endian>*>(
2085 parameters->sized_target<size, big_endian>());
2086 Output_data_got_powerpc<size, big_endian>* got
2087 = target->got_section(symtab, layout);
2088 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2089 Symbol_table::PREDEFINED,
2090 got, 0, 0,
2091 elfcpp::STT_OBJECT,
2092 elfcpp::STB_LOCAL,
2093 elfcpp::STV_HIDDEN, 0,
2094 false, false);
2095 }
2096
2097 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2098 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2099 if (sdasym != NULL && sdasym->is_undefined())
2100 {
2101 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2102 Output_section* os
2103 = layout->add_output_section_data(".sdata", 0,
2104 elfcpp::SHF_ALLOC
2105 | elfcpp::SHF_WRITE,
2106 sdata, ORDER_SMALL_DATA, false);
2107 symtab->define_in_output_data("_SDA_BASE_", NULL,
2108 Symbol_table::PREDEFINED,
2109 os, 32768, 0, elfcpp::STT_OBJECT,
2110 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2111 0, false, false);
2112 }
2113 }
2114 else
2115 {
2116 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2117 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2118 if (gotsym != NULL && gotsym->is_undefined())
2119 {
2120 Target_powerpc<size, big_endian>* target =
2121 static_cast<Target_powerpc<size, big_endian>*>(
2122 parameters->sized_target<size, big_endian>());
2123 Output_data_got_powerpc<size, big_endian>* got
2124 = target->got_section(symtab, layout);
2125 symtab->define_in_output_data(".TOC.", NULL,
2126 Symbol_table::PREDEFINED,
2127 got, 0x8000, 0,
2128 elfcpp::STT_OBJECT,
2129 elfcpp::STB_LOCAL,
2130 elfcpp::STV_HIDDEN, 0,
2131 false, false);
2132 }
2133 }
2134 }
2135
2136 // Set up PowerPC target specific relobj.
2137
2138 template<int size, bool big_endian>
2139 Object*
2140 Target_powerpc<size, big_endian>::do_make_elf_object(
2141 const std::string& name,
2142 Input_file* input_file,
2143 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2144 {
2145 int et = ehdr.get_e_type();
2146 // ET_EXEC files are valid input for --just-symbols/-R,
2147 // and we treat them as relocatable objects.
2148 if (et == elfcpp::ET_REL
2149 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2150 {
2151 Powerpc_relobj<size, big_endian>* obj =
2152 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2153 obj->setup();
2154 return obj;
2155 }
2156 else if (et == elfcpp::ET_DYN)
2157 {
2158 Powerpc_dynobj<size, big_endian>* obj =
2159 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2160 obj->setup();
2161 return obj;
2162 }
2163 else
2164 {
2165 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2166 return NULL;
2167 }
2168 }
2169
2170 template<int size, bool big_endian>
2171 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2172 {
2173 public:
2174 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2175 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2176
2177 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2178 : Output_data_got<size, big_endian>(),
2179 symtab_(symtab), layout_(layout),
2180 header_ent_cnt_(size == 32 ? 3 : 1),
2181 header_index_(size == 32 ? 0x2000 : 0)
2182 { }
2183
2184 // Override all the Output_data_got methods we use so as to first call
2185 // reserve_ent().
2186 bool
2187 add_global(Symbol* gsym, unsigned int got_type)
2188 {
2189 this->reserve_ent();
2190 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2191 }
2192
2193 bool
2194 add_global_plt(Symbol* gsym, unsigned int got_type)
2195 {
2196 this->reserve_ent();
2197 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2198 }
2199
2200 bool
2201 add_global_tls(Symbol* gsym, unsigned int got_type)
2202 { return this->add_global_plt(gsym, got_type); }
2203
2204 void
2205 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2206 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2207 {
2208 this->reserve_ent();
2209 Output_data_got<size, big_endian>::
2210 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2211 }
2212
2213 void
2214 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2215 Output_data_reloc_generic* rel_dyn,
2216 unsigned int r_type_1, unsigned int r_type_2)
2217 {
2218 this->reserve_ent(2);
2219 Output_data_got<size, big_endian>::
2220 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2221 }
2222
2223 bool
2224 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2225 {
2226 this->reserve_ent();
2227 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2228 got_type);
2229 }
2230
2231 bool
2232 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2233 {
2234 this->reserve_ent();
2235 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2236 got_type);
2237 }
2238
2239 bool
2240 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2241 { return this->add_local_plt(object, sym_index, got_type); }
2242
2243 void
2244 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2245 unsigned int got_type,
2246 Output_data_reloc_generic* rel_dyn,
2247 unsigned int r_type)
2248 {
2249 this->reserve_ent(2);
2250 Output_data_got<size, big_endian>::
2251 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2252 }
2253
2254 unsigned int
2255 add_constant(Valtype constant)
2256 {
2257 this->reserve_ent();
2258 return Output_data_got<size, big_endian>::add_constant(constant);
2259 }
2260
2261 unsigned int
2262 add_constant_pair(Valtype c1, Valtype c2)
2263 {
2264 this->reserve_ent(2);
2265 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2266 }
2267
2268 // Offset of _GLOBAL_OFFSET_TABLE_.
2269 unsigned int
2270 g_o_t() const
2271 {
2272 return this->got_offset(this->header_index_);
2273 }
2274
2275 // Offset of base used to access the GOT/TOC.
2276 // The got/toc pointer reg will be set to this value.
2277 Valtype
2278 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2279 {
2280 if (size == 32)
2281 return this->g_o_t();
2282 else
2283 return (this->output_section()->address()
2284 + object->toc_base_offset()
2285 - this->address());
2286 }
2287
2288 // Ensure our GOT has a header.
2289 void
2290 set_final_data_size()
2291 {
2292 if (this->header_ent_cnt_ != 0)
2293 this->make_header();
2294 Output_data_got<size, big_endian>::set_final_data_size();
2295 }
2296
2297 // First word of GOT header needs some values that are not
2298 // handled by Output_data_got so poke them in here.
2299 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2300 void
2301 do_write(Output_file* of)
2302 {
2303 Valtype val = 0;
2304 if (size == 32 && this->layout_->dynamic_data() != NULL)
2305 val = this->layout_->dynamic_section()->address();
2306 if (size == 64)
2307 val = this->output_section()->address() + 0x8000;
2308 this->replace_constant(this->header_index_, val);
2309 Output_data_got<size, big_endian>::do_write(of);
2310 }
2311
2312 private:
2313 void
2314 reserve_ent(unsigned int cnt = 1)
2315 {
2316 if (this->header_ent_cnt_ == 0)
2317 return;
2318 if (this->num_entries() + cnt > this->header_index_)
2319 this->make_header();
2320 }
2321
2322 void
2323 make_header()
2324 {
2325 this->header_ent_cnt_ = 0;
2326 this->header_index_ = this->num_entries();
2327 if (size == 32)
2328 {
2329 Output_data_got<size, big_endian>::add_constant(0);
2330 Output_data_got<size, big_endian>::add_constant(0);
2331 Output_data_got<size, big_endian>::add_constant(0);
2332
2333 // Define _GLOBAL_OFFSET_TABLE_ at the header
2334 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2335 if (gotsym != NULL)
2336 {
2337 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2338 sym->set_value(this->g_o_t());
2339 }
2340 else
2341 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2342 Symbol_table::PREDEFINED,
2343 this, this->g_o_t(), 0,
2344 elfcpp::STT_OBJECT,
2345 elfcpp::STB_LOCAL,
2346 elfcpp::STV_HIDDEN, 0,
2347 false, false);
2348 }
2349 else
2350 Output_data_got<size, big_endian>::add_constant(0);
2351 }
2352
2353 // Stashed pointers.
2354 Symbol_table* symtab_;
2355 Layout* layout_;
2356
2357 // GOT header size.
2358 unsigned int header_ent_cnt_;
2359 // GOT header index.
2360 unsigned int header_index_;
2361 };
2362
2363 // Get the GOT section, creating it if necessary.
2364
2365 template<int size, bool big_endian>
2366 Output_data_got_powerpc<size, big_endian>*
2367 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2368 Layout* layout)
2369 {
2370 if (this->got_ == NULL)
2371 {
2372 gold_assert(symtab != NULL && layout != NULL);
2373
2374 this->got_
2375 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2376
2377 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2378 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2379 this->got_, ORDER_DATA, false);
2380 }
2381
2382 return this->got_;
2383 }
2384
2385 // Get the dynamic reloc section, creating it if necessary.
2386
2387 template<int size, bool big_endian>
2388 typename Target_powerpc<size, big_endian>::Reloc_section*
2389 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2390 {
2391 if (this->rela_dyn_ == NULL)
2392 {
2393 gold_assert(layout != NULL);
2394 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2395 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2396 elfcpp::SHF_ALLOC, this->rela_dyn_,
2397 ORDER_DYNAMIC_RELOCS, false);
2398 }
2399 return this->rela_dyn_;
2400 }
2401
2402 // Similarly, but for ifunc symbols get the one for ifunc.
2403
2404 template<int size, bool big_endian>
2405 typename Target_powerpc<size, big_endian>::Reloc_section*
2406 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2407 Layout* layout,
2408 bool for_ifunc)
2409 {
2410 if (!for_ifunc)
2411 return this->rela_dyn_section(layout);
2412
2413 if (this->iplt_ == NULL)
2414 this->make_iplt_section(symtab, layout);
2415 return this->iplt_->rel_plt();
2416 }
2417
2418 class Stub_control
2419 {
2420 public:
2421 // Determine the stub group size. The group size is the absolute
2422 // value of the parameter --stub-group-size. If --stub-group-size
2423 // is passed a negative value, we restrict stubs to be always before
2424 // the stubbed branches.
2425 Stub_control(int32_t size)
2426 : state_(NO_GROUP), stub_group_size_(abs(size)),
2427 stub14_group_size_(abs(size)),
2428 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2429 group_end_addr_(0), owner_(NULL), output_section_(NULL)
2430 {
2431 if (stub_group_size_ == 1)
2432 {
2433 // Default values.
2434 if (stubs_always_before_branch_)
2435 {
2436 stub_group_size_ = 0x1e00000;
2437 stub14_group_size_ = 0x7800;
2438 }
2439 else
2440 {
2441 stub_group_size_ = 0x1c00000;
2442 stub14_group_size_ = 0x7000;
2443 }
2444 suppress_size_errors_ = true;
2445 }
2446 }
2447
2448 // Return true iff input section can be handled by current stub
2449 // group.
2450 bool
2451 can_add_to_stub_group(Output_section* o,
2452 const Output_section::Input_section* i,
2453 bool has14);
2454
2455 const Output_section::Input_section*
2456 owner()
2457 { return owner_; }
2458
2459 Output_section*
2460 output_section()
2461 { return output_section_; }
2462
2463 private:
2464 typedef enum
2465 {
2466 NO_GROUP,
2467 FINDING_STUB_SECTION,
2468 HAS_STUB_SECTION
2469 } State;
2470
2471 State state_;
2472 uint32_t stub_group_size_;
2473 uint32_t stub14_group_size_;
2474 bool stubs_always_before_branch_;
2475 bool suppress_size_errors_;
2476 uint64_t group_end_addr_;
2477 const Output_section::Input_section* owner_;
2478 Output_section* output_section_;
2479 };
2480
2481 // Return true iff input section can be handled by current stub
2482 // group.
2483
2484 bool
2485 Stub_control::can_add_to_stub_group(Output_section* o,
2486 const Output_section::Input_section* i,
2487 bool has14)
2488 {
2489 uint32_t group_size
2490 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2491 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2492 uint64_t this_size;
2493 uint64_t start_addr = o->address();
2494
2495 if (whole_sec)
2496 // .init and .fini sections are pasted together to form a single
2497 // function. We can't be adding stubs in the middle of the function.
2498 this_size = o->data_size();
2499 else
2500 {
2501 start_addr += i->relobj()->output_section_offset(i->shndx());
2502 this_size = i->data_size();
2503 }
2504 uint64_t end_addr = start_addr + this_size;
2505 bool toobig = this_size > group_size;
2506
2507 if (toobig && !this->suppress_size_errors_)
2508 gold_warning(_("%s:%s exceeds group size"),
2509 i->relobj()->name().c_str(),
2510 i->relobj()->section_name(i->shndx()).c_str());
2511
2512 if (this->state_ != HAS_STUB_SECTION
2513 && (!whole_sec || this->output_section_ != o)
2514 && (this->state_ == NO_GROUP
2515 || this->group_end_addr_ - end_addr < group_size))
2516 {
2517 this->owner_ = i;
2518 this->output_section_ = o;
2519 }
2520
2521 if (this->state_ == NO_GROUP)
2522 {
2523 this->state_ = FINDING_STUB_SECTION;
2524 this->group_end_addr_ = end_addr;
2525 }
2526 else if (this->group_end_addr_ - start_addr < group_size)
2527 ;
2528 // Adding this section would make the group larger than GROUP_SIZE.
2529 else if (this->state_ == FINDING_STUB_SECTION
2530 && !this->stubs_always_before_branch_
2531 && !toobig)
2532 {
2533 // But wait, there's more! Input sections up to GROUP_SIZE
2534 // bytes before the stub table can be handled by it too.
2535 this->state_ = HAS_STUB_SECTION;
2536 this->group_end_addr_ = end_addr;
2537 }
2538 else
2539 {
2540 this->state_ = NO_GROUP;
2541 return false;
2542 }
2543 return true;
2544 }
2545
2546 // Look over all the input sections, deciding where to place stubs.
2547
2548 template<int size, bool big_endian>
2549 void
2550 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2551 const Task*)
2552 {
2553 Stub_control stub_control(parameters->options().stub_group_size());
2554
2555 // Group input sections and insert stub table
2556 Stub_table<size, big_endian>* stub_table = NULL;
2557 Layout::Section_list section_list;
2558 layout->get_executable_sections(&section_list);
2559 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2560 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2561 o != section_list.rend();
2562 ++o)
2563 {
2564 typedef Output_section::Input_section_list Input_section_list;
2565 for (Input_section_list::const_reverse_iterator i
2566 = (*o)->input_sections().rbegin();
2567 i != (*o)->input_sections().rend();
2568 ++i)
2569 {
2570 if (i->is_input_section())
2571 {
2572 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2573 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2574 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2575 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2576 {
2577 stub_table->init(stub_control.owner(),
2578 stub_control.output_section());
2579 stub_table = NULL;
2580 }
2581 if (stub_table == NULL)
2582 stub_table = this->new_stub_table();
2583 ppcobj->set_stub_table(i->shndx(), stub_table);
2584 }
2585 }
2586 }
2587 if (stub_table != NULL)
2588 {
2589 const Output_section::Input_section* i = stub_control.owner();
2590 if (!i->is_input_section())
2591 {
2592 // Corner case. A new stub group was made for the first
2593 // section (last one looked at here) for some reason, but
2594 // the first section is already being used as the owner for
2595 // a stub table for following sections. Force it into that
2596 // stub group.
2597 gold_assert(this->stub_tables_.size() >= 2);
2598 this->stub_tables_.pop_back();
2599 delete stub_table;
2600 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2601 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2602 ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
2603 }
2604 else
2605 stub_table->init(i, stub_control.output_section());
2606 }
2607 }
2608
2609 // If this branch needs a plt call stub, or a long branch stub, make one.
2610
2611 template<int size, bool big_endian>
2612 void
2613 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2614 Stub_table<size, big_endian>* stub_table,
2615 Stub_table<size, big_endian>* ifunc_stub_table,
2616 Symbol_table* symtab) const
2617 {
2618 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2619 if (sym != NULL && sym->is_forwarder())
2620 sym = symtab->resolve_forwards(sym);
2621 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2622 if (gsym != NULL
2623 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
2624 : this->object_->local_has_plt_offset(this->r_sym_))
2625 {
2626 if (stub_table == NULL)
2627 stub_table = this->object_->stub_table(this->shndx_);
2628 if (stub_table == NULL)
2629 {
2630 // This is a ref from a data section to an ifunc symbol.
2631 stub_table = ifunc_stub_table;
2632 }
2633 gold_assert(stub_table != NULL);
2634 if (gsym != NULL)
2635 stub_table->add_plt_call_entry(this->object_, gsym,
2636 this->r_type_, this->addend_);
2637 else
2638 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2639 this->r_type_, this->addend_);
2640 }
2641 else
2642 {
2643 unsigned long max_branch_offset;
2644 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2645 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2646 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2647 max_branch_offset = 1 << 15;
2648 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2649 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2650 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2651 max_branch_offset = 1 << 25;
2652 else
2653 return;
2654 Address from = this->object_->get_output_section_offset(this->shndx_);
2655 gold_assert(from != invalid_address);
2656 from += (this->object_->output_section(this->shndx_)->address()
2657 + this->offset_);
2658 Address to;
2659 if (gsym != NULL)
2660 {
2661 switch (gsym->source())
2662 {
2663 case Symbol::FROM_OBJECT:
2664 {
2665 Object* symobj = gsym->object();
2666 if (symobj->is_dynamic()
2667 || symobj->pluginobj() != NULL)
2668 return;
2669 bool is_ordinary;
2670 unsigned int shndx = gsym->shndx(&is_ordinary);
2671 if (shndx == elfcpp::SHN_UNDEF)
2672 return;
2673 }
2674 break;
2675
2676 case Symbol::IS_UNDEFINED:
2677 return;
2678
2679 default:
2680 break;
2681 }
2682 Symbol_table::Compute_final_value_status status;
2683 to = symtab->compute_final_value<size>(gsym, &status);
2684 if (status != Symbol_table::CFVS_OK)
2685 return;
2686 to += this->object_->ppc64_local_entry_offset(gsym);
2687 }
2688 else
2689 {
2690 const Symbol_value<size>* psymval
2691 = this->object_->local_symbol(this->r_sym_);
2692 Symbol_value<size> symval;
2693 typedef Sized_relobj_file<size, big_endian> ObjType;
2694 typename ObjType::Compute_final_local_value_status status
2695 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2696 &symval, symtab);
2697 if (status != ObjType::CFLV_OK
2698 || !symval.has_output_value())
2699 return;
2700 to = symval.value(this->object_, 0);
2701 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
2702 }
2703 to += this->addend_;
2704 if (stub_table == NULL)
2705 stub_table = this->object_->stub_table(this->shndx_);
2706 if (size == 64 && is_branch_reloc(this->r_type_))
2707 {
2708 unsigned int dest_shndx;
2709 Target_powerpc<size, big_endian>* target =
2710 static_cast<Target_powerpc<size, big_endian>*>(
2711 parameters->sized_target<size, big_endian>());
2712 to = target->symval_for_branch(symtab, to, gsym,
2713 this->object_, &dest_shndx);
2714 }
2715 Address delta = to - from;
2716 if (delta + max_branch_offset >= 2 * max_branch_offset)
2717 {
2718 if (stub_table == NULL)
2719 {
2720 gold_warning(_("%s:%s: branch in non-executable section,"
2721 " no long branch stub for you"),
2722 this->object_->name().c_str(),
2723 this->object_->section_name(this->shndx_).c_str());
2724 return;
2725 }
2726 stub_table->add_long_branch_entry(this->object_, to);
2727 }
2728 }
2729 }
2730
2731 // Relaxation hook. This is where we do stub generation.
2732
2733 template<int size, bool big_endian>
2734 bool
2735 Target_powerpc<size, big_endian>::do_relax(int pass,
2736 const Input_objects*,
2737 Symbol_table* symtab,
2738 Layout* layout,
2739 const Task* task)
2740 {
2741 unsigned int prev_brlt_size = 0;
2742 if (pass == 1)
2743 {
2744 bool thread_safe
2745 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
2746 if (size == 64
2747 && this->abiversion() < 2
2748 && !thread_safe
2749 && !parameters->options().user_set_plt_thread_safe())
2750 {
2751 static const char* const thread_starter[] =
2752 {
2753 "pthread_create",
2754 /* libstdc++ */
2755 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2756 /* librt */
2757 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2758 "mq_notify", "create_timer",
2759 /* libanl */
2760 "getaddrinfo_a",
2761 /* libgomp */
2762 "GOMP_parallel_start",
2763 "GOMP_parallel_loop_static_start",
2764 "GOMP_parallel_loop_dynamic_start",
2765 "GOMP_parallel_loop_guided_start",
2766 "GOMP_parallel_loop_runtime_start",
2767 "GOMP_parallel_sections_start",
2768 };
2769
2770 if (parameters->options().shared())
2771 thread_safe = true;
2772 else
2773 {
2774 for (unsigned int i = 0;
2775 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2776 i++)
2777 {
2778 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2779 thread_safe = (sym != NULL
2780 && sym->in_reg()
2781 && sym->in_real_elf());
2782 if (thread_safe)
2783 break;
2784 }
2785 }
2786 }
2787 this->plt_thread_safe_ = thread_safe;
2788 this->group_sections(layout, task);
2789 }
2790
2791 // We need address of stub tables valid for make_stub.
2792 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2793 p != this->stub_tables_.end();
2794 ++p)
2795 {
2796 const Powerpc_relobj<size, big_endian>* object
2797 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2798 Address off = object->get_output_section_offset((*p)->shndx());
2799 gold_assert(off != invalid_address);
2800 Output_section* os = (*p)->output_section();
2801 (*p)->set_address_and_size(os, off);
2802 }
2803
2804 if (pass != 1)
2805 {
2806 // Clear plt call stubs, long branch stubs and branch lookup table.
2807 prev_brlt_size = this->branch_lookup_table_.size();
2808 this->branch_lookup_table_.clear();
2809 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2810 p != this->stub_tables_.end();
2811 ++p)
2812 {
2813 (*p)->clear_stubs();
2814 }
2815 }
2816
2817 // Build all the stubs.
2818 Stub_table<size, big_endian>* ifunc_stub_table
2819 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2820 Stub_table<size, big_endian>* one_stub_table
2821 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2822 for (typename Branches::const_iterator b = this->branch_info_.begin();
2823 b != this->branch_info_.end();
2824 b++)
2825 {
2826 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2827 }
2828
2829 // Did anything change size?
2830 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2831 bool again = num_huge_branches != prev_brlt_size;
2832 if (size == 64 && num_huge_branches != 0)
2833 this->make_brlt_section(layout);
2834 if (size == 64 && again)
2835 this->brlt_section_->set_current_size(num_huge_branches);
2836
2837 typedef Unordered_set<Output_section*> Output_sections;
2838 Output_sections os_need_update;
2839 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2840 p != this->stub_tables_.end();
2841 ++p)
2842 {
2843 if ((*p)->size_update())
2844 {
2845 again = true;
2846 (*p)->add_eh_frame(layout);
2847 os_need_update.insert((*p)->output_section());
2848 }
2849 }
2850
2851 // Set output section offsets for all input sections in an output
2852 // section that just changed size. Anything past the stubs will
2853 // need updating.
2854 for (typename Output_sections::iterator p = os_need_update.begin();
2855 p != os_need_update.end();
2856 p++)
2857 {
2858 Output_section* os = *p;
2859 Address off = 0;
2860 typedef Output_section::Input_section_list Input_section_list;
2861 for (Input_section_list::const_iterator i = os->input_sections().begin();
2862 i != os->input_sections().end();
2863 ++i)
2864 {
2865 off = align_address(off, i->addralign());
2866 if (i->is_input_section() || i->is_relaxed_input_section())
2867 i->relobj()->set_section_offset(i->shndx(), off);
2868 if (i->is_relaxed_input_section())
2869 {
2870 Stub_table<size, big_endian>* stub_table
2871 = static_cast<Stub_table<size, big_endian>*>(
2872 i->relaxed_input_section());
2873 off += stub_table->set_address_and_size(os, off);
2874 }
2875 else
2876 off += i->data_size();
2877 }
2878 // If .branch_lt is part of this output section, then we have
2879 // just done the offset adjustment.
2880 os->clear_section_offsets_need_adjustment();
2881 }
2882
2883 if (size == 64
2884 && !again
2885 && num_huge_branches != 0
2886 && parameters->options().output_is_position_independent())
2887 {
2888 // Fill in the BRLT relocs.
2889 this->brlt_section_->reset_brlt_sizes();
2890 for (typename Branch_lookup_table::const_iterator p
2891 = this->branch_lookup_table_.begin();
2892 p != this->branch_lookup_table_.end();
2893 ++p)
2894 {
2895 this->brlt_section_->add_reloc(p->first, p->second);
2896 }
2897 this->brlt_section_->finalize_brlt_sizes();
2898 }
2899 return again;
2900 }
2901
2902 template<int size, bool big_endian>
2903 void
2904 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2905 unsigned char* oview,
2906 uint64_t* paddress,
2907 off_t* plen) const
2908 {
2909 uint64_t address = plt->address();
2910 off_t len = plt->data_size();
2911
2912 if (plt == this->glink_)
2913 {
2914 // See Output_data_glink::do_write() for glink contents.
2915 if (size == 64)
2916 {
2917 // There is one word before __glink_PLTresolve
2918 address += 8;
2919 len -= 8;
2920 }
2921 else if (parameters->options().output_is_position_independent())
2922 {
2923 // There are two FDEs for a position independent glink.
2924 // The first covers the branch table, the second
2925 // __glink_PLTresolve at the end of glink.
2926 off_t resolve_size = this->glink_->pltresolve_size;
2927 if (oview[9] == 0)
2928 len -= resolve_size;
2929 else
2930 {
2931 address += len - resolve_size;
2932 len = resolve_size;
2933 }
2934 }
2935 }
2936 else
2937 {
2938 // Must be a stub table.
2939 const Stub_table<size, big_endian>* stub_table
2940 = static_cast<const Stub_table<size, big_endian>*>(plt);
2941 uint64_t stub_address = stub_table->stub_address();
2942 len -= stub_address - address;
2943 address = stub_address;
2944 }
2945
2946 *paddress = address;
2947 *plen = len;
2948 }
2949
2950 // A class to handle the PLT data.
2951
2952 template<int size, bool big_endian>
2953 class Output_data_plt_powerpc : public Output_section_data_build
2954 {
2955 public:
2956 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2957 size, big_endian> Reloc_section;
2958
2959 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2960 Reloc_section* plt_rel,
2961 const char* name)
2962 : Output_section_data_build(size == 32 ? 4 : 8),
2963 rel_(plt_rel),
2964 targ_(targ),
2965 name_(name)
2966 { }
2967
2968 // Add an entry to the PLT.
2969 void
2970 add_entry(Symbol*);
2971
2972 void
2973 add_ifunc_entry(Symbol*);
2974
2975 void
2976 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2977
2978 // Return the .rela.plt section data.
2979 Reloc_section*
2980 rel_plt() const
2981 {
2982 return this->rel_;
2983 }
2984
2985 // Return the number of PLT entries.
2986 unsigned int
2987 entry_count() const
2988 {
2989 if (this->current_data_size() == 0)
2990 return 0;
2991 return ((this->current_data_size() - this->first_plt_entry_offset())
2992 / this->plt_entry_size());
2993 }
2994
2995 protected:
2996 void
2997 do_adjust_output_section(Output_section* os)
2998 {
2999 os->set_entsize(0);
3000 }
3001
3002 // Write to a map file.
3003 void
3004 do_print_to_mapfile(Mapfile* mapfile) const
3005 { mapfile->print_output_data(this, this->name_); }
3006
3007 private:
3008 // Return the offset of the first non-reserved PLT entry.
3009 unsigned int
3010 first_plt_entry_offset() const
3011 {
3012 // IPLT has no reserved entry.
3013 if (this->name_[3] == 'I')
3014 return 0;
3015 return this->targ_->first_plt_entry_offset();
3016 }
3017
3018 // Return the size of each PLT entry.
3019 unsigned int
3020 plt_entry_size() const
3021 {
3022 return this->targ_->plt_entry_size();
3023 }
3024
3025 // Write out the PLT data.
3026 void
3027 do_write(Output_file*);
3028
3029 // The reloc section.
3030 Reloc_section* rel_;
3031 // Allows access to .glink for do_write.
3032 Target_powerpc<size, big_endian>* targ_;
3033 // What to report in map file.
3034 const char *name_;
3035 };
3036
3037 // Add an entry to the PLT.
3038
3039 template<int size, bool big_endian>
3040 void
3041 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3042 {
3043 if (!gsym->has_plt_offset())
3044 {
3045 section_size_type off = this->current_data_size();
3046 if (off == 0)
3047 off += this->first_plt_entry_offset();
3048 gsym->set_plt_offset(off);
3049 gsym->set_needs_dynsym_entry();
3050 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3051 this->rel_->add_global(gsym, dynrel, this, off, 0);
3052 off += this->plt_entry_size();
3053 this->set_current_data_size(off);
3054 }
3055 }
3056
3057 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3058
3059 template<int size, bool big_endian>
3060 void
3061 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3062 {
3063 if (!gsym->has_plt_offset())
3064 {
3065 section_size_type off = this->current_data_size();
3066 gsym->set_plt_offset(off);
3067 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3068 if (size == 64 && this->targ_->abiversion() < 2)
3069 dynrel = elfcpp::R_PPC64_JMP_IREL;
3070 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3071 off += this->plt_entry_size();
3072 this->set_current_data_size(off);
3073 }
3074 }
3075
3076 // Add an entry for a local ifunc symbol to the IPLT.
3077
3078 template<int size, bool big_endian>
3079 void
3080 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3081 Sized_relobj_file<size, big_endian>* relobj,
3082 unsigned int local_sym_index)
3083 {
3084 if (!relobj->local_has_plt_offset(local_sym_index))
3085 {
3086 section_size_type off = this->current_data_size();
3087 relobj->set_local_plt_offset(local_sym_index, off);
3088 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3089 if (size == 64 && this->targ_->abiversion() < 2)
3090 dynrel = elfcpp::R_PPC64_JMP_IREL;
3091 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3092 this, off, 0);
3093 off += this->plt_entry_size();
3094 this->set_current_data_size(off);
3095 }
3096 }
3097
3098 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3099 static const uint32_t add_2_2_11 = 0x7c425a14;
3100 static const uint32_t add_3_3_2 = 0x7c631214;
3101 static const uint32_t add_3_3_13 = 0x7c636a14;
3102 static const uint32_t add_11_0_11 = 0x7d605a14;
3103 static const uint32_t add_11_2_11 = 0x7d625a14;
3104 static const uint32_t add_11_11_2 = 0x7d6b1214;
3105 static const uint32_t addi_0_12 = 0x380c0000;
3106 static const uint32_t addi_2_2 = 0x38420000;
3107 static const uint32_t addi_3_3 = 0x38630000;
3108 static const uint32_t addi_11_11 = 0x396b0000;
3109 static const uint32_t addi_12_12 = 0x398c0000;
3110 static const uint32_t addis_0_2 = 0x3c020000;
3111 static const uint32_t addis_0_13 = 0x3c0d0000;
3112 static const uint32_t addis_3_2 = 0x3c620000;
3113 static const uint32_t addis_3_13 = 0x3c6d0000;
3114 static const uint32_t addis_11_2 = 0x3d620000;
3115 static const uint32_t addis_11_11 = 0x3d6b0000;
3116 static const uint32_t addis_11_30 = 0x3d7e0000;
3117 static const uint32_t addis_12_12 = 0x3d8c0000;
3118 static const uint32_t b = 0x48000000;
3119 static const uint32_t bcl_20_31 = 0x429f0005;
3120 static const uint32_t bctr = 0x4e800420;
3121 static const uint32_t blr = 0x4e800020;
3122 static const uint32_t bnectr_p4 = 0x4ce20420;
3123 static const uint32_t cmpldi_2_0 = 0x28220000;
3124 static const uint32_t cror_15_15_15 = 0x4def7b82;
3125 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3126 static const uint32_t ld_0_1 = 0xe8010000;
3127 static const uint32_t ld_0_12 = 0xe80c0000;
3128 static const uint32_t ld_2_1 = 0xe8410000;
3129 static const uint32_t ld_2_2 = 0xe8420000;
3130 static const uint32_t ld_2_11 = 0xe84b0000;
3131 static const uint32_t ld_11_2 = 0xe9620000;
3132 static const uint32_t ld_11_11 = 0xe96b0000;
3133 static const uint32_t ld_12_2 = 0xe9820000;
3134 static const uint32_t ld_12_11 = 0xe98b0000;
3135 static const uint32_t lfd_0_1 = 0xc8010000;
3136 static const uint32_t li_0_0 = 0x38000000;
3137 static const uint32_t li_12_0 = 0x39800000;
3138 static const uint32_t lis_0_0 = 0x3c000000;
3139 static const uint32_t lis_11 = 0x3d600000;
3140 static const uint32_t lis_12 = 0x3d800000;
3141 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3142 static const uint32_t lwz_0_12 = 0x800c0000;
3143 static const uint32_t lwz_11_11 = 0x816b0000;
3144 static const uint32_t lwz_11_30 = 0x817e0000;
3145 static const uint32_t lwz_12_12 = 0x818c0000;
3146 static const uint32_t lwzu_0_12 = 0x840c0000;
3147 static const uint32_t mflr_0 = 0x7c0802a6;
3148 static const uint32_t mflr_11 = 0x7d6802a6;
3149 static const uint32_t mflr_12 = 0x7d8802a6;
3150 static const uint32_t mtctr_0 = 0x7c0903a6;
3151 static const uint32_t mtctr_11 = 0x7d6903a6;
3152 static const uint32_t mtctr_12 = 0x7d8903a6;
3153 static const uint32_t mtlr_0 = 0x7c0803a6;
3154 static const uint32_t mtlr_12 = 0x7d8803a6;
3155 static const uint32_t nop = 0x60000000;
3156 static const uint32_t ori_0_0_0 = 0x60000000;
3157 static const uint32_t srdi_0_0_2 = 0x7800f082;
3158 static const uint32_t std_0_1 = 0xf8010000;
3159 static const uint32_t std_0_12 = 0xf80c0000;
3160 static const uint32_t std_2_1 = 0xf8410000;
3161 static const uint32_t stfd_0_1 = 0xd8010000;
3162 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3163 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3164 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3165 static const uint32_t xor_2_12_12 = 0x7d826278;
3166 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3167
3168 // Write out the PLT.
3169
3170 template<int size, bool big_endian>
3171 void
3172 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3173 {
3174 if (size == 32 && this->name_[3] != 'I')
3175 {
3176 const section_size_type offset = this->offset();
3177 const section_size_type oview_size
3178 = convert_to_section_size_type(this->data_size());
3179 unsigned char* const oview = of->get_output_view(offset, oview_size);
3180 unsigned char* pov = oview;
3181 unsigned char* endpov = oview + oview_size;
3182
3183 // The address of the .glink branch table
3184 const Output_data_glink<size, big_endian>* glink
3185 = this->targ_->glink_section();
3186 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3187
3188 while (pov < endpov)
3189 {
3190 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3191 pov += 4;
3192 branch_tab += 4;
3193 }
3194
3195 of->write_output_view(offset, oview_size, oview);
3196 }
3197 }
3198
3199 // Create the PLT section.
3200
3201 template<int size, bool big_endian>
3202 void
3203 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3204 Layout* layout)
3205 {
3206 if (this->plt_ == NULL)
3207 {
3208 if (this->got_ == NULL)
3209 this->got_section(symtab, layout);
3210
3211 if (this->glink_ == NULL)
3212 make_glink_section(layout);
3213
3214 // Ensure that .rela.dyn always appears before .rela.plt This is
3215 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3216 // needs to include .rela.plt in its range.
3217 this->rela_dyn_section(layout);
3218
3219 Reloc_section* plt_rel = new Reloc_section(false);
3220 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3221 elfcpp::SHF_ALLOC, plt_rel,
3222 ORDER_DYNAMIC_PLT_RELOCS, false);
3223 this->plt_
3224 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3225 "** PLT");
3226 layout->add_output_section_data(".plt",
3227 (size == 32
3228 ? elfcpp::SHT_PROGBITS
3229 : elfcpp::SHT_NOBITS),
3230 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3231 this->plt_,
3232 (size == 32
3233 ? ORDER_SMALL_DATA
3234 : ORDER_SMALL_BSS),
3235 false);
3236 }
3237 }
3238
3239 // Create the IPLT section.
3240
3241 template<int size, bool big_endian>
3242 void
3243 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3244 Layout* layout)
3245 {
3246 if (this->iplt_ == NULL)
3247 {
3248 this->make_plt_section(symtab, layout);
3249
3250 Reloc_section* iplt_rel = new Reloc_section(false);
3251 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3252 this->iplt_
3253 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3254 "** IPLT");
3255 this->plt_->output_section()->add_output_section_data(this->iplt_);
3256 }
3257 }
3258
3259 // A section for huge long branch addresses, similar to plt section.
3260
3261 template<int size, bool big_endian>
3262 class Output_data_brlt_powerpc : public Output_section_data_build
3263 {
3264 public:
3265 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3266 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3267 size, big_endian> Reloc_section;
3268
3269 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3270 Reloc_section* brlt_rel)
3271 : Output_section_data_build(size == 32 ? 4 : 8),
3272 rel_(brlt_rel),
3273 targ_(targ)
3274 { }
3275
3276 void
3277 reset_brlt_sizes()
3278 {
3279 this->reset_data_size();
3280 this->rel_->reset_data_size();
3281 }
3282
3283 void
3284 finalize_brlt_sizes()
3285 {
3286 this->finalize_data_size();
3287 this->rel_->finalize_data_size();
3288 }
3289
3290 // Add a reloc for an entry in the BRLT.
3291 void
3292 add_reloc(Address to, unsigned int off)
3293 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3294
3295 // Update section and reloc section size.
3296 void
3297 set_current_size(unsigned int num_branches)
3298 {
3299 this->reset_address_and_file_offset();
3300 this->set_current_data_size(num_branches * 16);
3301 this->finalize_data_size();
3302 Output_section* os = this->output_section();
3303 os->set_section_offsets_need_adjustment();
3304 if (this->rel_ != NULL)
3305 {
3306 unsigned int reloc_size
3307 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3308 this->rel_->reset_address_and_file_offset();
3309 this->rel_->set_current_data_size(num_branches * reloc_size);
3310 this->rel_->finalize_data_size();
3311 Output_section* os = this->rel_->output_section();
3312 os->set_section_offsets_need_adjustment();
3313 }
3314 }
3315
3316 protected:
3317 void
3318 do_adjust_output_section(Output_section* os)
3319 {
3320 os->set_entsize(0);
3321 }
3322
3323 // Write to a map file.
3324 void
3325 do_print_to_mapfile(Mapfile* mapfile) const
3326 { mapfile->print_output_data(this, "** BRLT"); }
3327
3328 private:
3329 // Write out the BRLT data.
3330 void
3331 do_write(Output_file*);
3332
3333 // The reloc section.
3334 Reloc_section* rel_;
3335 Target_powerpc<size, big_endian>* targ_;
3336 };
3337
3338 // Make the branch lookup table section.
3339
3340 template<int size, bool big_endian>
3341 void
3342 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3343 {
3344 if (size == 64 && this->brlt_section_ == NULL)
3345 {
3346 Reloc_section* brlt_rel = NULL;
3347 bool is_pic = parameters->options().output_is_position_independent();
3348 if (is_pic)
3349 {
3350 // When PIC we can't fill in .branch_lt (like .plt it can be
3351 // a bss style section) but must initialise at runtime via
3352 // dynamic relocats.
3353 this->rela_dyn_section(layout);
3354 brlt_rel = new Reloc_section(false);
3355 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3356 }
3357 this->brlt_section_
3358 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3359 if (this->plt_ && is_pic)
3360 this->plt_->output_section()
3361 ->add_output_section_data(this->brlt_section_);
3362 else
3363 layout->add_output_section_data(".branch_lt",
3364 (is_pic ? elfcpp::SHT_NOBITS
3365 : elfcpp::SHT_PROGBITS),
3366 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3367 this->brlt_section_,
3368 (is_pic ? ORDER_SMALL_BSS
3369 : ORDER_SMALL_DATA),
3370 false);
3371 }
3372 }
3373
3374 // Write out .branch_lt when non-PIC.
3375
3376 template<int size, bool big_endian>
3377 void
3378 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3379 {
3380 if (size == 64 && !parameters->options().output_is_position_independent())
3381 {
3382 const section_size_type offset = this->offset();
3383 const section_size_type oview_size
3384 = convert_to_section_size_type(this->data_size());
3385 unsigned char* const oview = of->get_output_view(offset, oview_size);
3386
3387 this->targ_->write_branch_lookup_table(oview);
3388 of->write_output_view(offset, oview_size, oview);
3389 }
3390 }
3391
3392 static inline uint32_t
3393 l(uint32_t a)
3394 {
3395 return a & 0xffff;
3396 }
3397
3398 static inline uint32_t
3399 hi(uint32_t a)
3400 {
3401 return l(a >> 16);
3402 }
3403
3404 static inline uint32_t
3405 ha(uint32_t a)
3406 {
3407 return hi(a + 0x8000);
3408 }
3409
3410 template<int size>
3411 struct Eh_cie
3412 {
3413 static const unsigned char eh_frame_cie[12];
3414 };
3415
3416 template<int size>
3417 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3418 {
3419 1, // CIE version.
3420 'z', 'R', 0, // Augmentation string.
3421 4, // Code alignment.
3422 0x80 - size / 8 , // Data alignment.
3423 65, // RA reg.
3424 1, // Augmentation size.
3425 (elfcpp::DW_EH_PE_pcrel
3426 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3427 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3428 };
3429
3430 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3431 static const unsigned char glink_eh_frame_fde_64v1[] =
3432 {
3433 0, 0, 0, 0, // Replaced with offset to .glink.
3434 0, 0, 0, 0, // Replaced with size of .glink.
3435 0, // Augmentation size.
3436 elfcpp::DW_CFA_advance_loc + 1,
3437 elfcpp::DW_CFA_register, 65, 12,
3438 elfcpp::DW_CFA_advance_loc + 4,
3439 elfcpp::DW_CFA_restore_extended, 65
3440 };
3441
3442 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3443 static const unsigned char glink_eh_frame_fde_64v2[] =
3444 {
3445 0, 0, 0, 0, // Replaced with offset to .glink.
3446 0, 0, 0, 0, // Replaced with size of .glink.
3447 0, // Augmentation size.
3448 elfcpp::DW_CFA_advance_loc + 1,
3449 elfcpp::DW_CFA_register, 65, 0,
3450 elfcpp::DW_CFA_advance_loc + 4,
3451 elfcpp::DW_CFA_restore_extended, 65
3452 };
3453
3454 // Describe __glink_PLTresolve use of LR, 32-bit version.
3455 static const unsigned char glink_eh_frame_fde_32[] =
3456 {
3457 0, 0, 0, 0, // Replaced with offset to .glink.
3458 0, 0, 0, 0, // Replaced with size of .glink.
3459 0, // Augmentation size.
3460 elfcpp::DW_CFA_advance_loc + 2,
3461 elfcpp::DW_CFA_register, 65, 0,
3462 elfcpp::DW_CFA_advance_loc + 4,
3463 elfcpp::DW_CFA_restore_extended, 65
3464 };
3465
3466 static const unsigned char default_fde[] =
3467 {
3468 0, 0, 0, 0, // Replaced with offset to stubs.
3469 0, 0, 0, 0, // Replaced with size of stubs.
3470 0, // Augmentation size.
3471 elfcpp::DW_CFA_nop, // Pad.
3472 elfcpp::DW_CFA_nop,
3473 elfcpp::DW_CFA_nop
3474 };
3475
3476 template<bool big_endian>
3477 static inline void
3478 write_insn(unsigned char* p, uint32_t v)
3479 {
3480 elfcpp::Swap<32, big_endian>::writeval(p, v);
3481 }
3482
3483 // Stub_table holds information about plt and long branch stubs.
3484 // Stubs are built in an area following some input section determined
3485 // by group_sections(). This input section is converted to a relaxed
3486 // input section allowing it to be resized to accommodate the stubs
3487
3488 template<int size, bool big_endian>
3489 class Stub_table : public Output_relaxed_input_section
3490 {
3491 public:
3492 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3493 static const Address invalid_address = static_cast<Address>(0) - 1;
3494
3495 Stub_table(Target_powerpc<size, big_endian>* targ)
3496 : Output_relaxed_input_section(NULL, 0, 0),
3497 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3498 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3499 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3500 { }
3501
3502 // Delayed Output_relaxed_input_section init.
3503 void
3504 init(const Output_section::Input_section*, Output_section*);
3505
3506 // Add a plt call stub.
3507 void
3508 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3509 const Symbol*,
3510 unsigned int,
3511 Address);
3512
3513 void
3514 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3515 unsigned int,
3516 unsigned int,
3517 Address);
3518
3519 // Find a given plt call stub.
3520 Address
3521 find_plt_call_entry(const Symbol*) const;
3522
3523 Address
3524 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3525 unsigned int) const;
3526
3527 Address
3528 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3529 const Symbol*,
3530 unsigned int,
3531 Address) const;
3532
3533 Address
3534 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3535 unsigned int,
3536 unsigned int,
3537 Address) const;
3538
3539 // Add a long branch stub.
3540 void
3541 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3542
3543 Address
3544 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3545 Address) const;
3546
3547 void
3548 clear_stubs()
3549 {
3550 this->plt_call_stubs_.clear();
3551 this->plt_size_ = 0;
3552 this->long_branch_stubs_.clear();
3553 this->branch_size_ = 0;
3554 }
3555
3556 Address
3557 set_address_and_size(const Output_section* os, Address off)
3558 {
3559 Address start_off = off;
3560 off += this->orig_data_size_;
3561 Address my_size = this->plt_size_ + this->branch_size_;
3562 if (my_size != 0)
3563 off = align_address(off, this->stub_align());
3564 // Include original section size and alignment padding in size
3565 my_size += off - start_off;
3566 this->reset_address_and_file_offset();
3567 this->set_current_data_size(my_size);
3568 this->set_address_and_file_offset(os->address() + start_off,
3569 os->offset() + start_off);
3570 return my_size;
3571 }
3572
3573 Address
3574 stub_address() const
3575 {
3576 return align_address(this->address() + this->orig_data_size_,
3577 this->stub_align());
3578 }
3579
3580 Address
3581 stub_offset() const
3582 {
3583 return align_address(this->offset() + this->orig_data_size_,
3584 this->stub_align());
3585 }
3586
3587 section_size_type
3588 plt_size() const
3589 { return this->plt_size_; }
3590
3591 bool
3592 size_update()
3593 {
3594 Output_section* os = this->output_section();
3595 if (os->addralign() < this->stub_align())
3596 {
3597 os->set_addralign(this->stub_align());
3598 // FIXME: get rid of the insane checkpointing.
3599 // We can't increase alignment of the input section to which
3600 // stubs are attached; The input section may be .init which
3601 // is pasted together with other .init sections to form a
3602 // function. Aligning might insert zero padding resulting in
3603 // sigill. However we do need to increase alignment of the
3604 // output section so that the align_address() on offset in
3605 // set_address_and_size() adds the same padding as the
3606 // align_address() on address in stub_address().
3607 // What's more, we need this alignment for the layout done in
3608 // relaxation_loop_body() so that the output section starts at
3609 // a suitably aligned address.
3610 os->checkpoint_set_addralign(this->stub_align());
3611 }
3612 if (this->last_plt_size_ != this->plt_size_
3613 || this->last_branch_size_ != this->branch_size_)
3614 {
3615 this->last_plt_size_ = this->plt_size_;
3616 this->last_branch_size_ = this->branch_size_;
3617 return true;
3618 }
3619 return false;
3620 }
3621
3622 // Add .eh_frame info for this stub section. Unlike other linker
3623 // generated .eh_frame this is added late in the link, because we
3624 // only want the .eh_frame info if this particular stub section is
3625 // non-empty.
3626 void
3627 add_eh_frame(Layout* layout)
3628 {
3629 if (!this->eh_frame_added_)
3630 {
3631 if (!parameters->options().ld_generated_unwind_info())
3632 return;
3633
3634 // Since we add stub .eh_frame info late, it must be placed
3635 // after all other linker generated .eh_frame info so that
3636 // merge mapping need not be updated for input sections.
3637 // There is no provision to use a different CIE to that used
3638 // by .glink.
3639 if (!this->targ_->has_glink())
3640 return;
3641
3642 layout->add_eh_frame_for_plt(this,
3643 Eh_cie<size>::eh_frame_cie,
3644 sizeof (Eh_cie<size>::eh_frame_cie),
3645 default_fde,
3646 sizeof (default_fde));
3647 this->eh_frame_added_ = true;
3648 }
3649 }
3650
3651 Target_powerpc<size, big_endian>*
3652 targ() const
3653 { return targ_; }
3654
3655 private:
3656 class Plt_stub_ent;
3657 class Plt_stub_ent_hash;
3658 typedef Unordered_map<Plt_stub_ent, unsigned int,
3659 Plt_stub_ent_hash> Plt_stub_entries;
3660
3661 // Alignment of stub section.
3662 unsigned int
3663 stub_align() const
3664 {
3665 if (size == 32)
3666 return 16;
3667 unsigned int min_align = 32;
3668 unsigned int user_align = 1 << parameters->options().plt_align();
3669 return std::max(user_align, min_align);
3670 }
3671
3672 // Return the plt offset for the given call stub.
3673 Address
3674 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3675 {
3676 const Symbol* gsym = p->first.sym_;
3677 if (gsym != NULL)
3678 {
3679 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3680 && gsym->can_use_relative_reloc(false));
3681 return gsym->plt_offset();
3682 }
3683 else
3684 {
3685 *is_iplt = true;
3686 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3687 unsigned int local_sym_index = p->first.locsym_;
3688 return relobj->local_plt_offset(local_sym_index);
3689 }
3690 }
3691
3692 // Size of a given plt call stub.
3693 unsigned int
3694 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3695 {
3696 if (size == 32)
3697 return 16;
3698
3699 bool is_iplt;
3700 Address plt_addr = this->plt_off(p, &is_iplt);
3701 if (is_iplt)
3702 plt_addr += this->targ_->iplt_section()->address();
3703 else
3704 plt_addr += this->targ_->plt_section()->address();
3705 Address got_addr = this->targ_->got_section()->output_section()->address();
3706 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3707 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3708 got_addr += ppcobj->toc_base_offset();
3709 Address off = plt_addr - got_addr;
3710 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
3711 if (this->targ_->abiversion() < 2)
3712 {
3713 bool static_chain = parameters->options().plt_static_chain();
3714 bool thread_safe = this->targ_->plt_thread_safe();
3715 bytes += (4
3716 + 4 * static_chain
3717 + 8 * thread_safe
3718 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3719 }
3720 unsigned int align = 1 << parameters->options().plt_align();
3721 if (align > 1)
3722 bytes = (bytes + align - 1) & -align;
3723 return bytes;
3724 }
3725
3726 // Return long branch stub size.
3727 unsigned int
3728 branch_stub_size(Address to)
3729 {
3730 Address loc
3731 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3732 if (to - loc + (1 << 25) < 2 << 25)
3733 return 4;
3734 if (size == 64 || !parameters->options().output_is_position_independent())
3735 return 16;
3736 return 32;
3737 }
3738
3739 // Write out stubs.
3740 void
3741 do_write(Output_file*);
3742
3743 // Plt call stub keys.
3744 class Plt_stub_ent
3745 {
3746 public:
3747 Plt_stub_ent(const Symbol* sym)
3748 : sym_(sym), object_(0), addend_(0), locsym_(0)
3749 { }
3750
3751 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3752 unsigned int locsym_index)
3753 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3754 { }
3755
3756 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3757 const Symbol* sym,
3758 unsigned int r_type,
3759 Address addend)
3760 : sym_(sym), object_(0), addend_(0), locsym_(0)
3761 {
3762 if (size != 32)
3763 this->addend_ = addend;
3764 else if (parameters->options().output_is_position_independent()
3765 && r_type == elfcpp::R_PPC_PLTREL24)
3766 {
3767 this->addend_ = addend;
3768 if (this->addend_ >= 32768)
3769 this->object_ = object;
3770 }
3771 }
3772
3773 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3774 unsigned int locsym_index,
3775 unsigned int r_type,
3776 Address addend)
3777 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3778 {
3779 if (size != 32)
3780 this->addend_ = addend;
3781 else if (parameters->options().output_is_position_independent()
3782 && r_type == elfcpp::R_PPC_PLTREL24)
3783 this->addend_ = addend;
3784 }
3785
3786 bool operator==(const Plt_stub_ent& that) const
3787 {
3788 return (this->sym_ == that.sym_
3789 && this->object_ == that.object_
3790 && this->addend_ == that.addend_
3791 && this->locsym_ == that.locsym_);
3792 }
3793
3794 const Symbol* sym_;
3795 const Sized_relobj_file<size, big_endian>* object_;
3796 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3797 unsigned int locsym_;
3798 };
3799
3800 class Plt_stub_ent_hash
3801 {
3802 public:
3803 size_t operator()(const Plt_stub_ent& ent) const
3804 {
3805 return (reinterpret_cast<uintptr_t>(ent.sym_)
3806 ^ reinterpret_cast<uintptr_t>(ent.object_)
3807 ^ ent.addend_
3808 ^ ent.locsym_);
3809 }
3810 };
3811
3812 // Long branch stub keys.
3813 class Branch_stub_ent
3814 {
3815 public:
3816 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3817 : dest_(to), toc_base_off_(0)
3818 {
3819 if (size == 64)
3820 toc_base_off_ = obj->toc_base_offset();
3821 }
3822
3823 bool operator==(const Branch_stub_ent& that) const
3824 {
3825 return (this->dest_ == that.dest_
3826 && (size == 32
3827 || this->toc_base_off_ == that.toc_base_off_));
3828 }
3829
3830 Address dest_;
3831 unsigned int toc_base_off_;
3832 };
3833
3834 class Branch_stub_ent_hash
3835 {
3836 public:
3837 size_t operator()(const Branch_stub_ent& ent) const
3838 { return ent.dest_ ^ ent.toc_base_off_; }
3839 };
3840
3841 // In a sane world this would be a global.
3842 Target_powerpc<size, big_endian>* targ_;
3843 // Map sym/object/addend to stub offset.
3844 Plt_stub_entries plt_call_stubs_;
3845 // Map destination address to stub offset.
3846 typedef Unordered_map<Branch_stub_ent, unsigned int,
3847 Branch_stub_ent_hash> Branch_stub_entries;
3848 Branch_stub_entries long_branch_stubs_;
3849 // size of input section
3850 section_size_type orig_data_size_;
3851 // size of stubs
3852 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3853 // Whether .eh_frame info has been created for this stub section.
3854 bool eh_frame_added_;
3855 };
3856
3857 // Make a new stub table, and record.
3858
3859 template<int size, bool big_endian>
3860 Stub_table<size, big_endian>*
3861 Target_powerpc<size, big_endian>::new_stub_table()
3862 {
3863 Stub_table<size, big_endian>* stub_table
3864 = new Stub_table<size, big_endian>(this);
3865 this->stub_tables_.push_back(stub_table);
3866 return stub_table;
3867 }
3868
3869 // Delayed stub table initialisation, because we create the stub table
3870 // before we know to which section it will be attached.
3871
3872 template<int size, bool big_endian>
3873 void
3874 Stub_table<size, big_endian>::init(
3875 const Output_section::Input_section* owner,
3876 Output_section* output_section)
3877 {
3878 this->set_relobj(owner->relobj());
3879 this->set_shndx(owner->shndx());
3880 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3881 this->set_output_section(output_section);
3882 this->orig_data_size_ = owner->current_data_size();
3883
3884 std::vector<Output_relaxed_input_section*> new_relaxed;
3885 new_relaxed.push_back(this);
3886 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3887 }
3888
3889 // Add a plt call stub, if we do not already have one for this
3890 // sym/object/addend combo.
3891
3892 template<int size, bool big_endian>
3893 void
3894 Stub_table<size, big_endian>::add_plt_call_entry(
3895 const Sized_relobj_file<size, big_endian>* object,
3896 const Symbol* gsym,
3897 unsigned int r_type,
3898 Address addend)
3899 {
3900 Plt_stub_ent ent(object, gsym, r_type, addend);
3901 Address off = this->plt_size_;
3902 std::pair<typename Plt_stub_entries::iterator, bool> p
3903 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3904 if (p.second)
3905 this->plt_size_ = off + this->plt_call_size(p.first);
3906 }
3907
3908 template<int size, bool big_endian>
3909 void
3910 Stub_table<size, big_endian>::add_plt_call_entry(
3911 const Sized_relobj_file<size, big_endian>* object,
3912 unsigned int locsym_index,
3913 unsigned int r_type,
3914 Address addend)
3915 {
3916 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3917 Address off = this->plt_size_;
3918 std::pair<typename Plt_stub_entries::iterator, bool> p
3919 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3920 if (p.second)
3921 this->plt_size_ = off + this->plt_call_size(p.first);
3922 }
3923
3924 // Find a plt call stub.
3925
3926 template<int size, bool big_endian>
3927 typename Stub_table<size, big_endian>::Address
3928 Stub_table<size, big_endian>::find_plt_call_entry(
3929 const Sized_relobj_file<size, big_endian>* object,
3930 const Symbol* gsym,
3931 unsigned int r_type,
3932 Address addend) const
3933 {
3934 Plt_stub_ent ent(object, gsym, r_type, addend);
3935 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3936 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3937 }
3938
3939 template<int size, bool big_endian>
3940 typename Stub_table<size, big_endian>::Address
3941 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3942 {
3943 Plt_stub_ent ent(gsym);
3944 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3945 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3946 }
3947
3948 template<int size, bool big_endian>
3949 typename Stub_table<size, big_endian>::Address
3950 Stub_table<size, big_endian>::find_plt_call_entry(
3951 const Sized_relobj_file<size, big_endian>* object,
3952 unsigned int locsym_index,
3953 unsigned int r_type,
3954 Address addend) const
3955 {
3956 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3957 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3958 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3959 }
3960
3961 template<int size, bool big_endian>
3962 typename Stub_table<size, big_endian>::Address
3963 Stub_table<size, big_endian>::find_plt_call_entry(
3964 const Sized_relobj_file<size, big_endian>* object,
3965 unsigned int locsym_index) const
3966 {
3967 Plt_stub_ent ent(object, locsym_index);
3968 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3969 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3970 }
3971
3972 // Add a long branch stub if we don't already have one to given
3973 // destination.
3974
3975 template<int size, bool big_endian>
3976 void
3977 Stub_table<size, big_endian>::add_long_branch_entry(
3978 const Powerpc_relobj<size, big_endian>* object,
3979 Address to)
3980 {
3981 Branch_stub_ent ent(object, to);
3982 Address off = this->branch_size_;
3983 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3984 {
3985 unsigned int stub_size = this->branch_stub_size(to);
3986 this->branch_size_ = off + stub_size;
3987 if (size == 64 && stub_size != 4)
3988 this->targ_->add_branch_lookup_table(to);
3989 }
3990 }
3991
3992 // Find long branch stub.
3993
3994 template<int size, bool big_endian>
3995 typename Stub_table<size, big_endian>::Address
3996 Stub_table<size, big_endian>::find_long_branch_entry(
3997 const Powerpc_relobj<size, big_endian>* object,
3998 Address to) const
3999 {
4000 Branch_stub_ent ent(object, to);
4001 typename Branch_stub_entries::const_iterator p
4002 = this->long_branch_stubs_.find(ent);
4003 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
4004 }
4005
4006 // A class to handle .glink.
4007
4008 template<int size, bool big_endian>
4009 class Output_data_glink : public Output_section_data
4010 {
4011 public:
4012 static const int pltresolve_size = 16*4;
4013
4014 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4015 : Output_section_data(16), targ_(targ)
4016 { }
4017
4018 void
4019 add_eh_frame(Layout* layout)
4020 {
4021 if (!parameters->options().ld_generated_unwind_info())
4022 return;
4023
4024 if (size == 64)
4025 {
4026 if (this->targ_->abiversion() < 2)
4027 layout->add_eh_frame_for_plt(this,
4028 Eh_cie<64>::eh_frame_cie,
4029 sizeof (Eh_cie<64>::eh_frame_cie),
4030 glink_eh_frame_fde_64v1,
4031 sizeof (glink_eh_frame_fde_64v1));
4032 else
4033 layout->add_eh_frame_for_plt(this,
4034 Eh_cie<64>::eh_frame_cie,
4035 sizeof (Eh_cie<64>::eh_frame_cie),
4036 glink_eh_frame_fde_64v2,
4037 sizeof (glink_eh_frame_fde_64v2));
4038 }
4039 else
4040 {
4041 // 32-bit .glink can use the default since the CIE return
4042 // address reg, LR, is valid.
4043 layout->add_eh_frame_for_plt(this,
4044 Eh_cie<32>::eh_frame_cie,
4045 sizeof (Eh_cie<32>::eh_frame_cie),
4046 default_fde,
4047 sizeof (default_fde));
4048 // Except where LR is used in a PIC __glink_PLTresolve.
4049 if (parameters->options().output_is_position_independent())
4050 layout->add_eh_frame_for_plt(this,
4051 Eh_cie<32>::eh_frame_cie,
4052 sizeof (Eh_cie<32>::eh_frame_cie),
4053 glink_eh_frame_fde_32,
4054 sizeof (glink_eh_frame_fde_32));
4055 }
4056 }
4057
4058 protected:
4059 // Write to a map file.
4060 void
4061 do_print_to_mapfile(Mapfile* mapfile) const
4062 { mapfile->print_output_data(this, _("** glink")); }
4063
4064 private:
4065 void
4066 set_final_data_size();
4067
4068 // Write out .glink
4069 void
4070 do_write(Output_file*);
4071
4072 // Allows access to .got and .plt for do_write.
4073 Target_powerpc<size, big_endian>* targ_;
4074 };
4075
4076 template<int size, bool big_endian>
4077 void
4078 Output_data_glink<size, big_endian>::set_final_data_size()
4079 {
4080 unsigned int count = this->targ_->plt_entry_count();
4081 section_size_type total = 0;
4082
4083 if (count != 0)
4084 {
4085 if (size == 32)
4086 {
4087 // space for branch table
4088 total += 4 * (count - 1);
4089
4090 total += -total & 15;
4091 total += this->pltresolve_size;
4092 }
4093 else
4094 {
4095 total += this->pltresolve_size;
4096
4097 // space for branch table
4098 total += 4 * count;
4099 if (this->targ_->abiversion() < 2)
4100 {
4101 total += 4 * count;
4102 if (count > 0x8000)
4103 total += 4 * (count - 0x8000);
4104 }
4105 }
4106 }
4107
4108 this->set_data_size(total);
4109 }
4110
4111 // Write out plt and long branch stub code.
4112
4113 template<int size, bool big_endian>
4114 void
4115 Stub_table<size, big_endian>::do_write(Output_file* of)
4116 {
4117 if (this->plt_call_stubs_.empty()
4118 && this->long_branch_stubs_.empty())
4119 return;
4120
4121 const section_size_type start_off = this->offset();
4122 const section_size_type off = this->stub_offset();
4123 const section_size_type oview_size =
4124 convert_to_section_size_type(this->data_size() - (off - start_off));
4125 unsigned char* const oview = of->get_output_view(off, oview_size);
4126 unsigned char* p;
4127
4128 if (size == 64)
4129 {
4130 const Output_data_got_powerpc<size, big_endian>* got
4131 = this->targ_->got_section();
4132 Address got_os_addr = got->output_section()->address();
4133
4134 if (!this->plt_call_stubs_.empty())
4135 {
4136 // The base address of the .plt section.
4137 Address plt_base = this->targ_->plt_section()->address();
4138 Address iplt_base = invalid_address;
4139
4140 // Write out plt call stubs.
4141 typename Plt_stub_entries::const_iterator cs;
4142 for (cs = this->plt_call_stubs_.begin();
4143 cs != this->plt_call_stubs_.end();
4144 ++cs)
4145 {
4146 bool is_iplt;
4147 Address pltoff = this->plt_off(cs, &is_iplt);
4148 Address plt_addr = pltoff;
4149 if (is_iplt)
4150 {
4151 if (iplt_base == invalid_address)
4152 iplt_base = this->targ_->iplt_section()->address();
4153 plt_addr += iplt_base;
4154 }
4155 else
4156 plt_addr += plt_base;
4157 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4158 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4159 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4160 Address off = plt_addr - got_addr;
4161
4162 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4163 gold_error(_("%s: linkage table error against `%s'"),
4164 cs->first.object_->name().c_str(),
4165 cs->first.sym_->demangled_name().c_str());
4166
4167 bool plt_load_toc = this->targ_->abiversion() < 2;
4168 bool static_chain
4169 = plt_load_toc && parameters->options().plt_static_chain();
4170 bool thread_safe
4171 = plt_load_toc && this->targ_->plt_thread_safe();
4172 bool use_fake_dep = false;
4173 Address cmp_branch_off = 0;
4174 if (thread_safe)
4175 {
4176 unsigned int pltindex
4177 = ((pltoff - this->targ_->first_plt_entry_offset())
4178 / this->targ_->plt_entry_size());
4179 Address glinkoff
4180 = (this->targ_->glink_section()->pltresolve_size
4181 + pltindex * 8);
4182 if (pltindex > 32768)
4183 glinkoff += (pltindex - 32768) * 4;
4184 Address to
4185 = this->targ_->glink_section()->address() + glinkoff;
4186 Address from
4187 = (this->stub_address() + cs->second + 24
4188 + 4 * (ha(off) != 0)
4189 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4190 + 4 * static_chain);
4191 cmp_branch_off = to - from;
4192 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4193 }
4194
4195 p = oview + cs->second;
4196 if (ha(off) != 0)
4197 {
4198 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4199 p += 4;
4200 write_insn<big_endian>(p, addis_11_2 + ha(off));
4201 p += 4;
4202 write_insn<big_endian>(p, ld_12_11 + l(off));
4203 p += 4;
4204 if (plt_load_toc
4205 && ha(off + 8 + 8 * static_chain) != ha(off))
4206 {
4207 write_insn<big_endian>(p, addi_11_11 + l(off));
4208 p += 4;
4209 off = 0;
4210 }
4211 write_insn<big_endian>(p, mtctr_12);
4212 p += 4;
4213 if (plt_load_toc)
4214 {
4215 if (use_fake_dep)
4216 {
4217 write_insn<big_endian>(p, xor_2_12_12);
4218 p += 4;
4219 write_insn<big_endian>(p, add_11_11_2);
4220 p += 4;
4221 }
4222 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4223 p += 4;
4224 if (static_chain)
4225 {
4226 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4227 p += 4;
4228 }
4229 }
4230 }
4231 else
4232 {
4233 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4234 p += 4;
4235 write_insn<big_endian>(p, ld_12_2 + l(off));
4236 p += 4;
4237 if (plt_load_toc
4238 && ha(off + 8 + 8 * static_chain) != ha(off))
4239 {
4240 write_insn<big_endian>(p, addi_2_2 + l(off));
4241 p += 4;
4242 off = 0;
4243 }
4244 write_insn<big_endian>(p, mtctr_12);
4245 p += 4;
4246 if (plt_load_toc)
4247 {
4248 if (use_fake_dep)
4249 {
4250 write_insn<big_endian>(p, xor_11_12_12);
4251 p += 4;
4252 write_insn<big_endian>(p, add_2_2_11);
4253 p += 4;
4254 }
4255 if (static_chain)
4256 {
4257 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4258 p += 4;
4259 }
4260 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4261 p += 4;
4262 }
4263 }
4264 if (thread_safe && !use_fake_dep)
4265 {
4266 write_insn<big_endian>(p, cmpldi_2_0);
4267 p += 4;
4268 write_insn<big_endian>(p, bnectr_p4);
4269 p += 4;
4270 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4271 }
4272 else
4273 write_insn<big_endian>(p, bctr);
4274 }
4275 }
4276
4277 // Write out long branch stubs.
4278 typename Branch_stub_entries::const_iterator bs;
4279 for (bs = this->long_branch_stubs_.begin();
4280 bs != this->long_branch_stubs_.end();
4281 ++bs)
4282 {
4283 p = oview + this->plt_size_ + bs->second;
4284 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4285 Address delta = bs->first.dest_ - loc;
4286 if (delta + (1 << 25) < 2 << 25)
4287 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4288 else
4289 {
4290 Address brlt_addr
4291 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4292 gold_assert(brlt_addr != invalid_address);
4293 brlt_addr += this->targ_->brlt_section()->address();
4294 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4295 Address brltoff = brlt_addr - got_addr;
4296 if (ha(brltoff) == 0)
4297 {
4298 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
4299 }
4300 else
4301 {
4302 write_insn<big_endian>(p, addis_11_2 + ha(brltoff)), p += 4;
4303 write_insn<big_endian>(p, ld_12_11 + l(brltoff)), p += 4;
4304 }
4305 write_insn<big_endian>(p, mtctr_12), p += 4;
4306 write_insn<big_endian>(p, bctr);
4307 }
4308 }
4309 }
4310 else
4311 {
4312 if (!this->plt_call_stubs_.empty())
4313 {
4314 // The base address of the .plt section.
4315 Address plt_base = this->targ_->plt_section()->address();
4316 Address iplt_base = invalid_address;
4317 // The address of _GLOBAL_OFFSET_TABLE_.
4318 Address g_o_t = invalid_address;
4319
4320 // Write out plt call stubs.
4321 typename Plt_stub_entries::const_iterator cs;
4322 for (cs = this->plt_call_stubs_.begin();
4323 cs != this->plt_call_stubs_.end();
4324 ++cs)
4325 {
4326 bool is_iplt;
4327 Address plt_addr = this->plt_off(cs, &is_iplt);
4328 if (is_iplt)
4329 {
4330 if (iplt_base == invalid_address)
4331 iplt_base = this->targ_->iplt_section()->address();
4332 plt_addr += iplt_base;
4333 }
4334 else
4335 plt_addr += plt_base;
4336
4337 p = oview + cs->second;
4338 if (parameters->options().output_is_position_independent())
4339 {
4340 Address got_addr;
4341 const Powerpc_relobj<size, big_endian>* ppcobj
4342 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4343 (cs->first.object_));
4344 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4345 {
4346 unsigned int got2 = ppcobj->got2_shndx();
4347 got_addr = ppcobj->get_output_section_offset(got2);
4348 gold_assert(got_addr != invalid_address);
4349 got_addr += (ppcobj->output_section(got2)->address()
4350 + cs->first.addend_);
4351 }
4352 else
4353 {
4354 if (g_o_t == invalid_address)
4355 {
4356 const Output_data_got_powerpc<size, big_endian>* got
4357 = this->targ_->got_section();
4358 g_o_t = got->address() + got->g_o_t();
4359 }
4360 got_addr = g_o_t;
4361 }
4362
4363 Address off = plt_addr - got_addr;
4364 if (ha(off) == 0)
4365 {
4366 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4367 write_insn<big_endian>(p + 4, mtctr_11);
4368 write_insn<big_endian>(p + 8, bctr);
4369 }
4370 else
4371 {
4372 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4373 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4374 write_insn<big_endian>(p + 8, mtctr_11);
4375 write_insn<big_endian>(p + 12, bctr);
4376 }
4377 }
4378 else
4379 {
4380 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4381 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4382 write_insn<big_endian>(p + 8, mtctr_11);
4383 write_insn<big_endian>(p + 12, bctr);
4384 }
4385 }
4386 }
4387
4388 // Write out long branch stubs.
4389 typename Branch_stub_entries::const_iterator bs;
4390 for (bs = this->long_branch_stubs_.begin();
4391 bs != this->long_branch_stubs_.end();
4392 ++bs)
4393 {
4394 p = oview + this->plt_size_ + bs->second;
4395 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4396 Address delta = bs->first.dest_ - loc;
4397 if (delta + (1 << 25) < 2 << 25)
4398 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4399 else if (!parameters->options().output_is_position_independent())
4400 {
4401 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4402 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4403 write_insn<big_endian>(p + 8, mtctr_12);
4404 write_insn<big_endian>(p + 12, bctr);
4405 }
4406 else
4407 {
4408 delta -= 8;
4409 write_insn<big_endian>(p + 0, mflr_0);
4410 write_insn<big_endian>(p + 4, bcl_20_31);
4411 write_insn<big_endian>(p + 8, mflr_12);
4412 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4413 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4414 write_insn<big_endian>(p + 20, mtlr_0);
4415 write_insn<big_endian>(p + 24, mtctr_12);
4416 write_insn<big_endian>(p + 28, bctr);
4417 }
4418 }
4419 }
4420 }
4421
4422 // Write out .glink.
4423
4424 template<int size, bool big_endian>
4425 void
4426 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4427 {
4428 const section_size_type off = this->offset();
4429 const section_size_type oview_size =
4430 convert_to_section_size_type(this->data_size());
4431 unsigned char* const oview = of->get_output_view(off, oview_size);
4432 unsigned char* p;
4433
4434 // The base address of the .plt section.
4435 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4436 Address plt_base = this->targ_->plt_section()->address();
4437
4438 if (size == 64)
4439 {
4440 // Write pltresolve stub.
4441 p = oview;
4442 Address after_bcl = this->address() + 16;
4443 Address pltoff = plt_base - after_bcl;
4444
4445 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4446
4447 if (this->targ_->abiversion() < 2)
4448 {
4449 write_insn<big_endian>(p, mflr_12), p += 4;
4450 write_insn<big_endian>(p, bcl_20_31), p += 4;
4451 write_insn<big_endian>(p, mflr_11), p += 4;
4452 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4453 write_insn<big_endian>(p, mtlr_12), p += 4;
4454 write_insn<big_endian>(p, add_11_2_11), p += 4;
4455 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4456 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
4457 write_insn<big_endian>(p, mtctr_12), p += 4;
4458 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
4459 }
4460 else
4461 {
4462 write_insn<big_endian>(p, mflr_0), p += 4;
4463 write_insn<big_endian>(p, bcl_20_31), p += 4;
4464 write_insn<big_endian>(p, mflr_11), p += 4;
4465 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4466 write_insn<big_endian>(p, mtlr_0), p += 4;
4467 write_insn<big_endian>(p, sub_12_12_11), p += 4;
4468 write_insn<big_endian>(p, add_11_2_11), p += 4;
4469 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
4470 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4471 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
4472 write_insn<big_endian>(p, mtctr_12), p += 4;
4473 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
4474 }
4475 write_insn<big_endian>(p, bctr), p += 4;
4476 while (p < oview + this->pltresolve_size)
4477 write_insn<big_endian>(p, nop), p += 4;
4478
4479 // Write lazy link call stubs.
4480 uint32_t indx = 0;
4481 while (p < oview + oview_size)
4482 {
4483 if (this->targ_->abiversion() < 2)
4484 {
4485 if (indx < 0x8000)
4486 {
4487 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4488 }
4489 else
4490 {
4491 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
4492 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4493 }
4494 }
4495 uint32_t branch_off = 8 - (p - oview);
4496 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4497 indx++;
4498 }
4499 }
4500 else
4501 {
4502 const Output_data_got_powerpc<size, big_endian>* got
4503 = this->targ_->got_section();
4504 // The address of _GLOBAL_OFFSET_TABLE_.
4505 Address g_o_t = got->address() + got->g_o_t();
4506
4507 // Write out pltresolve branch table.
4508 p = oview;
4509 unsigned int the_end = oview_size - this->pltresolve_size;
4510 unsigned char* end_p = oview + the_end;
4511 while (p < end_p - 8 * 4)
4512 write_insn<big_endian>(p, b + end_p - p), p += 4;
4513 while (p < end_p)
4514 write_insn<big_endian>(p, nop), p += 4;
4515
4516 // Write out pltresolve call stub.
4517 if (parameters->options().output_is_position_independent())
4518 {
4519 Address res0_off = 0;
4520 Address after_bcl_off = the_end + 12;
4521 Address bcl_res0 = after_bcl_off - res0_off;
4522
4523 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4524 write_insn<big_endian>(p + 4, mflr_0);
4525 write_insn<big_endian>(p + 8, bcl_20_31);
4526 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4527 write_insn<big_endian>(p + 16, mflr_12);
4528 write_insn<big_endian>(p + 20, mtlr_0);
4529 write_insn<big_endian>(p + 24, sub_11_11_12);
4530
4531 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4532
4533 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4534 if (ha(got_bcl) == ha(got_bcl + 4))
4535 {
4536 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4537 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4538 }
4539 else
4540 {
4541 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4542 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4543 }
4544 write_insn<big_endian>(p + 40, mtctr_0);
4545 write_insn<big_endian>(p + 44, add_0_11_11);
4546 write_insn<big_endian>(p + 48, add_11_0_11);
4547 write_insn<big_endian>(p + 52, bctr);
4548 write_insn<big_endian>(p + 56, nop);
4549 write_insn<big_endian>(p + 60, nop);
4550 }
4551 else
4552 {
4553 Address res0 = this->address();
4554
4555 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4556 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4557 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4558 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4559 else
4560 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4561 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4562 write_insn<big_endian>(p + 16, mtctr_0);
4563 write_insn<big_endian>(p + 20, add_0_11_11);
4564 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4565 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4566 else
4567 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4568 write_insn<big_endian>(p + 28, add_11_0_11);
4569 write_insn<big_endian>(p + 32, bctr);
4570 write_insn<big_endian>(p + 36, nop);
4571 write_insn<big_endian>(p + 40, nop);
4572 write_insn<big_endian>(p + 44, nop);
4573 write_insn<big_endian>(p + 48, nop);
4574 write_insn<big_endian>(p + 52, nop);
4575 write_insn<big_endian>(p + 56, nop);
4576 write_insn<big_endian>(p + 60, nop);
4577 }
4578 p += 64;
4579 }
4580
4581 of->write_output_view(off, oview_size, oview);
4582 }
4583
4584
4585 // A class to handle linker generated save/restore functions.
4586
4587 template<int size, bool big_endian>
4588 class Output_data_save_res : public Output_section_data_build
4589 {
4590 public:
4591 Output_data_save_res(Symbol_table* symtab);
4592
4593 protected:
4594 // Write to a map file.
4595 void
4596 do_print_to_mapfile(Mapfile* mapfile) const
4597 { mapfile->print_output_data(this, _("** save/restore")); }
4598
4599 void
4600 do_write(Output_file*);
4601
4602 private:
4603 // The maximum size of save/restore contents.
4604 static const unsigned int savres_max = 218*4;
4605
4606 void
4607 savres_define(Symbol_table* symtab,
4608 const char *name,
4609 unsigned int lo, unsigned int hi,
4610 unsigned char* write_ent(unsigned char*, int),
4611 unsigned char* write_tail(unsigned char*, int));
4612
4613 unsigned char *contents_;
4614 };
4615
4616 template<bool big_endian>
4617 static unsigned char*
4618 savegpr0(unsigned char* p, int r)
4619 {
4620 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4621 write_insn<big_endian>(p, insn);
4622 return p + 4;
4623 }
4624
4625 template<bool big_endian>
4626 static unsigned char*
4627 savegpr0_tail(unsigned char* p, int r)
4628 {
4629 p = savegpr0<big_endian>(p, r);
4630 uint32_t insn = std_0_1 + 16;
4631 write_insn<big_endian>(p, insn);
4632 p = p + 4;
4633 write_insn<big_endian>(p, blr);
4634 return p + 4;
4635 }
4636
4637 template<bool big_endian>
4638 static unsigned char*
4639 restgpr0(unsigned char* p, int r)
4640 {
4641 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4642 write_insn<big_endian>(p, insn);
4643 return p + 4;
4644 }
4645
4646 template<bool big_endian>
4647 static unsigned char*
4648 restgpr0_tail(unsigned char* p, int r)
4649 {
4650 uint32_t insn = ld_0_1 + 16;
4651 write_insn<big_endian>(p, insn);
4652 p = p + 4;
4653 p = restgpr0<big_endian>(p, r);
4654 write_insn<big_endian>(p, mtlr_0);
4655 p = p + 4;
4656 if (r == 29)
4657 {
4658 p = restgpr0<big_endian>(p, 30);
4659 p = restgpr0<big_endian>(p, 31);
4660 }
4661 write_insn<big_endian>(p, blr);
4662 return p + 4;
4663 }
4664
4665 template<bool big_endian>
4666 static unsigned char*
4667 savegpr1(unsigned char* p, int r)
4668 {
4669 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4670 write_insn<big_endian>(p, insn);
4671 return p + 4;
4672 }
4673
4674 template<bool big_endian>
4675 static unsigned char*
4676 savegpr1_tail(unsigned char* p, int r)
4677 {
4678 p = savegpr1<big_endian>(p, r);
4679 write_insn<big_endian>(p, blr);
4680 return p + 4;
4681 }
4682
4683 template<bool big_endian>
4684 static unsigned char*
4685 restgpr1(unsigned char* p, int r)
4686 {
4687 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4688 write_insn<big_endian>(p, insn);
4689 return p + 4;
4690 }
4691
4692 template<bool big_endian>
4693 static unsigned char*
4694 restgpr1_tail(unsigned char* p, int r)
4695 {
4696 p = restgpr1<big_endian>(p, r);
4697 write_insn<big_endian>(p, blr);
4698 return p + 4;
4699 }
4700
4701 template<bool big_endian>
4702 static unsigned char*
4703 savefpr(unsigned char* p, int r)
4704 {
4705 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4706 write_insn<big_endian>(p, insn);
4707 return p + 4;
4708 }
4709
4710 template<bool big_endian>
4711 static unsigned char*
4712 savefpr0_tail(unsigned char* p, int r)
4713 {
4714 p = savefpr<big_endian>(p, r);
4715 write_insn<big_endian>(p, std_0_1 + 16);
4716 p = p + 4;
4717 write_insn<big_endian>(p, blr);
4718 return p + 4;
4719 }
4720
4721 template<bool big_endian>
4722 static unsigned char*
4723 restfpr(unsigned char* p, int r)
4724 {
4725 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4726 write_insn<big_endian>(p, insn);
4727 return p + 4;
4728 }
4729
4730 template<bool big_endian>
4731 static unsigned char*
4732 restfpr0_tail(unsigned char* p, int r)
4733 {
4734 write_insn<big_endian>(p, ld_0_1 + 16);
4735 p = p + 4;
4736 p = restfpr<big_endian>(p, r);
4737 write_insn<big_endian>(p, mtlr_0);
4738 p = p + 4;
4739 if (r == 29)
4740 {
4741 p = restfpr<big_endian>(p, 30);
4742 p = restfpr<big_endian>(p, 31);
4743 }
4744 write_insn<big_endian>(p, blr);
4745 return p + 4;
4746 }
4747
4748 template<bool big_endian>
4749 static unsigned char*
4750 savefpr1_tail(unsigned char* p, int r)
4751 {
4752 p = savefpr<big_endian>(p, r);
4753 write_insn<big_endian>(p, blr);
4754 return p + 4;
4755 }
4756
4757 template<bool big_endian>
4758 static unsigned char*
4759 restfpr1_tail(unsigned char* p, int r)
4760 {
4761 p = restfpr<big_endian>(p, r);
4762 write_insn<big_endian>(p, blr);
4763 return p + 4;
4764 }
4765
4766 template<bool big_endian>
4767 static unsigned char*
4768 savevr(unsigned char* p, int r)
4769 {
4770 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4771 write_insn<big_endian>(p, insn);
4772 p = p + 4;
4773 insn = stvx_0_12_0 + (r << 21);
4774 write_insn<big_endian>(p, insn);
4775 return p + 4;
4776 }
4777
4778 template<bool big_endian>
4779 static unsigned char*
4780 savevr_tail(unsigned char* p, int r)
4781 {
4782 p = savevr<big_endian>(p, r);
4783 write_insn<big_endian>(p, blr);
4784 return p + 4;
4785 }
4786
4787 template<bool big_endian>
4788 static unsigned char*
4789 restvr(unsigned char* p, int r)
4790 {
4791 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4792 write_insn<big_endian>(p, insn);
4793 p = p + 4;
4794 insn = lvx_0_12_0 + (r << 21);
4795 write_insn<big_endian>(p, insn);
4796 return p + 4;
4797 }
4798
4799 template<bool big_endian>
4800 static unsigned char*
4801 restvr_tail(unsigned char* p, int r)
4802 {
4803 p = restvr<big_endian>(p, r);
4804 write_insn<big_endian>(p, blr);
4805 return p + 4;
4806 }
4807
4808
4809 template<int size, bool big_endian>
4810 Output_data_save_res<size, big_endian>::Output_data_save_res(
4811 Symbol_table* symtab)
4812 : Output_section_data_build(4),
4813 contents_(NULL)
4814 {
4815 this->savres_define(symtab,
4816 "_savegpr0_", 14, 31,
4817 savegpr0<big_endian>, savegpr0_tail<big_endian>);
4818 this->savres_define(symtab,
4819 "_restgpr0_", 14, 29,
4820 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4821 this->savres_define(symtab,
4822 "_restgpr0_", 30, 31,
4823 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4824 this->savres_define(symtab,
4825 "_savegpr1_", 14, 31,
4826 savegpr1<big_endian>, savegpr1_tail<big_endian>);
4827 this->savres_define(symtab,
4828 "_restgpr1_", 14, 31,
4829 restgpr1<big_endian>, restgpr1_tail<big_endian>);
4830 this->savres_define(symtab,
4831 "_savefpr_", 14, 31,
4832 savefpr<big_endian>, savefpr0_tail<big_endian>);
4833 this->savres_define(symtab,
4834 "_restfpr_", 14, 29,
4835 restfpr<big_endian>, restfpr0_tail<big_endian>);
4836 this->savres_define(symtab,
4837 "_restfpr_", 30, 31,
4838 restfpr<big_endian>, restfpr0_tail<big_endian>);
4839 this->savres_define(symtab,
4840 "._savef", 14, 31,
4841 savefpr<big_endian>, savefpr1_tail<big_endian>);
4842 this->savres_define(symtab,
4843 "._restf", 14, 31,
4844 restfpr<big_endian>, restfpr1_tail<big_endian>);
4845 this->savres_define(symtab,
4846 "_savevr_", 20, 31,
4847 savevr<big_endian>, savevr_tail<big_endian>);
4848 this->savres_define(symtab,
4849 "_restvr_", 20, 31,
4850 restvr<big_endian>, restvr_tail<big_endian>);
4851 }
4852
4853 template<int size, bool big_endian>
4854 void
4855 Output_data_save_res<size, big_endian>::savres_define(
4856 Symbol_table* symtab,
4857 const char *name,
4858 unsigned int lo, unsigned int hi,
4859 unsigned char* write_ent(unsigned char*, int),
4860 unsigned char* write_tail(unsigned char*, int))
4861 {
4862 size_t len = strlen(name);
4863 bool writing = false;
4864 char sym[16];
4865
4866 memcpy(sym, name, len);
4867 sym[len + 2] = 0;
4868
4869 for (unsigned int i = lo; i <= hi; i++)
4870 {
4871 sym[len + 0] = i / 10 + '0';
4872 sym[len + 1] = i % 10 + '0';
4873 Symbol* gsym = symtab->lookup(sym);
4874 bool refd = gsym != NULL && gsym->is_undefined();
4875 writing = writing || refd;
4876 if (writing)
4877 {
4878 if (this->contents_ == NULL)
4879 this->contents_ = new unsigned char[this->savres_max];
4880
4881 section_size_type value = this->current_data_size();
4882 unsigned char* p = this->contents_ + value;
4883 if (i != hi)
4884 p = write_ent(p, i);
4885 else
4886 p = write_tail(p, i);
4887 section_size_type cur_size = p - this->contents_;
4888 this->set_current_data_size(cur_size);
4889 if (refd)
4890 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4891 this, value, cur_size - value,
4892 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4893 elfcpp::STV_HIDDEN, 0, false, false);
4894 }
4895 }
4896 }
4897
4898 // Write out save/restore.
4899
4900 template<int size, bool big_endian>
4901 void
4902 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4903 {
4904 const section_size_type off = this->offset();
4905 const section_size_type oview_size =
4906 convert_to_section_size_type(this->data_size());
4907 unsigned char* const oview = of->get_output_view(off, oview_size);
4908 memcpy(oview, this->contents_, oview_size);
4909 of->write_output_view(off, oview_size, oview);
4910 }
4911
4912
4913 // Create the glink section.
4914
4915 template<int size, bool big_endian>
4916 void
4917 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4918 {
4919 if (this->glink_ == NULL)
4920 {
4921 this->glink_ = new Output_data_glink<size, big_endian>(this);
4922 this->glink_->add_eh_frame(layout);
4923 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4924 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4925 this->glink_, ORDER_TEXT, false);
4926 }
4927 }
4928
4929 // Create a PLT entry for a global symbol.
4930
4931 template<int size, bool big_endian>
4932 void
4933 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
4934 Layout* layout,
4935 Symbol* gsym)
4936 {
4937 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4938 && gsym->can_use_relative_reloc(false))
4939 {
4940 if (this->iplt_ == NULL)
4941 this->make_iplt_section(symtab, layout);
4942 this->iplt_->add_ifunc_entry(gsym);
4943 }
4944 else
4945 {
4946 if (this->plt_ == NULL)
4947 this->make_plt_section(symtab, layout);
4948 this->plt_->add_entry(gsym);
4949 }
4950 }
4951
4952 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4953
4954 template<int size, bool big_endian>
4955 void
4956 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
4957 Symbol_table* symtab,
4958 Layout* layout,
4959 Sized_relobj_file<size, big_endian>* relobj,
4960 unsigned int r_sym)
4961 {
4962 if (this->iplt_ == NULL)
4963 this->make_iplt_section(symtab, layout);
4964 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
4965 }
4966
4967 // Return the number of entries in the PLT.
4968
4969 template<int size, bool big_endian>
4970 unsigned int
4971 Target_powerpc<size, big_endian>::plt_entry_count() const
4972 {
4973 if (this->plt_ == NULL)
4974 return 0;
4975 return this->plt_->entry_count();
4976 }
4977
4978 // Create a GOT entry for local dynamic __tls_get_addr calls.
4979
4980 template<int size, bool big_endian>
4981 unsigned int
4982 Target_powerpc<size, big_endian>::tlsld_got_offset(
4983 Symbol_table* symtab,
4984 Layout* layout,
4985 Sized_relobj_file<size, big_endian>* object)
4986 {
4987 if (this->tlsld_got_offset_ == -1U)
4988 {
4989 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4990 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4991 Output_data_got_powerpc<size, big_endian>* got
4992 = this->got_section(symtab, layout);
4993 unsigned int got_offset = got->add_constant_pair(0, 0);
4994 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
4995 got_offset, 0);
4996 this->tlsld_got_offset_ = got_offset;
4997 }
4998 return this->tlsld_got_offset_;
4999 }
5000
5001 // Get the Reference_flags for a particular relocation.
5002
5003 template<int size, bool big_endian>
5004 int
5005 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
5006 {
5007 switch (r_type)
5008 {
5009 case elfcpp::R_POWERPC_NONE:
5010 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5011 case elfcpp::R_POWERPC_GNU_VTENTRY:
5012 case elfcpp::R_PPC64_TOC:
5013 // No symbol reference.
5014 return 0;
5015
5016 case elfcpp::R_PPC64_ADDR64:
5017 case elfcpp::R_PPC64_UADDR64:
5018 case elfcpp::R_POWERPC_ADDR32:
5019 case elfcpp::R_POWERPC_UADDR32:
5020 case elfcpp::R_POWERPC_ADDR16:
5021 case elfcpp::R_POWERPC_UADDR16:
5022 case elfcpp::R_POWERPC_ADDR16_LO:
5023 case elfcpp::R_POWERPC_ADDR16_HI:
5024 case elfcpp::R_POWERPC_ADDR16_HA:
5025 return Symbol::ABSOLUTE_REF;
5026
5027 case elfcpp::R_POWERPC_ADDR24:
5028 case elfcpp::R_POWERPC_ADDR14:
5029 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5030 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5031 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5032
5033 case elfcpp::R_PPC64_REL64:
5034 case elfcpp::R_POWERPC_REL32:
5035 case elfcpp::R_PPC_LOCAL24PC:
5036 case elfcpp::R_POWERPC_REL16:
5037 case elfcpp::R_POWERPC_REL16_LO:
5038 case elfcpp::R_POWERPC_REL16_HI:
5039 case elfcpp::R_POWERPC_REL16_HA:
5040 return Symbol::RELATIVE_REF;
5041
5042 case elfcpp::R_POWERPC_REL24:
5043 case elfcpp::R_PPC_PLTREL24:
5044 case elfcpp::R_POWERPC_REL14:
5045 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5046 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5047 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5048
5049 case elfcpp::R_POWERPC_GOT16:
5050 case elfcpp::R_POWERPC_GOT16_LO:
5051 case elfcpp::R_POWERPC_GOT16_HI:
5052 case elfcpp::R_POWERPC_GOT16_HA:
5053 case elfcpp::R_PPC64_GOT16_DS:
5054 case elfcpp::R_PPC64_GOT16_LO_DS:
5055 case elfcpp::R_PPC64_TOC16:
5056 case elfcpp::R_PPC64_TOC16_LO:
5057 case elfcpp::R_PPC64_TOC16_HI:
5058 case elfcpp::R_PPC64_TOC16_HA:
5059 case elfcpp::R_PPC64_TOC16_DS:
5060 case elfcpp::R_PPC64_TOC16_LO_DS:
5061 // Absolute in GOT.
5062 return Symbol::ABSOLUTE_REF;
5063
5064 case elfcpp::R_POWERPC_GOT_TPREL16:
5065 case elfcpp::R_POWERPC_TLS:
5066 return Symbol::TLS_REF;
5067
5068 case elfcpp::R_POWERPC_COPY:
5069 case elfcpp::R_POWERPC_GLOB_DAT:
5070 case elfcpp::R_POWERPC_JMP_SLOT:
5071 case elfcpp::R_POWERPC_RELATIVE:
5072 case elfcpp::R_POWERPC_DTPMOD:
5073 default:
5074 // Not expected. We will give an error later.
5075 return 0;
5076 }
5077 }
5078
5079 // Report an unsupported relocation against a local symbol.
5080
5081 template<int size, bool big_endian>
5082 void
5083 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5084 Sized_relobj_file<size, big_endian>* object,
5085 unsigned int r_type)
5086 {
5087 gold_error(_("%s: unsupported reloc %u against local symbol"),
5088 object->name().c_str(), r_type);
5089 }
5090
5091 // We are about to emit a dynamic relocation of type R_TYPE. If the
5092 // dynamic linker does not support it, issue an error.
5093
5094 template<int size, bool big_endian>
5095 void
5096 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5097 unsigned int r_type)
5098 {
5099 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5100
5101 // These are the relocation types supported by glibc for both 32-bit
5102 // and 64-bit powerpc.
5103 switch (r_type)
5104 {
5105 case elfcpp::R_POWERPC_NONE:
5106 case elfcpp::R_POWERPC_RELATIVE:
5107 case elfcpp::R_POWERPC_GLOB_DAT:
5108 case elfcpp::R_POWERPC_DTPMOD:
5109 case elfcpp::R_POWERPC_DTPREL:
5110 case elfcpp::R_POWERPC_TPREL:
5111 case elfcpp::R_POWERPC_JMP_SLOT:
5112 case elfcpp::R_POWERPC_COPY:
5113 case elfcpp::R_POWERPC_IRELATIVE:
5114 case elfcpp::R_POWERPC_ADDR32:
5115 case elfcpp::R_POWERPC_UADDR32:
5116 case elfcpp::R_POWERPC_ADDR24:
5117 case elfcpp::R_POWERPC_ADDR16:
5118 case elfcpp::R_POWERPC_UADDR16:
5119 case elfcpp::R_POWERPC_ADDR16_LO:
5120 case elfcpp::R_POWERPC_ADDR16_HI:
5121 case elfcpp::R_POWERPC_ADDR16_HA:
5122 case elfcpp::R_POWERPC_ADDR14:
5123 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5124 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5125 case elfcpp::R_POWERPC_REL32:
5126 case elfcpp::R_POWERPC_REL24:
5127 case elfcpp::R_POWERPC_TPREL16:
5128 case elfcpp::R_POWERPC_TPREL16_LO:
5129 case elfcpp::R_POWERPC_TPREL16_HI:
5130 case elfcpp::R_POWERPC_TPREL16_HA:
5131 return;
5132
5133 default:
5134 break;
5135 }
5136
5137 if (size == 64)
5138 {
5139 switch (r_type)
5140 {
5141 // These are the relocation types supported only on 64-bit.
5142 case elfcpp::R_PPC64_ADDR64:
5143 case elfcpp::R_PPC64_UADDR64:
5144 case elfcpp::R_PPC64_JMP_IREL:
5145 case elfcpp::R_PPC64_ADDR16_DS:
5146 case elfcpp::R_PPC64_ADDR16_LO_DS:
5147 case elfcpp::R_PPC64_ADDR16_HIGH:
5148 case elfcpp::R_PPC64_ADDR16_HIGHA:
5149 case elfcpp::R_PPC64_ADDR16_HIGHER:
5150 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5151 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5152 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5153 case elfcpp::R_PPC64_REL64:
5154 case elfcpp::R_POWERPC_ADDR30:
5155 case elfcpp::R_PPC64_TPREL16_DS:
5156 case elfcpp::R_PPC64_TPREL16_LO_DS:
5157 case elfcpp::R_PPC64_TPREL16_HIGH:
5158 case elfcpp::R_PPC64_TPREL16_HIGHA:
5159 case elfcpp::R_PPC64_TPREL16_HIGHER:
5160 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5161 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5162 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5163 return;
5164
5165 default:
5166 break;
5167 }
5168 }
5169 else
5170 {
5171 switch (r_type)
5172 {
5173 // These are the relocation types supported only on 32-bit.
5174 // ??? glibc ld.so doesn't need to support these.
5175 case elfcpp::R_POWERPC_DTPREL16:
5176 case elfcpp::R_POWERPC_DTPREL16_LO:
5177 case elfcpp::R_POWERPC_DTPREL16_HI:
5178 case elfcpp::R_POWERPC_DTPREL16_HA:
5179 return;
5180
5181 default:
5182 break;
5183 }
5184 }
5185
5186 // This prevents us from issuing more than one error per reloc
5187 // section. But we can still wind up issuing more than one
5188 // error per object file.
5189 if (this->issued_non_pic_error_)
5190 return;
5191 gold_assert(parameters->options().output_is_position_independent());
5192 object->error(_("requires unsupported dynamic reloc; "
5193 "recompile with -fPIC"));
5194 this->issued_non_pic_error_ = true;
5195 return;
5196 }
5197
5198 // Return whether we need to make a PLT entry for a relocation of the
5199 // given type against a STT_GNU_IFUNC symbol.
5200
5201 template<int size, bool big_endian>
5202 bool
5203 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5204 Sized_relobj_file<size, big_endian>* object,
5205 unsigned int r_type,
5206 bool report_err)
5207 {
5208 // In non-pic code any reference will resolve to the plt call stub
5209 // for the ifunc symbol.
5210 if (size == 32 && !parameters->options().output_is_position_independent())
5211 return true;
5212
5213 switch (r_type)
5214 {
5215 // Word size refs from data sections are OK, but don't need a PLT entry.
5216 case elfcpp::R_POWERPC_ADDR32:
5217 case elfcpp::R_POWERPC_UADDR32:
5218 if (size == 32)
5219 return false;
5220 break;
5221
5222 case elfcpp::R_PPC64_ADDR64:
5223 case elfcpp::R_PPC64_UADDR64:
5224 if (size == 64)
5225 return false;
5226 break;
5227
5228 // GOT refs are good, but also don't need a PLT entry.
5229 case elfcpp::R_POWERPC_GOT16:
5230 case elfcpp::R_POWERPC_GOT16_LO:
5231 case elfcpp::R_POWERPC_GOT16_HI:
5232 case elfcpp::R_POWERPC_GOT16_HA:
5233 case elfcpp::R_PPC64_GOT16_DS:
5234 case elfcpp::R_PPC64_GOT16_LO_DS:
5235 return false;
5236
5237 // Function calls are good, and these do need a PLT entry.
5238 case elfcpp::R_POWERPC_ADDR24:
5239 case elfcpp::R_POWERPC_ADDR14:
5240 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5241 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5242 case elfcpp::R_POWERPC_REL24:
5243 case elfcpp::R_PPC_PLTREL24:
5244 case elfcpp::R_POWERPC_REL14:
5245 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5246 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5247 return true;
5248
5249 default:
5250 break;
5251 }
5252
5253 // Anything else is a problem.
5254 // If we are building a static executable, the libc startup function
5255 // responsible for applying indirect function relocations is going
5256 // to complain about the reloc type.
5257 // If we are building a dynamic executable, we will have a text
5258 // relocation. The dynamic loader will set the text segment
5259 // writable and non-executable to apply text relocations. So we'll
5260 // segfault when trying to run the indirection function to resolve
5261 // the reloc.
5262 if (report_err)
5263 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5264 object->name().c_str(), r_type);
5265 return false;
5266 }
5267
5268 // Scan a relocation for a local symbol.
5269
5270 template<int size, bool big_endian>
5271 inline void
5272 Target_powerpc<size, big_endian>::Scan::local(
5273 Symbol_table* symtab,
5274 Layout* layout,
5275 Target_powerpc<size, big_endian>* target,
5276 Sized_relobj_file<size, big_endian>* object,
5277 unsigned int data_shndx,
5278 Output_section* output_section,
5279 const elfcpp::Rela<size, big_endian>& reloc,
5280 unsigned int r_type,
5281 const elfcpp::Sym<size, big_endian>& lsym,
5282 bool is_discarded)
5283 {
5284 this->maybe_skip_tls_get_addr_call(r_type, NULL);
5285
5286 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5287 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5288 {
5289 this->expect_tls_get_addr_call();
5290 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5291 if (tls_type != tls::TLSOPT_NONE)
5292 this->skip_next_tls_get_addr_call();
5293 }
5294 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5295 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5296 {
5297 this->expect_tls_get_addr_call();
5298 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5299 if (tls_type != tls::TLSOPT_NONE)
5300 this->skip_next_tls_get_addr_call();
5301 }
5302
5303 Powerpc_relobj<size, big_endian>* ppc_object
5304 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5305
5306 if (is_discarded)
5307 {
5308 if (size == 64
5309 && data_shndx == ppc_object->opd_shndx()
5310 && r_type == elfcpp::R_PPC64_ADDR64)
5311 ppc_object->set_opd_discard(reloc.get_r_offset());
5312 return;
5313 }
5314
5315 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5316 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5317 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5318 {
5319 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5320 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5321 r_type, r_sym, reloc.get_r_addend());
5322 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5323 }
5324
5325 switch (r_type)
5326 {
5327 case elfcpp::R_POWERPC_NONE:
5328 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5329 case elfcpp::R_POWERPC_GNU_VTENTRY:
5330 case elfcpp::R_PPC64_TOCSAVE:
5331 case elfcpp::R_POWERPC_TLS:
5332 break;
5333
5334 case elfcpp::R_PPC64_TOC:
5335 {
5336 Output_data_got_powerpc<size, big_endian>* got
5337 = target->got_section(symtab, layout);
5338 if (parameters->options().output_is_position_independent())
5339 {
5340 Address off = reloc.get_r_offset();
5341 if (size == 64
5342 && data_shndx == ppc_object->opd_shndx()
5343 && ppc_object->get_opd_discard(off - 8))
5344 break;
5345
5346 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5347 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5348 rela_dyn->add_output_section_relative(got->output_section(),
5349 elfcpp::R_POWERPC_RELATIVE,
5350 output_section,
5351 object, data_shndx, off,
5352 symobj->toc_base_offset());
5353 }
5354 }
5355 break;
5356
5357 case elfcpp::R_PPC64_ADDR64:
5358 case elfcpp::R_PPC64_UADDR64:
5359 case elfcpp::R_POWERPC_ADDR32:
5360 case elfcpp::R_POWERPC_UADDR32:
5361 case elfcpp::R_POWERPC_ADDR24:
5362 case elfcpp::R_POWERPC_ADDR16:
5363 case elfcpp::R_POWERPC_ADDR16_LO:
5364 case elfcpp::R_POWERPC_ADDR16_HI:
5365 case elfcpp::R_POWERPC_ADDR16_HA:
5366 case elfcpp::R_POWERPC_UADDR16:
5367 case elfcpp::R_PPC64_ADDR16_HIGH:
5368 case elfcpp::R_PPC64_ADDR16_HIGHA:
5369 case elfcpp::R_PPC64_ADDR16_HIGHER:
5370 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5371 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5372 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5373 case elfcpp::R_PPC64_ADDR16_DS:
5374 case elfcpp::R_PPC64_ADDR16_LO_DS:
5375 case elfcpp::R_POWERPC_ADDR14:
5376 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5377 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5378 // If building a shared library (or a position-independent
5379 // executable), we need to create a dynamic relocation for
5380 // this location.
5381 if (parameters->options().output_is_position_independent()
5382 || (size == 64 && is_ifunc))
5383 {
5384 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5385 is_ifunc);
5386 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5387 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5388 {
5389 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5390 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5391 : elfcpp::R_POWERPC_RELATIVE);
5392 rela_dyn->add_local_relative(object, r_sym, dynrel,
5393 output_section, data_shndx,
5394 reloc.get_r_offset(),
5395 reloc.get_r_addend(), false);
5396 }
5397 else
5398 {
5399 check_non_pic(object, r_type);
5400 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5401 rela_dyn->add_local(object, r_sym, r_type, output_section,
5402 data_shndx, reloc.get_r_offset(),
5403 reloc.get_r_addend());
5404 }
5405 }
5406 break;
5407
5408 case elfcpp::R_POWERPC_REL24:
5409 case elfcpp::R_PPC_PLTREL24:
5410 case elfcpp::R_PPC_LOCAL24PC:
5411 case elfcpp::R_POWERPC_REL14:
5412 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5413 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5414 if (!is_ifunc)
5415 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5416 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5417 reloc.get_r_addend());
5418 break;
5419
5420 case elfcpp::R_PPC64_REL64:
5421 case elfcpp::R_POWERPC_REL32:
5422 case elfcpp::R_POWERPC_REL16:
5423 case elfcpp::R_POWERPC_REL16_LO:
5424 case elfcpp::R_POWERPC_REL16_HI:
5425 case elfcpp::R_POWERPC_REL16_HA:
5426 case elfcpp::R_POWERPC_SECTOFF:
5427 case elfcpp::R_POWERPC_SECTOFF_LO:
5428 case elfcpp::R_POWERPC_SECTOFF_HI:
5429 case elfcpp::R_POWERPC_SECTOFF_HA:
5430 case elfcpp::R_PPC64_SECTOFF_DS:
5431 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5432 case elfcpp::R_POWERPC_TPREL16:
5433 case elfcpp::R_POWERPC_TPREL16_LO:
5434 case elfcpp::R_POWERPC_TPREL16_HI:
5435 case elfcpp::R_POWERPC_TPREL16_HA:
5436 case elfcpp::R_PPC64_TPREL16_DS:
5437 case elfcpp::R_PPC64_TPREL16_LO_DS:
5438 case elfcpp::R_PPC64_TPREL16_HIGH:
5439 case elfcpp::R_PPC64_TPREL16_HIGHA:
5440 case elfcpp::R_PPC64_TPREL16_HIGHER:
5441 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5442 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5443 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5444 case elfcpp::R_POWERPC_DTPREL16:
5445 case elfcpp::R_POWERPC_DTPREL16_LO:
5446 case elfcpp::R_POWERPC_DTPREL16_HI:
5447 case elfcpp::R_POWERPC_DTPREL16_HA:
5448 case elfcpp::R_PPC64_DTPREL16_DS:
5449 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5450 case elfcpp::R_PPC64_DTPREL16_HIGH:
5451 case elfcpp::R_PPC64_DTPREL16_HIGHA:
5452 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5453 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5454 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5455 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5456 case elfcpp::R_PPC64_TLSGD:
5457 case elfcpp::R_PPC64_TLSLD:
5458 break;
5459
5460 case elfcpp::R_POWERPC_GOT16:
5461 case elfcpp::R_POWERPC_GOT16_LO:
5462 case elfcpp::R_POWERPC_GOT16_HI:
5463 case elfcpp::R_POWERPC_GOT16_HA:
5464 case elfcpp::R_PPC64_GOT16_DS:
5465 case elfcpp::R_PPC64_GOT16_LO_DS:
5466 {
5467 // The symbol requires a GOT entry.
5468 Output_data_got_powerpc<size, big_endian>* got
5469 = target->got_section(symtab, layout);
5470 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5471
5472 if (!parameters->options().output_is_position_independent())
5473 {
5474 if (size == 32 && is_ifunc)
5475 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5476 else
5477 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5478 }
5479 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5480 {
5481 // If we are generating a shared object or a pie, this
5482 // symbol's GOT entry will be set by a dynamic relocation.
5483 unsigned int off;
5484 off = got->add_constant(0);
5485 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5486
5487 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5488 is_ifunc);
5489 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5490 : elfcpp::R_POWERPC_RELATIVE);
5491 rela_dyn->add_local_relative(object, r_sym, dynrel,
5492 got, off, 0, false);
5493 }
5494 }
5495 break;
5496
5497 case elfcpp::R_PPC64_TOC16:
5498 case elfcpp::R_PPC64_TOC16_LO:
5499 case elfcpp::R_PPC64_TOC16_HI:
5500 case elfcpp::R_PPC64_TOC16_HA:
5501 case elfcpp::R_PPC64_TOC16_DS:
5502 case elfcpp::R_PPC64_TOC16_LO_DS:
5503 // We need a GOT section.
5504 target->got_section(symtab, layout);
5505 break;
5506
5507 case elfcpp::R_POWERPC_GOT_TLSGD16:
5508 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5509 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5510 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5511 {
5512 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5513 if (tls_type == tls::TLSOPT_NONE)
5514 {
5515 Output_data_got_powerpc<size, big_endian>* got
5516 = target->got_section(symtab, layout);
5517 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5518 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5519 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5520 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5521 }
5522 else if (tls_type == tls::TLSOPT_TO_LE)
5523 {
5524 // no GOT relocs needed for Local Exec.
5525 }
5526 else
5527 gold_unreachable();
5528 }
5529 break;
5530
5531 case elfcpp::R_POWERPC_GOT_TLSLD16:
5532 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5533 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5534 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5535 {
5536 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5537 if (tls_type == tls::TLSOPT_NONE)
5538 target->tlsld_got_offset(symtab, layout, object);
5539 else if (tls_type == tls::TLSOPT_TO_LE)
5540 {
5541 // no GOT relocs needed for Local Exec.
5542 if (parameters->options().emit_relocs())
5543 {
5544 Output_section* os = layout->tls_segment()->first_section();
5545 gold_assert(os != NULL);
5546 os->set_needs_symtab_index();
5547 }
5548 }
5549 else
5550 gold_unreachable();
5551 }
5552 break;
5553
5554 case elfcpp::R_POWERPC_GOT_DTPREL16:
5555 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5556 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5557 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5558 {
5559 Output_data_got_powerpc<size, big_endian>* got
5560 = target->got_section(symtab, layout);
5561 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5562 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5563 }
5564 break;
5565
5566 case elfcpp::R_POWERPC_GOT_TPREL16:
5567 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5568 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5569 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5570 {
5571 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5572 if (tls_type == tls::TLSOPT_NONE)
5573 {
5574 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5575 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5576 {
5577 Output_data_got_powerpc<size, big_endian>* got
5578 = target->got_section(symtab, layout);
5579 unsigned int off = got->add_constant(0);
5580 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5581
5582 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5583 rela_dyn->add_symbolless_local_addend(object, r_sym,
5584 elfcpp::R_POWERPC_TPREL,
5585 got, off, 0);
5586 }
5587 }
5588 else if (tls_type == tls::TLSOPT_TO_LE)
5589 {
5590 // no GOT relocs needed for Local Exec.
5591 }
5592 else
5593 gold_unreachable();
5594 }
5595 break;
5596
5597 default:
5598 unsupported_reloc_local(object, r_type);
5599 break;
5600 }
5601
5602 switch (r_type)
5603 {
5604 case elfcpp::R_POWERPC_GOT_TLSLD16:
5605 case elfcpp::R_POWERPC_GOT_TLSGD16:
5606 case elfcpp::R_POWERPC_GOT_TPREL16:
5607 case elfcpp::R_POWERPC_GOT_DTPREL16:
5608 case elfcpp::R_POWERPC_GOT16:
5609 case elfcpp::R_PPC64_GOT16_DS:
5610 case elfcpp::R_PPC64_TOC16:
5611 case elfcpp::R_PPC64_TOC16_DS:
5612 ppc_object->set_has_small_toc_reloc();
5613 default:
5614 break;
5615 }
5616 }
5617
5618 // Report an unsupported relocation against a global symbol.
5619
5620 template<int size, bool big_endian>
5621 void
5622 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5623 Sized_relobj_file<size, big_endian>* object,
5624 unsigned int r_type,
5625 Symbol* gsym)
5626 {
5627 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5628 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5629 }
5630
5631 // Scan a relocation for a global symbol.
5632
5633 template<int size, bool big_endian>
5634 inline void
5635 Target_powerpc<size, big_endian>::Scan::global(
5636 Symbol_table* symtab,
5637 Layout* layout,
5638 Target_powerpc<size, big_endian>* target,
5639 Sized_relobj_file<size, big_endian>* object,
5640 unsigned int data_shndx,
5641 Output_section* output_section,
5642 const elfcpp::Rela<size, big_endian>& reloc,
5643 unsigned int r_type,
5644 Symbol* gsym)
5645 {
5646 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5647 return;
5648
5649 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5650 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5651 {
5652 this->expect_tls_get_addr_call();
5653 const bool final = gsym->final_value_is_known();
5654 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5655 if (tls_type != tls::TLSOPT_NONE)
5656 this->skip_next_tls_get_addr_call();
5657 }
5658 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5659 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5660 {
5661 this->expect_tls_get_addr_call();
5662 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5663 if (tls_type != tls::TLSOPT_NONE)
5664 this->skip_next_tls_get_addr_call();
5665 }
5666
5667 Powerpc_relobj<size, big_endian>* ppc_object
5668 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5669
5670 // A STT_GNU_IFUNC symbol may require a PLT entry.
5671 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5672 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5673 {
5674 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5675 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5676 reloc.get_r_addend());
5677 target->make_plt_entry(symtab, layout, gsym);
5678 }
5679
5680 switch (r_type)
5681 {
5682 case elfcpp::R_POWERPC_NONE:
5683 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5684 case elfcpp::R_POWERPC_GNU_VTENTRY:
5685 case elfcpp::R_PPC_LOCAL24PC:
5686 case elfcpp::R_POWERPC_TLS:
5687 break;
5688
5689 case elfcpp::R_PPC64_TOC:
5690 {
5691 Output_data_got_powerpc<size, big_endian>* got
5692 = target->got_section(symtab, layout);
5693 if (parameters->options().output_is_position_independent())
5694 {
5695 Address off = reloc.get_r_offset();
5696 if (size == 64
5697 && data_shndx == ppc_object->opd_shndx()
5698 && ppc_object->get_opd_discard(off - 8))
5699 break;
5700
5701 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5702 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5703 if (data_shndx != ppc_object->opd_shndx())
5704 symobj = static_cast
5705 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5706 rela_dyn->add_output_section_relative(got->output_section(),
5707 elfcpp::R_POWERPC_RELATIVE,
5708 output_section,
5709 object, data_shndx, off,
5710 symobj->toc_base_offset());
5711 }
5712 }
5713 break;
5714
5715 case elfcpp::R_PPC64_ADDR64:
5716 if (size == 64
5717 && data_shndx == ppc_object->opd_shndx()
5718 && (gsym->is_defined_in_discarded_section()
5719 || gsym->object() != object))
5720 {
5721 ppc_object->set_opd_discard(reloc.get_r_offset());
5722 break;
5723 }
5724 // Fall thru
5725 case elfcpp::R_PPC64_UADDR64:
5726 case elfcpp::R_POWERPC_ADDR32:
5727 case elfcpp::R_POWERPC_UADDR32:
5728 case elfcpp::R_POWERPC_ADDR24:
5729 case elfcpp::R_POWERPC_ADDR16:
5730 case elfcpp::R_POWERPC_ADDR16_LO:
5731 case elfcpp::R_POWERPC_ADDR16_HI:
5732 case elfcpp::R_POWERPC_ADDR16_HA:
5733 case elfcpp::R_POWERPC_UADDR16:
5734 case elfcpp::R_PPC64_ADDR16_HIGH:
5735 case elfcpp::R_PPC64_ADDR16_HIGHA:
5736 case elfcpp::R_PPC64_ADDR16_HIGHER:
5737 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5738 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5739 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5740 case elfcpp::R_PPC64_ADDR16_DS:
5741 case elfcpp::R_PPC64_ADDR16_LO_DS:
5742 case elfcpp::R_POWERPC_ADDR14:
5743 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5744 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5745 {
5746 // Make a PLT entry if necessary.
5747 if (gsym->needs_plt_entry())
5748 {
5749 if (!is_ifunc)
5750 {
5751 target->push_branch(ppc_object, data_shndx,
5752 reloc.get_r_offset(), r_type,
5753 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5754 reloc.get_r_addend());
5755 target->make_plt_entry(symtab, layout, gsym);
5756 }
5757 // Since this is not a PC-relative relocation, we may be
5758 // taking the address of a function. In that case we need to
5759 // set the entry in the dynamic symbol table to the address of
5760 // the PLT call stub.
5761 if (size == 32
5762 && gsym->is_from_dynobj()
5763 && !parameters->options().output_is_position_independent())
5764 gsym->set_needs_dynsym_value();
5765 }
5766 // Make a dynamic relocation if necessary.
5767 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
5768 || (size == 64 && is_ifunc))
5769 {
5770 if (gsym->may_need_copy_reloc())
5771 {
5772 target->copy_reloc(symtab, layout, object,
5773 data_shndx, output_section, gsym, reloc);
5774 }
5775 else if ((size == 32
5776 && r_type == elfcpp::R_POWERPC_ADDR32
5777 && gsym->can_use_relative_reloc(false)
5778 && !(gsym->visibility() == elfcpp::STV_PROTECTED
5779 && parameters->options().shared()))
5780 || (size == 64
5781 && r_type == elfcpp::R_PPC64_ADDR64
5782 && (gsym->can_use_relative_reloc(false)
5783 || data_shndx == ppc_object->opd_shndx())))
5784 {
5785 Reloc_section* rela_dyn
5786 = target->rela_dyn_section(symtab, layout, is_ifunc);
5787 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5788 : elfcpp::R_POWERPC_RELATIVE);
5789 rela_dyn->add_symbolless_global_addend(
5790 gsym, dynrel, output_section, object, data_shndx,
5791 reloc.get_r_offset(), reloc.get_r_addend());
5792 }
5793 else
5794 {
5795 Reloc_section* rela_dyn
5796 = target->rela_dyn_section(symtab, layout, is_ifunc);
5797 check_non_pic(object, r_type);
5798 rela_dyn->add_global(gsym, r_type, output_section,
5799 object, data_shndx,
5800 reloc.get_r_offset(),
5801 reloc.get_r_addend());
5802 }
5803 }
5804 }
5805 break;
5806
5807 case elfcpp::R_PPC_PLTREL24:
5808 case elfcpp::R_POWERPC_REL24:
5809 if (!is_ifunc)
5810 {
5811 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5812 r_type,
5813 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5814 reloc.get_r_addend());
5815 if (gsym->needs_plt_entry()
5816 || (!gsym->final_value_is_known()
5817 && (gsym->is_undefined()
5818 || gsym->is_from_dynobj()
5819 || gsym->is_preemptible())))
5820 target->make_plt_entry(symtab, layout, gsym);
5821 }
5822 // Fall thru
5823
5824 case elfcpp::R_PPC64_REL64:
5825 case elfcpp::R_POWERPC_REL32:
5826 // Make a dynamic relocation if necessary.
5827 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
5828 {
5829 if (gsym->may_need_copy_reloc())
5830 {
5831 target->copy_reloc(symtab, layout, object,
5832 data_shndx, output_section, gsym,
5833 reloc);
5834 }
5835 else
5836 {
5837 Reloc_section* rela_dyn
5838 = target->rela_dyn_section(symtab, layout, is_ifunc);
5839 check_non_pic(object, r_type);
5840 rela_dyn->add_global(gsym, r_type, output_section, object,
5841 data_shndx, reloc.get_r_offset(),
5842 reloc.get_r_addend());
5843 }
5844 }
5845 break;
5846
5847 case elfcpp::R_POWERPC_REL14:
5848 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5849 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5850 if (!is_ifunc)
5851 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5852 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5853 reloc.get_r_addend());
5854 break;
5855
5856 case elfcpp::R_POWERPC_REL16:
5857 case elfcpp::R_POWERPC_REL16_LO:
5858 case elfcpp::R_POWERPC_REL16_HI:
5859 case elfcpp::R_POWERPC_REL16_HA:
5860 case elfcpp::R_POWERPC_SECTOFF:
5861 case elfcpp::R_POWERPC_SECTOFF_LO:
5862 case elfcpp::R_POWERPC_SECTOFF_HI:
5863 case elfcpp::R_POWERPC_SECTOFF_HA:
5864 case elfcpp::R_PPC64_SECTOFF_DS:
5865 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5866 case elfcpp::R_POWERPC_TPREL16:
5867 case elfcpp::R_POWERPC_TPREL16_LO:
5868 case elfcpp::R_POWERPC_TPREL16_HI:
5869 case elfcpp::R_POWERPC_TPREL16_HA:
5870 case elfcpp::R_PPC64_TPREL16_DS:
5871 case elfcpp::R_PPC64_TPREL16_LO_DS:
5872 case elfcpp::R_PPC64_TPREL16_HIGH:
5873 case elfcpp::R_PPC64_TPREL16_HIGHA:
5874 case elfcpp::R_PPC64_TPREL16_HIGHER:
5875 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5876 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5877 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5878 case elfcpp::R_POWERPC_DTPREL16:
5879 case elfcpp::R_POWERPC_DTPREL16_LO:
5880 case elfcpp::R_POWERPC_DTPREL16_HI:
5881 case elfcpp::R_POWERPC_DTPREL16_HA:
5882 case elfcpp::R_PPC64_DTPREL16_DS:
5883 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5884 case elfcpp::R_PPC64_DTPREL16_HIGH:
5885 case elfcpp::R_PPC64_DTPREL16_HIGHA:
5886 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5887 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5888 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5889 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5890 case elfcpp::R_PPC64_TLSGD:
5891 case elfcpp::R_PPC64_TLSLD:
5892 break;
5893
5894 case elfcpp::R_POWERPC_GOT16:
5895 case elfcpp::R_POWERPC_GOT16_LO:
5896 case elfcpp::R_POWERPC_GOT16_HI:
5897 case elfcpp::R_POWERPC_GOT16_HA:
5898 case elfcpp::R_PPC64_GOT16_DS:
5899 case elfcpp::R_PPC64_GOT16_LO_DS:
5900 {
5901 // The symbol requires a GOT entry.
5902 Output_data_got_powerpc<size, big_endian>* got;
5903
5904 got = target->got_section(symtab, layout);
5905 if (gsym->final_value_is_known())
5906 {
5907 if (size == 32 && is_ifunc)
5908 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5909 else
5910 got->add_global(gsym, GOT_TYPE_STANDARD);
5911 }
5912 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
5913 {
5914 // If we are generating a shared object or a pie, this
5915 // symbol's GOT entry will be set by a dynamic relocation.
5916 unsigned int off = got->add_constant(0);
5917 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
5918
5919 Reloc_section* rela_dyn
5920 = target->rela_dyn_section(symtab, layout, is_ifunc);
5921
5922 if (gsym->can_use_relative_reloc(false)
5923 && !(size == 32
5924 && gsym->visibility() == elfcpp::STV_PROTECTED
5925 && parameters->options().shared()))
5926 {
5927 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5928 : elfcpp::R_POWERPC_RELATIVE);
5929 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5930 }
5931 else
5932 {
5933 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5934 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5935 }
5936 }
5937 }
5938 break;
5939
5940 case elfcpp::R_PPC64_TOC16:
5941 case elfcpp::R_PPC64_TOC16_LO:
5942 case elfcpp::R_PPC64_TOC16_HI:
5943 case elfcpp::R_PPC64_TOC16_HA:
5944 case elfcpp::R_PPC64_TOC16_DS:
5945 case elfcpp::R_PPC64_TOC16_LO_DS:
5946 // We need a GOT section.
5947 target->got_section(symtab, layout);
5948 break;
5949
5950 case elfcpp::R_POWERPC_GOT_TLSGD16:
5951 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5952 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5953 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5954 {
5955 const bool final = gsym->final_value_is_known();
5956 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5957 if (tls_type == tls::TLSOPT_NONE)
5958 {
5959 Output_data_got_powerpc<size, big_endian>* got
5960 = target->got_section(symtab, layout);
5961 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5962 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
5963 elfcpp::R_POWERPC_DTPMOD,
5964 elfcpp::R_POWERPC_DTPREL);
5965 }
5966 else if (tls_type == tls::TLSOPT_TO_IE)
5967 {
5968 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5969 {
5970 Output_data_got_powerpc<size, big_endian>* got
5971 = target->got_section(symtab, layout);
5972 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5973 if (gsym->is_undefined()
5974 || gsym->is_from_dynobj())
5975 {
5976 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5977 elfcpp::R_POWERPC_TPREL);
5978 }
5979 else
5980 {
5981 unsigned int off = got->add_constant(0);
5982 gsym->set_got_offset(GOT_TYPE_TPREL, off);
5983 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5984 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5985 got, off, 0);
5986 }
5987 }
5988 }
5989 else if (tls_type == tls::TLSOPT_TO_LE)
5990 {
5991 // no GOT relocs needed for Local Exec.
5992 }
5993 else
5994 gold_unreachable();
5995 }
5996 break;
5997
5998 case elfcpp::R_POWERPC_GOT_TLSLD16:
5999 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6000 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6001 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6002 {
6003 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6004 if (tls_type == tls::TLSOPT_NONE)
6005 target->tlsld_got_offset(symtab, layout, object);
6006 else if (tls_type == tls::TLSOPT_TO_LE)
6007 {
6008 // no GOT relocs needed for Local Exec.
6009 if (parameters->options().emit_relocs())
6010 {
6011 Output_section* os = layout->tls_segment()->first_section();
6012 gold_assert(os != NULL);
6013 os->set_needs_symtab_index();
6014 }
6015 }
6016 else
6017 gold_unreachable();
6018 }
6019 break;
6020
6021 case elfcpp::R_POWERPC_GOT_DTPREL16:
6022 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6023 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6024 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6025 {
6026 Output_data_got_powerpc<size, big_endian>* got
6027 = target->got_section(symtab, layout);
6028 if (!gsym->final_value_is_known()
6029 && (gsym->is_from_dynobj()
6030 || gsym->is_undefined()
6031 || gsym->is_preemptible()))
6032 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6033 target->rela_dyn_section(layout),
6034 elfcpp::R_POWERPC_DTPREL);
6035 else
6036 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6037 }
6038 break;
6039
6040 case elfcpp::R_POWERPC_GOT_TPREL16:
6041 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6042 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6043 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6044 {
6045 const bool final = gsym->final_value_is_known();
6046 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6047 if (tls_type == tls::TLSOPT_NONE)
6048 {
6049 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6050 {
6051 Output_data_got_powerpc<size, big_endian>* got
6052 = target->got_section(symtab, layout);
6053 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6054 if (gsym->is_undefined()
6055 || gsym->is_from_dynobj())
6056 {
6057 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6058 elfcpp::R_POWERPC_TPREL);
6059 }
6060 else
6061 {
6062 unsigned int off = got->add_constant(0);
6063 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6064 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6065 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6066 got, off, 0);
6067 }
6068 }
6069 }
6070 else if (tls_type == tls::TLSOPT_TO_LE)
6071 {
6072 // no GOT relocs needed for Local Exec.
6073 }
6074 else
6075 gold_unreachable();
6076 }
6077 break;
6078
6079 default:
6080 unsupported_reloc_global(object, r_type, gsym);
6081 break;
6082 }
6083
6084 switch (r_type)
6085 {
6086 case elfcpp::R_POWERPC_GOT_TLSLD16:
6087 case elfcpp::R_POWERPC_GOT_TLSGD16:
6088 case elfcpp::R_POWERPC_GOT_TPREL16:
6089 case elfcpp::R_POWERPC_GOT_DTPREL16:
6090 case elfcpp::R_POWERPC_GOT16:
6091 case elfcpp::R_PPC64_GOT16_DS:
6092 case elfcpp::R_PPC64_TOC16:
6093 case elfcpp::R_PPC64_TOC16_DS:
6094 ppc_object->set_has_small_toc_reloc();
6095 default:
6096 break;
6097 }
6098 }
6099
6100 // Process relocations for gc.
6101
6102 template<int size, bool big_endian>
6103 void
6104 Target_powerpc<size, big_endian>::gc_process_relocs(
6105 Symbol_table* symtab,
6106 Layout* layout,
6107 Sized_relobj_file<size, big_endian>* object,
6108 unsigned int data_shndx,
6109 unsigned int,
6110 const unsigned char* prelocs,
6111 size_t reloc_count,
6112 Output_section* output_section,
6113 bool needs_special_offset_handling,
6114 size_t local_symbol_count,
6115 const unsigned char* plocal_symbols)
6116 {
6117 typedef Target_powerpc<size, big_endian> Powerpc;
6118 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6119 Powerpc_relobj<size, big_endian>* ppc_object
6120 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6121 if (size == 64)
6122 ppc_object->set_opd_valid();
6123 if (size == 64 && data_shndx == ppc_object->opd_shndx())
6124 {
6125 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
6126 for (p = ppc_object->access_from_map()->begin();
6127 p != ppc_object->access_from_map()->end();
6128 ++p)
6129 {
6130 Address dst_off = p->first;
6131 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6132 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
6133 for (s = p->second.begin(); s != p->second.end(); ++s)
6134 {
6135 Object* src_obj = s->first;
6136 unsigned int src_indx = s->second;
6137 symtab->gc()->add_reference(src_obj, src_indx,
6138 ppc_object, dst_indx);
6139 }
6140 p->second.clear();
6141 }
6142 ppc_object->access_from_map()->clear();
6143 ppc_object->process_gc_mark(symtab);
6144 // Don't look at .opd relocs as .opd will reference everything.
6145 return;
6146 }
6147
6148 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
6149 typename Target_powerpc::Relocatable_size_for_reloc>(
6150 symtab,
6151 layout,
6152 this,
6153 object,
6154 data_shndx,
6155 prelocs,
6156 reloc_count,
6157 output_section,
6158 needs_special_offset_handling,
6159 local_symbol_count,
6160 plocal_symbols);
6161 }
6162
6163 // Handle target specific gc actions when adding a gc reference from
6164 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6165 // and DST_OFF. For powerpc64, this adds a referenc to the code
6166 // section of a function descriptor.
6167
6168 template<int size, bool big_endian>
6169 void
6170 Target_powerpc<size, big_endian>::do_gc_add_reference(
6171 Symbol_table* symtab,
6172 Object* src_obj,
6173 unsigned int src_shndx,
6174 Object* dst_obj,
6175 unsigned int dst_shndx,
6176 Address dst_off) const
6177 {
6178 if (size != 64 || dst_obj->is_dynamic())
6179 return;
6180
6181 Powerpc_relobj<size, big_endian>* ppc_object
6182 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
6183 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
6184 {
6185 if (ppc_object->opd_valid())
6186 {
6187 dst_shndx = ppc_object->get_opd_ent(dst_off);
6188 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
6189 }
6190 else
6191 {
6192 // If we haven't run scan_opd_relocs, we must delay
6193 // processing this function descriptor reference.
6194 ppc_object->add_reference(src_obj, src_shndx, dst_off);
6195 }
6196 }
6197 }
6198
6199 // Add any special sections for this symbol to the gc work list.
6200 // For powerpc64, this adds the code section of a function
6201 // descriptor.
6202
6203 template<int size, bool big_endian>
6204 void
6205 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
6206 Symbol_table* symtab,
6207 Symbol* sym) const
6208 {
6209 if (size == 64)
6210 {
6211 Powerpc_relobj<size, big_endian>* ppc_object
6212 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
6213 bool is_ordinary;
6214 unsigned int shndx = sym->shndx(&is_ordinary);
6215 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
6216 {
6217 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
6218 Address dst_off = gsym->value();
6219 if (ppc_object->opd_valid())
6220 {
6221 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6222 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
6223 }
6224 else
6225 ppc_object->add_gc_mark(dst_off);
6226 }
6227 }
6228 }
6229
6230 // For a symbol location in .opd, set LOC to the location of the
6231 // function entry.
6232
6233 template<int size, bool big_endian>
6234 void
6235 Target_powerpc<size, big_endian>::do_function_location(
6236 Symbol_location* loc) const
6237 {
6238 if (size == 64 && loc->shndx != 0)
6239 {
6240 if (loc->object->is_dynamic())
6241 {
6242 Powerpc_dynobj<size, big_endian>* ppc_object
6243 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
6244 if (loc->shndx == ppc_object->opd_shndx())
6245 {
6246 Address dest_off;
6247 Address off = loc->offset - ppc_object->opd_address();
6248 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
6249 loc->offset = dest_off;
6250 }
6251 }
6252 else
6253 {
6254 const Powerpc_relobj<size, big_endian>* ppc_object
6255 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
6256 if (loc->shndx == ppc_object->opd_shndx())
6257 {
6258 Address dest_off;
6259 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
6260 loc->offset = dest_off;
6261 }
6262 }
6263 }
6264 }
6265
6266 // Scan relocations for a section.
6267
6268 template<int size, bool big_endian>
6269 void
6270 Target_powerpc<size, big_endian>::scan_relocs(
6271 Symbol_table* symtab,
6272 Layout* layout,
6273 Sized_relobj_file<size, big_endian>* object,
6274 unsigned int data_shndx,
6275 unsigned int sh_type,
6276 const unsigned char* prelocs,
6277 size_t reloc_count,
6278 Output_section* output_section,
6279 bool needs_special_offset_handling,
6280 size_t local_symbol_count,
6281 const unsigned char* plocal_symbols)
6282 {
6283 typedef Target_powerpc<size, big_endian> Powerpc;
6284 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6285
6286 if (sh_type == elfcpp::SHT_REL)
6287 {
6288 gold_error(_("%s: unsupported REL reloc section"),
6289 object->name().c_str());
6290 return;
6291 }
6292
6293 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
6294 symtab,
6295 layout,
6296 this,
6297 object,
6298 data_shndx,
6299 prelocs,
6300 reloc_count,
6301 output_section,
6302 needs_special_offset_handling,
6303 local_symbol_count,
6304 plocal_symbols);
6305 }
6306
6307 // Functor class for processing the global symbol table.
6308 // Removes symbols defined on discarded opd entries.
6309
6310 template<bool big_endian>
6311 class Global_symbol_visitor_opd
6312 {
6313 public:
6314 Global_symbol_visitor_opd()
6315 { }
6316
6317 void
6318 operator()(Sized_symbol<64>* sym)
6319 {
6320 if (sym->has_symtab_index()
6321 || sym->source() != Symbol::FROM_OBJECT
6322 || !sym->in_real_elf())
6323 return;
6324
6325 if (sym->object()->is_dynamic())
6326 return;
6327
6328 Powerpc_relobj<64, big_endian>* symobj
6329 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6330 if (symobj->opd_shndx() == 0)
6331 return;
6332
6333 bool is_ordinary;
6334 unsigned int shndx = sym->shndx(&is_ordinary);
6335 if (shndx == symobj->opd_shndx()
6336 && symobj->get_opd_discard(sym->value()))
6337 sym->set_symtab_index(-1U);
6338 }
6339 };
6340
6341 template<int size, bool big_endian>
6342 void
6343 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6344 Layout* layout,
6345 Symbol_table* symtab)
6346 {
6347 if (size == 64)
6348 {
6349 Output_data_save_res<64, big_endian>* savres
6350 = new Output_data_save_res<64, big_endian>(symtab);
6351 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6352 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6353 savres, ORDER_TEXT, false);
6354 }
6355 }
6356
6357 // Sort linker created .got section first (for the header), then input
6358 // sections belonging to files using small model code.
6359
6360 template<bool big_endian>
6361 class Sort_toc_sections
6362 {
6363 public:
6364 bool
6365 operator()(const Output_section::Input_section& is1,
6366 const Output_section::Input_section& is2) const
6367 {
6368 if (!is1.is_input_section() && is2.is_input_section())
6369 return true;
6370 bool small1
6371 = (is1.is_input_section()
6372 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6373 ->has_small_toc_reloc()));
6374 bool small2
6375 = (is2.is_input_section()
6376 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6377 ->has_small_toc_reloc()));
6378 return small1 && !small2;
6379 }
6380 };
6381
6382 // Finalize the sections.
6383
6384 template<int size, bool big_endian>
6385 void
6386 Target_powerpc<size, big_endian>::do_finalize_sections(
6387 Layout* layout,
6388 const Input_objects*,
6389 Symbol_table* symtab)
6390 {
6391 if (parameters->doing_static_link())
6392 {
6393 // At least some versions of glibc elf-init.o have a strong
6394 // reference to __rela_iplt marker syms. A weak ref would be
6395 // better..
6396 if (this->iplt_ != NULL)
6397 {
6398 Reloc_section* rel = this->iplt_->rel_plt();
6399 symtab->define_in_output_data("__rela_iplt_start", NULL,
6400 Symbol_table::PREDEFINED, rel, 0, 0,
6401 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6402 elfcpp::STV_HIDDEN, 0, false, true);
6403 symtab->define_in_output_data("__rela_iplt_end", NULL,
6404 Symbol_table::PREDEFINED, rel, 0, 0,
6405 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6406 elfcpp::STV_HIDDEN, 0, true, true);
6407 }
6408 else
6409 {
6410 symtab->define_as_constant("__rela_iplt_start", NULL,
6411 Symbol_table::PREDEFINED, 0, 0,
6412 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6413 elfcpp::STV_HIDDEN, 0, true, false);
6414 symtab->define_as_constant("__rela_iplt_end", NULL,
6415 Symbol_table::PREDEFINED, 0, 0,
6416 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6417 elfcpp::STV_HIDDEN, 0, true, false);
6418 }
6419 }
6420
6421 if (size == 64)
6422 {
6423 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6424 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6425
6426 if (!parameters->options().relocatable())
6427 {
6428 this->define_save_restore_funcs(layout, symtab);
6429
6430 // Annoyingly, we need to make these sections now whether or
6431 // not we need them. If we delay until do_relax then we
6432 // need to mess with the relaxation machinery checkpointing.
6433 this->got_section(symtab, layout);
6434 this->make_brlt_section(layout);
6435
6436 if (parameters->options().toc_sort())
6437 {
6438 Output_section* os = this->got_->output_section();
6439 if (os != NULL && os->input_sections().size() > 1)
6440 std::stable_sort(os->input_sections().begin(),
6441 os->input_sections().end(),
6442 Sort_toc_sections<big_endian>());
6443 }
6444 }
6445 }
6446
6447 // Fill in some more dynamic tags.
6448 Output_data_dynamic* odyn = layout->dynamic_data();
6449 if (odyn != NULL)
6450 {
6451 const Reloc_section* rel_plt = (this->plt_ == NULL
6452 ? NULL
6453 : this->plt_->rel_plt());
6454 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6455 this->rela_dyn_, true, size == 32);
6456
6457 if (size == 32)
6458 {
6459 if (this->got_ != NULL)
6460 {
6461 this->got_->finalize_data_size();
6462 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6463 this->got_, this->got_->g_o_t());
6464 }
6465 }
6466 else
6467 {
6468 if (this->glink_ != NULL)
6469 {
6470 this->glink_->finalize_data_size();
6471 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6472 this->glink_,
6473 (this->glink_->pltresolve_size
6474 - 32));
6475 }
6476 }
6477 }
6478
6479 // Emit any relocs we saved in an attempt to avoid generating COPY
6480 // relocs.
6481 if (this->copy_relocs_.any_saved_relocs())
6482 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6483 }
6484
6485 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6486 // reloc.
6487
6488 static bool
6489 ok_lo_toc_insn(uint32_t insn)
6490 {
6491 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6492 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6493 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6494 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6495 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6496 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6497 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6498 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6499 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6500 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6501 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6502 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6503 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6504 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6505 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6506 && (insn & 3) != 1)
6507 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6508 && ((insn & 3) == 0 || (insn & 3) == 3))
6509 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6510 }
6511
6512 // Return the value to use for a branch relocation.
6513
6514 template<int size, bool big_endian>
6515 typename Target_powerpc<size, big_endian>::Address
6516 Target_powerpc<size, big_endian>::symval_for_branch(
6517 const Symbol_table* symtab,
6518 Address value,
6519 const Sized_symbol<size>* gsym,
6520 Powerpc_relobj<size, big_endian>* object,
6521 unsigned int *dest_shndx)
6522 {
6523 *dest_shndx = 0;
6524 if (size == 32)
6525 return value;
6526
6527 // If the symbol is defined in an opd section, ie. is a function
6528 // descriptor, use the function descriptor code entry address
6529 Powerpc_relobj<size, big_endian>* symobj = object;
6530 if (gsym != NULL
6531 && gsym->source() != Symbol::FROM_OBJECT)
6532 return value;
6533 if (gsym != NULL)
6534 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6535 unsigned int shndx = symobj->opd_shndx();
6536 if (shndx == 0)
6537 return value;
6538 Address opd_addr = symobj->get_output_section_offset(shndx);
6539 if (opd_addr == invalid_address)
6540 return value;
6541 opd_addr += symobj->output_section_address(shndx);
6542 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6543 {
6544 Address sec_off;
6545 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6546 if (symtab->is_section_folded(symobj, *dest_shndx))
6547 {
6548 Section_id folded
6549 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6550 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6551 *dest_shndx = folded.second;
6552 }
6553 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6554 gold_assert(sec_addr != invalid_address);
6555 sec_addr += symobj->output_section(*dest_shndx)->address();
6556 value = sec_addr + sec_off;
6557 }
6558 return value;
6559 }
6560
6561 // Perform a relocation.
6562
6563 template<int size, bool big_endian>
6564 inline bool
6565 Target_powerpc<size, big_endian>::Relocate::relocate(
6566 const Relocate_info<size, big_endian>* relinfo,
6567 Target_powerpc* target,
6568 Output_section* os,
6569 size_t relnum,
6570 const elfcpp::Rela<size, big_endian>& rela,
6571 unsigned int r_type,
6572 const Sized_symbol<size>* gsym,
6573 const Symbol_value<size>* psymval,
6574 unsigned char* view,
6575 Address address,
6576 section_size_type view_size)
6577 {
6578 if (view == NULL)
6579 return true;
6580
6581 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6582 {
6583 case Track_tls::NOT_EXPECTED:
6584 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6585 _("__tls_get_addr call lacks marker reloc"));
6586 break;
6587 case Track_tls::EXPECTED:
6588 // We have already complained.
6589 break;
6590 case Track_tls::SKIP:
6591 return true;
6592 case Track_tls::NORMAL:
6593 break;
6594 }
6595
6596 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6597 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6598 Powerpc_relobj<size, big_endian>* const object
6599 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6600 Address value = 0;
6601 bool has_plt_value = false;
6602 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6603 if ((gsym != NULL
6604 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
6605 : object->local_has_plt_offset(r_sym))
6606 && (!psymval->is_ifunc_symbol()
6607 || Scan::reloc_needs_plt_for_ifunc(object, r_type, false)))
6608 {
6609 Stub_table<size, big_endian>* stub_table
6610 = object->stub_table(relinfo->data_shndx);
6611 if (stub_table == NULL)
6612 {
6613 // This is a ref from a data section to an ifunc symbol.
6614 if (target->stub_tables().size() != 0)
6615 stub_table = target->stub_tables()[0];
6616 }
6617 gold_assert(stub_table != NULL);
6618 Address off;
6619 if (gsym != NULL)
6620 off = stub_table->find_plt_call_entry(object, gsym, r_type,
6621 rela.get_r_addend());
6622 else
6623 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6624 rela.get_r_addend());
6625 gold_assert(off != invalid_address);
6626 value = stub_table->stub_address() + off;
6627 has_plt_value = true;
6628 }
6629
6630 if (r_type == elfcpp::R_POWERPC_GOT16
6631 || r_type == elfcpp::R_POWERPC_GOT16_LO
6632 || r_type == elfcpp::R_POWERPC_GOT16_HI
6633 || r_type == elfcpp::R_POWERPC_GOT16_HA
6634 || r_type == elfcpp::R_PPC64_GOT16_DS
6635 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6636 {
6637 if (gsym != NULL)
6638 {
6639 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6640 value = gsym->got_offset(GOT_TYPE_STANDARD);
6641 }
6642 else
6643 {
6644 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6645 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6646 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6647 }
6648 value -= target->got_section()->got_base_offset(object);
6649 }
6650 else if (r_type == elfcpp::R_PPC64_TOC)
6651 {
6652 value = (target->got_section()->output_section()->address()
6653 + object->toc_base_offset());
6654 }
6655 else if (gsym != NULL
6656 && (r_type == elfcpp::R_POWERPC_REL24
6657 || r_type == elfcpp::R_PPC_PLTREL24)
6658 && has_plt_value)
6659 {
6660 if (size == 64)
6661 {
6662 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6663 Valtype* wv = reinterpret_cast<Valtype*>(view);
6664 bool can_plt_call = false;
6665 if (rela.get_r_offset() + 8 <= view_size)
6666 {
6667 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6668 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6669 if ((insn & 1) != 0
6670 && (insn2 == nop
6671 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6672 {
6673 elfcpp::Swap<32, big_endian>::
6674 writeval(wv + 1, ld_2_1 + target->stk_toc());
6675 can_plt_call = true;
6676 }
6677 }
6678 if (!can_plt_call)
6679 {
6680 // If we don't have a branch and link followed by a nop,
6681 // we can't go via the plt because there is no place to
6682 // put a toc restoring instruction.
6683 // Unless we know we won't be returning.
6684 if (strcmp(gsym->name(), "__libc_start_main") == 0)
6685 can_plt_call = true;
6686 }
6687 if (!can_plt_call)
6688 {
6689 // g++ as of 20130507 emits self-calls without a
6690 // following nop. This is arguably wrong since we have
6691 // conflicting information. On the one hand a global
6692 // symbol and on the other a local call sequence, but
6693 // don't error for this special case.
6694 // It isn't possible to cheaply verify we have exactly
6695 // such a call. Allow all calls to the same section.
6696 bool ok = false;
6697 Address code = value;
6698 if (gsym->source() == Symbol::FROM_OBJECT
6699 && gsym->object() == object)
6700 {
6701 Address addend = rela.get_r_addend();
6702 unsigned int dest_shndx;
6703 Address opdent = psymval->value(object, addend);
6704 code = target->symval_for_branch(relinfo->symtab, opdent,
6705 gsym, object, &dest_shndx);
6706 bool is_ordinary;
6707 if (dest_shndx == 0)
6708 dest_shndx = gsym->shndx(&is_ordinary);
6709 ok = dest_shndx == relinfo->data_shndx;
6710 }
6711 if (!ok)
6712 {
6713 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6714 _("call lacks nop, can't restore toc; "
6715 "recompile with -fPIC"));
6716 value = code;
6717 }
6718 }
6719 }
6720 }
6721 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6722 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6723 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6724 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6725 {
6726 // First instruction of a global dynamic sequence, arg setup insn.
6727 const bool final = gsym == NULL || gsym->final_value_is_known();
6728 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6729 enum Got_type got_type = GOT_TYPE_STANDARD;
6730 if (tls_type == tls::TLSOPT_NONE)
6731 got_type = GOT_TYPE_TLSGD;
6732 else if (tls_type == tls::TLSOPT_TO_IE)
6733 got_type = GOT_TYPE_TPREL;
6734 if (got_type != GOT_TYPE_STANDARD)
6735 {
6736 if (gsym != NULL)
6737 {
6738 gold_assert(gsym->has_got_offset(got_type));
6739 value = gsym->got_offset(got_type);
6740 }
6741 else
6742 {
6743 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6744 gold_assert(object->local_has_got_offset(r_sym, got_type));
6745 value = object->local_got_offset(r_sym, got_type);
6746 }
6747 value -= target->got_section()->got_base_offset(object);
6748 }
6749 if (tls_type == tls::TLSOPT_TO_IE)
6750 {
6751 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6752 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6753 {
6754 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6755 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6756 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6757 if (size == 32)
6758 insn |= 32 << 26; // lwz
6759 else
6760 insn |= 58 << 26; // ld
6761 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6762 }
6763 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6764 - elfcpp::R_POWERPC_GOT_TLSGD16);
6765 }
6766 else if (tls_type == tls::TLSOPT_TO_LE)
6767 {
6768 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6769 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6770 {
6771 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6772 Insn insn = addis_3_13;
6773 if (size == 32)
6774 insn = addis_3_2;
6775 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6776 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6777 value = psymval->value(object, rela.get_r_addend());
6778 }
6779 else
6780 {
6781 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6782 Insn insn = nop;
6783 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6784 r_type = elfcpp::R_POWERPC_NONE;
6785 }
6786 }
6787 }
6788 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6789 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6790 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6791 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6792 {
6793 // First instruction of a local dynamic sequence, arg setup insn.
6794 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6795 if (tls_type == tls::TLSOPT_NONE)
6796 {
6797 value = target->tlsld_got_offset();
6798 value -= target->got_section()->got_base_offset(object);
6799 }
6800 else
6801 {
6802 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6803 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6804 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6805 {
6806 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6807 Insn insn = addis_3_13;
6808 if (size == 32)
6809 insn = addis_3_2;
6810 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6811 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6812 value = dtp_offset;
6813 }
6814 else
6815 {
6816 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6817 Insn insn = nop;
6818 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6819 r_type = elfcpp::R_POWERPC_NONE;
6820 }
6821 }
6822 }
6823 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6824 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6825 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6826 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6827 {
6828 // Accesses relative to a local dynamic sequence address,
6829 // no optimisation here.
6830 if (gsym != NULL)
6831 {
6832 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6833 value = gsym->got_offset(GOT_TYPE_DTPREL);
6834 }
6835 else
6836 {
6837 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6838 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6839 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6840 }
6841 value -= target->got_section()->got_base_offset(object);
6842 }
6843 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6844 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6845 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6846 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6847 {
6848 // First instruction of initial exec sequence.
6849 const bool final = gsym == NULL || gsym->final_value_is_known();
6850 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6851 if (tls_type == tls::TLSOPT_NONE)
6852 {
6853 if (gsym != NULL)
6854 {
6855 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
6856 value = gsym->got_offset(GOT_TYPE_TPREL);
6857 }
6858 else
6859 {
6860 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6861 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
6862 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
6863 }
6864 value -= target->got_section()->got_base_offset(object);
6865 }
6866 else
6867 {
6868 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6869 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6870 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6871 {
6872 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6873 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6874 insn &= (1 << 26) - (1 << 21); // extract rt from ld
6875 if (size == 32)
6876 insn |= addis_0_2;
6877 else
6878 insn |= addis_0_13;
6879 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6880 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6881 value = psymval->value(object, rela.get_r_addend());
6882 }
6883 else
6884 {
6885 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6886 Insn insn = nop;
6887 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6888 r_type = elfcpp::R_POWERPC_NONE;
6889 }
6890 }
6891 }
6892 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6893 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6894 {
6895 // Second instruction of a global dynamic sequence,
6896 // the __tls_get_addr call
6897 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6898 const bool final = gsym == NULL || gsym->final_value_is_known();
6899 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6900 if (tls_type != tls::TLSOPT_NONE)
6901 {
6902 if (tls_type == tls::TLSOPT_TO_IE)
6903 {
6904 Insn* iview = reinterpret_cast<Insn*>(view);
6905 Insn insn = add_3_3_13;
6906 if (size == 32)
6907 insn = add_3_3_2;
6908 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6909 r_type = elfcpp::R_POWERPC_NONE;
6910 }
6911 else
6912 {
6913 Insn* iview = reinterpret_cast<Insn*>(view);
6914 Insn insn = addi_3_3;
6915 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6916 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6917 view += 2 * big_endian;
6918 value = psymval->value(object, rela.get_r_addend());
6919 }
6920 this->skip_next_tls_get_addr_call();
6921 }
6922 }
6923 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6924 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6925 {
6926 // Second instruction of a local dynamic sequence,
6927 // the __tls_get_addr call
6928 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6929 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6930 if (tls_type == tls::TLSOPT_TO_LE)
6931 {
6932 Insn* iview = reinterpret_cast<Insn*>(view);
6933 Insn insn = addi_3_3;
6934 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6935 this->skip_next_tls_get_addr_call();
6936 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6937 view += 2 * big_endian;
6938 value = dtp_offset;
6939 }
6940 }
6941 else if (r_type == elfcpp::R_POWERPC_TLS)
6942 {
6943 // Second instruction of an initial exec sequence
6944 const bool final = gsym == NULL || gsym->final_value_is_known();
6945 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6946 if (tls_type == tls::TLSOPT_TO_LE)
6947 {
6948 Insn* iview = reinterpret_cast<Insn*>(view);
6949 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6950 unsigned int reg = size == 32 ? 2 : 13;
6951 insn = at_tls_transform(insn, reg);
6952 gold_assert(insn != 0);
6953 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6954 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6955 view += 2 * big_endian;
6956 value = psymval->value(object, rela.get_r_addend());
6957 }
6958 }
6959 else if (!has_plt_value)
6960 {
6961 Address addend = 0;
6962 unsigned int dest_shndx;
6963 if (r_type != elfcpp::R_PPC_PLTREL24)
6964 addend = rela.get_r_addend();
6965 value = psymval->value(object, addend);
6966 if (gsym != NULL)
6967 value += object->ppc64_local_entry_offset(gsym);
6968 else
6969 value += object->ppc64_local_entry_offset(r_sym);
6970 if (size == 64 && is_branch_reloc(r_type))
6971 value = target->symval_for_branch(relinfo->symtab, value,
6972 gsym, object, &dest_shndx);
6973 unsigned int max_branch_offset = 0;
6974 if (r_type == elfcpp::R_POWERPC_REL24
6975 || r_type == elfcpp::R_PPC_PLTREL24
6976 || r_type == elfcpp::R_PPC_LOCAL24PC)
6977 max_branch_offset = 1 << 25;
6978 else if (r_type == elfcpp::R_POWERPC_REL14
6979 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
6980 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
6981 max_branch_offset = 1 << 15;
6982 if (max_branch_offset != 0
6983 && value - address + max_branch_offset >= 2 * max_branch_offset)
6984 {
6985 Stub_table<size, big_endian>* stub_table
6986 = object->stub_table(relinfo->data_shndx);
6987 if (stub_table != NULL)
6988 {
6989 Address off = stub_table->find_long_branch_entry(object, value);
6990 if (off != invalid_address)
6991 value = (stub_table->stub_address() + stub_table->plt_size()
6992 + off);
6993 }
6994 }
6995 }
6996
6997 switch (r_type)
6998 {
6999 case elfcpp::R_PPC64_REL64:
7000 case elfcpp::R_POWERPC_REL32:
7001 case elfcpp::R_POWERPC_REL24:
7002 case elfcpp::R_PPC_PLTREL24:
7003 case elfcpp::R_PPC_LOCAL24PC:
7004 case elfcpp::R_POWERPC_REL16:
7005 case elfcpp::R_POWERPC_REL16_LO:
7006 case elfcpp::R_POWERPC_REL16_HI:
7007 case elfcpp::R_POWERPC_REL16_HA:
7008 case elfcpp::R_POWERPC_REL14:
7009 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7010 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7011 value -= address;
7012 break;
7013
7014 case elfcpp::R_PPC64_TOC16:
7015 case elfcpp::R_PPC64_TOC16_LO:
7016 case elfcpp::R_PPC64_TOC16_HI:
7017 case elfcpp::R_PPC64_TOC16_HA:
7018 case elfcpp::R_PPC64_TOC16_DS:
7019 case elfcpp::R_PPC64_TOC16_LO_DS:
7020 // Subtract the TOC base address.
7021 value -= (target->got_section()->output_section()->address()
7022 + object->toc_base_offset());
7023 break;
7024
7025 case elfcpp::R_POWERPC_SECTOFF:
7026 case elfcpp::R_POWERPC_SECTOFF_LO:
7027 case elfcpp::R_POWERPC_SECTOFF_HI:
7028 case elfcpp::R_POWERPC_SECTOFF_HA:
7029 case elfcpp::R_PPC64_SECTOFF_DS:
7030 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7031 if (os != NULL)
7032 value -= os->address();
7033 break;
7034
7035 case elfcpp::R_PPC64_TPREL16_DS:
7036 case elfcpp::R_PPC64_TPREL16_LO_DS:
7037 case elfcpp::R_PPC64_TPREL16_HIGH:
7038 case elfcpp::R_PPC64_TPREL16_HIGHA:
7039 if (size != 64)
7040 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7041 break;
7042 case elfcpp::R_POWERPC_TPREL16:
7043 case elfcpp::R_POWERPC_TPREL16_LO:
7044 case elfcpp::R_POWERPC_TPREL16_HI:
7045 case elfcpp::R_POWERPC_TPREL16_HA:
7046 case elfcpp::R_POWERPC_TPREL:
7047 case elfcpp::R_PPC64_TPREL16_HIGHER:
7048 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7049 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7050 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7051 // tls symbol values are relative to tls_segment()->vaddr()
7052 value -= tp_offset;
7053 break;
7054
7055 case elfcpp::R_PPC64_DTPREL16_DS:
7056 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7057 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7058 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7059 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7060 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7061 if (size != 64)
7062 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7063 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7064 break;
7065 case elfcpp::R_POWERPC_DTPREL16:
7066 case elfcpp::R_POWERPC_DTPREL16_LO:
7067 case elfcpp::R_POWERPC_DTPREL16_HI:
7068 case elfcpp::R_POWERPC_DTPREL16_HA:
7069 case elfcpp::R_POWERPC_DTPREL:
7070 case elfcpp::R_PPC64_DTPREL16_HIGH:
7071 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7072 // tls symbol values are relative to tls_segment()->vaddr()
7073 value -= dtp_offset;
7074 break;
7075
7076 default:
7077 break;
7078 }
7079
7080 Insn branch_bit = 0;
7081 switch (r_type)
7082 {
7083 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7084 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7085 branch_bit = 1 << 21;
7086 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7087 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7088 {
7089 Insn* iview = reinterpret_cast<Insn*>(view);
7090 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7091 insn &= ~(1 << 21);
7092 insn |= branch_bit;
7093 if (this->is_isa_v2)
7094 {
7095 // Set 'a' bit. This is 0b00010 in BO field for branch
7096 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7097 // for branch on CTR insns (BO == 1a00t or 1a01t).
7098 if ((insn & (0x14 << 21)) == (0x04 << 21))
7099 insn |= 0x02 << 21;
7100 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7101 insn |= 0x08 << 21;
7102 else
7103 break;
7104 }
7105 else
7106 {
7107 // Invert 'y' bit if not the default.
7108 if (static_cast<Signed_address>(value) < 0)
7109 insn ^= 1 << 21;
7110 }
7111 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7112 }
7113 break;
7114
7115 default:
7116 break;
7117 }
7118
7119 if (size == 64)
7120 {
7121 // Multi-instruction sequences that access the TOC can be
7122 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7123 // to nop; addi rb,r2,x;
7124 switch (r_type)
7125 {
7126 default:
7127 break;
7128
7129 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7130 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7131 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7132 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7133 case elfcpp::R_POWERPC_GOT16_HA:
7134 case elfcpp::R_PPC64_TOC16_HA:
7135 if (parameters->options().toc_optimize())
7136 {
7137 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7138 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7139 if ((insn & ((0x3f << 26) | 0x1f << 16))
7140 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7141 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7142 _("toc optimization is not supported "
7143 "for %#08x instruction"), insn);
7144 else if (value + 0x8000 < 0x10000)
7145 {
7146 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
7147 return true;
7148 }
7149 }
7150 break;
7151
7152 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7153 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7154 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7155 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7156 case elfcpp::R_POWERPC_GOT16_LO:
7157 case elfcpp::R_PPC64_GOT16_LO_DS:
7158 case elfcpp::R_PPC64_TOC16_LO:
7159 case elfcpp::R_PPC64_TOC16_LO_DS:
7160 if (parameters->options().toc_optimize())
7161 {
7162 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7163 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7164 if (!ok_lo_toc_insn(insn))
7165 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7166 _("toc optimization is not supported "
7167 "for %#08x instruction"), insn);
7168 else if (value + 0x8000 < 0x10000)
7169 {
7170 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
7171 {
7172 // Transform addic to addi when we change reg.
7173 insn &= ~((0x3f << 26) | (0x1f << 16));
7174 insn |= (14u << 26) | (2 << 16);
7175 }
7176 else
7177 {
7178 insn &= ~(0x1f << 16);
7179 insn |= 2 << 16;
7180 }
7181 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7182 }
7183 }
7184 break;
7185 }
7186 }
7187
7188 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
7189 switch (r_type)
7190 {
7191 case elfcpp::R_POWERPC_ADDR32:
7192 case elfcpp::R_POWERPC_UADDR32:
7193 if (size == 64)
7194 overflow = Reloc::CHECK_BITFIELD;
7195 break;
7196
7197 case elfcpp::R_POWERPC_REL32:
7198 if (size == 64)
7199 overflow = Reloc::CHECK_SIGNED;
7200 break;
7201
7202 case elfcpp::R_POWERPC_ADDR24:
7203 case elfcpp::R_POWERPC_ADDR16:
7204 case elfcpp::R_POWERPC_UADDR16:
7205 case elfcpp::R_PPC64_ADDR16_DS:
7206 case elfcpp::R_POWERPC_ADDR14:
7207 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7208 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7209 overflow = Reloc::CHECK_BITFIELD;
7210 break;
7211
7212 case elfcpp::R_POWERPC_ADDR16_HI:
7213 case elfcpp::R_POWERPC_ADDR16_HA:
7214 case elfcpp::R_POWERPC_GOT16_HI:
7215 case elfcpp::R_POWERPC_GOT16_HA:
7216 case elfcpp::R_POWERPC_PLT16_HI:
7217 case elfcpp::R_POWERPC_PLT16_HA:
7218 case elfcpp::R_POWERPC_SECTOFF_HI:
7219 case elfcpp::R_POWERPC_SECTOFF_HA:
7220 case elfcpp::R_PPC64_TOC16_HI:
7221 case elfcpp::R_PPC64_TOC16_HA:
7222 case elfcpp::R_PPC64_PLTGOT16_HI:
7223 case elfcpp::R_PPC64_PLTGOT16_HA:
7224 case elfcpp::R_POWERPC_TPREL16_HI:
7225 case elfcpp::R_POWERPC_TPREL16_HA:
7226 case elfcpp::R_POWERPC_DTPREL16_HI:
7227 case elfcpp::R_POWERPC_DTPREL16_HA:
7228 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7229 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7230 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7231 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7232 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7233 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7234 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7235 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7236 case elfcpp::R_POWERPC_REL16_HI:
7237 case elfcpp::R_POWERPC_REL16_HA:
7238 if (size == 32)
7239 break;
7240 case elfcpp::R_POWERPC_REL24:
7241 case elfcpp::R_PPC_PLTREL24:
7242 case elfcpp::R_PPC_LOCAL24PC:
7243 case elfcpp::R_POWERPC_REL16:
7244 case elfcpp::R_PPC64_TOC16:
7245 case elfcpp::R_POWERPC_GOT16:
7246 case elfcpp::R_POWERPC_SECTOFF:
7247 case elfcpp::R_POWERPC_TPREL16:
7248 case elfcpp::R_POWERPC_DTPREL16:
7249 case elfcpp::R_PPC64_TPREL16_DS:
7250 case elfcpp::R_PPC64_DTPREL16_DS:
7251 case elfcpp::R_PPC64_TOC16_DS:
7252 case elfcpp::R_PPC64_GOT16_DS:
7253 case elfcpp::R_PPC64_SECTOFF_DS:
7254 case elfcpp::R_POWERPC_REL14:
7255 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7256 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7257 case elfcpp::R_POWERPC_GOT_TLSGD16:
7258 case elfcpp::R_POWERPC_GOT_TLSLD16:
7259 case elfcpp::R_POWERPC_GOT_TPREL16:
7260 case elfcpp::R_POWERPC_GOT_DTPREL16:
7261 overflow = Reloc::CHECK_SIGNED;
7262 break;
7263 }
7264
7265 typename Powerpc_relocate_functions<size, big_endian>::Status status
7266 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
7267 switch (r_type)
7268 {
7269 case elfcpp::R_POWERPC_NONE:
7270 case elfcpp::R_POWERPC_TLS:
7271 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7272 case elfcpp::R_POWERPC_GNU_VTENTRY:
7273 break;
7274
7275 case elfcpp::R_PPC64_ADDR64:
7276 case elfcpp::R_PPC64_REL64:
7277 case elfcpp::R_PPC64_TOC:
7278 Reloc::addr64(view, value);
7279 break;
7280
7281 case elfcpp::R_POWERPC_TPREL:
7282 case elfcpp::R_POWERPC_DTPREL:
7283 if (size == 64)
7284 Reloc::addr64(view, value);
7285 else
7286 status = Reloc::addr32(view, value, overflow);
7287 break;
7288
7289 case elfcpp::R_PPC64_UADDR64:
7290 Reloc::addr64_u(view, value);
7291 break;
7292
7293 case elfcpp::R_POWERPC_ADDR32:
7294 status = Reloc::addr32(view, value, overflow);
7295 break;
7296
7297 case elfcpp::R_POWERPC_REL32:
7298 case elfcpp::R_POWERPC_UADDR32:
7299 status = Reloc::addr32_u(view, value, overflow);
7300 break;
7301
7302 case elfcpp::R_POWERPC_ADDR24:
7303 case elfcpp::R_POWERPC_REL24:
7304 case elfcpp::R_PPC_PLTREL24:
7305 case elfcpp::R_PPC_LOCAL24PC:
7306 status = Reloc::addr24(view, value, overflow);
7307 break;
7308
7309 case elfcpp::R_POWERPC_GOT_DTPREL16:
7310 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7311 if (size == 64)
7312 {
7313 status = Reloc::addr16_ds(view, value, overflow);
7314 break;
7315 }
7316 case elfcpp::R_POWERPC_ADDR16:
7317 case elfcpp::R_POWERPC_REL16:
7318 case elfcpp::R_PPC64_TOC16:
7319 case elfcpp::R_POWERPC_GOT16:
7320 case elfcpp::R_POWERPC_SECTOFF:
7321 case elfcpp::R_POWERPC_TPREL16:
7322 case elfcpp::R_POWERPC_DTPREL16:
7323 case elfcpp::R_POWERPC_GOT_TLSGD16:
7324 case elfcpp::R_POWERPC_GOT_TLSLD16:
7325 case elfcpp::R_POWERPC_GOT_TPREL16:
7326 case elfcpp::R_POWERPC_ADDR16_LO:
7327 case elfcpp::R_POWERPC_REL16_LO:
7328 case elfcpp::R_PPC64_TOC16_LO:
7329 case elfcpp::R_POWERPC_GOT16_LO:
7330 case elfcpp::R_POWERPC_SECTOFF_LO:
7331 case elfcpp::R_POWERPC_TPREL16_LO:
7332 case elfcpp::R_POWERPC_DTPREL16_LO:
7333 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7334 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7335 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7336 status = Reloc::addr16(view, value, overflow);
7337 break;
7338
7339 case elfcpp::R_POWERPC_UADDR16:
7340 status = Reloc::addr16_u(view, value, overflow);
7341 break;
7342
7343 case elfcpp::R_PPC64_ADDR16_HIGH:
7344 case elfcpp::R_PPC64_TPREL16_HIGH:
7345 case elfcpp::R_PPC64_DTPREL16_HIGH:
7346 if (size == 32)
7347 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7348 goto unsupp;
7349 case elfcpp::R_POWERPC_ADDR16_HI:
7350 case elfcpp::R_POWERPC_REL16_HI:
7351 case elfcpp::R_PPC64_TOC16_HI:
7352 case elfcpp::R_POWERPC_GOT16_HI:
7353 case elfcpp::R_POWERPC_SECTOFF_HI:
7354 case elfcpp::R_POWERPC_TPREL16_HI:
7355 case elfcpp::R_POWERPC_DTPREL16_HI:
7356 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7357 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7358 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7359 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7360 Reloc::addr16_hi(view, value);
7361 break;
7362
7363 case elfcpp::R_PPC64_ADDR16_HIGHA:
7364 case elfcpp::R_PPC64_TPREL16_HIGHA:
7365 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7366 if (size == 32)
7367 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7368 goto unsupp;
7369 case elfcpp::R_POWERPC_ADDR16_HA:
7370 case elfcpp::R_POWERPC_REL16_HA:
7371 case elfcpp::R_PPC64_TOC16_HA:
7372 case elfcpp::R_POWERPC_GOT16_HA:
7373 case elfcpp::R_POWERPC_SECTOFF_HA:
7374 case elfcpp::R_POWERPC_TPREL16_HA:
7375 case elfcpp::R_POWERPC_DTPREL16_HA:
7376 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7377 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7378 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7379 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7380 Reloc::addr16_ha(view, value);
7381 break;
7382
7383 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7384 if (size == 32)
7385 // R_PPC_EMB_NADDR16_LO
7386 goto unsupp;
7387 case elfcpp::R_PPC64_ADDR16_HIGHER:
7388 case elfcpp::R_PPC64_TPREL16_HIGHER:
7389 Reloc::addr16_hi2(view, value);
7390 break;
7391
7392 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7393 if (size == 32)
7394 // R_PPC_EMB_NADDR16_HI
7395 goto unsupp;
7396 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7397 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7398 Reloc::addr16_ha2(view, value);
7399 break;
7400
7401 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7402 if (size == 32)
7403 // R_PPC_EMB_NADDR16_HA
7404 goto unsupp;
7405 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7406 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7407 Reloc::addr16_hi3(view, value);
7408 break;
7409
7410 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7411 if (size == 32)
7412 // R_PPC_EMB_SDAI16
7413 goto unsupp;
7414 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7415 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7416 Reloc::addr16_ha3(view, value);
7417 break;
7418
7419 case elfcpp::R_PPC64_DTPREL16_DS:
7420 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7421 if (size == 32)
7422 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7423 goto unsupp;
7424 case elfcpp::R_PPC64_TPREL16_DS:
7425 case elfcpp::R_PPC64_TPREL16_LO_DS:
7426 if (size == 32)
7427 // R_PPC_TLSGD, R_PPC_TLSLD
7428 break;
7429 case elfcpp::R_PPC64_ADDR16_DS:
7430 case elfcpp::R_PPC64_ADDR16_LO_DS:
7431 case elfcpp::R_PPC64_TOC16_DS:
7432 case elfcpp::R_PPC64_TOC16_LO_DS:
7433 case elfcpp::R_PPC64_GOT16_DS:
7434 case elfcpp::R_PPC64_GOT16_LO_DS:
7435 case elfcpp::R_PPC64_SECTOFF_DS:
7436 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7437 status = Reloc::addr16_ds(view, value, overflow);
7438 break;
7439
7440 case elfcpp::R_POWERPC_ADDR14:
7441 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7442 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7443 case elfcpp::R_POWERPC_REL14:
7444 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7445 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7446 status = Reloc::addr14(view, value, overflow);
7447 break;
7448
7449 case elfcpp::R_POWERPC_COPY:
7450 case elfcpp::R_POWERPC_GLOB_DAT:
7451 case elfcpp::R_POWERPC_JMP_SLOT:
7452 case elfcpp::R_POWERPC_RELATIVE:
7453 case elfcpp::R_POWERPC_DTPMOD:
7454 case elfcpp::R_PPC64_JMP_IREL:
7455 case elfcpp::R_POWERPC_IRELATIVE:
7456 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7457 _("unexpected reloc %u in object file"),
7458 r_type);
7459 break;
7460
7461 case elfcpp::R_PPC_EMB_SDA21:
7462 if (size == 32)
7463 goto unsupp;
7464 else
7465 {
7466 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7467 }
7468 break;
7469
7470 case elfcpp::R_PPC_EMB_SDA2I16:
7471 case elfcpp::R_PPC_EMB_SDA2REL:
7472 if (size == 32)
7473 goto unsupp;
7474 // R_PPC64_TLSGD, R_PPC64_TLSLD
7475 break;
7476
7477 case elfcpp::R_POWERPC_PLT32:
7478 case elfcpp::R_POWERPC_PLTREL32:
7479 case elfcpp::R_POWERPC_PLT16_LO:
7480 case elfcpp::R_POWERPC_PLT16_HI:
7481 case elfcpp::R_POWERPC_PLT16_HA:
7482 case elfcpp::R_PPC_SDAREL16:
7483 case elfcpp::R_POWERPC_ADDR30:
7484 case elfcpp::R_PPC64_PLT64:
7485 case elfcpp::R_PPC64_PLTREL64:
7486 case elfcpp::R_PPC64_PLTGOT16:
7487 case elfcpp::R_PPC64_PLTGOT16_LO:
7488 case elfcpp::R_PPC64_PLTGOT16_HI:
7489 case elfcpp::R_PPC64_PLTGOT16_HA:
7490 case elfcpp::R_PPC64_PLT16_LO_DS:
7491 case elfcpp::R_PPC64_PLTGOT16_DS:
7492 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7493 case elfcpp::R_PPC_EMB_RELSDA:
7494 case elfcpp::R_PPC_TOC16:
7495 default:
7496 unsupp:
7497 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7498 _("unsupported reloc %u"),
7499 r_type);
7500 break;
7501 }
7502 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
7503 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7504 _("relocation overflow"));
7505
7506 return true;
7507 }
7508
7509 // Relocate section data.
7510
7511 template<int size, bool big_endian>
7512 void
7513 Target_powerpc<size, big_endian>::relocate_section(
7514 const Relocate_info<size, big_endian>* relinfo,
7515 unsigned int sh_type,
7516 const unsigned char* prelocs,
7517 size_t reloc_count,
7518 Output_section* output_section,
7519 bool needs_special_offset_handling,
7520 unsigned char* view,
7521 Address address,
7522 section_size_type view_size,
7523 const Reloc_symbol_changes* reloc_symbol_changes)
7524 {
7525 typedef Target_powerpc<size, big_endian> Powerpc;
7526 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7527 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7528 Powerpc_comdat_behavior;
7529
7530 gold_assert(sh_type == elfcpp::SHT_RELA);
7531
7532 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7533 Powerpc_relocate, Powerpc_comdat_behavior>(
7534 relinfo,
7535 this,
7536 prelocs,
7537 reloc_count,
7538 output_section,
7539 needs_special_offset_handling,
7540 view,
7541 address,
7542 view_size,
7543 reloc_symbol_changes);
7544 }
7545
7546 class Powerpc_scan_relocatable_reloc
7547 {
7548 public:
7549 // Return the strategy to use for a local symbol which is not a
7550 // section symbol, given the relocation type.
7551 inline Relocatable_relocs::Reloc_strategy
7552 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7553 {
7554 if (r_type == 0 && r_sym == 0)
7555 return Relocatable_relocs::RELOC_DISCARD;
7556 return Relocatable_relocs::RELOC_COPY;
7557 }
7558
7559 // Return the strategy to use for a local symbol which is a section
7560 // symbol, given the relocation type.
7561 inline Relocatable_relocs::Reloc_strategy
7562 local_section_strategy(unsigned int, Relobj*)
7563 {
7564 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7565 }
7566
7567 // Return the strategy to use for a global symbol, given the
7568 // relocation type, the object, and the symbol index.
7569 inline Relocatable_relocs::Reloc_strategy
7570 global_strategy(unsigned int r_type, Relobj*, unsigned int)
7571 {
7572 if (r_type == elfcpp::R_PPC_PLTREL24)
7573 return Relocatable_relocs::RELOC_SPECIAL;
7574 return Relocatable_relocs::RELOC_COPY;
7575 }
7576 };
7577
7578 // Scan the relocs during a relocatable link.
7579
7580 template<int size, bool big_endian>
7581 void
7582 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7583 Symbol_table* symtab,
7584 Layout* layout,
7585 Sized_relobj_file<size, big_endian>* object,
7586 unsigned int data_shndx,
7587 unsigned int sh_type,
7588 const unsigned char* prelocs,
7589 size_t reloc_count,
7590 Output_section* output_section,
7591 bool needs_special_offset_handling,
7592 size_t local_symbol_count,
7593 const unsigned char* plocal_symbols,
7594 Relocatable_relocs* rr)
7595 {
7596 gold_assert(sh_type == elfcpp::SHT_RELA);
7597
7598 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7599 Powerpc_scan_relocatable_reloc>(
7600 symtab,
7601 layout,
7602 object,
7603 data_shndx,
7604 prelocs,
7605 reloc_count,
7606 output_section,
7607 needs_special_offset_handling,
7608 local_symbol_count,
7609 plocal_symbols,
7610 rr);
7611 }
7612
7613 // Emit relocations for a section.
7614 // This is a modified version of the function by the same name in
7615 // target-reloc.h. Using relocate_special_relocatable for
7616 // R_PPC_PLTREL24 would require duplication of the entire body of the
7617 // loop, so we may as well duplicate the whole thing.
7618
7619 template<int size, bool big_endian>
7620 void
7621 Target_powerpc<size, big_endian>::relocate_relocs(
7622 const Relocate_info<size, big_endian>* relinfo,
7623 unsigned int sh_type,
7624 const unsigned char* prelocs,
7625 size_t reloc_count,
7626 Output_section* output_section,
7627 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7628 const Relocatable_relocs* rr,
7629 unsigned char*,
7630 Address view_address,
7631 section_size_type,
7632 unsigned char* reloc_view,
7633 section_size_type reloc_view_size)
7634 {
7635 gold_assert(sh_type == elfcpp::SHT_RELA);
7636
7637 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7638 Reltype;
7639 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7640 Reltype_write;
7641 const int reloc_size
7642 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7643
7644 Powerpc_relobj<size, big_endian>* const object
7645 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7646 const unsigned int local_count = object->local_symbol_count();
7647 unsigned int got2_shndx = object->got2_shndx();
7648 Address got2_addend = 0;
7649 if (got2_shndx != 0)
7650 {
7651 got2_addend = object->get_output_section_offset(got2_shndx);
7652 gold_assert(got2_addend != invalid_address);
7653 }
7654
7655 unsigned char* pwrite = reloc_view;
7656 bool zap_next = false;
7657 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7658 {
7659 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7660 if (strategy == Relocatable_relocs::RELOC_DISCARD)
7661 continue;
7662
7663 Reltype reloc(prelocs);
7664 Reltype_write reloc_write(pwrite);
7665
7666 Address offset = reloc.get_r_offset();
7667 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7668 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7669 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7670 const unsigned int orig_r_sym = r_sym;
7671 typename elfcpp::Elf_types<size>::Elf_Swxword addend
7672 = reloc.get_r_addend();
7673 const Symbol* gsym = NULL;
7674
7675 if (zap_next)
7676 {
7677 // We could arrange to discard these and other relocs for
7678 // tls optimised sequences in the strategy methods, but for
7679 // now do as BFD ld does.
7680 r_type = elfcpp::R_POWERPC_NONE;
7681 zap_next = false;
7682 }
7683
7684 // Get the new symbol index.
7685 if (r_sym < local_count)
7686 {
7687 switch (strategy)
7688 {
7689 case Relocatable_relocs::RELOC_COPY:
7690 case Relocatable_relocs::RELOC_SPECIAL:
7691 if (r_sym != 0)
7692 {
7693 r_sym = object->symtab_index(r_sym);
7694 gold_assert(r_sym != -1U);
7695 }
7696 break;
7697
7698 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7699 {
7700 // We are adjusting a section symbol. We need to find
7701 // the symbol table index of the section symbol for
7702 // the output section corresponding to input section
7703 // in which this symbol is defined.
7704 gold_assert(r_sym < local_count);
7705 bool is_ordinary;
7706 unsigned int shndx =
7707 object->local_symbol_input_shndx(r_sym, &is_ordinary);
7708 gold_assert(is_ordinary);
7709 Output_section* os = object->output_section(shndx);
7710 gold_assert(os != NULL);
7711 gold_assert(os->needs_symtab_index());
7712 r_sym = os->symtab_index();
7713 }
7714 break;
7715
7716 default:
7717 gold_unreachable();
7718 }
7719 }
7720 else
7721 {
7722 gsym = object->global_symbol(r_sym);
7723 gold_assert(gsym != NULL);
7724 if (gsym->is_forwarder())
7725 gsym = relinfo->symtab->resolve_forwards(gsym);
7726
7727 gold_assert(gsym->has_symtab_index());
7728 r_sym = gsym->symtab_index();
7729 }
7730
7731 // Get the new offset--the location in the output section where
7732 // this relocation should be applied.
7733 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7734 offset += offset_in_output_section;
7735 else
7736 {
7737 section_offset_type sot_offset =
7738 convert_types<section_offset_type, Address>(offset);
7739 section_offset_type new_sot_offset =
7740 output_section->output_offset(object, relinfo->data_shndx,
7741 sot_offset);
7742 gold_assert(new_sot_offset != -1);
7743 offset = new_sot_offset;
7744 }
7745
7746 // In an object file, r_offset is an offset within the section.
7747 // In an executable or dynamic object, generated by
7748 // --emit-relocs, r_offset is an absolute address.
7749 if (!parameters->options().relocatable())
7750 {
7751 offset += view_address;
7752 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7753 offset -= offset_in_output_section;
7754 }
7755
7756 // Handle the reloc addend based on the strategy.
7757 if (strategy == Relocatable_relocs::RELOC_COPY)
7758 ;
7759 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7760 {
7761 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7762 addend = psymval->value(object, addend);
7763 }
7764 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7765 {
7766 if (addend >= 32768)
7767 addend += got2_addend;
7768 }
7769 else
7770 gold_unreachable();
7771
7772 if (!parameters->options().relocatable())
7773 {
7774 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7775 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7776 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7777 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7778 {
7779 // First instruction of a global dynamic sequence,
7780 // arg setup insn.
7781 const bool final = gsym == NULL || gsym->final_value_is_known();
7782 switch (this->optimize_tls_gd(final))
7783 {
7784 case tls::TLSOPT_TO_IE:
7785 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7786 - elfcpp::R_POWERPC_GOT_TLSGD16);
7787 break;
7788 case tls::TLSOPT_TO_LE:
7789 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7790 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7791 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7792 else
7793 {
7794 r_type = elfcpp::R_POWERPC_NONE;
7795 offset -= 2 * big_endian;
7796 }
7797 break;
7798 default:
7799 break;
7800 }
7801 }
7802 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7803 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7804 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7805 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7806 {
7807 // First instruction of a local dynamic sequence,
7808 // arg setup insn.
7809 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7810 {
7811 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7812 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7813 {
7814 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7815 const Output_section* os = relinfo->layout->tls_segment()
7816 ->first_section();
7817 gold_assert(os != NULL);
7818 gold_assert(os->needs_symtab_index());
7819 r_sym = os->symtab_index();
7820 addend = dtp_offset;
7821 }
7822 else
7823 {
7824 r_type = elfcpp::R_POWERPC_NONE;
7825 offset -= 2 * big_endian;
7826 }
7827 }
7828 }
7829 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7830 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7831 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7832 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7833 {
7834 // First instruction of initial exec sequence.
7835 const bool final = gsym == NULL || gsym->final_value_is_known();
7836 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7837 {
7838 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7839 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7840 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7841 else
7842 {
7843 r_type = elfcpp::R_POWERPC_NONE;
7844 offset -= 2 * big_endian;
7845 }
7846 }
7847 }
7848 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7849 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7850 {
7851 // Second instruction of a global dynamic sequence,
7852 // the __tls_get_addr call
7853 const bool final = gsym == NULL || gsym->final_value_is_known();
7854 switch (this->optimize_tls_gd(final))
7855 {
7856 case tls::TLSOPT_TO_IE:
7857 r_type = elfcpp::R_POWERPC_NONE;
7858 zap_next = true;
7859 break;
7860 case tls::TLSOPT_TO_LE:
7861 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7862 offset += 2 * big_endian;
7863 zap_next = true;
7864 break;
7865 default:
7866 break;
7867 }
7868 }
7869 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7870 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7871 {
7872 // Second instruction of a local dynamic sequence,
7873 // the __tls_get_addr call
7874 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7875 {
7876 const Output_section* os = relinfo->layout->tls_segment()
7877 ->first_section();
7878 gold_assert(os != NULL);
7879 gold_assert(os->needs_symtab_index());
7880 r_sym = os->symtab_index();
7881 addend = dtp_offset;
7882 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7883 offset += 2 * big_endian;
7884 zap_next = true;
7885 }
7886 }
7887 else if (r_type == elfcpp::R_POWERPC_TLS)
7888 {
7889 // Second instruction of an initial exec sequence
7890 const bool final = gsym == NULL || gsym->final_value_is_known();
7891 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7892 {
7893 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7894 offset += 2 * big_endian;
7895 }
7896 }
7897 }
7898
7899 reloc_write.put_r_offset(offset);
7900 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
7901 reloc_write.put_r_addend(addend);
7902
7903 pwrite += reloc_size;
7904 }
7905
7906 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
7907 == reloc_view_size);
7908 }
7909
7910 // Return the value to use for a dynamic symbol which requires special
7911 // treatment. This is how we support equality comparisons of function
7912 // pointers across shared library boundaries, as described in the
7913 // processor specific ABI supplement.
7914
7915 template<int size, bool big_endian>
7916 uint64_t
7917 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7918 {
7919 if (size == 32)
7920 {
7921 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7922 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7923 p != this->stub_tables_.end();
7924 ++p)
7925 {
7926 Address off = (*p)->find_plt_call_entry(gsym);
7927 if (off != invalid_address)
7928 return (*p)->stub_address() + off;
7929 }
7930 }
7931 gold_unreachable();
7932 }
7933
7934 // Return the PLT address to use for a local symbol.
7935 template<int size, bool big_endian>
7936 uint64_t
7937 Target_powerpc<size, big_endian>::do_plt_address_for_local(
7938 const Relobj* object,
7939 unsigned int symndx) const
7940 {
7941 if (size == 32)
7942 {
7943 const Sized_relobj<size, big_endian>* relobj
7944 = static_cast<const Sized_relobj<size, big_endian>*>(object);
7945 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7946 p != this->stub_tables_.end();
7947 ++p)
7948 {
7949 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
7950 symndx);
7951 if (off != invalid_address)
7952 return (*p)->stub_address() + off;
7953 }
7954 }
7955 gold_unreachable();
7956 }
7957
7958 // Return the PLT address to use for a global symbol.
7959 template<int size, bool big_endian>
7960 uint64_t
7961 Target_powerpc<size, big_endian>::do_plt_address_for_global(
7962 const Symbol* gsym) const
7963 {
7964 if (size == 32)
7965 {
7966 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7967 p != this->stub_tables_.end();
7968 ++p)
7969 {
7970 Address off = (*p)->find_plt_call_entry(gsym);
7971 if (off != invalid_address)
7972 return (*p)->stub_address() + off;
7973 }
7974 }
7975 gold_unreachable();
7976 }
7977
7978 // Return the offset to use for the GOT_INDX'th got entry which is
7979 // for a local tls symbol specified by OBJECT, SYMNDX.
7980 template<int size, bool big_endian>
7981 int64_t
7982 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
7983 const Relobj* object,
7984 unsigned int symndx,
7985 unsigned int got_indx) const
7986 {
7987 const Powerpc_relobj<size, big_endian>* ppc_object
7988 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
7989 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
7990 {
7991 for (Got_type got_type = GOT_TYPE_TLSGD;
7992 got_type <= GOT_TYPE_TPREL;
7993 got_type = Got_type(got_type + 1))
7994 if (ppc_object->local_has_got_offset(symndx, got_type))
7995 {
7996 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
7997 if (got_type == GOT_TYPE_TLSGD)
7998 off += size / 8;
7999 if (off == got_indx * (size / 8))
8000 {
8001 if (got_type == GOT_TYPE_TPREL)
8002 return -tp_offset;
8003 else
8004 return -dtp_offset;
8005 }
8006 }
8007 }
8008 gold_unreachable();
8009 }
8010
8011 // Return the offset to use for the GOT_INDX'th got entry which is
8012 // for global tls symbol GSYM.
8013 template<int size, bool big_endian>
8014 int64_t
8015 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
8016 Symbol* gsym,
8017 unsigned int got_indx) const
8018 {
8019 if (gsym->type() == elfcpp::STT_TLS)
8020 {
8021 for (Got_type got_type = GOT_TYPE_TLSGD;
8022 got_type <= GOT_TYPE_TPREL;
8023 got_type = Got_type(got_type + 1))
8024 if (gsym->has_got_offset(got_type))
8025 {
8026 unsigned int off = gsym->got_offset(got_type);
8027 if (got_type == GOT_TYPE_TLSGD)
8028 off += size / 8;
8029 if (off == got_indx * (size / 8))
8030 {
8031 if (got_type == GOT_TYPE_TPREL)
8032 return -tp_offset;
8033 else
8034 return -dtp_offset;
8035 }
8036 }
8037 }
8038 gold_unreachable();
8039 }
8040
8041 // The selector for powerpc object files.
8042
8043 template<int size, bool big_endian>
8044 class Target_selector_powerpc : public Target_selector
8045 {
8046 public:
8047 Target_selector_powerpc()
8048 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
8049 size, big_endian,
8050 (size == 64
8051 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
8052 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
8053 (size == 64
8054 ? (big_endian ? "elf64ppc" : "elf64lppc")
8055 : (big_endian ? "elf32ppc" : "elf32lppc")))
8056 { }
8057
8058 virtual Target*
8059 do_instantiate_target()
8060 { return new Target_powerpc<size, big_endian>(); }
8061 };
8062
8063 Target_selector_powerpc<32, true> target_selector_ppc32;
8064 Target_selector_powerpc<32, false> target_selector_ppc32le;
8065 Target_selector_powerpc<64, true> target_selector_ppc64;
8066 Target_selector_powerpc<64, false> target_selector_ppc64le;
8067
8068 // Instantiate these constants for -O0
8069 template<int size, bool big_endian>
8070 const int Output_data_glink<size, big_endian>::pltresolve_size;
8071 template<int size, bool big_endian>
8072 const typename Stub_table<size, big_endian>::Address
8073 Stub_table<size, big_endian>::invalid_address;
8074 template<int size, bool big_endian>
8075 const typename Target_powerpc<size, big_endian>::Address
8076 Target_powerpc<size, big_endian>::invalid_address;
8077
8078 } // End anonymous namespace.
This page took 0.29592 seconds and 4 git commands to generate.