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