[GOLD] PowerPC notoc eh_frame
[deliverable/binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright (C) 2008-2019 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 #include "attributes.h"
45
46 namespace
47 {
48
49 using namespace gold;
50
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
53
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
56
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
59
60 template<int size, bool big_endian>
61 class Output_data_glink;
62
63 template<int size, bool big_endian>
64 class Stub_table;
65
66 template<int size, bool big_endian>
67 class Output_data_save_res;
68
69 template<int size, bool big_endian>
70 class Target_powerpc;
71
72 struct Stub_table_owner
73 {
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80 };
81
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
84
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
87
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
90
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93 {
94 public:
95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
98
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
107 {
108 this->set_abiversion(0);
109 }
110
111 ~Powerpc_relobj()
112 { delete this->attributes_section_data_; }
113
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
164 // The .got2 section shndx.
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
169 return this->special_;
170 else
171 return 0;
172 }
173
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
199 if (value != NULL)
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
230 }
231
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
252 // Perform the Sized_relobj_file method, then set up opd info from
253 // .opd relocs.
254 void
255 do_read_relocs(Read_relocs_data*);
256
257 bool
258 do_find_special_sections(Read_symbols_data* sd);
259
260 // Adjust this local symbol value. Return false if the symbol
261 // should be discarded from the output file.
262 bool
263 do_adjust_local_symbol(Symbol_value<size>* lv) const
264 {
265 if (size == 64 && this->opd_shndx() != 0)
266 {
267 bool is_ordinary;
268 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
269 return true;
270 if (this->get_opd_discard(lv->input_value()))
271 return false;
272 }
273 return true;
274 }
275
276 Access_from*
277 access_from_map()
278 { return &this->access_from_map_; }
279
280 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
281 // section at DST_OFF.
282 void
283 add_reference(Relobj* src_obj,
284 unsigned int src_indx,
285 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
286 {
287 Section_id src_id(src_obj, src_indx);
288 this->access_from_map_[dst_off].insert(src_id);
289 }
290
291 // Add a reference to the code section specified by the .opd entry
292 // at DST_OFF
293 void
294 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
295 {
296 size_t ndx = this->opd_ent_ndx(dst_off);
297 if (ndx >= this->opd_ent_.size())
298 this->opd_ent_.resize(ndx + 1);
299 this->opd_ent_[ndx].gc_mark = true;
300 }
301
302 void
303 process_gc_mark(Symbol_table* symtab)
304 {
305 for (size_t i = 0; i < this->opd_ent_.size(); i++)
306 if (this->opd_ent_[i].gc_mark)
307 {
308 unsigned int shndx = this->opd_ent_[i].shndx;
309 symtab->gc()->worklist().push_back(Section_id(this, shndx));
310 }
311 }
312
313 // Return offset in output GOT section that this object will use
314 // as a TOC pointer. Won't be just a constant with multi-toc support.
315 Address
316 toc_base_offset() const
317 { return 0x8000; }
318
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
322
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
326
327 void
328 set_has_14bit_branch(unsigned int shndx)
329 {
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
333 }
334
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
338
339 void
340 set_stub_table(unsigned int shndx, unsigned int stub_index)
341 {
342 if (shndx >= this->stub_table_index_.size())
343 this->stub_table_index_.resize(shndx + 1, -1);
344 this->stub_table_index_[shndx] = stub_index;
345 }
346
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
349 {
350 if (shndx < this->stub_table_index_.size())
351 {
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
358 }
359 return NULL;
360 }
361
362 void
363 clear_stub_table()
364 {
365 this->stub_table_index_.clear();
366 }
367
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
371
372 int
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
379
380 unsigned int
381 st_other (unsigned int symndx) const
382 {
383 return this->st_other_[symndx];
384 }
385
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
397
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
401
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
406
407 private:
408 struct Opd_ent
409 {
410 unsigned int shndx;
411 bool discard : 1;
412 bool gc_mark : 1;
413 Address off;
414 };
415
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
429
430 // Per object unique identifier
431 uint32_t uniq_;
432
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
435
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
439
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
443
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
450
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
453
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
457
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
460 // relocation on this word. The following vector records the
461 // section and offset specified by these relocations.
462 std::vector<Opd_ent> opd_ent_;
463
464 // References made to this object's .opd section when running
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
467 Access_from access_from_map_;
468
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
471
472 // The stub table to use for a given input section.
473 std::vector<unsigned int> stub_table_index_;
474
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
477
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
480 };
481
482 template<int size, bool big_endian>
483 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484 {
485 public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
493 {
494 this->set_abiversion(0);
495 }
496
497 ~Powerpc_dynobj()
498 { delete this->attributes_section_data_; }
499
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
504
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
508 {
509 return this->opd_shndx_;
510 }
511
512 // The .opd section address.
513 Address
514 opd_address() const
515 {
516 return this->opd_address_;
517 }
518
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
522 {
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
525 }
526
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
530 {
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
537 }
538
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
542 {
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
547 }
548
549 int
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
556
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
561
562 private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
565 {
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
568 { }
569
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
573
574 Address start;
575 Address len;
576 unsigned int shndx;
577 };
578
579 struct Opd_ent
580 {
581 unsigned int shndx;
582 Address off;
583 };
584
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
589
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
593
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
596
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
602
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
605 };
606
607 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608 // base class will emit.
609
610 template<int sh_type, int size, bool big_endian>
611 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612 {
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616 { }
617
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622 };
623
624 template<int size, bool big_endian>
625 class Target_powerpc : public Sized_target<size, big_endian>
626 {
627 public:
628 typedef
629 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
630 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
631 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
632 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
633 static const Address invalid_address = static_cast<Address>(0) - 1;
634 // Offset of tp and dtp pointers from start of TLS block.
635 static const Address tp_offset = 0x7000;
636 static const Address dtp_offset = 0x8000;
637
638 Target_powerpc()
639 : Sized_target<size, big_endian>(&powerpc_info),
640 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
641 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
642 tlsld_got_offset_(-1U),
643 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
644 plt_thread_safe_(false), plt_localentry0_(false),
645 plt_localentry0_init_(false), has_localentry0_(false),
646 has_tls_get_addr_opt_(false),
647 relax_failed_(false), relax_fail_count_(0),
648 stub_group_size_(0), savres_section_(0),
649 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
650 attributes_section_data_(NULL),
651 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
652 {
653 }
654
655 // Process the relocations to determine unreferenced sections for
656 // garbage collection.
657 void
658 gc_process_relocs(Symbol_table* symtab,
659 Layout* layout,
660 Sized_relobj_file<size, big_endian>* object,
661 unsigned int data_shndx,
662 unsigned int sh_type,
663 const unsigned char* prelocs,
664 size_t reloc_count,
665 Output_section* output_section,
666 bool needs_special_offset_handling,
667 size_t local_symbol_count,
668 const unsigned char* plocal_symbols);
669
670 // Scan the relocations to look for symbol adjustments.
671 void
672 scan_relocs(Symbol_table* symtab,
673 Layout* layout,
674 Sized_relobj_file<size, big_endian>* object,
675 unsigned int data_shndx,
676 unsigned int sh_type,
677 const unsigned char* prelocs,
678 size_t reloc_count,
679 Output_section* output_section,
680 bool needs_special_offset_handling,
681 size_t local_symbol_count,
682 const unsigned char* plocal_symbols);
683
684 // Map input .toc section to output .got section.
685 const char*
686 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
687 {
688 if (size == 64 && strcmp(name, ".toc") == 0)
689 {
690 *plen = 4;
691 return ".got";
692 }
693 return NULL;
694 }
695
696 // Provide linker defined save/restore functions.
697 void
698 define_save_restore_funcs(Layout*, Symbol_table*);
699
700 // No stubs unless a final link.
701 bool
702 do_may_relax() const
703 { return !parameters->options().relocatable(); }
704
705 bool
706 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
707
708 void
709 do_plt_fde_location(const Output_data*, unsigned char*,
710 uint64_t*, off_t*) const;
711
712 // Stash info about branches, for stub generation.
713 void
714 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
715 unsigned int data_shndx, Address r_offset,
716 unsigned int r_type, unsigned int r_sym, Address addend)
717 {
718 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
719 this->branch_info_.push_back(info);
720 if (r_type == elfcpp::R_POWERPC_REL14
721 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
722 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
723 ppc_object->set_has_14bit_branch(data_shndx);
724 }
725
726 // Return whether the last branch is a plt call, and if so, mark the
727 // branch as having an R_PPC64_TOCSAVE.
728 bool
729 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
730 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
731 {
732 return (size == 64
733 && !this->branch_info_.empty()
734 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
735 r_offset, this, symtab));
736 }
737
738 // Say the given location, that of a nop in a function prologue with
739 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
740 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
741 void
742 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
743 unsigned int shndx, Address offset)
744 {
745 Symbol_location loc;
746 loc.object = ppc_object;
747 loc.shndx = shndx;
748 loc.offset = offset;
749 this->tocsave_loc_.insert(loc);
750 }
751
752 // Accessor
753 const Tocsave_loc
754 tocsave_loc() const
755 {
756 return this->tocsave_loc_;
757 }
758
759 void
760 do_define_standard_symbols(Symbol_table*, Layout*);
761
762 // Finalize the sections.
763 void
764 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
765
766 // Return the value to use for a dynamic which requires special
767 // treatment.
768 uint64_t
769 do_dynsym_value(const Symbol*) const;
770
771 // Return the PLT address to use for a local symbol.
772 uint64_t
773 do_plt_address_for_local(const Relobj*, unsigned int) const;
774
775 // Return the PLT address to use for a global symbol.
776 uint64_t
777 do_plt_address_for_global(const Symbol*) const;
778
779 // Return the offset to use for the GOT_INDX'th got entry which is
780 // for a local tls symbol specified by OBJECT, SYMNDX.
781 int64_t
782 do_tls_offset_for_local(const Relobj* object,
783 unsigned int symndx,
784 unsigned int got_indx) const;
785
786 // Return the offset to use for the GOT_INDX'th got entry which is
787 // for global tls symbol GSYM.
788 int64_t
789 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
790
791 void
792 do_function_location(Symbol_location*) const;
793
794 bool
795 do_can_check_for_function_pointers() const
796 { return true; }
797
798 // Adjust -fsplit-stack code which calls non-split-stack code.
799 void
800 do_calls_non_split(Relobj* object, unsigned int shndx,
801 section_offset_type fnoffset, section_size_type fnsize,
802 const unsigned char* prelocs, size_t reloc_count,
803 unsigned char* view, section_size_type view_size,
804 std::string* from, std::string* to) const;
805
806 // Relocate a section.
807 void
808 relocate_section(const Relocate_info<size, big_endian>*,
809 unsigned int sh_type,
810 const unsigned char* prelocs,
811 size_t reloc_count,
812 Output_section* output_section,
813 bool needs_special_offset_handling,
814 unsigned char* view,
815 Address view_address,
816 section_size_type view_size,
817 const Reloc_symbol_changes*);
818
819 // Scan the relocs during a relocatable link.
820 void
821 scan_relocatable_relocs(Symbol_table* symtab,
822 Layout* layout,
823 Sized_relobj_file<size, big_endian>* object,
824 unsigned int data_shndx,
825 unsigned int sh_type,
826 const unsigned char* prelocs,
827 size_t reloc_count,
828 Output_section* output_section,
829 bool needs_special_offset_handling,
830 size_t local_symbol_count,
831 const unsigned char* plocal_symbols,
832 Relocatable_relocs*);
833
834 // Scan the relocs for --emit-relocs.
835 void
836 emit_relocs_scan(Symbol_table* symtab,
837 Layout* layout,
838 Sized_relobj_file<size, big_endian>* object,
839 unsigned int data_shndx,
840 unsigned int sh_type,
841 const unsigned char* prelocs,
842 size_t reloc_count,
843 Output_section* output_section,
844 bool needs_special_offset_handling,
845 size_t local_symbol_count,
846 const unsigned char* plocal_syms,
847 Relocatable_relocs* rr);
848
849 // Emit relocations for a section.
850 void
851 relocate_relocs(const Relocate_info<size, big_endian>*,
852 unsigned int sh_type,
853 const unsigned char* prelocs,
854 size_t reloc_count,
855 Output_section* output_section,
856 typename elfcpp::Elf_types<size>::Elf_Off
857 offset_in_output_section,
858 unsigned char*,
859 Address view_address,
860 section_size_type,
861 unsigned char* reloc_view,
862 section_size_type reloc_view_size);
863
864 // Return whether SYM is defined by the ABI.
865 bool
866 do_is_defined_by_abi(const Symbol* sym) const
867 {
868 return strcmp(sym->name(), "__tls_get_addr") == 0;
869 }
870
871 // Return the size of the GOT section.
872 section_size_type
873 got_size() const
874 {
875 gold_assert(this->got_ != NULL);
876 return this->got_->data_size();
877 }
878
879 // Get the PLT section.
880 const Output_data_plt_powerpc<size, big_endian>*
881 plt_section() const
882 {
883 gold_assert(this->plt_ != NULL);
884 return this->plt_;
885 }
886
887 // Get the IPLT section.
888 const Output_data_plt_powerpc<size, big_endian>*
889 iplt_section() const
890 {
891 gold_assert(this->iplt_ != NULL);
892 return this->iplt_;
893 }
894
895 // Get the LPLT section.
896 const Output_data_plt_powerpc<size, big_endian>*
897 lplt_section() const
898 {
899 return this->lplt_;
900 }
901
902 // Return the plt offset and section for the given global sym.
903 Address
904 plt_off(const Symbol* gsym,
905 const Output_data_plt_powerpc<size, big_endian>** sec) const
906 {
907 if (gsym->type() == elfcpp::STT_GNU_IFUNC
908 && gsym->can_use_relative_reloc(false))
909 *sec = this->iplt_section();
910 else
911 *sec = this->plt_section();
912 return gsym->plt_offset();
913 }
914
915 // Return the plt offset and section for the given local sym.
916 Address
917 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
918 unsigned int local_sym_index,
919 const Output_data_plt_powerpc<size, big_endian>** sec) const
920 {
921 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
922 if (lsym->is_ifunc_symbol())
923 *sec = this->iplt_section();
924 else
925 *sec = this->lplt_section();
926 return relobj->local_plt_offset(local_sym_index);
927 }
928
929 // Get the .glink section.
930 const Output_data_glink<size, big_endian>*
931 glink_section() const
932 {
933 gold_assert(this->glink_ != NULL);
934 return this->glink_;
935 }
936
937 Output_data_glink<size, big_endian>*
938 glink_section()
939 {
940 gold_assert(this->glink_ != NULL);
941 return this->glink_;
942 }
943
944 bool has_glink() const
945 { return this->glink_ != NULL; }
946
947 // Get the GOT section.
948 const Output_data_got_powerpc<size, big_endian>*
949 got_section() const
950 {
951 gold_assert(this->got_ != NULL);
952 return this->got_;
953 }
954
955 // Get the GOT section, creating it if necessary.
956 Output_data_got_powerpc<size, big_endian>*
957 got_section(Symbol_table*, Layout*);
958
959 Object*
960 do_make_elf_object(const std::string&, Input_file*, off_t,
961 const elfcpp::Ehdr<size, big_endian>&);
962
963 // Return the number of entries in the GOT.
964 unsigned int
965 got_entry_count() const
966 {
967 if (this->got_ == NULL)
968 return 0;
969 return this->got_size() / (size / 8);
970 }
971
972 // Return the number of entries in the PLT.
973 unsigned int
974 plt_entry_count() const;
975
976 // Return the offset of the first non-reserved PLT entry.
977 unsigned int
978 first_plt_entry_offset() const
979 {
980 if (size == 32)
981 return 0;
982 if (this->abiversion() >= 2)
983 return 16;
984 return 24;
985 }
986
987 // Return the size of each PLT entry.
988 unsigned int
989 plt_entry_size() const
990 {
991 if (size == 32)
992 return 4;
993 if (this->abiversion() >= 2)
994 return 8;
995 return 24;
996 }
997
998 Output_data_save_res<size, big_endian>*
999 savres_section() const
1000 {
1001 return this->savres_section_;
1002 }
1003
1004 // Add any special sections for this symbol to the gc work list.
1005 // For powerpc64, this adds the code section of a function
1006 // descriptor.
1007 void
1008 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1009
1010 // Handle target specific gc actions when adding a gc reference from
1011 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1012 // and DST_OFF. For powerpc64, this adds a referenc to the code
1013 // section of a function descriptor.
1014 void
1015 do_gc_add_reference(Symbol_table* symtab,
1016 Relobj* src_obj,
1017 unsigned int src_shndx,
1018 Relobj* dst_obj,
1019 unsigned int dst_shndx,
1020 Address dst_off) const;
1021
1022 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1023 const Stub_tables&
1024 stub_tables() const
1025 { return this->stub_tables_; }
1026
1027 const Output_data_brlt_powerpc<size, big_endian>*
1028 brlt_section() const
1029 { return this->brlt_section_; }
1030
1031 void
1032 add_branch_lookup_table(Address to)
1033 {
1034 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1035 this->branch_lookup_table_.insert(std::make_pair(to, off));
1036 }
1037
1038 Address
1039 find_branch_lookup_table(Address to)
1040 {
1041 typename Branch_lookup_table::const_iterator p
1042 = this->branch_lookup_table_.find(to);
1043 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1044 }
1045
1046 void
1047 write_branch_lookup_table(unsigned char *oview)
1048 {
1049 for (typename Branch_lookup_table::const_iterator p
1050 = this->branch_lookup_table_.begin();
1051 p != this->branch_lookup_table_.end();
1052 ++p)
1053 {
1054 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1055 }
1056 }
1057
1058 // Wrapper used after relax to define a local symbol in output data,
1059 // from the end if value < 0.
1060 void
1061 define_local(Symbol_table* symtab, const char* name,
1062 Output_data* od, Address value, unsigned int symsize)
1063 {
1064 Symbol* sym
1065 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1066 od, value, symsize, elfcpp::STT_NOTYPE,
1067 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1068 static_cast<Signed_address>(value) < 0,
1069 false);
1070 // We are creating this symbol late, so need to fix up things
1071 // done early in Layout::finalize.
1072 sym->set_dynsym_index(-1U);
1073 }
1074
1075 bool
1076 plt_thread_safe() const
1077 { return this->plt_thread_safe_; }
1078
1079 bool
1080 plt_localentry0() const
1081 { return this->plt_localentry0_; }
1082
1083 void
1084 set_has_localentry0()
1085 {
1086 this->has_localentry0_ = true;
1087 }
1088
1089 bool
1090 is_elfv2_localentry0(const Symbol* gsym) const
1091 {
1092 return (size == 64
1093 && this->abiversion() >= 2
1094 && this->plt_localentry0()
1095 && gsym->type() == elfcpp::STT_FUNC
1096 && gsym->is_defined()
1097 && gsym->nonvis() >> 3 == 0
1098 && !gsym->non_zero_localentry());
1099 }
1100
1101 bool
1102 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1103 unsigned int r_sym) const
1104 {
1105 const Powerpc_relobj<size, big_endian>* ppc_object
1106 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1107
1108 if (size == 64
1109 && this->abiversion() >= 2
1110 && this->plt_localentry0()
1111 && ppc_object->st_other(r_sym) >> 5 == 0)
1112 {
1113 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1114 bool is_ordinary;
1115 if (!psymval->is_ifunc_symbol()
1116 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1117 && is_ordinary)
1118 return true;
1119 }
1120 return false;
1121 }
1122
1123 // Remember any symbols seen with non-zero localentry, even those
1124 // not providing a definition
1125 bool
1126 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1127 const char*)
1128 {
1129 if (size == 64)
1130 {
1131 unsigned char st_other = sym.get_st_other();
1132 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1133 to->set_non_zero_localentry();
1134 }
1135 // We haven't resolved anything, continue normal processing.
1136 return false;
1137 }
1138
1139 int
1140 abiversion() const
1141 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1142
1143 void
1144 set_abiversion(int ver)
1145 {
1146 elfcpp::Elf_Word flags = this->processor_specific_flags();
1147 flags &= ~elfcpp::EF_PPC64_ABI;
1148 flags |= ver & elfcpp::EF_PPC64_ABI;
1149 this->set_processor_specific_flags(flags);
1150 }
1151
1152 Symbol*
1153 tls_get_addr_opt() const
1154 { return this->tls_get_addr_opt_; }
1155
1156 Symbol*
1157 tls_get_addr() const
1158 { return this->tls_get_addr_; }
1159
1160 // If optimizing __tls_get_addr calls, whether this is the
1161 // "__tls_get_addr" symbol.
1162 bool
1163 is_tls_get_addr_opt(const Symbol* gsym) const
1164 {
1165 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1166 || gsym == this->tls_get_addr_opt_);
1167 }
1168
1169 bool
1170 replace_tls_get_addr(const Symbol* gsym) const
1171 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1172
1173 void
1174 set_has_tls_get_addr_opt()
1175 { this->has_tls_get_addr_opt_ = true; }
1176
1177 // Offset to toc save stack slot
1178 int
1179 stk_toc() const
1180 { return this->abiversion() < 2 ? 40 : 24; }
1181
1182 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1183 // so use the CR save slot. Used only by __tls_get_addr call stub,
1184 // relying on __tls_get_addr not saving CR itself.
1185 int
1186 stk_linker() const
1187 { return this->abiversion() < 2 ? 32 : 8; }
1188
1189 // Merge object attributes from input object with those in the output.
1190 void
1191 merge_object_attributes(const char*, const Attributes_section_data*);
1192
1193 private:
1194
1195 class Track_tls
1196 {
1197 public:
1198 enum Tls_get_addr
1199 {
1200 NOT_EXPECTED = 0,
1201 EXPECTED = 1,
1202 SKIP = 2,
1203 NORMAL = 3
1204 };
1205
1206 Track_tls()
1207 : tls_get_addr_state_(NOT_EXPECTED),
1208 relinfo_(NULL), relnum_(0), r_offset_(0)
1209 { }
1210
1211 ~Track_tls()
1212 {
1213 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1214 this->missing();
1215 }
1216
1217 void
1218 missing(void)
1219 {
1220 if (this->relinfo_ != NULL)
1221 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1222 _("missing expected __tls_get_addr call"));
1223 }
1224
1225 void
1226 expect_tls_get_addr_call(
1227 const Relocate_info<size, big_endian>* relinfo,
1228 size_t relnum,
1229 Address r_offset)
1230 {
1231 this->tls_get_addr_state_ = EXPECTED;
1232 this->relinfo_ = relinfo;
1233 this->relnum_ = relnum;
1234 this->r_offset_ = r_offset;
1235 }
1236
1237 void
1238 expect_tls_get_addr_call()
1239 { this->tls_get_addr_state_ = EXPECTED; }
1240
1241 void
1242 skip_next_tls_get_addr_call()
1243 {this->tls_get_addr_state_ = SKIP; }
1244
1245 Tls_get_addr
1246 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1247 unsigned int r_type, const Symbol* gsym)
1248 {
1249 bool is_tls_call
1250 = ((r_type == elfcpp::R_POWERPC_REL24
1251 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1252 || r_type == elfcpp::R_PPC_PLTREL24
1253 || is_plt16_reloc<size>(r_type)
1254 || r_type == elfcpp::R_POWERPC_PLTSEQ
1255 || r_type == elfcpp::R_POWERPC_PLTCALL
1256 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1257 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1258 && gsym != NULL
1259 && (gsym == target->tls_get_addr()
1260 || gsym == target->tls_get_addr_opt()));
1261 Tls_get_addr last_tls = this->tls_get_addr_state_;
1262 this->tls_get_addr_state_ = NOT_EXPECTED;
1263 if (is_tls_call && last_tls != EXPECTED)
1264 return last_tls;
1265 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1266 {
1267 this->missing();
1268 return EXPECTED;
1269 }
1270 return NORMAL;
1271 }
1272
1273 private:
1274 // What we're up to regarding calls to __tls_get_addr.
1275 // On powerpc, the branch and link insn making a call to
1276 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1277 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1278 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1279 // The marker relocation always comes first, and has the same
1280 // symbol as the reloc on the insn setting up the __tls_get_addr
1281 // argument. This ties the arg setup insn with the call insn,
1282 // allowing ld to safely optimize away the call. We check that
1283 // every call to __tls_get_addr has a marker relocation, and that
1284 // every marker relocation is on a call to __tls_get_addr.
1285 Tls_get_addr tls_get_addr_state_;
1286 // Info about the last reloc for error message.
1287 const Relocate_info<size, big_endian>* relinfo_;
1288 size_t relnum_;
1289 Address r_offset_;
1290 };
1291
1292 // The class which scans relocations.
1293 class Scan : protected Track_tls
1294 {
1295 public:
1296 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1297
1298 Scan()
1299 : Track_tls(), issued_non_pic_error_(false)
1300 { }
1301
1302 static inline int
1303 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1304
1305 inline void
1306 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1307 Sized_relobj_file<size, big_endian>* object,
1308 unsigned int data_shndx,
1309 Output_section* output_section,
1310 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1311 const elfcpp::Sym<size, big_endian>& lsym,
1312 bool is_discarded);
1313
1314 inline void
1315 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1316 Sized_relobj_file<size, big_endian>* object,
1317 unsigned int data_shndx,
1318 Output_section* output_section,
1319 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1320 Symbol* gsym);
1321
1322 inline bool
1323 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1324 Target_powerpc* ,
1325 Sized_relobj_file<size, big_endian>* relobj,
1326 unsigned int ,
1327 Output_section* ,
1328 const elfcpp::Rela<size, big_endian>& ,
1329 unsigned int r_type,
1330 const elfcpp::Sym<size, big_endian>&)
1331 {
1332 // PowerPC64 .opd is not folded, so any identical function text
1333 // may be folded and we'll still keep function addresses distinct.
1334 // That means no reloc is of concern here.
1335 if (size == 64)
1336 {
1337 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1338 <Powerpc_relobj<size, big_endian>*>(relobj);
1339 if (ppcobj->abiversion() == 1)
1340 return false;
1341 }
1342 // For 32-bit and ELFv2, conservatively assume anything but calls to
1343 // function code might be taking the address of the function.
1344 return !is_branch_reloc<size>(r_type);
1345 }
1346
1347 inline bool
1348 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1349 Target_powerpc* ,
1350 Sized_relobj_file<size, big_endian>* relobj,
1351 unsigned int ,
1352 Output_section* ,
1353 const elfcpp::Rela<size, big_endian>& ,
1354 unsigned int r_type,
1355 Symbol*)
1356 {
1357 // As above.
1358 if (size == 64)
1359 {
1360 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1361 <Powerpc_relobj<size, big_endian>*>(relobj);
1362 if (ppcobj->abiversion() == 1)
1363 return false;
1364 }
1365 return !is_branch_reloc<size>(r_type);
1366 }
1367
1368 static bool
1369 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1370 Sized_relobj_file<size, big_endian>* object,
1371 unsigned int r_type, bool report_err);
1372
1373 private:
1374 static void
1375 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1376 unsigned int r_type);
1377
1378 static void
1379 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1380 unsigned int r_type, Symbol*);
1381
1382 static void
1383 generate_tls_call(Symbol_table* symtab, Layout* layout,
1384 Target_powerpc* target);
1385
1386 void
1387 check_non_pic(Relobj*, unsigned int r_type);
1388
1389 // Whether we have issued an error about a non-PIC compilation.
1390 bool issued_non_pic_error_;
1391 };
1392
1393 bool
1394 symval_for_branch(const Symbol_table* symtab,
1395 const Sized_symbol<size>* gsym,
1396 Powerpc_relobj<size, big_endian>* object,
1397 Address *value, unsigned int *dest_shndx);
1398
1399 // The class which implements relocation.
1400 class Relocate : protected Track_tls
1401 {
1402 public:
1403 // Use 'at' branch hints when true, 'y' when false.
1404 // FIXME maybe: set this with an option.
1405 static const bool is_isa_v2 = true;
1406
1407 Relocate()
1408 : Track_tls()
1409 { }
1410
1411 // Do a relocation. Return false if the caller should not issue
1412 // any warnings about this relocation.
1413 inline bool
1414 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1415 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1416 const Sized_symbol<size>*, const Symbol_value<size>*,
1417 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1418 section_size_type);
1419 };
1420
1421 class Relocate_comdat_behavior
1422 {
1423 public:
1424 // Decide what the linker should do for relocations that refer to
1425 // discarded comdat sections.
1426 inline Comdat_behavior
1427 get(const char* name)
1428 {
1429 gold::Default_comdat_behavior default_behavior;
1430 Comdat_behavior ret = default_behavior.get(name);
1431 if (ret == CB_ERROR)
1432 {
1433 if (size == 32
1434 && (strcmp(name, ".fixup") == 0
1435 || strcmp(name, ".got2") == 0))
1436 ret = CB_IGNORE;
1437 if (size == 64
1438 && (strcmp(name, ".opd") == 0
1439 || strcmp(name, ".toc") == 0
1440 || strcmp(name, ".toc1") == 0))
1441 ret = CB_IGNORE;
1442 }
1443 return ret;
1444 }
1445 };
1446
1447 // Optimize the TLS relocation type based on what we know about the
1448 // symbol. IS_FINAL is true if the final address of this symbol is
1449 // known at link time.
1450
1451 tls::Tls_optimization
1452 optimize_tls_gd(bool is_final)
1453 {
1454 // If we are generating a shared library, then we can't do anything
1455 // in the linker.
1456 if (parameters->options().shared()
1457 || !parameters->options().tls_optimize())
1458 return tls::TLSOPT_NONE;
1459
1460 if (!is_final)
1461 return tls::TLSOPT_TO_IE;
1462 return tls::TLSOPT_TO_LE;
1463 }
1464
1465 tls::Tls_optimization
1466 optimize_tls_ld()
1467 {
1468 if (parameters->options().shared()
1469 || !parameters->options().tls_optimize())
1470 return tls::TLSOPT_NONE;
1471
1472 return tls::TLSOPT_TO_LE;
1473 }
1474
1475 tls::Tls_optimization
1476 optimize_tls_ie(bool is_final)
1477 {
1478 if (!is_final
1479 || parameters->options().shared()
1480 || !parameters->options().tls_optimize())
1481 return tls::TLSOPT_NONE;
1482
1483 return tls::TLSOPT_TO_LE;
1484 }
1485
1486 // Create glink.
1487 void
1488 make_glink_section(Layout*);
1489
1490 // Create the PLT section.
1491 void
1492 make_plt_section(Symbol_table*, Layout*);
1493
1494 void
1495 make_iplt_section(Symbol_table*, Layout*);
1496
1497 void
1498 make_lplt_section(Layout*);
1499
1500 void
1501 make_brlt_section(Layout*);
1502
1503 // Create a PLT entry for a global symbol.
1504 void
1505 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1506
1507 // Create a PLT entry for a local IFUNC symbol.
1508 void
1509 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1510 Sized_relobj_file<size, big_endian>*,
1511 unsigned int);
1512
1513 // Create a PLT entry for a local non-IFUNC symbol.
1514 void
1515 make_local_plt_entry(Layout*,
1516 Sized_relobj_file<size, big_endian>*,
1517 unsigned int);
1518
1519
1520 // Create a GOT entry for local dynamic __tls_get_addr.
1521 unsigned int
1522 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1523 Sized_relobj_file<size, big_endian>* object);
1524
1525 unsigned int
1526 tlsld_got_offset() const
1527 {
1528 return this->tlsld_got_offset_;
1529 }
1530
1531 // Get the dynamic reloc section, creating it if necessary.
1532 Reloc_section*
1533 rela_dyn_section(Layout*);
1534
1535 // Similarly, but for ifunc symbols get the one for ifunc.
1536 Reloc_section*
1537 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1538
1539 // Copy a relocation against a global symbol.
1540 void
1541 copy_reloc(Symbol_table* symtab, Layout* layout,
1542 Sized_relobj_file<size, big_endian>* object,
1543 unsigned int shndx, Output_section* output_section,
1544 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1545 {
1546 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1547 this->copy_relocs_.copy_reloc(symtab, layout,
1548 symtab->get_sized_symbol<size>(sym),
1549 object, shndx, output_section,
1550 r_type, reloc.get_r_offset(),
1551 reloc.get_r_addend(),
1552 this->rela_dyn_section(layout));
1553 }
1554
1555 // Look over all the input sections, deciding where to place stubs.
1556 void
1557 group_sections(Layout*, const Task*, bool);
1558
1559 // Sort output sections by address.
1560 struct Sort_sections
1561 {
1562 bool
1563 operator()(const Output_section* sec1, const Output_section* sec2)
1564 { return sec1->address() < sec2->address(); }
1565 };
1566
1567 class Branch_info
1568 {
1569 public:
1570 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1571 unsigned int data_shndx,
1572 Address r_offset,
1573 unsigned int r_type,
1574 unsigned int r_sym,
1575 Address addend)
1576 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1577 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1578 { }
1579
1580 ~Branch_info()
1581 { }
1582
1583 // Return whether this branch is going via a plt call stub, and if
1584 // so, mark it as having an R_PPC64_TOCSAVE.
1585 bool
1586 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1587 unsigned int shndx, Address offset,
1588 Target_powerpc* target, Symbol_table* symtab);
1589
1590 // If this branch needs a plt call stub, or a long branch stub, make one.
1591 bool
1592 make_stub(Stub_table<size, big_endian>*,
1593 Stub_table<size, big_endian>*,
1594 Symbol_table*) const;
1595
1596 private:
1597 // The branch location..
1598 Powerpc_relobj<size, big_endian>* object_;
1599 unsigned int shndx_;
1600 Address offset_;
1601 // ..and the branch type and destination.
1602 unsigned int r_type_ : 31;
1603 unsigned int tocsave_ : 1;
1604 unsigned int r_sym_;
1605 Address addend_;
1606 };
1607
1608 // Information about this specific target which we pass to the
1609 // general Target structure.
1610 static Target::Target_info powerpc_info;
1611
1612 // The types of GOT entries needed for this platform.
1613 // These values are exposed to the ABI in an incremental link.
1614 // Do not renumber existing values without changing the version
1615 // number of the .gnu_incremental_inputs section.
1616 enum Got_type
1617 {
1618 GOT_TYPE_STANDARD,
1619 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1620 GOT_TYPE_DTPREL, // entry for @got@dtprel
1621 GOT_TYPE_TPREL // entry for @got@tprel
1622 };
1623
1624 // The GOT section.
1625 Output_data_got_powerpc<size, big_endian>* got_;
1626 // The PLT section. This is a container for a table of addresses,
1627 // and their relocations. Each address in the PLT has a dynamic
1628 // relocation (R_*_JMP_SLOT) and each address will have a
1629 // corresponding entry in .glink for lazy resolution of the PLT.
1630 // ppc32 initialises the PLT to point at the .glink entry, while
1631 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1632 // linker adds a stub that loads the PLT entry into ctr then
1633 // branches to ctr. There may be more than one stub for each PLT
1634 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1635 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1636 Output_data_plt_powerpc<size, big_endian>* plt_;
1637 // The IPLT section. Like plt_, this is a container for a table of
1638 // addresses and their relocations, specifically for STT_GNU_IFUNC
1639 // functions that resolve locally (STT_GNU_IFUNC functions that
1640 // don't resolve locally go in PLT). Unlike plt_, these have no
1641 // entry in .glink for lazy resolution, and the relocation section
1642 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1643 // the relocation section may contain relocations against
1644 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1645 // relocation section will appear at the end of other dynamic
1646 // relocations, so that ld.so applies these relocations after other
1647 // dynamic relocations. In a static executable, the relocation
1648 // section is emitted and marked with __rela_iplt_start and
1649 // __rela_iplt_end symbols.
1650 Output_data_plt_powerpc<size, big_endian>* iplt_;
1651 // A PLT style section for local, non-ifunc symbols
1652 Output_data_plt_powerpc<size, big_endian>* lplt_;
1653 // Section holding long branch destinations.
1654 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1655 // The .glink section.
1656 Output_data_glink<size, big_endian>* glink_;
1657 // The dynamic reloc section.
1658 Reloc_section* rela_dyn_;
1659 // Relocs saved to avoid a COPY reloc.
1660 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1661 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1662 unsigned int tlsld_got_offset_;
1663
1664 Stub_tables stub_tables_;
1665 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1666 Branch_lookup_table branch_lookup_table_;
1667
1668 typedef std::vector<Branch_info> Branches;
1669 Branches branch_info_;
1670 Tocsave_loc tocsave_loc_;
1671
1672 bool plt_thread_safe_;
1673 bool plt_localentry0_;
1674 bool plt_localentry0_init_;
1675 bool has_localentry0_;
1676 bool has_tls_get_addr_opt_;
1677
1678 bool relax_failed_;
1679 int relax_fail_count_;
1680 int32_t stub_group_size_;
1681
1682 Output_data_save_res<size, big_endian> *savres_section_;
1683
1684 // The "__tls_get_addr" symbol, if present
1685 Symbol* tls_get_addr_;
1686 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1687 Symbol* tls_get_addr_opt_;
1688
1689 // Attributes in output.
1690 Attributes_section_data* attributes_section_data_;
1691
1692 // Last input file to change various attribute tags
1693 const char* last_fp_;
1694 const char* last_ld_;
1695 const char* last_vec_;
1696 const char* last_struct_;
1697 };
1698
1699 template<>
1700 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1701 {
1702 32, // size
1703 true, // is_big_endian
1704 elfcpp::EM_PPC, // machine_code
1705 false, // has_make_symbol
1706 false, // has_resolve
1707 false, // has_code_fill
1708 true, // is_default_stack_executable
1709 false, // can_icf_inline_merge_sections
1710 '\0', // wrap_char
1711 "/usr/lib/ld.so.1", // dynamic_linker
1712 0x10000000, // default_text_segment_address
1713 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1714 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1715 false, // isolate_execinstr
1716 0, // rosegment_gap
1717 elfcpp::SHN_UNDEF, // small_common_shndx
1718 elfcpp::SHN_UNDEF, // large_common_shndx
1719 0, // small_common_section_flags
1720 0, // large_common_section_flags
1721 NULL, // attributes_section
1722 NULL, // attributes_vendor
1723 "_start", // entry_symbol_name
1724 32, // hash_entry_size
1725 elfcpp::SHT_PROGBITS, // unwind_section_type
1726 };
1727
1728 template<>
1729 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1730 {
1731 32, // size
1732 false, // is_big_endian
1733 elfcpp::EM_PPC, // machine_code
1734 false, // has_make_symbol
1735 false, // has_resolve
1736 false, // has_code_fill
1737 true, // is_default_stack_executable
1738 false, // can_icf_inline_merge_sections
1739 '\0', // wrap_char
1740 "/usr/lib/ld.so.1", // dynamic_linker
1741 0x10000000, // default_text_segment_address
1742 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1743 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1744 false, // isolate_execinstr
1745 0, // rosegment_gap
1746 elfcpp::SHN_UNDEF, // small_common_shndx
1747 elfcpp::SHN_UNDEF, // large_common_shndx
1748 0, // small_common_section_flags
1749 0, // large_common_section_flags
1750 NULL, // attributes_section
1751 NULL, // attributes_vendor
1752 "_start", // entry_symbol_name
1753 32, // hash_entry_size
1754 elfcpp::SHT_PROGBITS, // unwind_section_type
1755 };
1756
1757 template<>
1758 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1759 {
1760 64, // size
1761 true, // is_big_endian
1762 elfcpp::EM_PPC64, // machine_code
1763 false, // has_make_symbol
1764 true, // has_resolve
1765 false, // has_code_fill
1766 false, // is_default_stack_executable
1767 false, // can_icf_inline_merge_sections
1768 '\0', // wrap_char
1769 "/usr/lib/ld.so.1", // dynamic_linker
1770 0x10000000, // default_text_segment_address
1771 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1772 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1773 false, // isolate_execinstr
1774 0, // rosegment_gap
1775 elfcpp::SHN_UNDEF, // small_common_shndx
1776 elfcpp::SHN_UNDEF, // large_common_shndx
1777 0, // small_common_section_flags
1778 0, // large_common_section_flags
1779 NULL, // attributes_section
1780 NULL, // attributes_vendor
1781 "_start", // entry_symbol_name
1782 32, // hash_entry_size
1783 elfcpp::SHT_PROGBITS, // unwind_section_type
1784 };
1785
1786 template<>
1787 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1788 {
1789 64, // size
1790 false, // is_big_endian
1791 elfcpp::EM_PPC64, // machine_code
1792 false, // has_make_symbol
1793 true, // has_resolve
1794 false, // has_code_fill
1795 false, // is_default_stack_executable
1796 false, // can_icf_inline_merge_sections
1797 '\0', // wrap_char
1798 "/usr/lib/ld.so.1", // dynamic_linker
1799 0x10000000, // default_text_segment_address
1800 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1801 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1802 false, // isolate_execinstr
1803 0, // rosegment_gap
1804 elfcpp::SHN_UNDEF, // small_common_shndx
1805 elfcpp::SHN_UNDEF, // large_common_shndx
1806 0, // small_common_section_flags
1807 0, // large_common_section_flags
1808 NULL, // attributes_section
1809 NULL, // attributes_vendor
1810 "_start", // entry_symbol_name
1811 32, // hash_entry_size
1812 elfcpp::SHT_PROGBITS, // unwind_section_type
1813 };
1814
1815 template<int size>
1816 inline bool
1817 is_branch_reloc(unsigned int r_type)
1818 {
1819 return (r_type == elfcpp::R_POWERPC_REL24
1820 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1821 || r_type == elfcpp::R_PPC_PLTREL24
1822 || r_type == elfcpp::R_PPC_LOCAL24PC
1823 || r_type == elfcpp::R_POWERPC_REL14
1824 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1825 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1826 || r_type == elfcpp::R_POWERPC_ADDR24
1827 || r_type == elfcpp::R_POWERPC_ADDR14
1828 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1829 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1830 }
1831
1832 // Reloc resolves to plt entry.
1833 template<int size>
1834 inline bool
1835 is_plt16_reloc(unsigned int r_type)
1836 {
1837 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1838 || r_type == elfcpp::R_POWERPC_PLT16_HI
1839 || r_type == elfcpp::R_POWERPC_PLT16_HA
1840 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1841 }
1842
1843 // If INSN is an opcode that may be used with an @tls operand, return
1844 // the transformed insn for TLS optimisation, otherwise return 0. If
1845 // REG is non-zero only match an insn with RB or RA equal to REG.
1846 uint32_t
1847 at_tls_transform(uint32_t insn, unsigned int reg)
1848 {
1849 if ((insn & (0x3f << 26)) != 31 << 26)
1850 return 0;
1851
1852 unsigned int rtra;
1853 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1854 rtra = insn & ((1 << 26) - (1 << 16));
1855 else if (((insn >> 16) & 0x1f) == reg)
1856 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1857 else
1858 return 0;
1859
1860 if ((insn & (0x3ff << 1)) == 266 << 1)
1861 // add -> addi
1862 insn = 14 << 26;
1863 else if ((insn & (0x1f << 1)) == 23 << 1
1864 && ((insn & (0x1f << 6)) < 14 << 6
1865 || ((insn & (0x1f << 6)) >= 16 << 6
1866 && (insn & (0x1f << 6)) < 24 << 6)))
1867 // load and store indexed -> dform
1868 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1869 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1870 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1871 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1872 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1873 // lwax -> lwa
1874 insn = (58 << 26) | 2;
1875 else
1876 return 0;
1877 insn |= rtra;
1878 return insn;
1879 }
1880
1881
1882 template<int size, bool big_endian>
1883 class Powerpc_relocate_functions
1884 {
1885 public:
1886 enum Overflow_check
1887 {
1888 CHECK_NONE,
1889 CHECK_SIGNED,
1890 CHECK_UNSIGNED,
1891 CHECK_BITFIELD,
1892 CHECK_LOW_INSN,
1893 CHECK_HIGH_INSN
1894 };
1895
1896 enum Status
1897 {
1898 STATUS_OK,
1899 STATUS_OVERFLOW
1900 };
1901
1902 private:
1903 typedef Powerpc_relocate_functions<size, big_endian> This;
1904 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1905 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1906
1907 template<int valsize>
1908 static inline bool
1909 has_overflow_signed(Address value)
1910 {
1911 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1912 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1913 limit <<= ((valsize - 1) >> 1);
1914 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1915 return value + limit > (limit << 1) - 1;
1916 }
1917
1918 template<int valsize>
1919 static inline bool
1920 has_overflow_unsigned(Address value)
1921 {
1922 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1923 limit <<= ((valsize - 1) >> 1);
1924 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1925 return value > (limit << 1) - 1;
1926 }
1927
1928 template<int valsize>
1929 static inline bool
1930 has_overflow_bitfield(Address value)
1931 {
1932 return (has_overflow_unsigned<valsize>(value)
1933 && has_overflow_signed<valsize>(value));
1934 }
1935
1936 template<int valsize>
1937 static inline Status
1938 overflowed(Address value, Overflow_check overflow)
1939 {
1940 if (overflow == CHECK_SIGNED)
1941 {
1942 if (has_overflow_signed<valsize>(value))
1943 return STATUS_OVERFLOW;
1944 }
1945 else if (overflow == CHECK_UNSIGNED)
1946 {
1947 if (has_overflow_unsigned<valsize>(value))
1948 return STATUS_OVERFLOW;
1949 }
1950 else if (overflow == CHECK_BITFIELD)
1951 {
1952 if (has_overflow_bitfield<valsize>(value))
1953 return STATUS_OVERFLOW;
1954 }
1955 return STATUS_OK;
1956 }
1957
1958 // Do a simple RELA relocation
1959 template<int fieldsize, int valsize>
1960 static inline Status
1961 rela(unsigned char* view, Address value, Overflow_check overflow)
1962 {
1963 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1964 Valtype* wv = reinterpret_cast<Valtype*>(view);
1965 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1966 return overflowed<valsize>(value, overflow);
1967 }
1968
1969 template<int fieldsize, int valsize>
1970 static inline Status
1971 rela(unsigned char* view,
1972 unsigned int right_shift,
1973 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1974 Address value,
1975 Overflow_check overflow)
1976 {
1977 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1978 Valtype* wv = reinterpret_cast<Valtype*>(view);
1979 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1980 Valtype reloc = value >> right_shift;
1981 val &= ~dst_mask;
1982 reloc &= dst_mask;
1983 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1984 return overflowed<valsize>(value >> right_shift, overflow);
1985 }
1986
1987 // Do a simple RELA relocation, unaligned.
1988 template<int fieldsize, int valsize>
1989 static inline Status
1990 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1991 {
1992 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1993 return overflowed<valsize>(value, overflow);
1994 }
1995
1996 template<int fieldsize, int valsize>
1997 static inline Status
1998 rela_ua(unsigned char* view,
1999 unsigned int right_shift,
2000 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2001 Address value,
2002 Overflow_check overflow)
2003 {
2004 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2005 Valtype;
2006 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2007 Valtype reloc = value >> right_shift;
2008 val &= ~dst_mask;
2009 reloc &= dst_mask;
2010 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2011 return overflowed<valsize>(value >> right_shift, overflow);
2012 }
2013
2014 public:
2015 // R_PPC64_ADDR64: (Symbol + Addend)
2016 static inline void
2017 addr64(unsigned char* view, Address value)
2018 { This::template rela<64,64>(view, value, CHECK_NONE); }
2019
2020 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2021 static inline void
2022 addr64_u(unsigned char* view, Address value)
2023 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2024
2025 // R_POWERPC_ADDR32: (Symbol + Addend)
2026 static inline Status
2027 addr32(unsigned char* view, Address value, Overflow_check overflow)
2028 { return This::template rela<32,32>(view, value, overflow); }
2029
2030 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2031 static inline Status
2032 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2033 { return This::template rela_ua<32,32>(view, value, overflow); }
2034
2035 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2036 static inline Status
2037 addr24(unsigned char* view, Address value, Overflow_check overflow)
2038 {
2039 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2040 value, overflow);
2041 if (overflow != CHECK_NONE && (value & 3) != 0)
2042 stat = STATUS_OVERFLOW;
2043 return stat;
2044 }
2045
2046 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2047 static inline Status
2048 addr16(unsigned char* view, Address value, Overflow_check overflow)
2049 { return This::template rela<16,16>(view, value, overflow); }
2050
2051 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2052 static inline Status
2053 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2054 { return This::template rela_ua<16,16>(view, value, overflow); }
2055
2056 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2057 static inline Status
2058 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2059 {
2060 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2061 if ((value & 3) != 0)
2062 stat = STATUS_OVERFLOW;
2063 return stat;
2064 }
2065
2066 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2067 static inline Status
2068 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2069 {
2070 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2071 if ((value & 15) != 0)
2072 stat = STATUS_OVERFLOW;
2073 return stat;
2074 }
2075
2076 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2077 static inline void
2078 addr16_hi(unsigned char* view, Address value)
2079 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2080
2081 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2082 static inline void
2083 addr16_ha(unsigned char* view, Address value)
2084 { This::addr16_hi(view, value + 0x8000); }
2085
2086 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2087 static inline void
2088 addr16_hi2(unsigned char* view, Address value)
2089 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2090
2091 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2092 static inline void
2093 addr16_ha2(unsigned char* view, Address value)
2094 { This::addr16_hi2(view, value + 0x8000); }
2095
2096 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2097 static inline void
2098 addr16_hi3(unsigned char* view, Address value)
2099 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2100
2101 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2102 static inline void
2103 addr16_ha3(unsigned char* view, Address value)
2104 { This::addr16_hi3(view, value + 0x8000); }
2105
2106 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2107 static inline Status
2108 addr14(unsigned char* view, Address value, Overflow_check overflow)
2109 {
2110 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2111 if (overflow != CHECK_NONE && (value & 3) != 0)
2112 stat = STATUS_OVERFLOW;
2113 return stat;
2114 }
2115
2116 // R_POWERPC_REL16DX_HA
2117 static inline Status
2118 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2119 {
2120 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2121 Valtype* wv = reinterpret_cast<Valtype*>(view);
2122 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2123 value += 0x8000;
2124 value = static_cast<SignedAddress>(value) >> 16;
2125 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2126 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2127 return overflowed<16>(value, overflow);
2128 }
2129 };
2130
2131 // Set ABI version for input and output.
2132
2133 template<int size, bool big_endian>
2134 void
2135 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2136 {
2137 this->e_flags_ |= ver;
2138 if (this->abiversion() != 0)
2139 {
2140 Target_powerpc<size, big_endian>* target =
2141 static_cast<Target_powerpc<size, big_endian>*>(
2142 parameters->sized_target<size, big_endian>());
2143 if (target->abiversion() == 0)
2144 target->set_abiversion(this->abiversion());
2145 else if (target->abiversion() != this->abiversion())
2146 gold_error(_("%s: ABI version %d is not compatible "
2147 "with ABI version %d output"),
2148 this->name().c_str(),
2149 this->abiversion(), target->abiversion());
2150
2151 }
2152 }
2153
2154 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2155 // relocatable object, if such sections exists.
2156
2157 template<int size, bool big_endian>
2158 bool
2159 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2160 Read_symbols_data* sd)
2161 {
2162 const unsigned char* const pshdrs = sd->section_headers->data();
2163 const unsigned char* namesu = sd->section_names->data();
2164 const char* names = reinterpret_cast<const char*>(namesu);
2165 section_size_type names_size = sd->section_names_size;
2166 const unsigned char* s;
2167
2168 s = this->template find_shdr<size, big_endian>(pshdrs,
2169 size == 32 ? ".got2" : ".opd",
2170 names, names_size, NULL);
2171 if (s != NULL)
2172 {
2173 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2174 this->special_ = ndx;
2175 if (size == 64)
2176 {
2177 if (this->abiversion() == 0)
2178 this->set_abiversion(1);
2179 else if (this->abiversion() > 1)
2180 gold_error(_("%s: .opd invalid in abiv%d"),
2181 this->name().c_str(), this->abiversion());
2182 }
2183 }
2184 if (size == 64)
2185 {
2186 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2187 names, names_size, NULL);
2188 if (s != NULL)
2189 {
2190 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2191 this->relatoc_ = ndx;
2192 typename elfcpp::Shdr<size, big_endian> shdr(s);
2193 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2194 }
2195 }
2196 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2197 }
2198
2199 // Examine .rela.opd to build info about function entry points.
2200
2201 template<int size, bool big_endian>
2202 void
2203 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2204 size_t reloc_count,
2205 const unsigned char* prelocs,
2206 const unsigned char* plocal_syms)
2207 {
2208 if (size == 64)
2209 {
2210 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2211 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2212 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2213 Address expected_off = 0;
2214 bool regular = true;
2215 unsigned int opd_ent_size = 0;
2216
2217 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2218 {
2219 Reltype reloc(prelocs);
2220 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2221 = reloc.get_r_info();
2222 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2223 if (r_type == elfcpp::R_PPC64_ADDR64)
2224 {
2225 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2226 typename elfcpp::Elf_types<size>::Elf_Addr value;
2227 bool is_ordinary;
2228 unsigned int shndx;
2229 if (r_sym < this->local_symbol_count())
2230 {
2231 typename elfcpp::Sym<size, big_endian>
2232 lsym(plocal_syms + r_sym * sym_size);
2233 shndx = lsym.get_st_shndx();
2234 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2235 value = lsym.get_st_value();
2236 }
2237 else
2238 shndx = this->symbol_section_and_value(r_sym, &value,
2239 &is_ordinary);
2240 this->set_opd_ent(reloc.get_r_offset(), shndx,
2241 value + reloc.get_r_addend());
2242 if (i == 2)
2243 {
2244 expected_off = reloc.get_r_offset();
2245 opd_ent_size = expected_off;
2246 }
2247 else if (expected_off != reloc.get_r_offset())
2248 regular = false;
2249 expected_off += opd_ent_size;
2250 }
2251 else if (r_type == elfcpp::R_PPC64_TOC)
2252 {
2253 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2254 regular = false;
2255 }
2256 else
2257 {
2258 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2259 this->name().c_str(), r_type);
2260 regular = false;
2261 }
2262 }
2263 if (reloc_count <= 2)
2264 opd_ent_size = this->section_size(this->opd_shndx());
2265 if (opd_ent_size != 24 && opd_ent_size != 16)
2266 regular = false;
2267 if (!regular)
2268 {
2269 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2270 this->name().c_str());
2271 opd_ent_size = 0;
2272 }
2273 }
2274 }
2275
2276 // Returns true if a code sequence loading the TOC entry at VALUE
2277 // relative to the TOC pointer can be converted into code calculating
2278 // a TOC pointer relative offset.
2279 // If so, the TOC pointer relative offset is stored to VALUE.
2280
2281 template<int size, bool big_endian>
2282 bool
2283 Powerpc_relobj<size, big_endian>::make_toc_relative(
2284 Target_powerpc<size, big_endian>* target,
2285 Address* value)
2286 {
2287 if (size != 64)
2288 return false;
2289
2290 // With -mcmodel=medium code it is quite possible to have
2291 // toc-relative relocs referring to objects outside the TOC.
2292 // Don't try to look at a non-existent TOC.
2293 if (this->toc_shndx() == 0)
2294 return false;
2295
2296 // Convert VALUE back to an address by adding got_base (see below),
2297 // then to an offset in the TOC by subtracting the TOC output
2298 // section address and the TOC output offset. Since this TOC output
2299 // section and the got output section are one and the same, we can
2300 // omit adding and subtracting the output section address.
2301 Address off = (*value + this->toc_base_offset()
2302 - this->output_section_offset(this->toc_shndx()));
2303 // Is this offset in the TOC? -mcmodel=medium code may be using
2304 // TOC relative access to variables outside the TOC. Those of
2305 // course can't be optimized. We also don't try to optimize code
2306 // that is using a different object's TOC.
2307 if (off >= this->section_size(this->toc_shndx()))
2308 return false;
2309
2310 if (this->no_toc_opt(off))
2311 return false;
2312
2313 section_size_type vlen;
2314 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2315 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2316 // The TOC pointer
2317 Address got_base = (target->got_section()->output_section()->address()
2318 + this->toc_base_offset());
2319 addr -= got_base;
2320 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2321 return false;
2322
2323 *value = addr;
2324 return true;
2325 }
2326
2327 // Perform the Sized_relobj_file method, then set up opd info from
2328 // .opd relocs.
2329
2330 template<int size, bool big_endian>
2331 void
2332 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2333 {
2334 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2335 if (size == 64)
2336 {
2337 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2338 p != rd->relocs.end();
2339 ++p)
2340 {
2341 if (p->data_shndx == this->opd_shndx())
2342 {
2343 uint64_t opd_size = this->section_size(this->opd_shndx());
2344 gold_assert(opd_size == static_cast<size_t>(opd_size));
2345 if (opd_size != 0)
2346 {
2347 this->init_opd(opd_size);
2348 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2349 rd->local_symbols->data());
2350 }
2351 break;
2352 }
2353 }
2354 }
2355 }
2356
2357 // Read the symbols then set up st_other vector.
2358
2359 template<int size, bool big_endian>
2360 void
2361 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2362 {
2363 this->base_read_symbols(sd);
2364 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2365 return;
2366 if (size == 64)
2367 {
2368 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2369 const unsigned char* const pshdrs = sd->section_headers->data();
2370 const unsigned int loccount = this->do_local_symbol_count();
2371 if (loccount != 0)
2372 {
2373 this->st_other_.resize(loccount);
2374 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2375 off_t locsize = loccount * sym_size;
2376 const unsigned int symtab_shndx = this->symtab_shndx();
2377 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2378 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2379 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2380 locsize, true, false);
2381 psyms += sym_size;
2382 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2383 {
2384 elfcpp::Sym<size, big_endian> sym(psyms);
2385 unsigned char st_other = sym.get_st_other();
2386 this->st_other_[i] = st_other;
2387 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2388 {
2389 if (this->abiversion() == 0)
2390 this->set_abiversion(2);
2391 else if (this->abiversion() < 2)
2392 gold_error(_("%s: local symbol %d has invalid st_other"
2393 " for ABI version 1"),
2394 this->name().c_str(), i);
2395 }
2396 }
2397 }
2398 }
2399
2400 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2401 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2402 bool merge_attributes = false;
2403 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2404 {
2405 elfcpp::Shdr<size, big_endian> shdr(ps);
2406 switch (shdr.get_sh_type())
2407 {
2408 case elfcpp::SHT_GNU_ATTRIBUTES:
2409 {
2410 gold_assert(this->attributes_section_data_ == NULL);
2411 section_offset_type section_offset = shdr.get_sh_offset();
2412 section_size_type section_size =
2413 convert_to_section_size_type(shdr.get_sh_size());
2414 const unsigned char* view =
2415 this->get_view(section_offset, section_size, true, false);
2416 this->attributes_section_data_ =
2417 new Attributes_section_data(view, section_size);
2418 }
2419 break;
2420
2421 case elfcpp::SHT_SYMTAB:
2422 {
2423 // Sometimes an object has no contents except the section
2424 // name string table and an empty symbol table with the
2425 // undefined symbol. We don't want to merge
2426 // processor-specific flags from such an object.
2427 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2428 elfcpp::Elf_sizes<size>::sym_size;
2429 if (shdr.get_sh_size() > sym_size)
2430 merge_attributes = true;
2431 }
2432 break;
2433
2434 case elfcpp::SHT_STRTAB:
2435 break;
2436
2437 default:
2438 merge_attributes = true;
2439 break;
2440 }
2441 }
2442
2443 if (!merge_attributes)
2444 {
2445 // Should rarely happen.
2446 delete this->attributes_section_data_;
2447 this->attributes_section_data_ = NULL;
2448 }
2449 }
2450
2451 template<int size, bool big_endian>
2452 void
2453 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2454 {
2455 this->e_flags_ |= ver;
2456 if (this->abiversion() != 0)
2457 {
2458 Target_powerpc<size, big_endian>* target =
2459 static_cast<Target_powerpc<size, big_endian>*>(
2460 parameters->sized_target<size, big_endian>());
2461 if (target->abiversion() == 0)
2462 target->set_abiversion(this->abiversion());
2463 else if (target->abiversion() != this->abiversion())
2464 gold_error(_("%s: ABI version %d is not compatible "
2465 "with ABI version %d output"),
2466 this->name().c_str(),
2467 this->abiversion(), target->abiversion());
2468
2469 }
2470 }
2471
2472 // Call Sized_dynobj::base_read_symbols to read the symbols then
2473 // read .opd from a dynamic object, filling in opd_ent_ vector,
2474
2475 template<int size, bool big_endian>
2476 void
2477 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2478 {
2479 this->base_read_symbols(sd);
2480 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2481 const unsigned char* ps =
2482 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2483 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2484 {
2485 elfcpp::Shdr<size, big_endian> shdr(ps);
2486 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2487 {
2488 section_offset_type section_offset = shdr.get_sh_offset();
2489 section_size_type section_size =
2490 convert_to_section_size_type(shdr.get_sh_size());
2491 const unsigned char* view =
2492 this->get_view(section_offset, section_size, true, false);
2493 this->attributes_section_data_ =
2494 new Attributes_section_data(view, section_size);
2495 break;
2496 }
2497 }
2498 if (size == 64)
2499 {
2500 const unsigned char* const pshdrs = sd->section_headers->data();
2501 const unsigned char* namesu = sd->section_names->data();
2502 const char* names = reinterpret_cast<const char*>(namesu);
2503 const unsigned char* s = NULL;
2504 const unsigned char* opd;
2505 section_size_type opd_size;
2506
2507 // Find and read .opd section.
2508 while (1)
2509 {
2510 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2511 sd->section_names_size,
2512 s);
2513 if (s == NULL)
2514 return;
2515
2516 typename elfcpp::Shdr<size, big_endian> shdr(s);
2517 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2518 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2519 {
2520 if (this->abiversion() == 0)
2521 this->set_abiversion(1);
2522 else if (this->abiversion() > 1)
2523 gold_error(_("%s: .opd invalid in abiv%d"),
2524 this->name().c_str(), this->abiversion());
2525
2526 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2527 this->opd_address_ = shdr.get_sh_addr();
2528 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2529 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2530 true, false);
2531 break;
2532 }
2533 }
2534
2535 // Build set of executable sections.
2536 // Using a set is probably overkill. There is likely to be only
2537 // a few executable sections, typically .init, .text and .fini,
2538 // and they are generally grouped together.
2539 typedef std::set<Sec_info> Exec_sections;
2540 Exec_sections exec_sections;
2541 s = pshdrs;
2542 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2543 {
2544 typename elfcpp::Shdr<size, big_endian> shdr(s);
2545 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2546 && ((shdr.get_sh_flags()
2547 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2548 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2549 && shdr.get_sh_size() != 0)
2550 {
2551 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2552 shdr.get_sh_size(), i));
2553 }
2554 }
2555 if (exec_sections.empty())
2556 return;
2557
2558 // Look over the OPD entries. This is complicated by the fact
2559 // that some binaries will use two-word entries while others
2560 // will use the standard three-word entries. In most cases
2561 // the third word (the environment pointer for languages like
2562 // Pascal) is unused and will be zero. If the third word is
2563 // used it should not be pointing into executable sections,
2564 // I think.
2565 this->init_opd(opd_size);
2566 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2567 {
2568 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2569 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2570 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2571 if (val == 0)
2572 // Chances are that this is the third word of an OPD entry.
2573 continue;
2574 typename Exec_sections::const_iterator e
2575 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2576 if (e != exec_sections.begin())
2577 {
2578 --e;
2579 if (e->start <= val && val < e->start + e->len)
2580 {
2581 // We have an address in an executable section.
2582 // VAL ought to be the function entry, set it up.
2583 this->set_opd_ent(p - opd, e->shndx, val);
2584 // Skip second word of OPD entry, the TOC pointer.
2585 p += 8;
2586 }
2587 }
2588 // If we didn't match any executable sections, we likely
2589 // have a non-zero third word in the OPD entry.
2590 }
2591 }
2592 }
2593
2594 // Relocate sections.
2595
2596 template<int size, bool big_endian>
2597 void
2598 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2599 const Symbol_table* symtab, const Layout* layout,
2600 const unsigned char* pshdrs, Output_file* of,
2601 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2602 {
2603 unsigned int start = 1;
2604 if (size == 64
2605 && this->relatoc_ != 0
2606 && !parameters->options().relocatable())
2607 {
2608 // Relocate .toc first.
2609 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2610 this->relatoc_, this->relatoc_);
2611 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2612 1, this->relatoc_ - 1);
2613 start = this->relatoc_ + 1;
2614 }
2615 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2616 start, this->shnum() - 1);
2617
2618 if (!parameters->options().output_is_position_independent())
2619 {
2620 Target_powerpc<size, big_endian>* target
2621 = static_cast<Target_powerpc<size, big_endian>*>(
2622 parameters->sized_target<size, big_endian>());
2623 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2624 {
2625 const section_size_type offset = target->lplt_section()->offset();
2626 const section_size_type oview_size
2627 = convert_to_section_size_type(target->lplt_section()->data_size());
2628 unsigned char* const oview = of->get_output_view(offset, oview_size);
2629
2630 bool modified = false;
2631 unsigned int nsyms = this->local_symbol_count();
2632 for (unsigned int i = 0; i < nsyms; i++)
2633 if (this->local_has_plt_offset(i))
2634 {
2635 Address value = this->local_symbol_value(i, 0);
2636 if (size == 64)
2637 value += ppc64_local_entry_offset(i);
2638 size_t off = this->local_plt_offset(i);
2639 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2640 modified = true;
2641 }
2642 if (modified)
2643 of->write_output_view(offset, oview_size, oview);
2644 }
2645 }
2646 }
2647
2648 // Set up some symbols.
2649
2650 template<int size, bool big_endian>
2651 void
2652 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2653 Symbol_table* symtab,
2654 Layout* layout)
2655 {
2656 if (size == 32)
2657 {
2658 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2659 // undefined when scanning relocs (and thus requires
2660 // non-relative dynamic relocs). The proper value will be
2661 // updated later.
2662 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2663 if (gotsym != NULL && gotsym->is_undefined())
2664 {
2665 Target_powerpc<size, big_endian>* target =
2666 static_cast<Target_powerpc<size, big_endian>*>(
2667 parameters->sized_target<size, big_endian>());
2668 Output_data_got_powerpc<size, big_endian>* got
2669 = target->got_section(symtab, layout);
2670 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2671 Symbol_table::PREDEFINED,
2672 got, 0, 0,
2673 elfcpp::STT_OBJECT,
2674 elfcpp::STB_LOCAL,
2675 elfcpp::STV_HIDDEN, 0,
2676 false, false);
2677 }
2678
2679 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2680 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2681 if (sdasym != NULL && sdasym->is_undefined())
2682 {
2683 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2684 Output_section* os
2685 = layout->add_output_section_data(".sdata", 0,
2686 elfcpp::SHF_ALLOC
2687 | elfcpp::SHF_WRITE,
2688 sdata, ORDER_SMALL_DATA, false);
2689 symtab->define_in_output_data("_SDA_BASE_", NULL,
2690 Symbol_table::PREDEFINED,
2691 os, 32768, 0, elfcpp::STT_OBJECT,
2692 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2693 0, false, false);
2694 }
2695 }
2696 else
2697 {
2698 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2699 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2700 if (gotsym != NULL && gotsym->is_undefined())
2701 {
2702 Target_powerpc<size, big_endian>* target =
2703 static_cast<Target_powerpc<size, big_endian>*>(
2704 parameters->sized_target<size, big_endian>());
2705 Output_data_got_powerpc<size, big_endian>* got
2706 = target->got_section(symtab, layout);
2707 symtab->define_in_output_data(".TOC.", NULL,
2708 Symbol_table::PREDEFINED,
2709 got, 0x8000, 0,
2710 elfcpp::STT_OBJECT,
2711 elfcpp::STB_LOCAL,
2712 elfcpp::STV_HIDDEN, 0,
2713 false, false);
2714 }
2715 }
2716
2717 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2718 if (parameters->options().tls_get_addr_optimize()
2719 && this->tls_get_addr_ != NULL
2720 && this->tls_get_addr_->in_reg())
2721 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2722 if (this->tls_get_addr_opt_ != NULL)
2723 {
2724 if (this->tls_get_addr_->is_undefined()
2725 || this->tls_get_addr_->is_from_dynobj())
2726 {
2727 // Make it seem as if references to __tls_get_addr are
2728 // really to __tls_get_addr_opt, so the latter symbol is
2729 // made dynamic, not the former.
2730 this->tls_get_addr_->clear_in_reg();
2731 this->tls_get_addr_opt_->set_in_reg();
2732 }
2733 // We have a non-dynamic definition for __tls_get_addr.
2734 // Make __tls_get_addr_opt the same, if it does not already have
2735 // a non-dynamic definition.
2736 else if (this->tls_get_addr_opt_->is_undefined()
2737 || this->tls_get_addr_opt_->is_from_dynobj())
2738 {
2739 Sized_symbol<size>* from
2740 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2741 Sized_symbol<size>* to
2742 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2743 symtab->clone<size>(to, from);
2744 }
2745 }
2746 }
2747
2748 // Set up PowerPC target specific relobj.
2749
2750 template<int size, bool big_endian>
2751 Object*
2752 Target_powerpc<size, big_endian>::do_make_elf_object(
2753 const std::string& name,
2754 Input_file* input_file,
2755 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2756 {
2757 int et = ehdr.get_e_type();
2758 // ET_EXEC files are valid input for --just-symbols/-R,
2759 // and we treat them as relocatable objects.
2760 if (et == elfcpp::ET_REL
2761 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2762 {
2763 Powerpc_relobj<size, big_endian>* obj =
2764 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2765 obj->setup();
2766 return obj;
2767 }
2768 else if (et == elfcpp::ET_DYN)
2769 {
2770 Powerpc_dynobj<size, big_endian>* obj =
2771 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2772 obj->setup();
2773 return obj;
2774 }
2775 else
2776 {
2777 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2778 return NULL;
2779 }
2780 }
2781
2782 template<int size, bool big_endian>
2783 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2784 {
2785 public:
2786 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2787 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2788
2789 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2790 : Output_data_got<size, big_endian>(),
2791 symtab_(symtab), layout_(layout),
2792 header_ent_cnt_(size == 32 ? 3 : 1),
2793 header_index_(size == 32 ? 0x2000 : 0)
2794 {
2795 if (size == 64)
2796 this->set_addralign(256);
2797 }
2798
2799 // Override all the Output_data_got methods we use so as to first call
2800 // reserve_ent().
2801 bool
2802 add_global(Symbol* gsym, unsigned int got_type)
2803 {
2804 this->reserve_ent();
2805 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2806 }
2807
2808 bool
2809 add_global_plt(Symbol* gsym, unsigned int got_type)
2810 {
2811 this->reserve_ent();
2812 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2813 }
2814
2815 bool
2816 add_global_tls(Symbol* gsym, unsigned int got_type)
2817 { return this->add_global_plt(gsym, got_type); }
2818
2819 void
2820 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2821 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2822 {
2823 this->reserve_ent();
2824 Output_data_got<size, big_endian>::
2825 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2826 }
2827
2828 void
2829 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2830 Output_data_reloc_generic* rel_dyn,
2831 unsigned int r_type_1, unsigned int r_type_2)
2832 {
2833 if (gsym->has_got_offset(got_type))
2834 return;
2835
2836 this->reserve_ent(2);
2837 Output_data_got<size, big_endian>::
2838 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2839 }
2840
2841 bool
2842 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2843 {
2844 this->reserve_ent();
2845 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2846 got_type);
2847 }
2848
2849 bool
2850 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2851 {
2852 this->reserve_ent();
2853 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2854 got_type);
2855 }
2856
2857 bool
2858 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2859 { return this->add_local_plt(object, sym_index, got_type); }
2860
2861 void
2862 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2863 unsigned int got_type,
2864 Output_data_reloc_generic* rel_dyn,
2865 unsigned int r_type)
2866 {
2867 if (object->local_has_got_offset(sym_index, got_type))
2868 return;
2869
2870 this->reserve_ent(2);
2871 Output_data_got<size, big_endian>::
2872 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2873 }
2874
2875 unsigned int
2876 add_constant(Valtype constant)
2877 {
2878 this->reserve_ent();
2879 return Output_data_got<size, big_endian>::add_constant(constant);
2880 }
2881
2882 unsigned int
2883 add_constant_pair(Valtype c1, Valtype c2)
2884 {
2885 this->reserve_ent(2);
2886 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2887 }
2888
2889 // Offset of _GLOBAL_OFFSET_TABLE_.
2890 unsigned int
2891 g_o_t() const
2892 {
2893 return this->got_offset(this->header_index_);
2894 }
2895
2896 // Offset of base used to access the GOT/TOC.
2897 // The got/toc pointer reg will be set to this value.
2898 Valtype
2899 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2900 {
2901 if (size == 32)
2902 return this->g_o_t();
2903 else
2904 return (this->output_section()->address()
2905 + object->toc_base_offset()
2906 - this->address());
2907 }
2908
2909 // Ensure our GOT has a header.
2910 void
2911 set_final_data_size()
2912 {
2913 if (this->header_ent_cnt_ != 0)
2914 this->make_header();
2915 Output_data_got<size, big_endian>::set_final_data_size();
2916 }
2917
2918 // First word of GOT header needs some values that are not
2919 // handled by Output_data_got so poke them in here.
2920 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2921 void
2922 do_write(Output_file* of)
2923 {
2924 Valtype val = 0;
2925 if (size == 32 && this->layout_->dynamic_data() != NULL)
2926 val = this->layout_->dynamic_section()->address();
2927 if (size == 64)
2928 val = this->output_section()->address() + 0x8000;
2929 this->replace_constant(this->header_index_, val);
2930 Output_data_got<size, big_endian>::do_write(of);
2931 }
2932
2933 private:
2934 void
2935 reserve_ent(unsigned int cnt = 1)
2936 {
2937 if (this->header_ent_cnt_ == 0)
2938 return;
2939 if (this->num_entries() + cnt > this->header_index_)
2940 this->make_header();
2941 }
2942
2943 void
2944 make_header()
2945 {
2946 this->header_ent_cnt_ = 0;
2947 this->header_index_ = this->num_entries();
2948 if (size == 32)
2949 {
2950 Output_data_got<size, big_endian>::add_constant(0);
2951 Output_data_got<size, big_endian>::add_constant(0);
2952 Output_data_got<size, big_endian>::add_constant(0);
2953
2954 // Define _GLOBAL_OFFSET_TABLE_ at the header
2955 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2956 if (gotsym != NULL)
2957 {
2958 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2959 sym->set_value(this->g_o_t());
2960 }
2961 else
2962 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2963 Symbol_table::PREDEFINED,
2964 this, this->g_o_t(), 0,
2965 elfcpp::STT_OBJECT,
2966 elfcpp::STB_LOCAL,
2967 elfcpp::STV_HIDDEN, 0,
2968 false, false);
2969 }
2970 else
2971 Output_data_got<size, big_endian>::add_constant(0);
2972 }
2973
2974 // Stashed pointers.
2975 Symbol_table* symtab_;
2976 Layout* layout_;
2977
2978 // GOT header size.
2979 unsigned int header_ent_cnt_;
2980 // GOT header index.
2981 unsigned int header_index_;
2982 };
2983
2984 // Get the GOT section, creating it if necessary.
2985
2986 template<int size, bool big_endian>
2987 Output_data_got_powerpc<size, big_endian>*
2988 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2989 Layout* layout)
2990 {
2991 if (this->got_ == NULL)
2992 {
2993 gold_assert(symtab != NULL && layout != NULL);
2994
2995 this->got_
2996 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2997
2998 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2999 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3000 this->got_, ORDER_DATA, false);
3001 }
3002
3003 return this->got_;
3004 }
3005
3006 // Get the dynamic reloc section, creating it if necessary.
3007
3008 template<int size, bool big_endian>
3009 typename Target_powerpc<size, big_endian>::Reloc_section*
3010 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3011 {
3012 if (this->rela_dyn_ == NULL)
3013 {
3014 gold_assert(layout != NULL);
3015 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3016 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3017 elfcpp::SHF_ALLOC, this->rela_dyn_,
3018 ORDER_DYNAMIC_RELOCS, false);
3019 }
3020 return this->rela_dyn_;
3021 }
3022
3023 // Similarly, but for ifunc symbols get the one for ifunc.
3024
3025 template<int size, bool big_endian>
3026 typename Target_powerpc<size, big_endian>::Reloc_section*
3027 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3028 Layout* layout,
3029 bool for_ifunc)
3030 {
3031 if (!for_ifunc)
3032 return this->rela_dyn_section(layout);
3033
3034 if (this->iplt_ == NULL)
3035 this->make_iplt_section(symtab, layout);
3036 return this->iplt_->rel_plt();
3037 }
3038
3039 class Stub_control
3040 {
3041 public:
3042 // Determine the stub group size. The group size is the absolute
3043 // value of the parameter --stub-group-size. If --stub-group-size
3044 // is passed a negative value, we restrict stubs to be always after
3045 // the stubbed branches.
3046 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3047 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3048 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3049 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3050 owner_(NULL), output_section_(NULL)
3051 {
3052 }
3053
3054 // Return true iff input section can be handled by current stub
3055 // group.
3056 bool
3057 can_add_to_stub_group(Output_section* o,
3058 const Output_section::Input_section* i,
3059 bool has14);
3060
3061 const Output_section::Input_section*
3062 owner()
3063 { return owner_; }
3064
3065 Output_section*
3066 output_section()
3067 { return output_section_; }
3068
3069 void
3070 set_output_and_owner(Output_section* o,
3071 const Output_section::Input_section* i)
3072 {
3073 this->output_section_ = o;
3074 this->owner_ = i;
3075 }
3076
3077 private:
3078 typedef enum
3079 {
3080 // Initial state.
3081 NO_GROUP,
3082 // Adding group sections before the stubs.
3083 FINDING_STUB_SECTION,
3084 // Adding group sections after the stubs.
3085 HAS_STUB_SECTION
3086 } State;
3087
3088 uint32_t stub_group_size_;
3089 bool stubs_always_after_branch_;
3090 bool suppress_size_errors_;
3091 // True if a stub group can serve multiple output sections.
3092 bool multi_os_;
3093 State state_;
3094 // Current max size of group. Starts at stub_group_size_ but is
3095 // reduced to stub_group_size_/1024 on seeing a section with
3096 // external conditional branches.
3097 uint32_t group_size_;
3098 uint64_t group_start_addr_;
3099 // owner_ and output_section_ specify the section to which stubs are
3100 // attached. The stubs are placed at the end of this section.
3101 const Output_section::Input_section* owner_;
3102 Output_section* output_section_;
3103 };
3104
3105 // Return true iff input section can be handled by current stub
3106 // group. Sections are presented to this function in order,
3107 // so the first section is the head of the group.
3108
3109 bool
3110 Stub_control::can_add_to_stub_group(Output_section* o,
3111 const Output_section::Input_section* i,
3112 bool has14)
3113 {
3114 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3115 uint64_t this_size;
3116 uint64_t start_addr = o->address();
3117
3118 if (whole_sec)
3119 // .init and .fini sections are pasted together to form a single
3120 // function. We can't be adding stubs in the middle of the function.
3121 this_size = o->data_size();
3122 else
3123 {
3124 start_addr += i->relobj()->output_section_offset(i->shndx());
3125 this_size = i->data_size();
3126 }
3127
3128 uint64_t end_addr = start_addr + this_size;
3129 uint32_t group_size = this->stub_group_size_;
3130 if (has14)
3131 this->group_size_ = group_size = group_size >> 10;
3132
3133 if (this_size > group_size && !this->suppress_size_errors_)
3134 gold_warning(_("%s:%s exceeds group size"),
3135 i->relobj()->name().c_str(),
3136 i->relobj()->section_name(i->shndx()).c_str());
3137
3138 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3139 has14 ? " 14bit" : "",
3140 i->relobj()->name().c_str(),
3141 i->relobj()->section_name(i->shndx()).c_str(),
3142 (long long) this_size,
3143 (this->state_ == NO_GROUP
3144 ? this_size
3145 : (long long) end_addr - this->group_start_addr_));
3146
3147 if (this->state_ == NO_GROUP)
3148 {
3149 // Only here on very first use of Stub_control
3150 this->owner_ = i;
3151 this->output_section_ = o;
3152 this->state_ = FINDING_STUB_SECTION;
3153 this->group_size_ = group_size;
3154 this->group_start_addr_ = start_addr;
3155 return true;
3156 }
3157 else if (!this->multi_os_ && this->output_section_ != o)
3158 ;
3159 else if (this->state_ == HAS_STUB_SECTION)
3160 {
3161 // Can we add this section, which is after the stubs, to the
3162 // group?
3163 if (end_addr - this->group_start_addr_ <= this->group_size_)
3164 return true;
3165 }
3166 else if (this->state_ == FINDING_STUB_SECTION)
3167 {
3168 if ((whole_sec && this->output_section_ == o)
3169 || end_addr - this->group_start_addr_ <= this->group_size_)
3170 {
3171 // Stubs are added at the end of "owner_".
3172 this->owner_ = i;
3173 this->output_section_ = o;
3174 return true;
3175 }
3176 // The group before the stubs has reached maximum size.
3177 // Now see about adding sections after the stubs to the
3178 // group. If the current section has a 14-bit branch and
3179 // the group before the stubs exceeds group_size_ (because
3180 // they didn't have 14-bit branches), don't add sections
3181 // after the stubs: The size of stubs for such a large
3182 // group may exceed the reach of a 14-bit branch.
3183 if (!this->stubs_always_after_branch_
3184 && this_size <= this->group_size_
3185 && start_addr - this->group_start_addr_ <= this->group_size_)
3186 {
3187 gold_debug(DEBUG_TARGET, "adding after stubs");
3188 this->state_ = HAS_STUB_SECTION;
3189 this->group_start_addr_ = start_addr;
3190 return true;
3191 }
3192 }
3193 else
3194 gold_unreachable();
3195
3196 gold_debug(DEBUG_TARGET,
3197 !this->multi_os_ && this->output_section_ != o
3198 ? "nope, new output section\n"
3199 : "nope, didn't fit\n");
3200
3201 // The section fails to fit in the current group. Set up a few
3202 // things for the next group. owner_ and output_section_ will be
3203 // set later after we've retrieved those values for the current
3204 // group.
3205 this->state_ = FINDING_STUB_SECTION;
3206 this->group_size_ = group_size;
3207 this->group_start_addr_ = start_addr;
3208 return false;
3209 }
3210
3211 // Look over all the input sections, deciding where to place stubs.
3212
3213 template<int size, bool big_endian>
3214 void
3215 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3216 const Task*,
3217 bool no_size_errors)
3218 {
3219 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3220 parameters->options().stub_group_multi());
3221
3222 // Group input sections and insert stub table
3223 Stub_table_owner* table_owner = NULL;
3224 std::vector<Stub_table_owner*> tables;
3225 Layout::Section_list section_list;
3226 layout->get_executable_sections(&section_list);
3227 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3228 for (Layout::Section_list::iterator o = section_list.begin();
3229 o != section_list.end();
3230 ++o)
3231 {
3232 typedef Output_section::Input_section_list Input_section_list;
3233 for (Input_section_list::const_iterator i
3234 = (*o)->input_sections().begin();
3235 i != (*o)->input_sections().end();
3236 ++i)
3237 {
3238 if (i->is_input_section()
3239 || i->is_relaxed_input_section())
3240 {
3241 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3242 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3243 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3244 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3245 {
3246 table_owner->output_section = stub_control.output_section();
3247 table_owner->owner = stub_control.owner();
3248 stub_control.set_output_and_owner(*o, &*i);
3249 table_owner = NULL;
3250 }
3251 if (table_owner == NULL)
3252 {
3253 table_owner = new Stub_table_owner;
3254 tables.push_back(table_owner);
3255 }
3256 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3257 }
3258 }
3259 }
3260 if (table_owner != NULL)
3261 {
3262 table_owner->output_section = stub_control.output_section();
3263 table_owner->owner = stub_control.owner();;
3264 }
3265 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3266 t != tables.end();
3267 ++t)
3268 {
3269 Stub_table<size, big_endian>* stub_table;
3270
3271 if ((*t)->owner->is_input_section())
3272 stub_table = new Stub_table<size, big_endian>(this,
3273 (*t)->output_section,
3274 (*t)->owner,
3275 this->stub_tables_.size());
3276 else if ((*t)->owner->is_relaxed_input_section())
3277 stub_table = static_cast<Stub_table<size, big_endian>*>(
3278 (*t)->owner->relaxed_input_section());
3279 else
3280 gold_unreachable();
3281 this->stub_tables_.push_back(stub_table);
3282 delete *t;
3283 }
3284 }
3285
3286 template<int size>
3287 static unsigned long
3288 max_branch_delta (unsigned int r_type)
3289 {
3290 if (r_type == elfcpp::R_POWERPC_REL14
3291 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3292 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3293 return 1L << 15;
3294 if (r_type == elfcpp::R_POWERPC_REL24
3295 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3296 || r_type == elfcpp::R_PPC_PLTREL24
3297 || r_type == elfcpp::R_PPC_LOCAL24PC)
3298 return 1L << 25;
3299 return 0;
3300 }
3301
3302 // Return whether this branch is going via a plt call stub.
3303
3304 template<int size, bool big_endian>
3305 bool
3306 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3307 Powerpc_relobj<size, big_endian>* ppc_object,
3308 unsigned int shndx,
3309 Address offset,
3310 Target_powerpc* target,
3311 Symbol_table* symtab)
3312 {
3313 if (this->object_ != ppc_object
3314 || this->shndx_ != shndx
3315 || this->offset_ != offset)
3316 return false;
3317
3318 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3319 if (sym != NULL && sym->is_forwarder())
3320 sym = symtab->resolve_forwards(sym);
3321 if (target->replace_tls_get_addr(sym))
3322 sym = target->tls_get_addr_opt();
3323 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3324 if (gsym != NULL
3325 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3326 && !target->is_elfv2_localentry0(gsym))
3327 : (this->object_->local_has_plt_offset(this->r_sym_)
3328 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3329 {
3330 this->tocsave_ = 1;
3331 return true;
3332 }
3333 return false;
3334 }
3335
3336 // If this branch needs a plt call stub, or a long branch stub, make one.
3337
3338 template<int size, bool big_endian>
3339 bool
3340 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3341 Stub_table<size, big_endian>* stub_table,
3342 Stub_table<size, big_endian>* ifunc_stub_table,
3343 Symbol_table* symtab) const
3344 {
3345 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3346 Target_powerpc<size, big_endian>* target =
3347 static_cast<Target_powerpc<size, big_endian>*>(
3348 parameters->sized_target<size, big_endian>());
3349 if (sym != NULL && sym->is_forwarder())
3350 sym = symtab->resolve_forwards(sym);
3351 if (target->replace_tls_get_addr(sym))
3352 sym = target->tls_get_addr_opt();
3353 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3354 bool ok = true;
3355
3356 if (gsym != NULL
3357 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3358 : this->object_->local_has_plt_offset(this->r_sym_))
3359 {
3360 if (size == 64
3361 && gsym != NULL
3362 && target->abiversion() >= 2
3363 && !parameters->options().output_is_position_independent()
3364 && !is_branch_reloc<size>(this->r_type_))
3365 target->glink_section()->add_global_entry(gsym);
3366 else
3367 {
3368 if (stub_table == NULL
3369 && !(size == 32
3370 && gsym != NULL
3371 && !parameters->options().output_is_position_independent()
3372 && !is_branch_reloc<size>(this->r_type_)))
3373 stub_table = this->object_->stub_table(this->shndx_);
3374 if (stub_table == NULL)
3375 {
3376 // This is a ref from a data section to an ifunc symbol,
3377 // or a non-branch reloc for which we always want to use
3378 // one set of stubs for resolving function addresses.
3379 stub_table = ifunc_stub_table;
3380 }
3381 gold_assert(stub_table != NULL);
3382 Address from = this->object_->get_output_section_offset(this->shndx_);
3383 if (from != invalid_address)
3384 from += (this->object_->output_section(this->shndx_)->address()
3385 + this->offset_);
3386 if (gsym != NULL)
3387 ok = stub_table->add_plt_call_entry(from,
3388 this->object_, gsym,
3389 this->r_type_, this->addend_,
3390 this->tocsave_);
3391 else
3392 ok = stub_table->add_plt_call_entry(from,
3393 this->object_, this->r_sym_,
3394 this->r_type_, this->addend_,
3395 this->tocsave_);
3396 }
3397 }
3398 else
3399 {
3400 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3401 if (max_branch_offset == 0)
3402 return true;
3403 Address from = this->object_->get_output_section_offset(this->shndx_);
3404 gold_assert(from != invalid_address);
3405 from += (this->object_->output_section(this->shndx_)->address()
3406 + this->offset_);
3407 Address to;
3408 if (gsym != NULL)
3409 {
3410 switch (gsym->source())
3411 {
3412 case Symbol::FROM_OBJECT:
3413 {
3414 Object* symobj = gsym->object();
3415 if (symobj->is_dynamic()
3416 || symobj->pluginobj() != NULL)
3417 return true;
3418 bool is_ordinary;
3419 unsigned int shndx = gsym->shndx(&is_ordinary);
3420 if (shndx == elfcpp::SHN_UNDEF)
3421 return true;
3422 }
3423 break;
3424
3425 case Symbol::IS_UNDEFINED:
3426 return true;
3427
3428 default:
3429 break;
3430 }
3431 Symbol_table::Compute_final_value_status status;
3432 to = symtab->compute_final_value<size>(gsym, &status);
3433 if (status != Symbol_table::CFVS_OK)
3434 return true;
3435 if (size == 64)
3436 to += this->object_->ppc64_local_entry_offset(gsym);
3437 }
3438 else
3439 {
3440 const Symbol_value<size>* psymval
3441 = this->object_->local_symbol(this->r_sym_);
3442 Symbol_value<size> symval;
3443 if (psymval->is_section_symbol())
3444 symval.set_is_section_symbol();
3445 typedef Sized_relobj_file<size, big_endian> ObjType;
3446 typename ObjType::Compute_final_local_value_status status
3447 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3448 &symval, symtab);
3449 if (status != ObjType::CFLV_OK
3450 || !symval.has_output_value())
3451 return true;
3452 to = symval.value(this->object_, 0);
3453 if (size == 64)
3454 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3455 }
3456 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3457 to += this->addend_;
3458 if (stub_table == NULL)
3459 stub_table = this->object_->stub_table(this->shndx_);
3460 if (size == 64 && target->abiversion() < 2)
3461 {
3462 unsigned int dest_shndx;
3463 if (!target->symval_for_branch(symtab, gsym, this->object_,
3464 &to, &dest_shndx))
3465 return true;
3466 }
3467 Address delta = to - from;
3468 if (delta + max_branch_offset >= 2 * max_branch_offset
3469 || (size == 64
3470 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3471 && (gsym != NULL
3472 ? this->object_->ppc64_needs_toc(gsym)
3473 : this->object_->ppc64_needs_toc(this->r_sym_))))
3474 {
3475 if (stub_table == NULL)
3476 {
3477 gold_warning(_("%s:%s: branch in non-executable section,"
3478 " no long branch stub for you"),
3479 this->object_->name().c_str(),
3480 this->object_->section_name(this->shndx_).c_str());
3481 return true;
3482 }
3483 bool save_res = (size == 64
3484 && gsym != NULL
3485 && gsym->source() == Symbol::IN_OUTPUT_DATA
3486 && gsym->output_data() == target->savres_section());
3487 ok = stub_table->add_long_branch_entry(this->object_,
3488 this->r_type_,
3489 from, to, save_res);
3490 }
3491 }
3492 if (!ok)
3493 gold_debug(DEBUG_TARGET,
3494 "branch at %s:%s+%#lx\n"
3495 "can't reach stub attached to %s:%s",
3496 this->object_->name().c_str(),
3497 this->object_->section_name(this->shndx_).c_str(),
3498 (unsigned long) this->offset_,
3499 stub_table->relobj()->name().c_str(),
3500 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3501
3502 return ok;
3503 }
3504
3505 // Relaxation hook. This is where we do stub generation.
3506
3507 template<int size, bool big_endian>
3508 bool
3509 Target_powerpc<size, big_endian>::do_relax(int pass,
3510 const Input_objects*,
3511 Symbol_table* symtab,
3512 Layout* layout,
3513 const Task* task)
3514 {
3515 unsigned int prev_brlt_size = 0;
3516 if (pass == 1)
3517 {
3518 bool thread_safe
3519 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3520 if (size == 64
3521 && this->abiversion() < 2
3522 && !thread_safe
3523 && !parameters->options().user_set_plt_thread_safe())
3524 {
3525 static const char* const thread_starter[] =
3526 {
3527 "pthread_create",
3528 /* libstdc++ */
3529 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3530 /* librt */
3531 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3532 "mq_notify", "create_timer",
3533 /* libanl */
3534 "getaddrinfo_a",
3535 /* libgomp */
3536 "GOMP_parallel",
3537 "GOMP_parallel_start",
3538 "GOMP_parallel_loop_static",
3539 "GOMP_parallel_loop_static_start",
3540 "GOMP_parallel_loop_dynamic",
3541 "GOMP_parallel_loop_dynamic_start",
3542 "GOMP_parallel_loop_guided",
3543 "GOMP_parallel_loop_guided_start",
3544 "GOMP_parallel_loop_runtime",
3545 "GOMP_parallel_loop_runtime_start",
3546 "GOMP_parallel_sections",
3547 "GOMP_parallel_sections_start",
3548 /* libgo */
3549 "__go_go",
3550 };
3551
3552 if (parameters->options().shared())
3553 thread_safe = true;
3554 else
3555 {
3556 for (unsigned int i = 0;
3557 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3558 i++)
3559 {
3560 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3561 thread_safe = (sym != NULL
3562 && sym->in_reg()
3563 && sym->in_real_elf());
3564 if (thread_safe)
3565 break;
3566 }
3567 }
3568 }
3569 this->plt_thread_safe_ = thread_safe;
3570 }
3571
3572 if (pass == 1)
3573 {
3574 this->stub_group_size_ = parameters->options().stub_group_size();
3575 bool no_size_errors = true;
3576 if (this->stub_group_size_ == 1)
3577 this->stub_group_size_ = 0x1c00000;
3578 else if (this->stub_group_size_ == -1)
3579 this->stub_group_size_ = -0x1e00000;
3580 else
3581 no_size_errors = false;
3582 this->group_sections(layout, task, no_size_errors);
3583 }
3584 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3585 {
3586 this->branch_lookup_table_.clear();
3587 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3588 p != this->stub_tables_.end();
3589 ++p)
3590 {
3591 (*p)->clear_stubs(true);
3592 }
3593 this->stub_tables_.clear();
3594 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3595 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3596 program_name, this->stub_group_size_);
3597 this->group_sections(layout, task, true);
3598 }
3599
3600 // We need address of stub tables valid for make_stub.
3601 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3602 p != this->stub_tables_.end();
3603 ++p)
3604 {
3605 const Powerpc_relobj<size, big_endian>* object
3606 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3607 Address off = object->get_output_section_offset((*p)->shndx());
3608 gold_assert(off != invalid_address);
3609 Output_section* os = (*p)->output_section();
3610 (*p)->set_address_and_size(os, off);
3611 }
3612
3613 if (pass != 1)
3614 {
3615 // Clear plt call stubs, long branch stubs and branch lookup table.
3616 prev_brlt_size = this->branch_lookup_table_.size();
3617 this->branch_lookup_table_.clear();
3618 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3619 p != this->stub_tables_.end();
3620 ++p)
3621 {
3622 (*p)->clear_stubs(false);
3623 }
3624 }
3625
3626 // Build all the stubs.
3627 this->relax_failed_ = false;
3628 Stub_table<size, big_endian>* ifunc_stub_table
3629 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3630 Stub_table<size, big_endian>* one_stub_table
3631 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3632 for (typename Branches::const_iterator b = this->branch_info_.begin();
3633 b != this->branch_info_.end();
3634 b++)
3635 {
3636 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3637 && !this->relax_failed_)
3638 {
3639 this->relax_failed_ = true;
3640 this->relax_fail_count_++;
3641 if (this->relax_fail_count_ < 3)
3642 return true;
3643 }
3644 }
3645 bool do_resize = false;
3646 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3647 p != this->stub_tables_.end();
3648 ++p)
3649 if ((*p)->need_resize())
3650 {
3651 do_resize = true;
3652 break;
3653 }
3654 if (do_resize)
3655 {
3656 this->branch_lookup_table_.clear();
3657 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3658 p != this->stub_tables_.end();
3659 ++p)
3660 (*p)->set_resizing(true);
3661 for (typename Branches::const_iterator b = this->branch_info_.begin();
3662 b != this->branch_info_.end();
3663 b++)
3664 {
3665 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3666 && !this->relax_failed_)
3667 {
3668 this->relax_failed_ = true;
3669 this->relax_fail_count_++;
3670 if (this->relax_fail_count_ < 3)
3671 return true;
3672 }
3673 }
3674 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3675 p != this->stub_tables_.end();
3676 ++p)
3677 (*p)->set_resizing(false);
3678 }
3679
3680 // Did anything change size?
3681 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3682 bool again = num_huge_branches != prev_brlt_size;
3683 if (size == 64 && num_huge_branches != 0)
3684 this->make_brlt_section(layout);
3685 if (size == 64 && again)
3686 this->brlt_section_->set_current_size(num_huge_branches);
3687
3688 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3689 p != this->stub_tables_.rend();
3690 ++p)
3691 (*p)->remove_eh_frame(layout);
3692
3693 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3694 p != this->stub_tables_.end();
3695 ++p)
3696 (*p)->add_eh_frame(layout);
3697
3698 typedef Unordered_set<Output_section*> Output_sections;
3699 Output_sections os_need_update;
3700 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3701 p != this->stub_tables_.end();
3702 ++p)
3703 {
3704 if ((*p)->size_update())
3705 {
3706 again = true;
3707 os_need_update.insert((*p)->output_section());
3708 }
3709 }
3710
3711 // Set output section offsets for all input sections in an output
3712 // section that just changed size. Anything past the stubs will
3713 // need updating.
3714 for (typename Output_sections::iterator p = os_need_update.begin();
3715 p != os_need_update.end();
3716 p++)
3717 {
3718 Output_section* os = *p;
3719 Address off = 0;
3720 typedef Output_section::Input_section_list Input_section_list;
3721 for (Input_section_list::const_iterator i = os->input_sections().begin();
3722 i != os->input_sections().end();
3723 ++i)
3724 {
3725 off = align_address(off, i->addralign());
3726 if (i->is_input_section() || i->is_relaxed_input_section())
3727 i->relobj()->set_section_offset(i->shndx(), off);
3728 if (i->is_relaxed_input_section())
3729 {
3730 Stub_table<size, big_endian>* stub_table
3731 = static_cast<Stub_table<size, big_endian>*>(
3732 i->relaxed_input_section());
3733 Address stub_table_size = stub_table->set_address_and_size(os, off);
3734 off += stub_table_size;
3735 // After a few iterations, set current stub table size
3736 // as min size threshold, so later stub tables can only
3737 // grow in size.
3738 if (pass >= 4)
3739 stub_table->set_min_size_threshold(stub_table_size);
3740 }
3741 else
3742 off += i->data_size();
3743 }
3744 // If .branch_lt is part of this output section, then we have
3745 // just done the offset adjustment.
3746 os->clear_section_offsets_need_adjustment();
3747 }
3748
3749 if (size == 64
3750 && !again
3751 && num_huge_branches != 0
3752 && parameters->options().output_is_position_independent())
3753 {
3754 // Fill in the BRLT relocs.
3755 this->brlt_section_->reset_brlt_sizes();
3756 for (typename Branch_lookup_table::const_iterator p
3757 = this->branch_lookup_table_.begin();
3758 p != this->branch_lookup_table_.end();
3759 ++p)
3760 {
3761 this->brlt_section_->add_reloc(p->first, p->second);
3762 }
3763 this->brlt_section_->finalize_brlt_sizes();
3764 }
3765
3766 if (!again
3767 && (parameters->options().user_set_emit_stub_syms()
3768 ? parameters->options().emit_stub_syms()
3769 : (size == 64
3770 || parameters->options().output_is_position_independent()
3771 || parameters->options().emit_relocs())))
3772 {
3773 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3774 p != this->stub_tables_.end();
3775 ++p)
3776 (*p)->define_stub_syms(symtab);
3777
3778 if (this->glink_ != NULL)
3779 {
3780 int stub_size = this->glink_->pltresolve_size();
3781 Address value = -stub_size;
3782 if (size == 64)
3783 {
3784 value = 8;
3785 stub_size -= 8;
3786 }
3787 this->define_local(symtab, "__glink_PLTresolve",
3788 this->glink_, value, stub_size);
3789
3790 if (size != 64)
3791 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3792 }
3793 }
3794
3795 return again;
3796 }
3797
3798 template<int size, bool big_endian>
3799 void
3800 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3801 unsigned char* oview,
3802 uint64_t* paddress,
3803 off_t* plen) const
3804 {
3805 uint64_t address = plt->address();
3806 off_t len = plt->data_size();
3807
3808 if (plt == this->glink_)
3809 {
3810 // See Output_data_glink::do_write() for glink contents.
3811 if (len == 0)
3812 {
3813 gold_assert(parameters->doing_static_link());
3814 // Static linking may need stubs, to support ifunc and long
3815 // branches. We need to create an output section for
3816 // .eh_frame early in the link process, to have a place to
3817 // attach stub .eh_frame info. We also need to have
3818 // registered a CIE that matches the stub CIE. Both of
3819 // these requirements are satisfied by creating an FDE and
3820 // CIE for .glink, even though static linking will leave
3821 // .glink zero length.
3822 // ??? Hopefully generating an FDE with a zero address range
3823 // won't confuse anything that consumes .eh_frame info.
3824 }
3825 else if (size == 64)
3826 {
3827 // There is one word before __glink_PLTresolve
3828 address += 8;
3829 len -= 8;
3830 }
3831 else if (parameters->options().output_is_position_independent())
3832 {
3833 // There are two FDEs for a position independent glink.
3834 // The first covers the branch table, the second
3835 // __glink_PLTresolve at the end of glink.
3836 off_t resolve_size = this->glink_->pltresolve_size();
3837 if (oview[9] == elfcpp::DW_CFA_nop)
3838 len -= resolve_size;
3839 else
3840 {
3841 address += len - resolve_size;
3842 len = resolve_size;
3843 }
3844 }
3845 }
3846 else
3847 {
3848 // Must be a stub table.
3849 const Stub_table<size, big_endian>* stub_table
3850 = static_cast<const Stub_table<size, big_endian>*>(plt);
3851 uint64_t stub_address = stub_table->stub_address();
3852 len -= stub_address - address;
3853 address = stub_address;
3854 }
3855
3856 *paddress = address;
3857 *plen = len;
3858 }
3859
3860 // A class to handle the PLT data.
3861
3862 template<int size, bool big_endian>
3863 class Output_data_plt_powerpc : public Output_section_data_build
3864 {
3865 public:
3866 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3867 size, big_endian> Reloc_section;
3868
3869 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3870 Reloc_section* plt_rel,
3871 const char* name)
3872 : Output_section_data_build(size == 32 ? 4 : 8),
3873 rel_(plt_rel),
3874 targ_(targ),
3875 name_(name)
3876 { }
3877
3878 // Add an entry to the PLT.
3879 void
3880 add_entry(Symbol*);
3881
3882 void
3883 add_ifunc_entry(Symbol*);
3884
3885 void
3886 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3887
3888 void
3889 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3890
3891 // Return the .rela.plt section data.
3892 Reloc_section*
3893 rel_plt() const
3894 {
3895 return this->rel_;
3896 }
3897
3898 // Return the number of PLT entries.
3899 unsigned int
3900 entry_count() const
3901 {
3902 if (this->current_data_size() == 0)
3903 return 0;
3904 return ((this->current_data_size() - this->first_plt_entry_offset())
3905 / this->plt_entry_size());
3906 }
3907
3908 protected:
3909 void
3910 do_adjust_output_section(Output_section* os)
3911 {
3912 os->set_entsize(0);
3913 }
3914
3915 // Write to a map file.
3916 void
3917 do_print_to_mapfile(Mapfile* mapfile) const
3918 { mapfile->print_output_data(this, this->name_); }
3919
3920 private:
3921 // Return the offset of the first non-reserved PLT entry.
3922 unsigned int
3923 first_plt_entry_offset() const
3924 {
3925 // IPLT and LPLT have no reserved entry.
3926 if (this->name_[3] == 'I' || this->name_[3] == 'L')
3927 return 0;
3928 return this->targ_->first_plt_entry_offset();
3929 }
3930
3931 // Return the size of each PLT entry.
3932 unsigned int
3933 plt_entry_size() const
3934 {
3935 return this->targ_->plt_entry_size();
3936 }
3937
3938 // Write out the PLT data.
3939 void
3940 do_write(Output_file*);
3941
3942 // The reloc section.
3943 Reloc_section* rel_;
3944 // Allows access to .glink for do_write.
3945 Target_powerpc<size, big_endian>* targ_;
3946 // What to report in map file.
3947 const char *name_;
3948 };
3949
3950 // Add an entry to the PLT.
3951
3952 template<int size, bool big_endian>
3953 void
3954 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3955 {
3956 if (!gsym->has_plt_offset())
3957 {
3958 section_size_type off = this->current_data_size();
3959 if (off == 0)
3960 off += this->first_plt_entry_offset();
3961 gsym->set_plt_offset(off);
3962 gsym->set_needs_dynsym_entry();
3963 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3964 this->rel_->add_global(gsym, dynrel, this, off, 0);
3965 off += this->plt_entry_size();
3966 this->set_current_data_size(off);
3967 }
3968 }
3969
3970 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3971
3972 template<int size, bool big_endian>
3973 void
3974 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3975 {
3976 if (!gsym->has_plt_offset())
3977 {
3978 section_size_type off = this->current_data_size();
3979 gsym->set_plt_offset(off);
3980 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3981 if (size == 64 && this->targ_->abiversion() < 2)
3982 dynrel = elfcpp::R_PPC64_JMP_IREL;
3983 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3984 off += this->plt_entry_size();
3985 this->set_current_data_size(off);
3986 }
3987 }
3988
3989 // Add an entry for a local symbol to the PLT.
3990
3991 template<int size, bool big_endian>
3992 void
3993 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
3994 Sized_relobj_file<size, big_endian>* relobj,
3995 unsigned int local_sym_index)
3996 {
3997 if (!relobj->local_has_plt_offset(local_sym_index))
3998 {
3999 section_size_type off = this->current_data_size();
4000 relobj->set_local_plt_offset(local_sym_index, off);
4001 if (this->rel_)
4002 {
4003 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4004 if (size == 64 && this->targ_->abiversion() < 2)
4005 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4006 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4007 dynrel, this, off, 0);
4008 }
4009 off += this->plt_entry_size();
4010 this->set_current_data_size(off);
4011 }
4012 }
4013
4014 // Add an entry for a local ifunc symbol to the IPLT.
4015
4016 template<int size, bool big_endian>
4017 void
4018 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4019 Sized_relobj_file<size, big_endian>* relobj,
4020 unsigned int local_sym_index)
4021 {
4022 if (!relobj->local_has_plt_offset(local_sym_index))
4023 {
4024 section_size_type off = this->current_data_size();
4025 relobj->set_local_plt_offset(local_sym_index, off);
4026 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4027 if (size == 64 && this->targ_->abiversion() < 2)
4028 dynrel = elfcpp::R_PPC64_JMP_IREL;
4029 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4030 this, off, 0);
4031 off += this->plt_entry_size();
4032 this->set_current_data_size(off);
4033 }
4034 }
4035
4036 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4037 static const uint32_t add_2_2_11 = 0x7c425a14;
4038 static const uint32_t add_2_2_12 = 0x7c426214;
4039 static const uint32_t add_3_3_2 = 0x7c631214;
4040 static const uint32_t add_3_3_13 = 0x7c636a14;
4041 static const uint32_t add_3_12_2 = 0x7c6c1214;
4042 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4043 static const uint32_t add_11_0_11 = 0x7d605a14;
4044 static const uint32_t add_11_2_11 = 0x7d625a14;
4045 static const uint32_t add_11_11_2 = 0x7d6b1214;
4046 static const uint32_t add_12_11_12 = 0x7d8b6214;
4047 static const uint32_t addi_0_12 = 0x380c0000;
4048 static const uint32_t addi_2_2 = 0x38420000;
4049 static const uint32_t addi_3_3 = 0x38630000;
4050 static const uint32_t addi_11_11 = 0x396b0000;
4051 static const uint32_t addi_12_1 = 0x39810000;
4052 static const uint32_t addi_12_11 = 0x398b0000;
4053 static const uint32_t addi_12_12 = 0x398c0000;
4054 static const uint32_t addis_0_2 = 0x3c020000;
4055 static const uint32_t addis_0_13 = 0x3c0d0000;
4056 static const uint32_t addis_2_12 = 0x3c4c0000;
4057 static const uint32_t addis_11_2 = 0x3d620000;
4058 static const uint32_t addis_11_11 = 0x3d6b0000;
4059 static const uint32_t addis_11_30 = 0x3d7e0000;
4060 static const uint32_t addis_12_1 = 0x3d810000;
4061 static const uint32_t addis_12_2 = 0x3d820000;
4062 static const uint32_t addis_12_11 = 0x3d8b0000;
4063 static const uint32_t addis_12_12 = 0x3d8c0000;
4064 static const uint32_t b = 0x48000000;
4065 static const uint32_t bcl_20_31 = 0x429f0005;
4066 static const uint32_t bctr = 0x4e800420;
4067 static const uint32_t bctrl = 0x4e800421;
4068 static const uint32_t beqlr = 0x4d820020;
4069 static const uint32_t blr = 0x4e800020;
4070 static const uint32_t bnectr_p4 = 0x4ce20420;
4071 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4072 static const uint32_t cmpldi_2_0 = 0x28220000;
4073 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4074 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4075 static const uint32_t cror_15_15_15 = 0x4def7b82;
4076 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4077 static const uint32_t ld_0_1 = 0xe8010000;
4078 static const uint32_t ld_0_12 = 0xe80c0000;
4079 static const uint32_t ld_2_1 = 0xe8410000;
4080 static const uint32_t ld_2_2 = 0xe8420000;
4081 static const uint32_t ld_2_11 = 0xe84b0000;
4082 static const uint32_t ld_2_12 = 0xe84c0000;
4083 static const uint32_t ld_11_1 = 0xe9610000;
4084 static const uint32_t ld_11_2 = 0xe9620000;
4085 static const uint32_t ld_11_3 = 0xe9630000;
4086 static const uint32_t ld_11_11 = 0xe96b0000;
4087 static const uint32_t ld_12_2 = 0xe9820000;
4088 static const uint32_t ld_12_3 = 0xe9830000;
4089 static const uint32_t ld_12_11 = 0xe98b0000;
4090 static const uint32_t ld_12_12 = 0xe98c0000;
4091 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4092 static const uint32_t lfd_0_1 = 0xc8010000;
4093 static const uint32_t li_0_0 = 0x38000000;
4094 static const uint32_t li_12_0 = 0x39800000;
4095 static const uint32_t lis_0 = 0x3c000000;
4096 static const uint32_t lis_2 = 0x3c400000;
4097 static const uint32_t lis_11 = 0x3d600000;
4098 static const uint32_t lis_12 = 0x3d800000;
4099 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4100 static const uint32_t lwz_0_12 = 0x800c0000;
4101 static const uint32_t lwz_11_3 = 0x81630000;
4102 static const uint32_t lwz_11_11 = 0x816b0000;
4103 static const uint32_t lwz_11_30 = 0x817e0000;
4104 static const uint32_t lwz_12_3 = 0x81830000;
4105 static const uint32_t lwz_12_12 = 0x818c0000;
4106 static const uint32_t lwzu_0_12 = 0x840c0000;
4107 static const uint32_t mflr_0 = 0x7c0802a6;
4108 static const uint32_t mflr_11 = 0x7d6802a6;
4109 static const uint32_t mflr_12 = 0x7d8802a6;
4110 static const uint32_t mr_0_3 = 0x7c601b78;
4111 static const uint32_t mr_3_0 = 0x7c030378;
4112 static const uint32_t mtctr_0 = 0x7c0903a6;
4113 static const uint32_t mtctr_11 = 0x7d6903a6;
4114 static const uint32_t mtctr_12 = 0x7d8903a6;
4115 static const uint32_t mtlr_0 = 0x7c0803a6;
4116 static const uint32_t mtlr_11 = 0x7d6803a6;
4117 static const uint32_t mtlr_12 = 0x7d8803a6;
4118 static const uint32_t nop = 0x60000000;
4119 static const uint32_t ori_0_0_0 = 0x60000000;
4120 static const uint32_t ori_12_12_0 = 0x618c0000;
4121 static const uint32_t oris_12_12_0 = 0x658c0000;
4122 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4123 static const uint32_t srdi_0_0_2 = 0x7800f082;
4124 static const uint32_t std_0_1 = 0xf8010000;
4125 static const uint32_t std_0_12 = 0xf80c0000;
4126 static const uint32_t std_2_1 = 0xf8410000;
4127 static const uint32_t std_11_1 = 0xf9610000;
4128 static const uint32_t stfd_0_1 = 0xd8010000;
4129 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4130 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4131 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4132 static const uint32_t xor_2_12_12 = 0x7d826278;
4133 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4134
4135 // Write out the PLT.
4136
4137 template<int size, bool big_endian>
4138 void
4139 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4140 {
4141 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4142 {
4143 const section_size_type offset = this->offset();
4144 const section_size_type oview_size
4145 = convert_to_section_size_type(this->data_size());
4146 unsigned char* const oview = of->get_output_view(offset, oview_size);
4147 unsigned char* pov = oview;
4148 unsigned char* endpov = oview + oview_size;
4149
4150 // The address of the .glink branch table
4151 const Output_data_glink<size, big_endian>* glink
4152 = this->targ_->glink_section();
4153 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4154
4155 while (pov < endpov)
4156 {
4157 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4158 pov += 4;
4159 branch_tab += 4;
4160 }
4161
4162 of->write_output_view(offset, oview_size, oview);
4163 }
4164 }
4165
4166 // Create the PLT section.
4167
4168 template<int size, bool big_endian>
4169 void
4170 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4171 Layout* layout)
4172 {
4173 if (this->plt_ == NULL)
4174 {
4175 if (this->got_ == NULL)
4176 this->got_section(symtab, layout);
4177
4178 if (this->glink_ == NULL)
4179 make_glink_section(layout);
4180
4181 // Ensure that .rela.dyn always appears before .rela.plt This is
4182 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4183 // needs to include .rela.plt in its range.
4184 this->rela_dyn_section(layout);
4185
4186 Reloc_section* plt_rel = new Reloc_section(false);
4187 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4188 elfcpp::SHF_ALLOC, plt_rel,
4189 ORDER_DYNAMIC_PLT_RELOCS, false);
4190 this->plt_
4191 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4192 "** PLT");
4193 layout->add_output_section_data(".plt",
4194 (size == 32
4195 ? elfcpp::SHT_PROGBITS
4196 : elfcpp::SHT_NOBITS),
4197 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4198 this->plt_,
4199 (size == 32
4200 ? ORDER_SMALL_DATA
4201 : ORDER_SMALL_BSS),
4202 false);
4203
4204 Output_section* rela_plt_os = plt_rel->output_section();
4205 rela_plt_os->set_info_section(this->plt_->output_section());
4206 }
4207 }
4208
4209 // Create the IPLT section.
4210
4211 template<int size, bool big_endian>
4212 void
4213 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4214 Layout* layout)
4215 {
4216 if (this->iplt_ == NULL)
4217 {
4218 this->make_plt_section(symtab, layout);
4219 this->make_lplt_section(layout);
4220
4221 Reloc_section* iplt_rel = new Reloc_section(false);
4222 if (this->rela_dyn_->output_section())
4223 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4224 this->iplt_
4225 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4226 "** IPLT");
4227 if (this->plt_->output_section())
4228 this->plt_->output_section()->add_output_section_data(this->iplt_);
4229 }
4230 }
4231
4232 // Create the LPLT section.
4233
4234 template<int size, bool big_endian>
4235 void
4236 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4237 {
4238 if (this->lplt_ == NULL)
4239 {
4240 Reloc_section* lplt_rel = NULL;
4241 if (parameters->options().output_is_position_independent())
4242 {
4243 lplt_rel = new Reloc_section(false);
4244 this->rela_dyn_section(layout);
4245 if (this->rela_dyn_->output_section())
4246 this->rela_dyn_->output_section()
4247 ->add_output_section_data(lplt_rel);
4248 }
4249 this->lplt_
4250 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4251 "** LPLT");
4252 this->make_brlt_section(layout);
4253 if (this->brlt_section_ && this->brlt_section_->output_section())
4254 this->brlt_section_->output_section()
4255 ->add_output_section_data(this->lplt_);
4256 else
4257 layout->add_output_section_data(".branch_lt",
4258 elfcpp::SHT_PROGBITS,
4259 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4260 this->lplt_,
4261 ORDER_RELRO,
4262 true);
4263 }
4264 }
4265
4266 // A section for huge long branch addresses, similar to plt section.
4267
4268 template<int size, bool big_endian>
4269 class Output_data_brlt_powerpc : public Output_section_data_build
4270 {
4271 public:
4272 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4273 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4274 size, big_endian> Reloc_section;
4275
4276 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4277 Reloc_section* brlt_rel)
4278 : Output_section_data_build(size == 32 ? 4 : 8),
4279 rel_(brlt_rel),
4280 targ_(targ)
4281 { }
4282
4283 void
4284 reset_brlt_sizes()
4285 {
4286 this->reset_data_size();
4287 this->rel_->reset_data_size();
4288 }
4289
4290 void
4291 finalize_brlt_sizes()
4292 {
4293 this->finalize_data_size();
4294 this->rel_->finalize_data_size();
4295 }
4296
4297 // Add a reloc for an entry in the BRLT.
4298 void
4299 add_reloc(Address to, unsigned int off)
4300 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4301
4302 // Update section and reloc section size.
4303 void
4304 set_current_size(unsigned int num_branches)
4305 {
4306 this->reset_address_and_file_offset();
4307 this->set_current_data_size(num_branches * 16);
4308 this->finalize_data_size();
4309 Output_section* os = this->output_section();
4310 os->set_section_offsets_need_adjustment();
4311 if (this->rel_ != NULL)
4312 {
4313 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4314 this->rel_->reset_address_and_file_offset();
4315 this->rel_->set_current_data_size(num_branches * reloc_size);
4316 this->rel_->finalize_data_size();
4317 Output_section* os = this->rel_->output_section();
4318 os->set_section_offsets_need_adjustment();
4319 }
4320 }
4321
4322 protected:
4323 void
4324 do_adjust_output_section(Output_section* os)
4325 {
4326 os->set_entsize(0);
4327 }
4328
4329 // Write to a map file.
4330 void
4331 do_print_to_mapfile(Mapfile* mapfile) const
4332 { mapfile->print_output_data(this, "** BRLT"); }
4333
4334 private:
4335 // Write out the BRLT data.
4336 void
4337 do_write(Output_file*);
4338
4339 // The reloc section.
4340 Reloc_section* rel_;
4341 Target_powerpc<size, big_endian>* targ_;
4342 };
4343
4344 // Make the branch lookup table section.
4345
4346 template<int size, bool big_endian>
4347 void
4348 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4349 {
4350 if (size == 64 && this->brlt_section_ == NULL)
4351 {
4352 Reloc_section* brlt_rel = NULL;
4353 bool is_pic = parameters->options().output_is_position_independent();
4354 if (is_pic)
4355 {
4356 // When PIC we can't fill in .branch_lt but must initialise at
4357 // runtime via dynamic relocations.
4358 this->rela_dyn_section(layout);
4359 brlt_rel = new Reloc_section(false);
4360 if (this->rela_dyn_->output_section())
4361 this->rela_dyn_->output_section()
4362 ->add_output_section_data(brlt_rel);
4363 }
4364 this->brlt_section_
4365 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4366 if (this->plt_ && is_pic && this->plt_->output_section())
4367 this->plt_->output_section()
4368 ->add_output_section_data(this->brlt_section_);
4369 else
4370 layout->add_output_section_data(".branch_lt",
4371 elfcpp::SHT_PROGBITS,
4372 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4373 this->brlt_section_,
4374 ORDER_RELRO,
4375 true);
4376 }
4377 }
4378
4379 // Write out .branch_lt when non-PIC.
4380
4381 template<int size, bool big_endian>
4382 void
4383 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4384 {
4385 if (size == 64 && !parameters->options().output_is_position_independent())
4386 {
4387 const section_size_type offset = this->offset();
4388 const section_size_type oview_size
4389 = convert_to_section_size_type(this->data_size());
4390 unsigned char* const oview = of->get_output_view(offset, oview_size);
4391
4392 this->targ_->write_branch_lookup_table(oview);
4393 of->write_output_view(offset, oview_size, oview);
4394 }
4395 }
4396
4397 static inline uint32_t
4398 l(uint32_t a)
4399 {
4400 return a & 0xffff;
4401 }
4402
4403 static inline uint32_t
4404 hi(uint32_t a)
4405 {
4406 return l(a >> 16);
4407 }
4408
4409 static inline uint32_t
4410 ha(uint32_t a)
4411 {
4412 return hi(a + 0x8000);
4413 }
4414
4415 template<int size>
4416 struct Eh_cie
4417 {
4418 static const unsigned char eh_frame_cie[12];
4419 };
4420
4421 template<int size>
4422 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4423 {
4424 1, // CIE version.
4425 'z', 'R', 0, // Augmentation string.
4426 4, // Code alignment.
4427 0x80 - size / 8 , // Data alignment.
4428 65, // RA reg.
4429 1, // Augmentation size.
4430 (elfcpp::DW_EH_PE_pcrel
4431 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4432 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4433 };
4434
4435 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4436 static const unsigned char glink_eh_frame_fde_64v1[] =
4437 {
4438 0, 0, 0, 0, // Replaced with offset to .glink.
4439 0, 0, 0, 0, // Replaced with size of .glink.
4440 0, // Augmentation size.
4441 elfcpp::DW_CFA_advance_loc + 1,
4442 elfcpp::DW_CFA_register, 65, 12,
4443 elfcpp::DW_CFA_advance_loc + 5,
4444 elfcpp::DW_CFA_restore_extended, 65
4445 };
4446
4447 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4448 static const unsigned char glink_eh_frame_fde_64v2[] =
4449 {
4450 0, 0, 0, 0, // Replaced with offset to .glink.
4451 0, 0, 0, 0, // Replaced with size of .glink.
4452 0, // Augmentation size.
4453 elfcpp::DW_CFA_advance_loc + 1,
4454 elfcpp::DW_CFA_register, 65, 0,
4455 elfcpp::DW_CFA_advance_loc + 7,
4456 elfcpp::DW_CFA_restore_extended, 65
4457 };
4458
4459 // Describe __glink_PLTresolve use of LR, 32-bit version.
4460 static const unsigned char glink_eh_frame_fde_32[] =
4461 {
4462 0, 0, 0, 0, // Replaced with offset to .glink.
4463 0, 0, 0, 0, // Replaced with size of .glink.
4464 0, // Augmentation size.
4465 elfcpp::DW_CFA_advance_loc + 2,
4466 elfcpp::DW_CFA_register, 65, 0,
4467 elfcpp::DW_CFA_advance_loc + 4,
4468 elfcpp::DW_CFA_restore_extended, 65
4469 };
4470
4471 static const unsigned char default_fde[] =
4472 {
4473 0, 0, 0, 0, // Replaced with offset to stubs.
4474 0, 0, 0, 0, // Replaced with size of stubs.
4475 0, // Augmentation size.
4476 elfcpp::DW_CFA_nop, // Pad.
4477 elfcpp::DW_CFA_nop,
4478 elfcpp::DW_CFA_nop
4479 };
4480
4481 template<bool big_endian>
4482 static inline void
4483 write_insn(unsigned char* p, uint32_t v)
4484 {
4485 elfcpp::Swap<32, big_endian>::writeval(p, v);
4486 }
4487
4488 template<int size>
4489 static inline unsigned int
4490 param_plt_align()
4491 {
4492 if (!parameters->options().user_set_plt_align())
4493 return size == 64 ? 32 : 8;
4494 return 1 << parameters->options().plt_align();
4495 }
4496
4497 // Stub_table holds information about plt and long branch stubs.
4498 // Stubs are built in an area following some input section determined
4499 // by group_sections(). This input section is converted to a relaxed
4500 // input section allowing it to be resized to accommodate the stubs
4501
4502 template<int size, bool big_endian>
4503 class Stub_table : public Output_relaxed_input_section
4504 {
4505 public:
4506 struct Plt_stub_ent
4507 {
4508 Plt_stub_ent(unsigned int off, unsigned int indx)
4509 : off_(off), indx_(indx), iter_(0), notoc_(0), r2save_(0), localentry0_(0)
4510 { }
4511
4512 unsigned int off_;
4513 unsigned int indx_ : 28;
4514 unsigned int iter_ : 1;
4515 unsigned int notoc_ : 1;
4516 unsigned int r2save_ : 1;
4517 unsigned int localentry0_ : 1;
4518 };
4519 struct Branch_stub_ent
4520 {
4521 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4522 : off_(off), iter_(false), notoc_(notoc), save_res_(save_res)
4523 { }
4524
4525 unsigned int off_;
4526 bool iter_;
4527 bool notoc_;
4528 bool save_res_;
4529 };
4530 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4531 static const Address invalid_address = static_cast<Address>(0) - 1;
4532
4533 Stub_table(Target_powerpc<size, big_endian>* targ,
4534 Output_section* output_section,
4535 const Output_section::Input_section* owner,
4536 uint32_t id)
4537 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4538 owner->relobj()
4539 ->section_addralign(owner->shndx())),
4540 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4541 orig_data_size_(owner->current_data_size()),
4542 plt_size_(0), last_plt_size_(0),
4543 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4544 need_save_res_(false), need_resize_(false), resizing_(false),
4545 uniq_(id)
4546 {
4547 this->set_output_section(output_section);
4548
4549 std::vector<Output_relaxed_input_section*> new_relaxed;
4550 new_relaxed.push_back(this);
4551 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4552 }
4553
4554 // Add a plt call stub.
4555 bool
4556 add_plt_call_entry(Address,
4557 const Sized_relobj_file<size, big_endian>*,
4558 const Symbol*,
4559 unsigned int,
4560 Address,
4561 bool);
4562
4563 bool
4564 add_plt_call_entry(Address,
4565 const Sized_relobj_file<size, big_endian>*,
4566 unsigned int,
4567 unsigned int,
4568 Address,
4569 bool);
4570
4571 // Find a given plt call stub.
4572 const Plt_stub_ent*
4573 find_plt_call_entry(const Symbol*) const;
4574
4575 const Plt_stub_ent*
4576 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4577 unsigned int) const;
4578
4579 const Plt_stub_ent*
4580 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4581 const Symbol*,
4582 unsigned int,
4583 Address) const;
4584
4585 const Plt_stub_ent*
4586 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4587 unsigned int,
4588 unsigned int,
4589 Address) const;
4590
4591 // Add a long branch stub.
4592 bool
4593 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4594 unsigned int, Address, Address, bool);
4595
4596 const Branch_stub_ent*
4597 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4598 Address) const;
4599
4600 bool
4601 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4602 {
4603 Address max_branch_offset = max_branch_delta<size>(r_type);
4604 if (max_branch_offset == 0)
4605 return true;
4606 gold_assert(from != invalid_address);
4607 Address loc = off + this->stub_address();
4608 return loc - from + max_branch_offset < 2 * max_branch_offset;
4609 }
4610
4611 void
4612 clear_stubs(bool all)
4613 {
4614 this->plt_call_stubs_.clear();
4615 this->plt_size_ = 0;
4616 this->long_branch_stubs_.clear();
4617 this->branch_size_ = 0;
4618 this->need_save_res_ = false;
4619 if (all)
4620 {
4621 this->last_plt_size_ = 0;
4622 this->last_branch_size_ = 0;
4623 }
4624 }
4625
4626 bool
4627 need_resize() const
4628 { return need_resize_; }
4629
4630 void
4631 set_resizing(bool val)
4632 {
4633 this->resizing_ = val;
4634 if (val)
4635 {
4636 this->need_resize_ = false;
4637 this->plt_size_ = 0;
4638 this->branch_size_ = 0;
4639 this->need_save_res_ = false;
4640 }
4641 }
4642
4643 Address
4644 set_address_and_size(const Output_section* os, Address off)
4645 {
4646 Address start_off = off;
4647 off += this->orig_data_size_;
4648 Address my_size = this->plt_size_ + this->branch_size_;
4649 if (this->need_save_res_)
4650 my_size += this->targ_->savres_section()->data_size();
4651 if (my_size != 0)
4652 off = align_address(off, this->stub_align());
4653 // Include original section size and alignment padding in size
4654 my_size += off - start_off;
4655 // Ensure new size is always larger than min size
4656 // threshold. Alignment requirement is included in "my_size", so
4657 // increase "my_size" does not invalidate alignment.
4658 if (my_size < this->min_size_threshold_)
4659 my_size = this->min_size_threshold_;
4660 this->reset_address_and_file_offset();
4661 this->set_current_data_size(my_size);
4662 this->set_address_and_file_offset(os->address() + start_off,
4663 os->offset() + start_off);
4664 return my_size;
4665 }
4666
4667 Address
4668 stub_address() const
4669 {
4670 return align_address(this->address() + this->orig_data_size_,
4671 this->stub_align());
4672 }
4673
4674 Address
4675 stub_offset() const
4676 {
4677 return align_address(this->offset() + this->orig_data_size_,
4678 this->stub_align());
4679 }
4680
4681 section_size_type
4682 plt_size() const
4683 { return this->plt_size_; }
4684
4685 section_size_type
4686 branch_size() const
4687 { return this->branch_size_; }
4688
4689 void
4690 set_min_size_threshold(Address min_size)
4691 { this->min_size_threshold_ = min_size; }
4692
4693 void
4694 define_stub_syms(Symbol_table*);
4695
4696 bool
4697 size_update()
4698 {
4699 Output_section* os = this->output_section();
4700 if (os->addralign() < this->stub_align())
4701 {
4702 os->set_addralign(this->stub_align());
4703 // FIXME: get rid of the insane checkpointing.
4704 // We can't increase alignment of the input section to which
4705 // stubs are attached; The input section may be .init which
4706 // is pasted together with other .init sections to form a
4707 // function. Aligning might insert zero padding resulting in
4708 // sigill. However we do need to increase alignment of the
4709 // output section so that the align_address() on offset in
4710 // set_address_and_size() adds the same padding as the
4711 // align_address() on address in stub_address().
4712 // What's more, we need this alignment for the layout done in
4713 // relaxation_loop_body() so that the output section starts at
4714 // a suitably aligned address.
4715 os->checkpoint_set_addralign(this->stub_align());
4716 }
4717 if (this->last_plt_size_ != this->plt_size_
4718 || this->last_branch_size_ != this->branch_size_)
4719 {
4720 this->last_plt_size_ = this->plt_size_;
4721 this->last_branch_size_ = this->branch_size_;
4722 return true;
4723 }
4724 return false;
4725 }
4726
4727 // Add .eh_frame info for this stub section.
4728 void
4729 add_eh_frame(Layout* layout);
4730
4731 // Remove .eh_frame info for this stub section.
4732 void
4733 remove_eh_frame(Layout* layout);
4734
4735 Target_powerpc<size, big_endian>*
4736 targ() const
4737 { return targ_; }
4738
4739 private:
4740 class Plt_stub_key;
4741 class Plt_stub_key_hash;
4742 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4743 Plt_stub_key_hash> Plt_stub_entries;
4744 class Branch_stub_key;
4745 class Branch_stub_key_hash;
4746 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4747 Branch_stub_key_hash> Branch_stub_entries;
4748
4749 // Alignment of stub section.
4750 unsigned int
4751 stub_align() const
4752 {
4753 unsigned int min_align = size == 64 ? 32 : 16;
4754 unsigned int user_align = 1 << parameters->options().plt_align();
4755 return std::max(user_align, min_align);
4756 }
4757
4758 // Return the plt offset for the given call stub.
4759 Address
4760 plt_off(typename Plt_stub_entries::const_iterator p,
4761 const Output_data_plt_powerpc<size, big_endian>** sec) const
4762 {
4763 const Symbol* gsym = p->first.sym_;
4764 if (gsym != NULL)
4765 return this->targ_->plt_off(gsym, sec);
4766 else
4767 {
4768 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4769 unsigned int local_sym_index = p->first.locsym_;
4770 return this->targ_->plt_off(relobj, local_sym_index, sec);
4771 }
4772 }
4773
4774 // Size of a given plt call stub.
4775 unsigned int
4776 plt_call_size(typename Plt_stub_entries::const_iterator p) const;
4777
4778 unsigned int
4779 plt_call_align(unsigned int bytes) const
4780 {
4781 unsigned int align = param_plt_align<size>();
4782 return (bytes + align - 1) & -align;
4783 }
4784
4785 // Return long branch stub size.
4786 unsigned int
4787 branch_stub_size(typename Branch_stub_entries::const_iterator p,
4788 bool* need_lt);
4789
4790 bool
4791 build_tls_opt_head(unsigned char** pp,
4792 typename Plt_stub_entries::const_iterator cs);
4793
4794 bool
4795 build_tls_opt_tail(unsigned char* p,
4796 typename Plt_stub_entries::const_iterator cs);
4797
4798 void
4799 plt_error(const Plt_stub_key& p);
4800
4801 // Write out stubs.
4802 void
4803 do_write(Output_file*);
4804
4805 // Plt call stub keys.
4806 class Plt_stub_key
4807 {
4808 public:
4809 Plt_stub_key(const Symbol* sym)
4810 : sym_(sym), object_(0), addend_(0), locsym_(0)
4811 { }
4812
4813 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4814 unsigned int locsym_index)
4815 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4816 { }
4817
4818 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4819 const Symbol* sym,
4820 unsigned int r_type,
4821 Address addend)
4822 : sym_(sym), object_(0), addend_(0), locsym_(0)
4823 {
4824 if (size != 32)
4825 this->addend_ = addend;
4826 else if (parameters->options().output_is_position_independent()
4827 && (r_type == elfcpp::R_PPC_PLTREL24
4828 || r_type == elfcpp::R_POWERPC_PLTCALL))
4829 {
4830 this->addend_ = addend;
4831 if (this->addend_ >= 32768)
4832 this->object_ = object;
4833 }
4834 }
4835
4836 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4837 unsigned int locsym_index,
4838 unsigned int r_type,
4839 Address addend)
4840 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4841 {
4842 if (size != 32)
4843 this->addend_ = addend;
4844 else if (parameters->options().output_is_position_independent()
4845 && (r_type == elfcpp::R_PPC_PLTREL24
4846 || r_type == elfcpp::R_POWERPC_PLTCALL))
4847 this->addend_ = addend;
4848 }
4849
4850 bool operator==(const Plt_stub_key& that) const
4851 {
4852 return (this->sym_ == that.sym_
4853 && this->object_ == that.object_
4854 && this->addend_ == that.addend_
4855 && this->locsym_ == that.locsym_);
4856 }
4857
4858 const Symbol* sym_;
4859 const Sized_relobj_file<size, big_endian>* object_;
4860 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4861 unsigned int locsym_;
4862 };
4863
4864 class Plt_stub_key_hash
4865 {
4866 public:
4867 size_t operator()(const Plt_stub_key& ent) const
4868 {
4869 return (reinterpret_cast<uintptr_t>(ent.sym_)
4870 ^ reinterpret_cast<uintptr_t>(ent.object_)
4871 ^ ent.addend_
4872 ^ ent.locsym_);
4873 }
4874 };
4875
4876 // Long branch stub keys.
4877 class Branch_stub_key
4878 {
4879 public:
4880 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
4881 : dest_(to), toc_base_off_(0)
4882 {
4883 if (size == 64)
4884 toc_base_off_ = obj->toc_base_offset();
4885 }
4886
4887 bool operator==(const Branch_stub_key& that) const
4888 {
4889 return (this->dest_ == that.dest_
4890 && (size == 32
4891 || this->toc_base_off_ == that.toc_base_off_));
4892 }
4893
4894 Address dest_;
4895 unsigned int toc_base_off_;
4896 };
4897
4898 class Branch_stub_key_hash
4899 {
4900 public:
4901 size_t operator()(const Branch_stub_key& key) const
4902 { return key.dest_ ^ key.toc_base_off_; }
4903 };
4904
4905 // In a sane world this would be a global.
4906 Target_powerpc<size, big_endian>* targ_;
4907 // Map sym/object/addend to stub offset.
4908 Plt_stub_entries plt_call_stubs_;
4909 // Map destination address to stub offset.
4910 Branch_stub_entries long_branch_stubs_;
4911 // size of input section
4912 section_size_type orig_data_size_;
4913 // size of stubs
4914 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4915 // Some rare cases cause (PR/20529) fluctuation in stub table
4916 // size, which leads to an endless relax loop. This is to be fixed
4917 // by, after the first few iterations, allowing only increase of
4918 // stub table size. This variable sets the minimal possible size of
4919 // a stub table, it is zero for the first few iterations, then
4920 // increases monotonically.
4921 Address min_size_threshold_;
4922 // Set if this stub group needs a copy of out-of-line register
4923 // save/restore functions.
4924 bool need_save_res_;
4925 // Set when notoc_/r2save_ changes after sizing a stub
4926 bool need_resize_;
4927 // Set when resizing stubs
4928 bool resizing_;
4929 // Per stub table unique identifier.
4930 uint32_t uniq_;
4931 };
4932
4933 // Add a plt call stub, if we do not already have one for this
4934 // sym/object/addend combo.
4935
4936 template<int size, bool big_endian>
4937 bool
4938 Stub_table<size, big_endian>::add_plt_call_entry(
4939 Address from,
4940 const Sized_relobj_file<size, big_endian>* object,
4941 const Symbol* gsym,
4942 unsigned int r_type,
4943 Address addend,
4944 bool tocsave)
4945 {
4946 Plt_stub_key key(object, gsym, r_type, addend);
4947 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4948 std::pair<typename Plt_stub_entries::iterator, bool> p
4949 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4950 if (size == 64)
4951 {
4952 if (p.second
4953 && this->targ_->is_elfv2_localentry0(gsym))
4954 {
4955 p.first->second.localentry0_ = 1;
4956 this->targ_->set_has_localentry0();
4957 }
4958 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
4959 {
4960 if (!p.second && !p.first->second.notoc_)
4961 this->need_resize_ = true;
4962 p.first->second.notoc_ = 1;
4963 }
4964 else if (!tocsave && !p.first->second.localentry0_)
4965 {
4966 if (!p.second && !p.first->second.r2save_)
4967 this->need_resize_ = true;
4968 p.first->second.r2save_ = 1;
4969 }
4970 }
4971 if (p.second || (this->resizing_ && !p.first->second.iter_))
4972 {
4973 if (this->resizing_)
4974 {
4975 p.first->second.iter_ = 1;
4976 p.first->second.off_ = this->plt_size_;
4977 }
4978 this->plt_size_ += this->plt_call_size(p.first);
4979 if (this->targ_->is_tls_get_addr_opt(gsym))
4980 this->targ_->set_has_tls_get_addr_opt();
4981 this->plt_size_ = this->plt_call_align(this->plt_size_);
4982 }
4983 return this->can_reach_stub(from, p.first->second.off_, r_type);
4984 }
4985
4986 template<int size, bool big_endian>
4987 bool
4988 Stub_table<size, big_endian>::add_plt_call_entry(
4989 Address from,
4990 const Sized_relobj_file<size, big_endian>* object,
4991 unsigned int locsym_index,
4992 unsigned int r_type,
4993 Address addend,
4994 bool tocsave)
4995 {
4996 Plt_stub_key key(object, locsym_index, r_type, addend);
4997 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4998 std::pair<typename Plt_stub_entries::iterator, bool> p
4999 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5000 if (size == 64)
5001 {
5002 if (p.second
5003 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5004 {
5005 p.first->second.localentry0_ = 1;
5006 this->targ_->set_has_localentry0();
5007 }
5008 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5009 {
5010 if (!p.second && !p.first->second.notoc_)
5011 this->need_resize_ = true;
5012 p.first->second.notoc_ = 1;
5013 }
5014 else if (!tocsave && !p.first->second.localentry0_)
5015 {
5016 if (!p.second && !p.first->second.r2save_)
5017 this->need_resize_ = true;
5018 p.first->second.r2save_ = 1;
5019 }
5020 }
5021 if (p.second || (this->resizing_ && !p.first->second.iter_))
5022 {
5023 if (this->resizing_)
5024 {
5025 p.first->second.iter_ = 1;
5026 p.first->second.off_ = this->plt_size_;
5027 }
5028 this->plt_size_ += this->plt_call_size(p.first);
5029 this->plt_size_ = this->plt_call_align(this->plt_size_);
5030 }
5031 return this->can_reach_stub(from, p.first->second.off_, r_type);
5032 }
5033
5034 // Find a plt call stub.
5035
5036 template<int size, bool big_endian>
5037 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5038 Stub_table<size, big_endian>::find_plt_call_entry(
5039 const Sized_relobj_file<size, big_endian>* object,
5040 const Symbol* gsym,
5041 unsigned int r_type,
5042 Address addend) const
5043 {
5044 Plt_stub_key key(object, gsym, r_type, addend);
5045 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5046 if (p == this->plt_call_stubs_.end())
5047 return NULL;
5048 return &p->second;
5049 }
5050
5051 template<int size, bool big_endian>
5052 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5053 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5054 {
5055 Plt_stub_key key(gsym);
5056 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5057 if (p == this->plt_call_stubs_.end())
5058 return NULL;
5059 return &p->second;
5060 }
5061
5062 template<int size, bool big_endian>
5063 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5064 Stub_table<size, big_endian>::find_plt_call_entry(
5065 const Sized_relobj_file<size, big_endian>* object,
5066 unsigned int locsym_index,
5067 unsigned int r_type,
5068 Address addend) const
5069 {
5070 Plt_stub_key key(object, locsym_index, r_type, addend);
5071 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5072 if (p == this->plt_call_stubs_.end())
5073 return NULL;
5074 return &p->second;
5075 }
5076
5077 template<int size, bool big_endian>
5078 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5079 Stub_table<size, big_endian>::find_plt_call_entry(
5080 const Sized_relobj_file<size, big_endian>* object,
5081 unsigned int locsym_index) const
5082 {
5083 Plt_stub_key key(object, locsym_index);
5084 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5085 if (p == this->plt_call_stubs_.end())
5086 return NULL;
5087 return &p->second;
5088 }
5089
5090 // Add a long branch stub if we don't already have one to given
5091 // destination.
5092
5093 template<int size, bool big_endian>
5094 bool
5095 Stub_table<size, big_endian>::add_long_branch_entry(
5096 const Powerpc_relobj<size, big_endian>* object,
5097 unsigned int r_type,
5098 Address from,
5099 Address to,
5100 bool save_res)
5101 {
5102 Branch_stub_key key(object, to);
5103 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5104 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5105 std::pair<typename Branch_stub_entries::iterator, bool> p
5106 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5107 if (notoc && !p.first->second.notoc_)
5108 {
5109 this->need_resize_ = true;
5110 p.first->second.notoc_ = true;
5111 }
5112 gold_assert(save_res == p.first->second.save_res_);
5113 if (p.second || (this->resizing_ && !p.first->second.iter_))
5114 {
5115 if (this->resizing_)
5116 {
5117 p.first->second.iter_ = 1;
5118 p.first->second.off_ = this->branch_size_;
5119 }
5120 if (save_res)
5121 this->need_save_res_ = true;
5122 else
5123 {
5124 bool need_lt = false;
5125 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5126 this->branch_size_ += stub_size;
5127 if (size == 64 && need_lt)
5128 this->targ_->add_branch_lookup_table(to);
5129 }
5130 }
5131 return this->can_reach_stub(from, p.first->second.off_, r_type);
5132 }
5133
5134 // Find long branch stub offset.
5135
5136 template<int size, bool big_endian>
5137 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5138 Stub_table<size, big_endian>::find_long_branch_entry(
5139 const Powerpc_relobj<size, big_endian>* object,
5140 Address to) const
5141 {
5142 Branch_stub_key key(object, to);
5143 typename Branch_stub_entries::const_iterator p
5144 = this->long_branch_stubs_.find(key);
5145 if (p == this->long_branch_stubs_.end())
5146 return NULL;
5147 return &p->second;
5148 }
5149
5150 template<bool big_endian>
5151 static void
5152 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5153 {
5154 delta /= 4;
5155 if (delta < 64)
5156 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5157 else if (delta < 256)
5158 {
5159 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5160 fde.push_back(delta);
5161 }
5162 else if (delta < 65536)
5163 {
5164 fde.resize(fde.size() + 3);
5165 unsigned char *p = &*fde.end() - 3;
5166 *p++ = elfcpp::DW_CFA_advance_loc2;
5167 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5168 }
5169 else
5170 {
5171 fde.resize(fde.size() + 5);
5172 unsigned char *p = &*fde.end() - 5;
5173 *p++ = elfcpp::DW_CFA_advance_loc4;
5174 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5175 }
5176 }
5177
5178 template<typename T>
5179 static bool
5180 stub_sort(T s1, T s2)
5181 {
5182 return s1->second.off_ < s2->second.off_;
5183 }
5184
5185 // Add .eh_frame info for this stub section. Unlike other linker
5186 // generated .eh_frame this is added late in the link, because we
5187 // only want the .eh_frame info if this particular stub section is
5188 // non-empty.
5189
5190 template<int size, bool big_endian>
5191 void
5192 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5193 {
5194 if (size != 64
5195 || !parameters->options().ld_generated_unwind_info())
5196 return;
5197
5198 // Since we add stub .eh_frame info late, it must be placed
5199 // after all other linker generated .eh_frame info so that
5200 // merge mapping need not be updated for input sections.
5201 // There is no provision to use a different CIE to that used
5202 // by .glink.
5203 if (!this->targ_->has_glink())
5204 return;
5205
5206 typedef typename Plt_stub_entries::const_iterator plt_iter;
5207 std::vector<plt_iter> calls;
5208 if (!this->plt_call_stubs_.empty())
5209 for (plt_iter cs = this->plt_call_stubs_.begin();
5210 cs != this->plt_call_stubs_.end();
5211 ++cs)
5212 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5213 && cs->second.r2save_
5214 && !cs->second.localentry0_)
5215 || cs->second.notoc_)
5216 calls.push_back(cs);
5217 if (calls.size() > 1)
5218 std::stable_sort(calls.begin(), calls.end(),
5219 stub_sort<plt_iter>);
5220
5221 typedef typename Branch_stub_entries::const_iterator branch_iter;
5222 std::vector<branch_iter> branches;
5223 if (!this->long_branch_stubs_.empty())
5224 for (branch_iter bs = this->long_branch_stubs_.begin();
5225 bs != this->long_branch_stubs_.end();
5226 ++bs)
5227 if (bs->second.notoc_)
5228 branches.push_back(bs);
5229 if (branches.size() > 1)
5230 std::stable_sort(branches.begin(), branches.end(),
5231 stub_sort<branch_iter>);
5232
5233 if (calls.empty() && branches.empty())
5234 return;
5235
5236 unsigned int last_eh_loc = 0;
5237 // offset pcrel sdata4, size udata4, and augmentation size byte.
5238 std::vector<unsigned char> fde(9, 0);
5239
5240 for (unsigned int i = 0; i < calls.size(); i++)
5241 {
5242 plt_iter cs = calls[i];
5243 unsigned int off = cs->second.off_;
5244 // The __tls_get_addr_opt call stub needs to describe where
5245 // it saves LR, to support exceptions that might be thrown
5246 // from __tls_get_addr, and to support asynchronous exceptions.
5247 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5248 {
5249 off += 7 * 4;
5250 if (cs->second.r2save_
5251 && !cs->second.localentry0_)
5252 {
5253 off += 2 * 4;
5254 eh_advance<big_endian>(fde, off - last_eh_loc);
5255 fde.resize(fde.size() + 6);
5256 unsigned char* p = &*fde.end() - 6;
5257 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5258 *p++ = 65;
5259 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5260 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5261 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5262 *p++ = elfcpp::DW_CFA_restore_extended;
5263 *p++ = 65;
5264 last_eh_loc = off + delta;
5265 continue;
5266 }
5267 }
5268 // notoc stubs also should describe LR changes, to support
5269 // asynchronous exceptions.
5270 off += (cs->second.r2save_ ? 4 : 0) + 8;
5271 eh_advance<big_endian>(fde, off - last_eh_loc);
5272 fde.resize(fde.size() + 6);
5273 unsigned char* p = &*fde.end() - 6;
5274 *p++ = elfcpp::DW_CFA_register;
5275 *p++ = 65;
5276 *p++ = 12;
5277 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5278 *p++ = elfcpp::DW_CFA_restore_extended;
5279 *p++ = 65;
5280 last_eh_loc = off + 8;
5281 }
5282
5283 for (unsigned int i = 0; i < branches.size(); i++)
5284 {
5285 branch_iter bs = branches[i];
5286 unsigned int off = bs->second.off_ + 8;
5287 eh_advance<big_endian>(fde, off - last_eh_loc);
5288 fde.resize(fde.size() + 6);
5289 unsigned char* p = &*fde.end() - 6;
5290 *p++ = elfcpp::DW_CFA_register;
5291 *p++ = 65;
5292 *p++ = 12;
5293 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5294 *p++ = elfcpp::DW_CFA_restore_extended;
5295 *p++ = 65;
5296 last_eh_loc = off + 8;
5297 }
5298
5299 layout->add_eh_frame_for_plt(this,
5300 Eh_cie<size>::eh_frame_cie,
5301 sizeof (Eh_cie<size>::eh_frame_cie),
5302 &*fde.begin(), fde.size());
5303 }
5304
5305 template<int size, bool big_endian>
5306 void
5307 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5308 {
5309 if (size == 64
5310 && parameters->options().ld_generated_unwind_info()
5311 && this->targ_->has_glink())
5312 layout->remove_eh_frame_for_plt(this,
5313 Eh_cie<size>::eh_frame_cie,
5314 sizeof (Eh_cie<size>::eh_frame_cie));
5315 }
5316
5317 // A class to handle .glink.
5318
5319 template<int size, bool big_endian>
5320 class Output_data_glink : public Output_section_data
5321 {
5322 public:
5323 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5324 static const Address invalid_address = static_cast<Address>(0) - 1;
5325
5326 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5327 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5328 end_branch_table_(), ge_size_(0)
5329 { }
5330
5331 void
5332 add_eh_frame(Layout* layout);
5333
5334 void
5335 add_global_entry(const Symbol*);
5336
5337 Address
5338 find_global_entry(const Symbol*) const;
5339
5340 unsigned int
5341 global_entry_align(unsigned int off) const
5342 {
5343 unsigned int align = param_plt_align<size>();
5344 return (off + align - 1) & -align;
5345 }
5346
5347 unsigned int
5348 global_entry_off() const
5349 {
5350 return this->global_entry_align(this->end_branch_table_);
5351 }
5352
5353 Address
5354 global_entry_address() const
5355 {
5356 gold_assert(this->is_data_size_valid());
5357 return this->address() + this->global_entry_off();
5358 }
5359
5360 int
5361 pltresolve_size() const
5362 {
5363 if (size == 64)
5364 return (8
5365 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
5366 return 16 * 4;
5367 }
5368
5369 protected:
5370 // Write to a map file.
5371 void
5372 do_print_to_mapfile(Mapfile* mapfile) const
5373 { mapfile->print_output_data(this, _("** glink")); }
5374
5375 private:
5376 void
5377 set_final_data_size();
5378
5379 // Write out .glink
5380 void
5381 do_write(Output_file*);
5382
5383 // Allows access to .got and .plt for do_write.
5384 Target_powerpc<size, big_endian>* targ_;
5385
5386 // Map sym to stub offset.
5387 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5388 Global_entry_stub_entries global_entry_stubs_;
5389
5390 unsigned int end_branch_table_, ge_size_;
5391 };
5392
5393 template<int size, bool big_endian>
5394 void
5395 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5396 {
5397 if (!parameters->options().ld_generated_unwind_info())
5398 return;
5399
5400 if (size == 64)
5401 {
5402 if (this->targ_->abiversion() < 2)
5403 layout->add_eh_frame_for_plt(this,
5404 Eh_cie<64>::eh_frame_cie,
5405 sizeof (Eh_cie<64>::eh_frame_cie),
5406 glink_eh_frame_fde_64v1,
5407 sizeof (glink_eh_frame_fde_64v1));
5408 else
5409 layout->add_eh_frame_for_plt(this,
5410 Eh_cie<64>::eh_frame_cie,
5411 sizeof (Eh_cie<64>::eh_frame_cie),
5412 glink_eh_frame_fde_64v2,
5413 sizeof (glink_eh_frame_fde_64v2));
5414 }
5415 else
5416 {
5417 // 32-bit .glink can use the default since the CIE return
5418 // address reg, LR, is valid.
5419 layout->add_eh_frame_for_plt(this,
5420 Eh_cie<32>::eh_frame_cie,
5421 sizeof (Eh_cie<32>::eh_frame_cie),
5422 default_fde,
5423 sizeof (default_fde));
5424 // Except where LR is used in a PIC __glink_PLTresolve.
5425 if (parameters->options().output_is_position_independent())
5426 layout->add_eh_frame_for_plt(this,
5427 Eh_cie<32>::eh_frame_cie,
5428 sizeof (Eh_cie<32>::eh_frame_cie),
5429 glink_eh_frame_fde_32,
5430 sizeof (glink_eh_frame_fde_32));
5431 }
5432 }
5433
5434 template<int size, bool big_endian>
5435 void
5436 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5437 {
5438 unsigned int off = this->global_entry_align(this->ge_size_);
5439 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5440 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5441 if (p.second)
5442 this->ge_size_ = off + 16;
5443 }
5444
5445 template<int size, bool big_endian>
5446 typename Output_data_glink<size, big_endian>::Address
5447 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5448 {
5449 typename Global_entry_stub_entries::const_iterator p
5450 = this->global_entry_stubs_.find(gsym);
5451 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5452 }
5453
5454 template<int size, bool big_endian>
5455 void
5456 Output_data_glink<size, big_endian>::set_final_data_size()
5457 {
5458 unsigned int count = this->targ_->plt_entry_count();
5459 section_size_type total = 0;
5460
5461 if (count != 0)
5462 {
5463 if (size == 32)
5464 {
5465 // space for branch table
5466 total += 4 * (count - 1);
5467
5468 total += -total & 15;
5469 total += this->pltresolve_size();
5470 }
5471 else
5472 {
5473 total += this->pltresolve_size();
5474
5475 // space for branch table
5476 total += 4 * count;
5477 if (this->targ_->abiversion() < 2)
5478 {
5479 total += 4 * count;
5480 if (count > 0x8000)
5481 total += 4 * (count - 0x8000);
5482 }
5483 }
5484 }
5485 this->end_branch_table_ = total;
5486 total = this->global_entry_align(total);
5487 total += this->ge_size_;
5488
5489 this->set_data_size(total);
5490 }
5491
5492 // Define symbols on stubs, identifying the stub.
5493
5494 template<int size, bool big_endian>
5495 void
5496 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5497 {
5498 if (!this->plt_call_stubs_.empty())
5499 {
5500 // The key for the plt call stub hash table includes addresses,
5501 // therefore traversal order depends on those addresses, which
5502 // can change between runs if gold is a PIE. Unfortunately the
5503 // output .symtab ordering depends on the order in which symbols
5504 // are added to the linker symtab. We want reproducible output
5505 // so must sort the call stub symbols.
5506 typedef typename Plt_stub_entries::const_iterator plt_iter;
5507 std::vector<plt_iter> sorted;
5508 sorted.resize(this->plt_call_stubs_.size());
5509
5510 for (plt_iter cs = this->plt_call_stubs_.begin();
5511 cs != this->plt_call_stubs_.end();
5512 ++cs)
5513 sorted[cs->second.indx_] = cs;
5514
5515 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5516 {
5517 plt_iter cs = sorted[i];
5518 char add[10];
5519 add[0] = 0;
5520 if (cs->first.addend_ != 0)
5521 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5522 char obj[10];
5523 obj[0] = 0;
5524 if (cs->first.object_)
5525 {
5526 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5527 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5528 sprintf(obj, "%x:", ppcobj->uniq());
5529 }
5530 char localname[9];
5531 const char *symname;
5532 if (cs->first.sym_ == NULL)
5533 {
5534 sprintf(localname, "%x", cs->first.locsym_);
5535 symname = localname;
5536 }
5537 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5538 symname = this->targ_->tls_get_addr_opt()->name();
5539 else
5540 symname = cs->first.sym_->name();
5541 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5542 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5543 Address value
5544 = this->stub_address() - this->address() + cs->second.off_;
5545 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5546 this->targ_->define_local(symtab, name, this, value, stub_size);
5547 }
5548 }
5549
5550 typedef typename Branch_stub_entries::const_iterator branch_iter;
5551 for (branch_iter bs = this->long_branch_stubs_.begin();
5552 bs != this->long_branch_stubs_.end();
5553 ++bs)
5554 {
5555 if (bs->second.save_res_)
5556 continue;
5557
5558 char* name = new char[8 + 13 + 16 + 1];
5559 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5560 static_cast<unsigned long long>(bs->first.dest_));
5561 Address value = (this->stub_address() - this->address()
5562 + this->plt_size_ + bs->second.off_);
5563 bool need_lt = false;
5564 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5565 this->targ_->define_local(symtab, name, this, value, stub_size);
5566 }
5567 }
5568
5569 // Emit the start of a __tls_get_addr_opt plt call stub.
5570
5571 template<int size, bool big_endian>
5572 bool
5573 Stub_table<size, big_endian>::build_tls_opt_head(
5574 unsigned char** pp,
5575 typename Plt_stub_entries::const_iterator cs)
5576 {
5577 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5578 {
5579 unsigned char* p = *pp;
5580 if (size == 64)
5581 {
5582 write_insn<big_endian>(p, ld_11_3 + 0);
5583 p += 4;
5584 write_insn<big_endian>(p, ld_12_3 + 8);
5585 p += 4;
5586 write_insn<big_endian>(p, mr_0_3);
5587 p += 4;
5588 write_insn<big_endian>(p, cmpdi_11_0);
5589 p += 4;
5590 write_insn<big_endian>(p, add_3_12_13);
5591 p += 4;
5592 write_insn<big_endian>(p, beqlr);
5593 p += 4;
5594 write_insn<big_endian>(p, mr_3_0);
5595 p += 4;
5596 if (cs->second.r2save_ && !cs->second.localentry0_)
5597 {
5598 write_insn<big_endian>(p, mflr_11);
5599 p += 4;
5600 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5601 p += 4;
5602 }
5603 }
5604 else
5605 {
5606 write_insn<big_endian>(p, lwz_11_3 + 0);
5607 p += 4;
5608 write_insn<big_endian>(p, lwz_12_3 + 4);
5609 p += 4;
5610 write_insn<big_endian>(p, mr_0_3);
5611 p += 4;
5612 write_insn<big_endian>(p, cmpwi_11_0);
5613 p += 4;
5614 write_insn<big_endian>(p, add_3_12_2);
5615 p += 4;
5616 write_insn<big_endian>(p, beqlr);
5617 p += 4;
5618 write_insn<big_endian>(p, mr_3_0);
5619 p += 4;
5620 write_insn<big_endian>(p, nop);
5621 p += 4;
5622 }
5623 *pp = p;
5624 return true;
5625 }
5626 return false;
5627 }
5628
5629 // Emit the tail of a __tls_get_addr_opt plt call stub.
5630
5631 template<int size, bool big_endian>
5632 bool
5633 Stub_table<size, big_endian>::build_tls_opt_tail(
5634 unsigned char* p,
5635 typename Plt_stub_entries::const_iterator cs)
5636 {
5637 if (size == 64
5638 && cs->second.r2save_
5639 && !cs->second.localentry0_
5640 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5641 {
5642 write_insn<big_endian>(p, bctrl);
5643 p += 4;
5644 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5645 p += 4;
5646 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5647 p += 4;
5648 write_insn<big_endian>(p, mtlr_11);
5649 p += 4;
5650 write_insn<big_endian>(p, blr);
5651 return true;
5652 }
5653 return false;
5654 }
5655
5656 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5657 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5658 // mflr %r12
5659 // bcl 20,31,1f
5660 // 1: mflr %r11
5661 // mtlr %r12
5662 // lis %r12,xxx-1b@highest
5663 // ori %r12,%r12,xxx-1b@higher
5664 // sldi %r12,%r12,32
5665 // oris %r12,%r12,xxx-1b@high
5666 // ori %r12,%r12,xxx-1b@l
5667 // add/ldx %r12,%r11,%r12
5668
5669 template<bool big_endian>
5670 static unsigned char*
5671 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5672 {
5673 write_insn<big_endian>(p, mflr_12);
5674 p += 4;
5675 write_insn<big_endian>(p, bcl_20_31);
5676 p += 4;
5677 write_insn<big_endian>(p, mflr_11);
5678 p += 4;
5679 write_insn<big_endian>(p, mtlr_12);
5680 p += 4;
5681 if (off + 0x8000 < 0x10000)
5682 {
5683 if (load)
5684 write_insn<big_endian>(p, ld_12_11 + l(off));
5685 else
5686 write_insn<big_endian>(p, addi_12_11 + l(off));
5687 }
5688 else if (off + 0x80008000ULL < 0x100000000ULL)
5689 {
5690 write_insn<big_endian>(p, addis_12_11 + ha(off));
5691 p += 4;
5692 if (load)
5693 write_insn<big_endian>(p, ld_12_12 + l(off));
5694 else
5695 write_insn<big_endian>(p, addi_12_12 + l(off));
5696 }
5697 else
5698 {
5699 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5700 {
5701 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5702 p += 4;
5703 }
5704 else
5705 {
5706 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5707 p += 4;
5708 if (((off >> 32) & 0xffff) != 0)
5709 {
5710 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5711 p += 4;
5712 }
5713 }
5714 if (((off >> 32) & 0xffffffffULL) != 0)
5715 {
5716 write_insn<big_endian>(p, sldi_12_12_32);
5717 p += 4;
5718 }
5719 if (hi(off) != 0)
5720 {
5721 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5722 p += 4;
5723 }
5724 if (l(off) != 0)
5725 {
5726 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5727 p += 4;
5728 }
5729 if (load)
5730 write_insn<big_endian>(p, ldx_12_11_12);
5731 else
5732 write_insn<big_endian>(p, add_12_11_12);
5733 }
5734 p += 4;
5735 return p;
5736 }
5737
5738 // Size of a given plt call stub.
5739
5740 template<int size, bool big_endian>
5741 unsigned int
5742 Stub_table<size, big_endian>::plt_call_size(
5743 typename Plt_stub_entries::const_iterator p) const
5744 {
5745 if (size == 32)
5746 {
5747 const Symbol* gsym = p->first.sym_;
5748 return (4 * 4
5749 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
5750 }
5751
5752 const Output_data_plt_powerpc<size, big_endian>* plt;
5753 uint64_t plt_addr = this->plt_off(p, &plt);
5754 plt_addr += plt->address();
5755 unsigned int bytes = 0;
5756 const Symbol* gsym = p->first.sym_;
5757 if (this->targ_->is_tls_get_addr_opt(gsym))
5758 {
5759 if (p->second.r2save_ && !p->second.localentry0_)
5760 bytes = 13 * 4;
5761 else
5762 bytes = 7 * 4;
5763 }
5764
5765 if (p->second.r2save_)
5766 bytes += 4;
5767
5768 if (p->second.notoc_)
5769 {
5770 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
5771 if (bytes > 32)
5772 from -= 4 * 4;
5773 uint64_t off = plt_addr - from;
5774 if (off + 0x8000 < 0x10000)
5775 bytes += 7 * 4;
5776 else if (off + 0x80008000ULL < 0x100000000ULL)
5777 bytes += 8 * 4;
5778 else
5779 {
5780 bytes += 8 * 4;
5781 if (off + 0x800000000000ULL >= 0x1000000000000ULL
5782 && ((off >> 32) & 0xffff) != 0)
5783 bytes += 4;
5784 if (((off >> 32) & 0xffffffffULL) != 0)
5785 bytes += 4;
5786 if (hi(off) != 0)
5787 bytes += 4;
5788 if (l(off) != 0)
5789 bytes += 4;
5790 }
5791 return bytes;
5792 }
5793
5794 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
5795 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5796 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
5797 got_addr += ppcobj->toc_base_offset();
5798 uint64_t off = plt_addr - got_addr;
5799 bytes += 3 * 4 + 4 * (ha(off) != 0);
5800 if (this->targ_->abiversion() < 2)
5801 {
5802 bool static_chain = parameters->options().plt_static_chain();
5803 bool thread_safe = this->targ_->plt_thread_safe();
5804 bytes += (4
5805 + 4 * static_chain
5806 + 8 * thread_safe
5807 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
5808 }
5809 return bytes;
5810 }
5811
5812 // Return long branch stub size.
5813
5814 template<int size, bool big_endian>
5815 unsigned int
5816 Stub_table<size, big_endian>::branch_stub_size(
5817 typename Branch_stub_entries::const_iterator p,
5818 bool* need_lt)
5819 {
5820 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
5821 if (size == 32)
5822 {
5823 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
5824 return 4;
5825 if (parameters->options().output_is_position_independent())
5826 return 32;
5827 return 16;
5828 }
5829
5830 uint64_t off = p->first.dest_ - loc;
5831 if (p->second.notoc_)
5832 {
5833 off -= 8;
5834 if (off + 0x8000 < 0x10000)
5835 return 24;
5836 if (off + 0x80008000ULL < 0x100000000ULL)
5837 {
5838 if (off + 24 + (1 << 25) < 2 << 25)
5839 return 28;
5840 return 32;
5841 }
5842 unsigned int bytes = 32;
5843 if (off + 0x800000000000ULL >= 0x1000000000000ULL
5844 && ((off >> 32) & 0xffff) != 0)
5845 bytes += 4;
5846 if (((off >> 32) & 0xffffffffULL) != 0)
5847 bytes += 4;
5848 if (hi(off) != 0)
5849 bytes += 4;
5850 if (l(off) != 0)
5851 bytes += 4;
5852 return bytes;
5853 }
5854
5855 if (off + (1 << 25) < 2 << 25)
5856 return 4;
5857 *need_lt = true;
5858 return 16;
5859 }
5860
5861 template<int size, bool big_endian>
5862 void
5863 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
5864 {
5865 if (p.sym_)
5866 gold_error(_("linkage table error against `%s'"),
5867 p.sym_->demangled_name().c_str());
5868 else
5869 gold_error(_("linkage table error against `%s:[local %u]'"),
5870 p.object_->name().c_str(),
5871 p.locsym_);
5872 }
5873
5874 // Write out plt and long branch stub code.
5875
5876 template<int size, bool big_endian>
5877 void
5878 Stub_table<size, big_endian>::do_write(Output_file* of)
5879 {
5880 if (this->plt_call_stubs_.empty()
5881 && this->long_branch_stubs_.empty())
5882 return;
5883
5884 const section_size_type start_off = this->offset();
5885 const section_size_type off = this->stub_offset();
5886 const section_size_type oview_size =
5887 convert_to_section_size_type(this->data_size() - (off - start_off));
5888 unsigned char* const oview = of->get_output_view(off, oview_size);
5889 unsigned char* p;
5890
5891 if (size == 64)
5892 {
5893 const Output_data_got_powerpc<size, big_endian>* got
5894 = this->targ_->got_section();
5895 Address got_os_addr = got->output_section()->address();
5896
5897 if (!this->plt_call_stubs_.empty()
5898 && this->targ_->abiversion() >= 2)
5899 {
5900 // Write out plt call stubs for ELFv2.
5901 typename Plt_stub_entries::const_iterator cs;
5902 for (cs = this->plt_call_stubs_.begin();
5903 cs != this->plt_call_stubs_.end();
5904 ++cs)
5905 {
5906 const Output_data_plt_powerpc<size, big_endian>* plt;
5907 Address pltoff = this->plt_off(cs, &plt);
5908 Address plt_addr = pltoff + plt->address();
5909
5910 p = oview + cs->second.off_;
5911 this->build_tls_opt_head(&p, cs);
5912 if (cs->second.r2save_)
5913 {
5914 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
5915 p += 4;
5916 }
5917 if (cs->second.notoc_)
5918 {
5919 Address from = this->stub_address() + (p - oview) + 8;
5920 Address off = plt_addr - from;
5921 p = build_notoc_offset<big_endian>(p, off, true);
5922 }
5923 else
5924 {
5925 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5926 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5927 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
5928 Address off = plt_addr - got_addr;
5929
5930 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5931 this->plt_error(cs->first);
5932
5933 if (ha(off) != 0)
5934 {
5935 write_insn<big_endian>(p, addis_12_2 + ha(off));
5936 p += 4;
5937 write_insn<big_endian>(p, ld_12_12 + l(off));
5938 p += 4;
5939 }
5940 else
5941 {
5942 write_insn<big_endian>(p, ld_12_2 + l(off));
5943 p += 4;
5944 }
5945 }
5946 write_insn<big_endian>(p, mtctr_12);
5947 p += 4;
5948 if (!this->build_tls_opt_tail(p, cs))
5949 write_insn<big_endian>(p, bctr);
5950 }
5951 }
5952 else if (!this->plt_call_stubs_.empty())
5953 {
5954 // Write out plt call stubs for ELFv1.
5955 typename Plt_stub_entries::const_iterator cs;
5956 for (cs = this->plt_call_stubs_.begin();
5957 cs != this->plt_call_stubs_.end();
5958 ++cs)
5959 {
5960 const Output_data_plt_powerpc<size, big_endian>* plt;
5961 Address pltoff = this->plt_off(cs, &plt);
5962 Address plt_addr = pltoff + plt->address();
5963 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5964 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5965 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
5966 Address off = plt_addr - got_addr;
5967
5968 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
5969 || cs->second.notoc_)
5970 this->plt_error(cs->first);
5971
5972 bool static_chain = parameters->options().plt_static_chain();
5973 bool thread_safe = this->targ_->plt_thread_safe();
5974 bool use_fake_dep = false;
5975 Address cmp_branch_off = 0;
5976 if (thread_safe)
5977 {
5978 unsigned int pltindex
5979 = ((pltoff - this->targ_->first_plt_entry_offset())
5980 / this->targ_->plt_entry_size());
5981 Address glinkoff
5982 = (this->targ_->glink_section()->pltresolve_size()
5983 + pltindex * 8);
5984 if (pltindex > 32768)
5985 glinkoff += (pltindex - 32768) * 4;
5986 Address to
5987 = this->targ_->glink_section()->address() + glinkoff;
5988 Address from
5989 = (this->stub_address() + cs->second.off_ + 20
5990 + 4 * cs->second.r2save_
5991 + 4 * (ha(off) != 0)
5992 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
5993 + 4 * static_chain);
5994 cmp_branch_off = to - from;
5995 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
5996 }
5997
5998 p = oview + cs->second.off_;
5999 if (this->build_tls_opt_head(&p, cs))
6000 use_fake_dep = thread_safe;
6001 if (cs->second.r2save_)
6002 {
6003 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6004 p += 4;
6005 }
6006 if (ha(off) != 0)
6007 {
6008 write_insn<big_endian>(p, addis_11_2 + ha(off));
6009 p += 4;
6010 write_insn<big_endian>(p, ld_12_11 + l(off));
6011 p += 4;
6012 if (ha(off + 8 + 8 * static_chain) != ha(off))
6013 {
6014 write_insn<big_endian>(p, addi_11_11 + l(off));
6015 p += 4;
6016 off = 0;
6017 }
6018 write_insn<big_endian>(p, mtctr_12);
6019 p += 4;
6020 if (use_fake_dep)
6021 {
6022 write_insn<big_endian>(p, xor_2_12_12);
6023 p += 4;
6024 write_insn<big_endian>(p, add_11_11_2);
6025 p += 4;
6026 }
6027 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6028 p += 4;
6029 if (static_chain)
6030 {
6031 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6032 p += 4;
6033 }
6034 }
6035 else
6036 {
6037 write_insn<big_endian>(p, ld_12_2 + l(off));
6038 p += 4;
6039 if (ha(off + 8 + 8 * static_chain) != ha(off))
6040 {
6041 write_insn<big_endian>(p, addi_2_2 + l(off));
6042 p += 4;
6043 off = 0;
6044 }
6045 write_insn<big_endian>(p, mtctr_12);
6046 p += 4;
6047 if (use_fake_dep)
6048 {
6049 write_insn<big_endian>(p, xor_11_12_12);
6050 p += 4;
6051 write_insn<big_endian>(p, add_2_2_11);
6052 p += 4;
6053 }
6054 if (static_chain)
6055 {
6056 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6057 p += 4;
6058 }
6059 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6060 p += 4;
6061 }
6062 if (this->build_tls_opt_tail(p, cs))
6063 ;
6064 else if (thread_safe && !use_fake_dep)
6065 {
6066 write_insn<big_endian>(p, cmpldi_2_0);
6067 p += 4;
6068 write_insn<big_endian>(p, bnectr_p4);
6069 p += 4;
6070 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6071 }
6072 else
6073 write_insn<big_endian>(p, bctr);
6074 }
6075 }
6076
6077 // Write out long branch stubs.
6078 typename Branch_stub_entries::const_iterator bs;
6079 for (bs = this->long_branch_stubs_.begin();
6080 bs != this->long_branch_stubs_.end();
6081 ++bs)
6082 {
6083 if (bs->second.save_res_)
6084 continue;
6085 Address off = this->plt_size_ + bs->second.off_;
6086 p = oview + off;
6087 Address loc = this->stub_address() + off;
6088 Address delta = bs->first.dest_ - loc;
6089 if (bs->second.notoc_)
6090 {
6091 unsigned char* startp = p;
6092 p = build_notoc_offset<big_endian>(p, off, false);
6093 delta -= p - startp;
6094 }
6095 else if (delta + (1 << 25) >= 2 << 25)
6096 {
6097 Address brlt_addr
6098 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6099 gold_assert(brlt_addr != invalid_address);
6100 brlt_addr += this->targ_->brlt_section()->address();
6101 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6102 Address brltoff = brlt_addr - got_addr;
6103 if (ha(brltoff) == 0)
6104 {
6105 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6106 p += 4;
6107 }
6108 else
6109 {
6110 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6111 p += 4;
6112 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6113 p += 4;
6114 }
6115 }
6116 if (delta + (1 << 25) < 2 << 25)
6117 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6118 else
6119 {
6120 write_insn<big_endian>(p, mtctr_12);
6121 p += 4;
6122 write_insn<big_endian>(p, bctr);
6123 }
6124 }
6125 }
6126 else // size == 32
6127 {
6128 if (!this->plt_call_stubs_.empty())
6129 {
6130 // The address of _GLOBAL_OFFSET_TABLE_.
6131 Address g_o_t = invalid_address;
6132
6133 // Write out plt call stubs.
6134 typename Plt_stub_entries::const_iterator cs;
6135 for (cs = this->plt_call_stubs_.begin();
6136 cs != this->plt_call_stubs_.end();
6137 ++cs)
6138 {
6139 const Output_data_plt_powerpc<size, big_endian>* plt;
6140 Address plt_addr = this->plt_off(cs, &plt);
6141 plt_addr += plt->address();
6142
6143 p = oview + cs->second.off_;
6144 this->build_tls_opt_head(&p, cs);
6145 if (parameters->options().output_is_position_independent())
6146 {
6147 Address got_addr;
6148 const Powerpc_relobj<size, big_endian>* ppcobj
6149 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6150 (cs->first.object_));
6151 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6152 {
6153 unsigned int got2 = ppcobj->got2_shndx();
6154 got_addr = ppcobj->get_output_section_offset(got2);
6155 gold_assert(got_addr != invalid_address);
6156 got_addr += (ppcobj->output_section(got2)->address()
6157 + cs->first.addend_);
6158 }
6159 else
6160 {
6161 if (g_o_t == invalid_address)
6162 {
6163 const Output_data_got_powerpc<size, big_endian>* got
6164 = this->targ_->got_section();
6165 g_o_t = got->address() + got->g_o_t();
6166 }
6167 got_addr = g_o_t;
6168 }
6169
6170 Address off = plt_addr - got_addr;
6171 if (ha(off) == 0)
6172 write_insn<big_endian>(p, lwz_11_30 + l(off));
6173 else
6174 {
6175 write_insn<big_endian>(p, addis_11_30 + ha(off));
6176 p += 4;
6177 write_insn<big_endian>(p, lwz_11_11 + l(off));
6178 }
6179 }
6180 else
6181 {
6182 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6183 p += 4;
6184 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6185 }
6186 p += 4;
6187 write_insn<big_endian>(p, mtctr_11);
6188 p += 4;
6189 write_insn<big_endian>(p, bctr);
6190 }
6191 }
6192
6193 // Write out long branch stubs.
6194 typename Branch_stub_entries::const_iterator bs;
6195 for (bs = this->long_branch_stubs_.begin();
6196 bs != this->long_branch_stubs_.end();
6197 ++bs)
6198 {
6199 if (bs->second.save_res_)
6200 continue;
6201 Address off = this->plt_size_ + bs->second.off_;
6202 p = oview + off;
6203 Address loc = this->stub_address() + off;
6204 Address delta = bs->first.dest_ - loc;
6205 if (delta + (1 << 25) < 2 << 25)
6206 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6207 else if (!parameters->options().output_is_position_independent())
6208 {
6209 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6210 p += 4;
6211 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6212 }
6213 else
6214 {
6215 delta -= 8;
6216 write_insn<big_endian>(p, mflr_0);
6217 p += 4;
6218 write_insn<big_endian>(p, bcl_20_31);
6219 p += 4;
6220 write_insn<big_endian>(p, mflr_12);
6221 p += 4;
6222 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6223 p += 4;
6224 write_insn<big_endian>(p, addi_12_12 + l(delta));
6225 p += 4;
6226 write_insn<big_endian>(p, mtlr_0);
6227 }
6228 p += 4;
6229 write_insn<big_endian>(p, mtctr_12);
6230 p += 4;
6231 write_insn<big_endian>(p, bctr);
6232 }
6233 }
6234 if (this->need_save_res_)
6235 {
6236 p = oview + this->plt_size_ + this->branch_size_;
6237 memcpy (p, this->targ_->savres_section()->contents(),
6238 this->targ_->savres_section()->data_size());
6239 }
6240 }
6241
6242 // Write out .glink.
6243
6244 template<int size, bool big_endian>
6245 void
6246 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6247 {
6248 const section_size_type off = this->offset();
6249 const section_size_type oview_size =
6250 convert_to_section_size_type(this->data_size());
6251 unsigned char* const oview = of->get_output_view(off, oview_size);
6252 unsigned char* p;
6253
6254 // The base address of the .plt section.
6255 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6256 Address plt_base = this->targ_->plt_section()->address();
6257
6258 if (size == 64)
6259 {
6260 if (this->end_branch_table_ != 0)
6261 {
6262 // Write pltresolve stub.
6263 p = oview;
6264 Address after_bcl = this->address() + 16;
6265 Address pltoff = plt_base - after_bcl;
6266
6267 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
6268
6269 if (this->targ_->abiversion() < 2)
6270 {
6271 write_insn<big_endian>(p, mflr_12), p += 4;
6272 write_insn<big_endian>(p, bcl_20_31), p += 4;
6273 write_insn<big_endian>(p, mflr_11), p += 4;
6274 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6275 write_insn<big_endian>(p, mtlr_12), p += 4;
6276 write_insn<big_endian>(p, add_11_2_11), p += 4;
6277 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6278 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6279 write_insn<big_endian>(p, mtctr_12), p += 4;
6280 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6281 }
6282 else
6283 {
6284 write_insn<big_endian>(p, mflr_0), p += 4;
6285 write_insn<big_endian>(p, bcl_20_31), p += 4;
6286 write_insn<big_endian>(p, mflr_11), p += 4;
6287 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6288 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6289 write_insn<big_endian>(p, mtlr_0), p += 4;
6290 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6291 write_insn<big_endian>(p, add_11_2_11), p += 4;
6292 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
6293 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6294 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6295 write_insn<big_endian>(p, mtctr_12), p += 4;
6296 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6297 }
6298 write_insn<big_endian>(p, bctr), p += 4;
6299 gold_assert(p == oview + this->pltresolve_size());
6300
6301 // Write lazy link call stubs.
6302 uint32_t indx = 0;
6303 while (p < oview + this->end_branch_table_)
6304 {
6305 if (this->targ_->abiversion() < 2)
6306 {
6307 if (indx < 0x8000)
6308 {
6309 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6310 }
6311 else
6312 {
6313 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
6314 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6315 }
6316 }
6317 uint32_t branch_off = 8 - (p - oview);
6318 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6319 indx++;
6320 }
6321 }
6322
6323 Address plt_base = this->targ_->plt_section()->address();
6324 Address iplt_base = invalid_address;
6325 unsigned int global_entry_off = this->global_entry_off();
6326 Address global_entry_base = this->address() + global_entry_off;
6327 typename Global_entry_stub_entries::const_iterator ge;
6328 for (ge = this->global_entry_stubs_.begin();
6329 ge != this->global_entry_stubs_.end();
6330 ++ge)
6331 {
6332 p = oview + global_entry_off + ge->second;
6333 Address plt_addr = ge->first->plt_offset();
6334 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6335 && ge->first->can_use_relative_reloc(false))
6336 {
6337 if (iplt_base == invalid_address)
6338 iplt_base = this->targ_->iplt_section()->address();
6339 plt_addr += iplt_base;
6340 }
6341 else
6342 plt_addr += plt_base;
6343 Address my_addr = global_entry_base + ge->second;
6344 Address off = plt_addr - my_addr;
6345
6346 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6347 gold_error(_("linkage table error against `%s'"),
6348 ge->first->demangled_name().c_str());
6349
6350 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6351 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6352 write_insn<big_endian>(p, mtctr_12), p += 4;
6353 write_insn<big_endian>(p, bctr);
6354 }
6355 }
6356 else
6357 {
6358 const Output_data_got_powerpc<size, big_endian>* got
6359 = this->targ_->got_section();
6360 // The address of _GLOBAL_OFFSET_TABLE_.
6361 Address g_o_t = got->address() + got->g_o_t();
6362
6363 // Write out pltresolve branch table.
6364 p = oview;
6365 unsigned int the_end = oview_size - this->pltresolve_size();
6366 unsigned char* end_p = oview + the_end;
6367 while (p < end_p - 8 * 4)
6368 write_insn<big_endian>(p, b + end_p - p), p += 4;
6369 while (p < end_p)
6370 write_insn<big_endian>(p, nop), p += 4;
6371
6372 // Write out pltresolve call stub.
6373 end_p = oview + oview_size;
6374 if (parameters->options().output_is_position_independent())
6375 {
6376 Address res0_off = 0;
6377 Address after_bcl_off = the_end + 12;
6378 Address bcl_res0 = after_bcl_off - res0_off;
6379
6380 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6381 p += 4;
6382 write_insn<big_endian>(p, mflr_0);
6383 p += 4;
6384 write_insn<big_endian>(p, bcl_20_31);
6385 p += 4;
6386 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6387 p += 4;
6388 write_insn<big_endian>(p, mflr_12);
6389 p += 4;
6390 write_insn<big_endian>(p, mtlr_0);
6391 p += 4;
6392 write_insn<big_endian>(p, sub_11_11_12);
6393 p += 4;
6394
6395 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
6396
6397 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6398 p += 4;
6399 if (ha(got_bcl) == ha(got_bcl + 4))
6400 {
6401 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6402 p += 4;
6403 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
6404 }
6405 else
6406 {
6407 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6408 p += 4;
6409 write_insn<big_endian>(p, lwz_12_12 + 4);
6410 }
6411 p += 4;
6412 write_insn<big_endian>(p, mtctr_0);
6413 p += 4;
6414 write_insn<big_endian>(p, add_0_11_11);
6415 p += 4;
6416 write_insn<big_endian>(p, add_11_0_11);
6417 }
6418 else
6419 {
6420 Address res0 = this->address();
6421
6422 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6423 p += 4;
6424 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
6425 p += 4;
6426 if (ha(g_o_t + 4) == ha(g_o_t + 8))
6427 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
6428 else
6429 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
6430 p += 4;
6431 write_insn<big_endian>(p, addi_11_11 + l(-res0));
6432 p += 4;
6433 write_insn<big_endian>(p, mtctr_0);
6434 p += 4;
6435 write_insn<big_endian>(p, add_0_11_11);
6436 p += 4;
6437 if (ha(g_o_t + 4) == ha(g_o_t + 8))
6438 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
6439 else
6440 write_insn<big_endian>(p, lwz_12_12 + 4);
6441 p += 4;
6442 write_insn<big_endian>(p, add_11_0_11);
6443 }
6444 p += 4;
6445 write_insn<big_endian>(p, bctr);
6446 p += 4;
6447 while (p < end_p)
6448 {
6449 write_insn<big_endian>(p, nop);
6450 p += 4;
6451 }
6452 }
6453
6454 of->write_output_view(off, oview_size, oview);
6455 }
6456
6457
6458 // A class to handle linker generated save/restore functions.
6459
6460 template<int size, bool big_endian>
6461 class Output_data_save_res : public Output_section_data_build
6462 {
6463 public:
6464 Output_data_save_res(Symbol_table* symtab);
6465
6466 const unsigned char*
6467 contents() const
6468 {
6469 return contents_;
6470 }
6471
6472 protected:
6473 // Write to a map file.
6474 void
6475 do_print_to_mapfile(Mapfile* mapfile) const
6476 { mapfile->print_output_data(this, _("** save/restore")); }
6477
6478 void
6479 do_write(Output_file*);
6480
6481 private:
6482 // The maximum size of save/restore contents.
6483 static const unsigned int savres_max = 218*4;
6484
6485 void
6486 savres_define(Symbol_table* symtab,
6487 const char *name,
6488 unsigned int lo, unsigned int hi,
6489 unsigned char* write_ent(unsigned char*, int),
6490 unsigned char* write_tail(unsigned char*, int));
6491
6492 unsigned char *contents_;
6493 };
6494
6495 template<bool big_endian>
6496 static unsigned char*
6497 savegpr0(unsigned char* p, int r)
6498 {
6499 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6500 write_insn<big_endian>(p, insn);
6501 return p + 4;
6502 }
6503
6504 template<bool big_endian>
6505 static unsigned char*
6506 savegpr0_tail(unsigned char* p, int r)
6507 {
6508 p = savegpr0<big_endian>(p, r);
6509 uint32_t insn = std_0_1 + 16;
6510 write_insn<big_endian>(p, insn);
6511 p = p + 4;
6512 write_insn<big_endian>(p, blr);
6513 return p + 4;
6514 }
6515
6516 template<bool big_endian>
6517 static unsigned char*
6518 restgpr0(unsigned char* p, int r)
6519 {
6520 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6521 write_insn<big_endian>(p, insn);
6522 return p + 4;
6523 }
6524
6525 template<bool big_endian>
6526 static unsigned char*
6527 restgpr0_tail(unsigned char* p, int r)
6528 {
6529 uint32_t insn = ld_0_1 + 16;
6530 write_insn<big_endian>(p, insn);
6531 p = p + 4;
6532 p = restgpr0<big_endian>(p, r);
6533 write_insn<big_endian>(p, mtlr_0);
6534 p = p + 4;
6535 if (r == 29)
6536 {
6537 p = restgpr0<big_endian>(p, 30);
6538 p = restgpr0<big_endian>(p, 31);
6539 }
6540 write_insn<big_endian>(p, blr);
6541 return p + 4;
6542 }
6543
6544 template<bool big_endian>
6545 static unsigned char*
6546 savegpr1(unsigned char* p, int r)
6547 {
6548 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6549 write_insn<big_endian>(p, insn);
6550 return p + 4;
6551 }
6552
6553 template<bool big_endian>
6554 static unsigned char*
6555 savegpr1_tail(unsigned char* p, int r)
6556 {
6557 p = savegpr1<big_endian>(p, r);
6558 write_insn<big_endian>(p, blr);
6559 return p + 4;
6560 }
6561
6562 template<bool big_endian>
6563 static unsigned char*
6564 restgpr1(unsigned char* p, int r)
6565 {
6566 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6567 write_insn<big_endian>(p, insn);
6568 return p + 4;
6569 }
6570
6571 template<bool big_endian>
6572 static unsigned char*
6573 restgpr1_tail(unsigned char* p, int r)
6574 {
6575 p = restgpr1<big_endian>(p, r);
6576 write_insn<big_endian>(p, blr);
6577 return p + 4;
6578 }
6579
6580 template<bool big_endian>
6581 static unsigned char*
6582 savefpr(unsigned char* p, int r)
6583 {
6584 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6585 write_insn<big_endian>(p, insn);
6586 return p + 4;
6587 }
6588
6589 template<bool big_endian>
6590 static unsigned char*
6591 savefpr0_tail(unsigned char* p, int r)
6592 {
6593 p = savefpr<big_endian>(p, r);
6594 write_insn<big_endian>(p, std_0_1 + 16);
6595 p = p + 4;
6596 write_insn<big_endian>(p, blr);
6597 return p + 4;
6598 }
6599
6600 template<bool big_endian>
6601 static unsigned char*
6602 restfpr(unsigned char* p, int r)
6603 {
6604 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6605 write_insn<big_endian>(p, insn);
6606 return p + 4;
6607 }
6608
6609 template<bool big_endian>
6610 static unsigned char*
6611 restfpr0_tail(unsigned char* p, int r)
6612 {
6613 write_insn<big_endian>(p, ld_0_1 + 16);
6614 p = p + 4;
6615 p = restfpr<big_endian>(p, r);
6616 write_insn<big_endian>(p, mtlr_0);
6617 p = p + 4;
6618 if (r == 29)
6619 {
6620 p = restfpr<big_endian>(p, 30);
6621 p = restfpr<big_endian>(p, 31);
6622 }
6623 write_insn<big_endian>(p, blr);
6624 return p + 4;
6625 }
6626
6627 template<bool big_endian>
6628 static unsigned char*
6629 savefpr1_tail(unsigned char* p, int r)
6630 {
6631 p = savefpr<big_endian>(p, r);
6632 write_insn<big_endian>(p, blr);
6633 return p + 4;
6634 }
6635
6636 template<bool big_endian>
6637 static unsigned char*
6638 restfpr1_tail(unsigned char* p, int r)
6639 {
6640 p = restfpr<big_endian>(p, r);
6641 write_insn<big_endian>(p, blr);
6642 return p + 4;
6643 }
6644
6645 template<bool big_endian>
6646 static unsigned char*
6647 savevr(unsigned char* p, int r)
6648 {
6649 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6650 write_insn<big_endian>(p, insn);
6651 p = p + 4;
6652 insn = stvx_0_12_0 + (r << 21);
6653 write_insn<big_endian>(p, insn);
6654 return p + 4;
6655 }
6656
6657 template<bool big_endian>
6658 static unsigned char*
6659 savevr_tail(unsigned char* p, int r)
6660 {
6661 p = savevr<big_endian>(p, r);
6662 write_insn<big_endian>(p, blr);
6663 return p + 4;
6664 }
6665
6666 template<bool big_endian>
6667 static unsigned char*
6668 restvr(unsigned char* p, int r)
6669 {
6670 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6671 write_insn<big_endian>(p, insn);
6672 p = p + 4;
6673 insn = lvx_0_12_0 + (r << 21);
6674 write_insn<big_endian>(p, insn);
6675 return p + 4;
6676 }
6677
6678 template<bool big_endian>
6679 static unsigned char*
6680 restvr_tail(unsigned char* p, int r)
6681 {
6682 p = restvr<big_endian>(p, r);
6683 write_insn<big_endian>(p, blr);
6684 return p + 4;
6685 }
6686
6687
6688 template<int size, bool big_endian>
6689 Output_data_save_res<size, big_endian>::Output_data_save_res(
6690 Symbol_table* symtab)
6691 : Output_section_data_build(4),
6692 contents_(NULL)
6693 {
6694 this->savres_define(symtab,
6695 "_savegpr0_", 14, 31,
6696 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6697 this->savres_define(symtab,
6698 "_restgpr0_", 14, 29,
6699 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6700 this->savres_define(symtab,
6701 "_restgpr0_", 30, 31,
6702 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6703 this->savres_define(symtab,
6704 "_savegpr1_", 14, 31,
6705 savegpr1<big_endian>, savegpr1_tail<big_endian>);
6706 this->savres_define(symtab,
6707 "_restgpr1_", 14, 31,
6708 restgpr1<big_endian>, restgpr1_tail<big_endian>);
6709 this->savres_define(symtab,
6710 "_savefpr_", 14, 31,
6711 savefpr<big_endian>, savefpr0_tail<big_endian>);
6712 this->savres_define(symtab,
6713 "_restfpr_", 14, 29,
6714 restfpr<big_endian>, restfpr0_tail<big_endian>);
6715 this->savres_define(symtab,
6716 "_restfpr_", 30, 31,
6717 restfpr<big_endian>, restfpr0_tail<big_endian>);
6718 this->savres_define(symtab,
6719 "._savef", 14, 31,
6720 savefpr<big_endian>, savefpr1_tail<big_endian>);
6721 this->savres_define(symtab,
6722 "._restf", 14, 31,
6723 restfpr<big_endian>, restfpr1_tail<big_endian>);
6724 this->savres_define(symtab,
6725 "_savevr_", 20, 31,
6726 savevr<big_endian>, savevr_tail<big_endian>);
6727 this->savres_define(symtab,
6728 "_restvr_", 20, 31,
6729 restvr<big_endian>, restvr_tail<big_endian>);
6730 }
6731
6732 template<int size, bool big_endian>
6733 void
6734 Output_data_save_res<size, big_endian>::savres_define(
6735 Symbol_table* symtab,
6736 const char *name,
6737 unsigned int lo, unsigned int hi,
6738 unsigned char* write_ent(unsigned char*, int),
6739 unsigned char* write_tail(unsigned char*, int))
6740 {
6741 size_t len = strlen(name);
6742 bool writing = false;
6743 char sym[16];
6744
6745 memcpy(sym, name, len);
6746 sym[len + 2] = 0;
6747
6748 for (unsigned int i = lo; i <= hi; i++)
6749 {
6750 sym[len + 0] = i / 10 + '0';
6751 sym[len + 1] = i % 10 + '0';
6752 Symbol* gsym = symtab->lookup(sym);
6753 bool refd = gsym != NULL && gsym->is_undefined();
6754 writing = writing || refd;
6755 if (writing)
6756 {
6757 if (this->contents_ == NULL)
6758 this->contents_ = new unsigned char[this->savres_max];
6759
6760 section_size_type value = this->current_data_size();
6761 unsigned char* p = this->contents_ + value;
6762 if (i != hi)
6763 p = write_ent(p, i);
6764 else
6765 p = write_tail(p, i);
6766 section_size_type cur_size = p - this->contents_;
6767 this->set_current_data_size(cur_size);
6768 if (refd)
6769 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
6770 this, value, cur_size - value,
6771 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
6772 elfcpp::STV_HIDDEN, 0, false, false);
6773 }
6774 }
6775 }
6776
6777 // Write out save/restore.
6778
6779 template<int size, bool big_endian>
6780 void
6781 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
6782 {
6783 const section_size_type off = this->offset();
6784 const section_size_type oview_size =
6785 convert_to_section_size_type(this->data_size());
6786 unsigned char* const oview = of->get_output_view(off, oview_size);
6787 memcpy(oview, this->contents_, oview_size);
6788 of->write_output_view(off, oview_size, oview);
6789 }
6790
6791
6792 // Create the glink section.
6793
6794 template<int size, bool big_endian>
6795 void
6796 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
6797 {
6798 if (this->glink_ == NULL)
6799 {
6800 this->glink_ = new Output_data_glink<size, big_endian>(this);
6801 this->glink_->add_eh_frame(layout);
6802 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6803 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6804 this->glink_, ORDER_TEXT, false);
6805 }
6806 }
6807
6808 // Create a PLT entry for a global symbol.
6809
6810 template<int size, bool big_endian>
6811 void
6812 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
6813 Layout* layout,
6814 Symbol* gsym)
6815 {
6816 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6817 && gsym->can_use_relative_reloc(false))
6818 {
6819 if (this->iplt_ == NULL)
6820 this->make_iplt_section(symtab, layout);
6821 this->iplt_->add_ifunc_entry(gsym);
6822 }
6823 else
6824 {
6825 if (this->plt_ == NULL)
6826 this->make_plt_section(symtab, layout);
6827 this->plt_->add_entry(gsym);
6828 }
6829 }
6830
6831 // Make a PLT entry for a local symbol.
6832
6833 template<int size, bool big_endian>
6834 void
6835 Target_powerpc<size, big_endian>::make_local_plt_entry(
6836 Layout* layout,
6837 Sized_relobj_file<size, big_endian>* relobj,
6838 unsigned int r_sym)
6839 {
6840 if (this->lplt_ == NULL)
6841 this->make_lplt_section(layout);
6842 this->lplt_->add_local_entry(relobj, r_sym);
6843 }
6844
6845 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6846
6847 template<int size, bool big_endian>
6848 void
6849 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
6850 Symbol_table* symtab,
6851 Layout* layout,
6852 Sized_relobj_file<size, big_endian>* relobj,
6853 unsigned int r_sym)
6854 {
6855 if (this->iplt_ == NULL)
6856 this->make_iplt_section(symtab, layout);
6857 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
6858 }
6859
6860 // Return the number of entries in the PLT.
6861
6862 template<int size, bool big_endian>
6863 unsigned int
6864 Target_powerpc<size, big_endian>::plt_entry_count() const
6865 {
6866 if (this->plt_ == NULL)
6867 return 0;
6868 return this->plt_->entry_count();
6869 }
6870
6871 // Create a GOT entry for local dynamic __tls_get_addr calls.
6872
6873 template<int size, bool big_endian>
6874 unsigned int
6875 Target_powerpc<size, big_endian>::tlsld_got_offset(
6876 Symbol_table* symtab,
6877 Layout* layout,
6878 Sized_relobj_file<size, big_endian>* object)
6879 {
6880 if (this->tlsld_got_offset_ == -1U)
6881 {
6882 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6883 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
6884 Output_data_got_powerpc<size, big_endian>* got
6885 = this->got_section(symtab, layout);
6886 unsigned int got_offset = got->add_constant_pair(0, 0);
6887 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
6888 got_offset, 0);
6889 this->tlsld_got_offset_ = got_offset;
6890 }
6891 return this->tlsld_got_offset_;
6892 }
6893
6894 // Get the Reference_flags for a particular relocation.
6895
6896 template<int size, bool big_endian>
6897 int
6898 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
6899 unsigned int r_type,
6900 const Target_powerpc* target)
6901 {
6902 int ref = 0;
6903
6904 switch (r_type)
6905 {
6906 case elfcpp::R_POWERPC_NONE:
6907 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6908 case elfcpp::R_POWERPC_GNU_VTENTRY:
6909 case elfcpp::R_PPC64_TOC:
6910 // No symbol reference.
6911 break;
6912
6913 case elfcpp::R_PPC64_ADDR64:
6914 case elfcpp::R_PPC64_UADDR64:
6915 case elfcpp::R_POWERPC_ADDR32:
6916 case elfcpp::R_POWERPC_UADDR32:
6917 case elfcpp::R_POWERPC_ADDR16:
6918 case elfcpp::R_POWERPC_UADDR16:
6919 case elfcpp::R_POWERPC_ADDR16_LO:
6920 case elfcpp::R_POWERPC_ADDR16_HI:
6921 case elfcpp::R_POWERPC_ADDR16_HA:
6922 ref = Symbol::ABSOLUTE_REF;
6923 break;
6924
6925 case elfcpp::R_POWERPC_ADDR24:
6926 case elfcpp::R_POWERPC_ADDR14:
6927 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6928 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6929 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
6930 break;
6931
6932 case elfcpp::R_PPC64_REL64:
6933 case elfcpp::R_POWERPC_REL32:
6934 case elfcpp::R_PPC_LOCAL24PC:
6935 case elfcpp::R_POWERPC_REL16:
6936 case elfcpp::R_POWERPC_REL16_LO:
6937 case elfcpp::R_POWERPC_REL16_HI:
6938 case elfcpp::R_POWERPC_REL16_HA:
6939 case elfcpp::R_PPC64_REL16_HIGH:
6940 case elfcpp::R_PPC64_REL16_HIGHA:
6941 case elfcpp::R_PPC64_REL16_HIGHER:
6942 case elfcpp::R_PPC64_REL16_HIGHERA:
6943 case elfcpp::R_PPC64_REL16_HIGHEST:
6944 case elfcpp::R_PPC64_REL16_HIGHESTA:
6945 ref = Symbol::RELATIVE_REF;
6946 break;
6947
6948 case elfcpp::R_PPC64_REL24_NOTOC:
6949 if (size == 32)
6950 break;
6951 // Fall through.
6952 case elfcpp::R_POWERPC_REL24:
6953 case elfcpp::R_PPC_PLTREL24:
6954 case elfcpp::R_POWERPC_REL14:
6955 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6956 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6957 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
6958 break;
6959
6960 case elfcpp::R_POWERPC_GOT16:
6961 case elfcpp::R_POWERPC_GOT16_LO:
6962 case elfcpp::R_POWERPC_GOT16_HI:
6963 case elfcpp::R_POWERPC_GOT16_HA:
6964 case elfcpp::R_PPC64_GOT16_DS:
6965 case elfcpp::R_PPC64_GOT16_LO_DS:
6966 case elfcpp::R_PPC64_TOC16:
6967 case elfcpp::R_PPC64_TOC16_LO:
6968 case elfcpp::R_PPC64_TOC16_HI:
6969 case elfcpp::R_PPC64_TOC16_HA:
6970 case elfcpp::R_PPC64_TOC16_DS:
6971 case elfcpp::R_PPC64_TOC16_LO_DS:
6972 case elfcpp::R_POWERPC_PLT16_LO:
6973 case elfcpp::R_POWERPC_PLT16_HI:
6974 case elfcpp::R_POWERPC_PLT16_HA:
6975 case elfcpp::R_PPC64_PLT16_LO_DS:
6976 ref = Symbol::RELATIVE_REF;
6977 break;
6978
6979 case elfcpp::R_POWERPC_GOT_TPREL16:
6980 case elfcpp::R_POWERPC_TLS:
6981 ref = Symbol::TLS_REF;
6982 break;
6983
6984 case elfcpp::R_POWERPC_COPY:
6985 case elfcpp::R_POWERPC_GLOB_DAT:
6986 case elfcpp::R_POWERPC_JMP_SLOT:
6987 case elfcpp::R_POWERPC_RELATIVE:
6988 case elfcpp::R_POWERPC_DTPMOD:
6989 default:
6990 // Not expected. We will give an error later.
6991 break;
6992 }
6993
6994 if (size == 64 && target->abiversion() < 2)
6995 ref |= Symbol::FUNC_DESC_ABI;
6996 return ref;
6997 }
6998
6999 // Report an unsupported relocation against a local symbol.
7000
7001 template<int size, bool big_endian>
7002 void
7003 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7004 Sized_relobj_file<size, big_endian>* object,
7005 unsigned int r_type)
7006 {
7007 gold_error(_("%s: unsupported reloc %u against local symbol"),
7008 object->name().c_str(), r_type);
7009 }
7010
7011 // We are about to emit a dynamic relocation of type R_TYPE. If the
7012 // dynamic linker does not support it, issue an error.
7013
7014 template<int size, bool big_endian>
7015 void
7016 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7017 unsigned int r_type)
7018 {
7019 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7020
7021 // These are the relocation types supported by glibc for both 32-bit
7022 // and 64-bit powerpc.
7023 switch (r_type)
7024 {
7025 case elfcpp::R_POWERPC_NONE:
7026 case elfcpp::R_POWERPC_RELATIVE:
7027 case elfcpp::R_POWERPC_GLOB_DAT:
7028 case elfcpp::R_POWERPC_DTPMOD:
7029 case elfcpp::R_POWERPC_DTPREL:
7030 case elfcpp::R_POWERPC_TPREL:
7031 case elfcpp::R_POWERPC_JMP_SLOT:
7032 case elfcpp::R_POWERPC_COPY:
7033 case elfcpp::R_POWERPC_IRELATIVE:
7034 case elfcpp::R_POWERPC_ADDR32:
7035 case elfcpp::R_POWERPC_UADDR32:
7036 case elfcpp::R_POWERPC_ADDR24:
7037 case elfcpp::R_POWERPC_ADDR16:
7038 case elfcpp::R_POWERPC_UADDR16:
7039 case elfcpp::R_POWERPC_ADDR16_LO:
7040 case elfcpp::R_POWERPC_ADDR16_HI:
7041 case elfcpp::R_POWERPC_ADDR16_HA:
7042 case elfcpp::R_POWERPC_ADDR14:
7043 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7044 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7045 case elfcpp::R_POWERPC_REL32:
7046 case elfcpp::R_POWERPC_REL24:
7047 case elfcpp::R_POWERPC_TPREL16:
7048 case elfcpp::R_POWERPC_TPREL16_LO:
7049 case elfcpp::R_POWERPC_TPREL16_HI:
7050 case elfcpp::R_POWERPC_TPREL16_HA:
7051 return;
7052
7053 default:
7054 break;
7055 }
7056
7057 if (size == 64)
7058 {
7059 switch (r_type)
7060 {
7061 // These are the relocation types supported only on 64-bit.
7062 case elfcpp::R_PPC64_ADDR64:
7063 case elfcpp::R_PPC64_UADDR64:
7064 case elfcpp::R_PPC64_JMP_IREL:
7065 case elfcpp::R_PPC64_ADDR16_DS:
7066 case elfcpp::R_PPC64_ADDR16_LO_DS:
7067 case elfcpp::R_PPC64_ADDR16_HIGH:
7068 case elfcpp::R_PPC64_ADDR16_HIGHA:
7069 case elfcpp::R_PPC64_ADDR16_HIGHER:
7070 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7071 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7072 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7073 case elfcpp::R_PPC64_REL64:
7074 case elfcpp::R_POWERPC_ADDR30:
7075 case elfcpp::R_PPC64_TPREL16_DS:
7076 case elfcpp::R_PPC64_TPREL16_LO_DS:
7077 case elfcpp::R_PPC64_TPREL16_HIGH:
7078 case elfcpp::R_PPC64_TPREL16_HIGHA:
7079 case elfcpp::R_PPC64_TPREL16_HIGHER:
7080 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7081 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7082 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7083 return;
7084
7085 default:
7086 break;
7087 }
7088 }
7089 else
7090 {
7091 switch (r_type)
7092 {
7093 // These are the relocation types supported only on 32-bit.
7094 // ??? glibc ld.so doesn't need to support these.
7095 case elfcpp::R_POWERPC_DTPREL16:
7096 case elfcpp::R_POWERPC_DTPREL16_LO:
7097 case elfcpp::R_POWERPC_DTPREL16_HI:
7098 case elfcpp::R_POWERPC_DTPREL16_HA:
7099 return;
7100
7101 default:
7102 break;
7103 }
7104 }
7105
7106 // This prevents us from issuing more than one error per reloc
7107 // section. But we can still wind up issuing more than one
7108 // error per object file.
7109 if (this->issued_non_pic_error_)
7110 return;
7111 gold_assert(parameters->options().output_is_position_independent());
7112 object->error(_("requires unsupported dynamic reloc; "
7113 "recompile with -fPIC"));
7114 this->issued_non_pic_error_ = true;
7115 return;
7116 }
7117
7118 // Return whether we need to make a PLT entry for a relocation of the
7119 // given type against a STT_GNU_IFUNC symbol.
7120
7121 template<int size, bool big_endian>
7122 bool
7123 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7124 Target_powerpc<size, big_endian>* target,
7125 Sized_relobj_file<size, big_endian>* object,
7126 unsigned int r_type,
7127 bool report_err)
7128 {
7129 // In non-pic code any reference will resolve to the plt call stub
7130 // for the ifunc symbol.
7131 if ((size == 32 || target->abiversion() >= 2)
7132 && !parameters->options().output_is_position_independent())
7133 return true;
7134
7135 switch (r_type)
7136 {
7137 // Word size refs from data sections are OK, but don't need a PLT entry.
7138 case elfcpp::R_POWERPC_ADDR32:
7139 case elfcpp::R_POWERPC_UADDR32:
7140 if (size == 32)
7141 return false;
7142 break;
7143
7144 case elfcpp::R_PPC64_ADDR64:
7145 case elfcpp::R_PPC64_UADDR64:
7146 if (size == 64)
7147 return false;
7148 break;
7149
7150 // GOT refs are good, but also don't need a PLT entry.
7151 case elfcpp::R_POWERPC_GOT16:
7152 case elfcpp::R_POWERPC_GOT16_LO:
7153 case elfcpp::R_POWERPC_GOT16_HI:
7154 case elfcpp::R_POWERPC_GOT16_HA:
7155 case elfcpp::R_PPC64_GOT16_DS:
7156 case elfcpp::R_PPC64_GOT16_LO_DS:
7157 return false;
7158
7159 // PLT relocs are OK and need a PLT entry.
7160 case elfcpp::R_POWERPC_PLT16_LO:
7161 case elfcpp::R_POWERPC_PLT16_HI:
7162 case elfcpp::R_POWERPC_PLT16_HA:
7163 case elfcpp::R_PPC64_PLT16_LO_DS:
7164 case elfcpp::R_POWERPC_PLTSEQ:
7165 case elfcpp::R_POWERPC_PLTCALL:
7166 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7167 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7168 return true;
7169 break;
7170
7171 // Function calls are good, and these do need a PLT entry.
7172 case elfcpp::R_PPC64_REL24_NOTOC:
7173 if (size == 32)
7174 break;
7175 // Fall through.
7176 case elfcpp::R_POWERPC_ADDR24:
7177 case elfcpp::R_POWERPC_ADDR14:
7178 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7179 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7180 case elfcpp::R_POWERPC_REL24:
7181 case elfcpp::R_PPC_PLTREL24:
7182 case elfcpp::R_POWERPC_REL14:
7183 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7184 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7185 return true;
7186
7187 default:
7188 break;
7189 }
7190
7191 // Anything else is a problem.
7192 // If we are building a static executable, the libc startup function
7193 // responsible for applying indirect function relocations is going
7194 // to complain about the reloc type.
7195 // If we are building a dynamic executable, we will have a text
7196 // relocation. The dynamic loader will set the text segment
7197 // writable and non-executable to apply text relocations. So we'll
7198 // segfault when trying to run the indirection function to resolve
7199 // the reloc.
7200 if (report_err)
7201 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7202 object->name().c_str(), r_type);
7203 return false;
7204 }
7205
7206 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7207 // reloc.
7208
7209 static bool
7210 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7211 {
7212 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7213 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7214 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7215 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7216 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7217 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7218 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7219 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7220 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7221 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7222 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7223 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7224 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7225 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7226 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7227 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7228 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7229 /* Exclude lfqu by testing reloc. If relocs are ever
7230 defined for the reduced D field in psq_lu then those
7231 will need testing too. */
7232 && r_type != elfcpp::R_PPC64_TOC16_LO
7233 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7234 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7235 && (insn & 1) == 0)
7236 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7237 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7238 /* Exclude stfqu. psq_stu as above for psq_lu. */
7239 && r_type != elfcpp::R_PPC64_TOC16_LO
7240 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7241 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7242 && (insn & 1) == 0));
7243 }
7244
7245 // Scan a relocation for a local symbol.
7246
7247 template<int size, bool big_endian>
7248 inline void
7249 Target_powerpc<size, big_endian>::Scan::local(
7250 Symbol_table* symtab,
7251 Layout* layout,
7252 Target_powerpc<size, big_endian>* target,
7253 Sized_relobj_file<size, big_endian>* object,
7254 unsigned int data_shndx,
7255 Output_section* output_section,
7256 const elfcpp::Rela<size, big_endian>& reloc,
7257 unsigned int r_type,
7258 const elfcpp::Sym<size, big_endian>& lsym,
7259 bool is_discarded)
7260 {
7261 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
7262
7263 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7264 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7265 {
7266 this->expect_tls_get_addr_call();
7267 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7268 if (tls_type != tls::TLSOPT_NONE)
7269 this->skip_next_tls_get_addr_call();
7270 }
7271 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7272 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7273 {
7274 this->expect_tls_get_addr_call();
7275 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7276 if (tls_type != tls::TLSOPT_NONE)
7277 this->skip_next_tls_get_addr_call();
7278 }
7279
7280 Powerpc_relobj<size, big_endian>* ppc_object
7281 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7282
7283 if (is_discarded)
7284 {
7285 if (size == 64
7286 && data_shndx == ppc_object->opd_shndx()
7287 && r_type == elfcpp::R_PPC64_ADDR64)
7288 ppc_object->set_opd_discard(reloc.get_r_offset());
7289 return;
7290 }
7291
7292 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7293 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7294 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7295 {
7296 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7297 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7298 r_type, r_sym, reloc.get_r_addend());
7299 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7300 }
7301
7302 switch (r_type)
7303 {
7304 case elfcpp::R_POWERPC_NONE:
7305 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7306 case elfcpp::R_POWERPC_GNU_VTENTRY:
7307 case elfcpp::R_POWERPC_TLS:
7308 case elfcpp::R_PPC64_ENTRY:
7309 case elfcpp::R_POWERPC_PLTSEQ:
7310 case elfcpp::R_POWERPC_PLTCALL:
7311 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7312 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7313 break;
7314
7315 case elfcpp::R_PPC64_TOC:
7316 {
7317 Output_data_got_powerpc<size, big_endian>* got
7318 = target->got_section(symtab, layout);
7319 if (parameters->options().output_is_position_independent())
7320 {
7321 Address off = reloc.get_r_offset();
7322 if (size == 64
7323 && target->abiversion() < 2
7324 && data_shndx == ppc_object->opd_shndx()
7325 && ppc_object->get_opd_discard(off - 8))
7326 break;
7327
7328 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7329 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7330 rela_dyn->add_output_section_relative(got->output_section(),
7331 elfcpp::R_POWERPC_RELATIVE,
7332 output_section,
7333 object, data_shndx, off,
7334 symobj->toc_base_offset());
7335 }
7336 }
7337 break;
7338
7339 case elfcpp::R_PPC64_ADDR64:
7340 case elfcpp::R_PPC64_UADDR64:
7341 case elfcpp::R_POWERPC_ADDR32:
7342 case elfcpp::R_POWERPC_UADDR32:
7343 case elfcpp::R_POWERPC_ADDR24:
7344 case elfcpp::R_POWERPC_ADDR16:
7345 case elfcpp::R_POWERPC_ADDR16_LO:
7346 case elfcpp::R_POWERPC_ADDR16_HI:
7347 case elfcpp::R_POWERPC_ADDR16_HA:
7348 case elfcpp::R_POWERPC_UADDR16:
7349 case elfcpp::R_PPC64_ADDR16_HIGH:
7350 case elfcpp::R_PPC64_ADDR16_HIGHA:
7351 case elfcpp::R_PPC64_ADDR16_HIGHER:
7352 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7353 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7354 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7355 case elfcpp::R_PPC64_ADDR16_DS:
7356 case elfcpp::R_PPC64_ADDR16_LO_DS:
7357 case elfcpp::R_POWERPC_ADDR14:
7358 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7359 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7360 // If building a shared library (or a position-independent
7361 // executable), we need to create a dynamic relocation for
7362 // this location.
7363 if (parameters->options().output_is_position_independent()
7364 || (size == 64 && is_ifunc && target->abiversion() < 2))
7365 {
7366 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7367 is_ifunc);
7368 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7369 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7370 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
7371 {
7372 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7373 : elfcpp::R_POWERPC_RELATIVE);
7374 rela_dyn->add_local_relative(object, r_sym, dynrel,
7375 output_section, data_shndx,
7376 reloc.get_r_offset(),
7377 reloc.get_r_addend(), false);
7378 }
7379 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
7380 {
7381 check_non_pic(object, r_type);
7382 rela_dyn->add_local(object, r_sym, r_type, output_section,
7383 data_shndx, reloc.get_r_offset(),
7384 reloc.get_r_addend());
7385 }
7386 else
7387 {
7388 gold_assert(lsym.get_st_value() == 0);
7389 unsigned int shndx = lsym.get_st_shndx();
7390 bool is_ordinary;
7391 shndx = object->adjust_sym_shndx(r_sym, shndx,
7392 &is_ordinary);
7393 if (!is_ordinary)
7394 object->error(_("section symbol %u has bad shndx %u"),
7395 r_sym, shndx);
7396 else
7397 rela_dyn->add_local_section(object, shndx, r_type,
7398 output_section, data_shndx,
7399 reloc.get_r_offset());
7400 }
7401 }
7402 break;
7403
7404 case elfcpp::R_POWERPC_PLT16_LO:
7405 case elfcpp::R_POWERPC_PLT16_HI:
7406 case elfcpp::R_POWERPC_PLT16_HA:
7407 case elfcpp::R_PPC64_PLT16_LO_DS:
7408 if (!is_ifunc)
7409 {
7410 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7411 target->make_local_plt_entry(layout, object, r_sym);
7412 }
7413 break;
7414
7415 case elfcpp::R_PPC64_REL24_NOTOC:
7416 if (size == 32)
7417 break;
7418 // Fall through.
7419 case elfcpp::R_POWERPC_REL24:
7420 case elfcpp::R_PPC_PLTREL24:
7421 case elfcpp::R_PPC_LOCAL24PC:
7422 case elfcpp::R_POWERPC_REL14:
7423 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7424 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7425 if (!is_ifunc)
7426 {
7427 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7428 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7429 r_type, r_sym, reloc.get_r_addend());
7430 }
7431 break;
7432
7433 case elfcpp::R_PPC64_TOCSAVE:
7434 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7435 // caller has already saved r2 and thus a plt call stub need not
7436 // save r2.
7437 if (size == 64
7438 && target->mark_pltcall(ppc_object, data_shndx,
7439 reloc.get_r_offset() - 4, symtab))
7440 {
7441 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7442 unsigned int shndx = lsym.get_st_shndx();
7443 bool is_ordinary;
7444 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7445 if (!is_ordinary)
7446 object->error(_("tocsave symbol %u has bad shndx %u"),
7447 r_sym, shndx);
7448 else
7449 target->add_tocsave(ppc_object, shndx,
7450 lsym.get_st_value() + reloc.get_r_addend());
7451 }
7452 break;
7453
7454 case elfcpp::R_PPC64_REL64:
7455 case elfcpp::R_POWERPC_REL32:
7456 case elfcpp::R_POWERPC_REL16:
7457 case elfcpp::R_POWERPC_REL16_LO:
7458 case elfcpp::R_POWERPC_REL16_HI:
7459 case elfcpp::R_POWERPC_REL16_HA:
7460 case elfcpp::R_POWERPC_REL16DX_HA:
7461 case elfcpp::R_PPC64_REL16_HIGH:
7462 case elfcpp::R_PPC64_REL16_HIGHA:
7463 case elfcpp::R_PPC64_REL16_HIGHER:
7464 case elfcpp::R_PPC64_REL16_HIGHERA:
7465 case elfcpp::R_PPC64_REL16_HIGHEST:
7466 case elfcpp::R_PPC64_REL16_HIGHESTA:
7467 case elfcpp::R_POWERPC_SECTOFF:
7468 case elfcpp::R_POWERPC_SECTOFF_LO:
7469 case elfcpp::R_POWERPC_SECTOFF_HI:
7470 case elfcpp::R_POWERPC_SECTOFF_HA:
7471 case elfcpp::R_PPC64_SECTOFF_DS:
7472 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7473 case elfcpp::R_POWERPC_TPREL16:
7474 case elfcpp::R_POWERPC_TPREL16_LO:
7475 case elfcpp::R_POWERPC_TPREL16_HI:
7476 case elfcpp::R_POWERPC_TPREL16_HA:
7477 case elfcpp::R_PPC64_TPREL16_DS:
7478 case elfcpp::R_PPC64_TPREL16_LO_DS:
7479 case elfcpp::R_PPC64_TPREL16_HIGH:
7480 case elfcpp::R_PPC64_TPREL16_HIGHA:
7481 case elfcpp::R_PPC64_TPREL16_HIGHER:
7482 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7483 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7484 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7485 case elfcpp::R_POWERPC_DTPREL16:
7486 case elfcpp::R_POWERPC_DTPREL16_LO:
7487 case elfcpp::R_POWERPC_DTPREL16_HI:
7488 case elfcpp::R_POWERPC_DTPREL16_HA:
7489 case elfcpp::R_PPC64_DTPREL16_DS:
7490 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7491 case elfcpp::R_PPC64_DTPREL16_HIGH:
7492 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7493 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7494 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7495 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7496 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7497 case elfcpp::R_PPC64_TLSGD:
7498 case elfcpp::R_PPC64_TLSLD:
7499 case elfcpp::R_PPC64_ADDR64_LOCAL:
7500 break;
7501
7502 case elfcpp::R_POWERPC_GOT16:
7503 case elfcpp::R_POWERPC_GOT16_LO:
7504 case elfcpp::R_POWERPC_GOT16_HI:
7505 case elfcpp::R_POWERPC_GOT16_HA:
7506 case elfcpp::R_PPC64_GOT16_DS:
7507 case elfcpp::R_PPC64_GOT16_LO_DS:
7508 {
7509 // The symbol requires a GOT entry.
7510 Output_data_got_powerpc<size, big_endian>* got
7511 = target->got_section(symtab, layout);
7512 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7513
7514 if (!parameters->options().output_is_position_independent())
7515 {
7516 if (is_ifunc
7517 && (size == 32 || target->abiversion() >= 2))
7518 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
7519 else
7520 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
7521 }
7522 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
7523 {
7524 // If we are generating a shared object or a pie, this
7525 // symbol's GOT entry will be set by a dynamic relocation.
7526 unsigned int off;
7527 off = got->add_constant(0);
7528 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
7529
7530 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7531 is_ifunc);
7532 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7533 : elfcpp::R_POWERPC_RELATIVE);
7534 rela_dyn->add_local_relative(object, r_sym, dynrel,
7535 got, off, 0, false);
7536 }
7537 }
7538 break;
7539
7540 case elfcpp::R_PPC64_TOC16:
7541 case elfcpp::R_PPC64_TOC16_LO:
7542 case elfcpp::R_PPC64_TOC16_HI:
7543 case elfcpp::R_PPC64_TOC16_HA:
7544 case elfcpp::R_PPC64_TOC16_DS:
7545 case elfcpp::R_PPC64_TOC16_LO_DS:
7546 // We need a GOT section.
7547 target->got_section(symtab, layout);
7548 break;
7549
7550 case elfcpp::R_POWERPC_GOT_TLSGD16:
7551 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7552 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7553 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7554 {
7555 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7556 if (tls_type == tls::TLSOPT_NONE)
7557 {
7558 Output_data_got_powerpc<size, big_endian>* got
7559 = target->got_section(symtab, layout);
7560 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7561 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7562 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
7563 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
7564 }
7565 else if (tls_type == tls::TLSOPT_TO_LE)
7566 {
7567 // no GOT relocs needed for Local Exec.
7568 }
7569 else
7570 gold_unreachable();
7571 }
7572 break;
7573
7574 case elfcpp::R_POWERPC_GOT_TLSLD16:
7575 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7576 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7577 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7578 {
7579 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7580 if (tls_type == tls::TLSOPT_NONE)
7581 target->tlsld_got_offset(symtab, layout, object);
7582 else if (tls_type == tls::TLSOPT_TO_LE)
7583 {
7584 // no GOT relocs needed for Local Exec.
7585 if (parameters->options().emit_relocs())
7586 {
7587 Output_section* os = layout->tls_segment()->first_section();
7588 gold_assert(os != NULL);
7589 os->set_needs_symtab_index();
7590 }
7591 }
7592 else
7593 gold_unreachable();
7594 }
7595 break;
7596
7597 case elfcpp::R_POWERPC_GOT_DTPREL16:
7598 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7599 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7600 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7601 {
7602 Output_data_got_powerpc<size, big_endian>* got
7603 = target->got_section(symtab, layout);
7604 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7605 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
7606 }
7607 break;
7608
7609 case elfcpp::R_POWERPC_GOT_TPREL16:
7610 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7611 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7612 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7613 {
7614 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7615 if (tls_type == tls::TLSOPT_NONE)
7616 {
7617 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7618 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7619 {
7620 Output_data_got_powerpc<size, big_endian>* got
7621 = target->got_section(symtab, layout);
7622 unsigned int off = got->add_constant(0);
7623 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7624
7625 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7626 rela_dyn->add_symbolless_local_addend(object, r_sym,
7627 elfcpp::R_POWERPC_TPREL,
7628 got, off, 0);
7629 }
7630 }
7631 else if (tls_type == tls::TLSOPT_TO_LE)
7632 {
7633 // no GOT relocs needed for Local Exec.
7634 }
7635 else
7636 gold_unreachable();
7637 }
7638 break;
7639
7640 default:
7641 unsupported_reloc_local(object, r_type);
7642 break;
7643 }
7644
7645 if (size == 64
7646 && parameters->options().toc_optimize())
7647 {
7648 if (data_shndx == ppc_object->toc_shndx())
7649 {
7650 bool ok = true;
7651 if (r_type != elfcpp::R_PPC64_ADDR64
7652 || (is_ifunc && target->abiversion() < 2))
7653 ok = false;
7654 else if (parameters->options().output_is_position_independent())
7655 {
7656 if (is_ifunc)
7657 ok = false;
7658 else
7659 {
7660 unsigned int shndx = lsym.get_st_shndx();
7661 if (shndx >= elfcpp::SHN_LORESERVE
7662 && shndx != elfcpp::SHN_XINDEX)
7663 ok = false;
7664 }
7665 }
7666 if (!ok)
7667 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7668 }
7669
7670 enum {no_check, check_lo, check_ha} insn_check;
7671 switch (r_type)
7672 {
7673 default:
7674 insn_check = no_check;
7675 break;
7676
7677 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7678 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7679 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7680 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7681 case elfcpp::R_POWERPC_GOT16_HA:
7682 case elfcpp::R_PPC64_TOC16_HA:
7683 insn_check = check_ha;
7684 break;
7685
7686 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7687 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7688 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7689 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7690 case elfcpp::R_POWERPC_GOT16_LO:
7691 case elfcpp::R_PPC64_GOT16_LO_DS:
7692 case elfcpp::R_PPC64_TOC16_LO:
7693 case elfcpp::R_PPC64_TOC16_LO_DS:
7694 insn_check = check_lo;
7695 break;
7696 }
7697
7698 section_size_type slen;
7699 const unsigned char* view = NULL;
7700 if (insn_check != no_check)
7701 {
7702 view = ppc_object->section_contents(data_shndx, &slen, false);
7703 section_size_type off =
7704 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7705 if (off < slen)
7706 {
7707 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7708 if (insn_check == check_lo
7709 ? !ok_lo_toc_insn(insn, r_type)
7710 : ((insn & ((0x3f << 26) | 0x1f << 16))
7711 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7712 {
7713 ppc_object->set_no_toc_opt();
7714 gold_warning(_("%s: toc optimization is not supported "
7715 "for %#08x instruction"),
7716 ppc_object->name().c_str(), insn);
7717 }
7718 }
7719 }
7720
7721 switch (r_type)
7722 {
7723 default:
7724 break;
7725 case elfcpp::R_PPC64_TOC16:
7726 case elfcpp::R_PPC64_TOC16_LO:
7727 case elfcpp::R_PPC64_TOC16_HI:
7728 case elfcpp::R_PPC64_TOC16_HA:
7729 case elfcpp::R_PPC64_TOC16_DS:
7730 case elfcpp::R_PPC64_TOC16_LO_DS:
7731 unsigned int shndx = lsym.get_st_shndx();
7732 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7733 bool is_ordinary;
7734 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7735 if (is_ordinary && shndx == ppc_object->toc_shndx())
7736 {
7737 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
7738 if (dst_off < ppc_object->section_size(shndx))
7739 {
7740 bool ok = false;
7741 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7742 ok = true;
7743 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7744 {
7745 // Need to check that the insn is a ld
7746 if (!view)
7747 view = ppc_object->section_contents(data_shndx,
7748 &slen,
7749 false);
7750 section_size_type off =
7751 (convert_to_section_size_type(reloc.get_r_offset())
7752 + (big_endian ? -2 : 3));
7753 if (off < slen
7754 && (view[off] & (0x3f << 2)) == 58u << 2)
7755 ok = true;
7756 }
7757 if (!ok)
7758 ppc_object->set_no_toc_opt(dst_off);
7759 }
7760 }
7761 break;
7762 }
7763 }
7764
7765 if (size == 32)
7766 {
7767 switch (r_type)
7768 {
7769 case elfcpp::R_POWERPC_REL32:
7770 if (ppc_object->got2_shndx() != 0
7771 && parameters->options().output_is_position_independent())
7772 {
7773 unsigned int shndx = lsym.get_st_shndx();
7774 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7775 bool is_ordinary;
7776 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7777 if (is_ordinary && shndx == ppc_object->got2_shndx()
7778 && (ppc_object->section_flags(data_shndx)
7779 & elfcpp::SHF_EXECINSTR) != 0)
7780 gold_error(_("%s: unsupported -mbss-plt code"),
7781 ppc_object->name().c_str());
7782 }
7783 break;
7784 default:
7785 break;
7786 }
7787 }
7788
7789 switch (r_type)
7790 {
7791 case elfcpp::R_POWERPC_GOT_TLSLD16:
7792 case elfcpp::R_POWERPC_GOT_TLSGD16:
7793 case elfcpp::R_POWERPC_GOT_TPREL16:
7794 case elfcpp::R_POWERPC_GOT_DTPREL16:
7795 case elfcpp::R_POWERPC_GOT16:
7796 case elfcpp::R_PPC64_GOT16_DS:
7797 case elfcpp::R_PPC64_TOC16:
7798 case elfcpp::R_PPC64_TOC16_DS:
7799 ppc_object->set_has_small_toc_reloc();
7800 default:
7801 break;
7802 }
7803 }
7804
7805 // Report an unsupported relocation against a global symbol.
7806
7807 template<int size, bool big_endian>
7808 void
7809 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
7810 Sized_relobj_file<size, big_endian>* object,
7811 unsigned int r_type,
7812 Symbol* gsym)
7813 {
7814 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7815 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7816 }
7817
7818 // Scan a relocation for a global symbol.
7819
7820 template<int size, bool big_endian>
7821 inline void
7822 Target_powerpc<size, big_endian>::Scan::global(
7823 Symbol_table* symtab,
7824 Layout* layout,
7825 Target_powerpc<size, big_endian>* target,
7826 Sized_relobj_file<size, big_endian>* object,
7827 unsigned int data_shndx,
7828 Output_section* output_section,
7829 const elfcpp::Rela<size, big_endian>& reloc,
7830 unsigned int r_type,
7831 Symbol* gsym)
7832 {
7833 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
7834 == Track_tls::SKIP)
7835 return;
7836
7837 if (target->replace_tls_get_addr(gsym))
7838 // Change a __tls_get_addr reference to __tls_get_addr_opt
7839 // so dynamic relocs are emitted against the latter symbol.
7840 gsym = target->tls_get_addr_opt();
7841
7842 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7843 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7844 {
7845 this->expect_tls_get_addr_call();
7846 const bool final = gsym->final_value_is_known();
7847 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7848 if (tls_type != tls::TLSOPT_NONE)
7849 this->skip_next_tls_get_addr_call();
7850 }
7851 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7852 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7853 {
7854 this->expect_tls_get_addr_call();
7855 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7856 if (tls_type != tls::TLSOPT_NONE)
7857 this->skip_next_tls_get_addr_call();
7858 }
7859
7860 Powerpc_relobj<size, big_endian>* ppc_object
7861 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7862
7863 // A STT_GNU_IFUNC symbol may require a PLT entry.
7864 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
7865 bool pushed_ifunc = false;
7866 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7867 {
7868 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7869 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7870 r_type, r_sym, reloc.get_r_addend());
7871 target->make_plt_entry(symtab, layout, gsym);
7872 pushed_ifunc = true;
7873 }
7874
7875 switch (r_type)
7876 {
7877 case elfcpp::R_POWERPC_NONE:
7878 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7879 case elfcpp::R_POWERPC_GNU_VTENTRY:
7880 case elfcpp::R_PPC_LOCAL24PC:
7881 case elfcpp::R_POWERPC_TLS:
7882 case elfcpp::R_PPC64_ENTRY:
7883 case elfcpp::R_POWERPC_PLTSEQ:
7884 case elfcpp::R_POWERPC_PLTCALL:
7885 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7886 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7887 break;
7888
7889 case elfcpp::R_PPC64_TOC:
7890 {
7891 Output_data_got_powerpc<size, big_endian>* got
7892 = target->got_section(symtab, layout);
7893 if (parameters->options().output_is_position_independent())
7894 {
7895 Address off = reloc.get_r_offset();
7896 if (size == 64
7897 && data_shndx == ppc_object->opd_shndx()
7898 && ppc_object->get_opd_discard(off - 8))
7899 break;
7900
7901 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7902 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7903 if (data_shndx != ppc_object->opd_shndx())
7904 symobj = static_cast
7905 <Powerpc_relobj<size, big_endian>*>(gsym->object());
7906 rela_dyn->add_output_section_relative(got->output_section(),
7907 elfcpp::R_POWERPC_RELATIVE,
7908 output_section,
7909 object, data_shndx, off,
7910 symobj->toc_base_offset());
7911 }
7912 }
7913 break;
7914
7915 case elfcpp::R_PPC64_ADDR64:
7916 if (size == 64
7917 && target->abiversion() < 2
7918 && data_shndx == ppc_object->opd_shndx()
7919 && (gsym->is_defined_in_discarded_section()
7920 || gsym->object() != object))
7921 {
7922 ppc_object->set_opd_discard(reloc.get_r_offset());
7923 break;
7924 }
7925 // Fall through.
7926 case elfcpp::R_PPC64_UADDR64:
7927 case elfcpp::R_POWERPC_ADDR32:
7928 case elfcpp::R_POWERPC_UADDR32:
7929 case elfcpp::R_POWERPC_ADDR24:
7930 case elfcpp::R_POWERPC_ADDR16:
7931 case elfcpp::R_POWERPC_ADDR16_LO:
7932 case elfcpp::R_POWERPC_ADDR16_HI:
7933 case elfcpp::R_POWERPC_ADDR16_HA:
7934 case elfcpp::R_POWERPC_UADDR16:
7935 case elfcpp::R_PPC64_ADDR16_HIGH:
7936 case elfcpp::R_PPC64_ADDR16_HIGHA:
7937 case elfcpp::R_PPC64_ADDR16_HIGHER:
7938 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7939 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7940 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7941 case elfcpp::R_PPC64_ADDR16_DS:
7942 case elfcpp::R_PPC64_ADDR16_LO_DS:
7943 case elfcpp::R_POWERPC_ADDR14:
7944 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7945 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7946 {
7947 // Make a PLT entry if necessary.
7948 if (gsym->needs_plt_entry())
7949 {
7950 // Since this is not a PC-relative relocation, we may be
7951 // taking the address of a function. In that case we need to
7952 // set the entry in the dynamic symbol table to the address of
7953 // the PLT call stub.
7954 bool need_ifunc_plt = false;
7955 if ((size == 32 || target->abiversion() >= 2)
7956 && gsym->is_from_dynobj()
7957 && !parameters->options().output_is_position_independent())
7958 {
7959 gsym->set_needs_dynsym_value();
7960 need_ifunc_plt = true;
7961 }
7962 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
7963 {
7964 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7965 target->push_branch(ppc_object, data_shndx,
7966 reloc.get_r_offset(), r_type, r_sym,
7967 reloc.get_r_addend());
7968 target->make_plt_entry(symtab, layout, gsym);
7969 }
7970 }
7971 // Make a dynamic relocation if necessary.
7972 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
7973 || (size == 64 && is_ifunc && target->abiversion() < 2))
7974 {
7975 if (!parameters->options().output_is_position_independent()
7976 && gsym->may_need_copy_reloc())
7977 {
7978 target->copy_reloc(symtab, layout, object,
7979 data_shndx, output_section, gsym, reloc);
7980 }
7981 else if ((((size == 32
7982 && r_type == elfcpp::R_POWERPC_ADDR32)
7983 || (size == 64
7984 && r_type == elfcpp::R_PPC64_ADDR64
7985 && target->abiversion() >= 2))
7986 && gsym->can_use_relative_reloc(false)
7987 && !(gsym->visibility() == elfcpp::STV_PROTECTED
7988 && parameters->options().shared()))
7989 || (size == 64
7990 && r_type == elfcpp::R_PPC64_ADDR64
7991 && target->abiversion() < 2
7992 && (gsym->can_use_relative_reloc(false)
7993 || data_shndx == ppc_object->opd_shndx())))
7994 {
7995 Reloc_section* rela_dyn
7996 = target->rela_dyn_section(symtab, layout, is_ifunc);
7997 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7998 : elfcpp::R_POWERPC_RELATIVE);
7999 rela_dyn->add_symbolless_global_addend(
8000 gsym, dynrel, output_section, object, data_shndx,
8001 reloc.get_r_offset(), reloc.get_r_addend());
8002 }
8003 else
8004 {
8005 Reloc_section* rela_dyn
8006 = target->rela_dyn_section(symtab, layout, is_ifunc);
8007 check_non_pic(object, r_type);
8008 rela_dyn->add_global(gsym, r_type, output_section,
8009 object, data_shndx,
8010 reloc.get_r_offset(),
8011 reloc.get_r_addend());
8012
8013 if (size == 64
8014 && parameters->options().toc_optimize()
8015 && data_shndx == ppc_object->toc_shndx())
8016 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8017 }
8018 }
8019 }
8020 break;
8021
8022 case elfcpp::R_POWERPC_PLT16_LO:
8023 case elfcpp::R_POWERPC_PLT16_HI:
8024 case elfcpp::R_POWERPC_PLT16_HA:
8025 case elfcpp::R_PPC64_PLT16_LO_DS:
8026 if (!pushed_ifunc)
8027 target->make_plt_entry(symtab, layout, gsym);
8028 break;
8029
8030 case elfcpp::R_PPC64_REL24_NOTOC:
8031 if (size == 32)
8032 break;
8033 // Fall through.
8034 case elfcpp::R_PPC_PLTREL24:
8035 case elfcpp::R_POWERPC_REL24:
8036 if (!is_ifunc)
8037 {
8038 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8039 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8040 r_type, r_sym, reloc.get_r_addend());
8041 if (gsym->needs_plt_entry()
8042 || (!gsym->final_value_is_known()
8043 && (gsym->is_undefined()
8044 || gsym->is_from_dynobj()
8045 || gsym->is_preemptible())))
8046 target->make_plt_entry(symtab, layout, gsym);
8047 }
8048 // Fall through.
8049
8050 case elfcpp::R_PPC64_REL64:
8051 case elfcpp::R_POWERPC_REL32:
8052 // Make a dynamic relocation if necessary.
8053 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8054 {
8055 if (!parameters->options().output_is_position_independent()
8056 && gsym->may_need_copy_reloc())
8057 {
8058 target->copy_reloc(symtab, layout, object,
8059 data_shndx, output_section, gsym,
8060 reloc);
8061 }
8062 else
8063 {
8064 Reloc_section* rela_dyn
8065 = target->rela_dyn_section(symtab, layout, is_ifunc);
8066 check_non_pic(object, r_type);
8067 rela_dyn->add_global(gsym, r_type, output_section, object,
8068 data_shndx, reloc.get_r_offset(),
8069 reloc.get_r_addend());
8070 }
8071 }
8072 break;
8073
8074 case elfcpp::R_POWERPC_REL14:
8075 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8076 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8077 if (!is_ifunc)
8078 {
8079 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8080 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8081 r_type, r_sym, reloc.get_r_addend());
8082 }
8083 break;
8084
8085 case elfcpp::R_PPC64_TOCSAVE:
8086 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8087 // caller has already saved r2 and thus a plt call stub need not
8088 // save r2.
8089 if (size == 64
8090 && target->mark_pltcall(ppc_object, data_shndx,
8091 reloc.get_r_offset() - 4, symtab))
8092 {
8093 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8094 bool is_ordinary;
8095 unsigned int shndx = gsym->shndx(&is_ordinary);
8096 if (!is_ordinary)
8097 object->error(_("tocsave symbol %u has bad shndx %u"),
8098 r_sym, shndx);
8099 else
8100 {
8101 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8102 target->add_tocsave(ppc_object, shndx,
8103 sym->value() + reloc.get_r_addend());
8104 }
8105 }
8106 break;
8107
8108 case elfcpp::R_POWERPC_REL16:
8109 case elfcpp::R_POWERPC_REL16_LO:
8110 case elfcpp::R_POWERPC_REL16_HI:
8111 case elfcpp::R_POWERPC_REL16_HA:
8112 case elfcpp::R_POWERPC_REL16DX_HA:
8113 case elfcpp::R_PPC64_REL16_HIGH:
8114 case elfcpp::R_PPC64_REL16_HIGHA:
8115 case elfcpp::R_PPC64_REL16_HIGHER:
8116 case elfcpp::R_PPC64_REL16_HIGHERA:
8117 case elfcpp::R_PPC64_REL16_HIGHEST:
8118 case elfcpp::R_PPC64_REL16_HIGHESTA:
8119 case elfcpp::R_POWERPC_SECTOFF:
8120 case elfcpp::R_POWERPC_SECTOFF_LO:
8121 case elfcpp::R_POWERPC_SECTOFF_HI:
8122 case elfcpp::R_POWERPC_SECTOFF_HA:
8123 case elfcpp::R_PPC64_SECTOFF_DS:
8124 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8125 case elfcpp::R_POWERPC_TPREL16:
8126 case elfcpp::R_POWERPC_TPREL16_LO:
8127 case elfcpp::R_POWERPC_TPREL16_HI:
8128 case elfcpp::R_POWERPC_TPREL16_HA:
8129 case elfcpp::R_PPC64_TPREL16_DS:
8130 case elfcpp::R_PPC64_TPREL16_LO_DS:
8131 case elfcpp::R_PPC64_TPREL16_HIGH:
8132 case elfcpp::R_PPC64_TPREL16_HIGHA:
8133 case elfcpp::R_PPC64_TPREL16_HIGHER:
8134 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8135 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8136 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8137 case elfcpp::R_POWERPC_DTPREL16:
8138 case elfcpp::R_POWERPC_DTPREL16_LO:
8139 case elfcpp::R_POWERPC_DTPREL16_HI:
8140 case elfcpp::R_POWERPC_DTPREL16_HA:
8141 case elfcpp::R_PPC64_DTPREL16_DS:
8142 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8143 case elfcpp::R_PPC64_DTPREL16_HIGH:
8144 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8145 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8146 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8147 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8148 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8149 case elfcpp::R_PPC64_TLSGD:
8150 case elfcpp::R_PPC64_TLSLD:
8151 case elfcpp::R_PPC64_ADDR64_LOCAL:
8152 break;
8153
8154 case elfcpp::R_POWERPC_GOT16:
8155 case elfcpp::R_POWERPC_GOT16_LO:
8156 case elfcpp::R_POWERPC_GOT16_HI:
8157 case elfcpp::R_POWERPC_GOT16_HA:
8158 case elfcpp::R_PPC64_GOT16_DS:
8159 case elfcpp::R_PPC64_GOT16_LO_DS:
8160 {
8161 // The symbol requires a GOT entry.
8162 Output_data_got_powerpc<size, big_endian>* got;
8163
8164 got = target->got_section(symtab, layout);
8165 if (gsym->final_value_is_known())
8166 {
8167 if (is_ifunc
8168 && (size == 32 || target->abiversion() >= 2))
8169 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8170 else
8171 got->add_global(gsym, GOT_TYPE_STANDARD);
8172 }
8173 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8174 {
8175 // If we are generating a shared object or a pie, this
8176 // symbol's GOT entry will be set by a dynamic relocation.
8177 unsigned int off = got->add_constant(0);
8178 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8179
8180 Reloc_section* rela_dyn
8181 = target->rela_dyn_section(symtab, layout, is_ifunc);
8182
8183 if (gsym->can_use_relative_reloc(false)
8184 && !((size == 32
8185 || target->abiversion() >= 2)
8186 && gsym->visibility() == elfcpp::STV_PROTECTED
8187 && parameters->options().shared()))
8188 {
8189 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8190 : elfcpp::R_POWERPC_RELATIVE);
8191 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8192 }
8193 else
8194 {
8195 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8196 rela_dyn->add_global(gsym, dynrel, got, off, 0);
8197 }
8198 }
8199 }
8200 break;
8201
8202 case elfcpp::R_PPC64_TOC16:
8203 case elfcpp::R_PPC64_TOC16_LO:
8204 case elfcpp::R_PPC64_TOC16_HI:
8205 case elfcpp::R_PPC64_TOC16_HA:
8206 case elfcpp::R_PPC64_TOC16_DS:
8207 case elfcpp::R_PPC64_TOC16_LO_DS:
8208 // We need a GOT section.
8209 target->got_section(symtab, layout);
8210 break;
8211
8212 case elfcpp::R_POWERPC_GOT_TLSGD16:
8213 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8214 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8215 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8216 {
8217 const bool final = gsym->final_value_is_known();
8218 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8219 if (tls_type == tls::TLSOPT_NONE)
8220 {
8221 Output_data_got_powerpc<size, big_endian>* got
8222 = target->got_section(symtab, layout);
8223 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8224 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
8225 elfcpp::R_POWERPC_DTPMOD,
8226 elfcpp::R_POWERPC_DTPREL);
8227 }
8228 else if (tls_type == tls::TLSOPT_TO_IE)
8229 {
8230 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8231 {
8232 Output_data_got_powerpc<size, big_endian>* got
8233 = target->got_section(symtab, layout);
8234 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8235 if (gsym->is_undefined()
8236 || gsym->is_from_dynobj())
8237 {
8238 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8239 elfcpp::R_POWERPC_TPREL);
8240 }
8241 else
8242 {
8243 unsigned int off = got->add_constant(0);
8244 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8245 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8246 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8247 got, off, 0);
8248 }
8249 }
8250 }
8251 else if (tls_type == tls::TLSOPT_TO_LE)
8252 {
8253 // no GOT relocs needed for Local Exec.
8254 }
8255 else
8256 gold_unreachable();
8257 }
8258 break;
8259
8260 case elfcpp::R_POWERPC_GOT_TLSLD16:
8261 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8262 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8263 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8264 {
8265 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8266 if (tls_type == tls::TLSOPT_NONE)
8267 target->tlsld_got_offset(symtab, layout, object);
8268 else if (tls_type == tls::TLSOPT_TO_LE)
8269 {
8270 // no GOT relocs needed for Local Exec.
8271 if (parameters->options().emit_relocs())
8272 {
8273 Output_section* os = layout->tls_segment()->first_section();
8274 gold_assert(os != NULL);
8275 os->set_needs_symtab_index();
8276 }
8277 }
8278 else
8279 gold_unreachable();
8280 }
8281 break;
8282
8283 case elfcpp::R_POWERPC_GOT_DTPREL16:
8284 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8285 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8286 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8287 {
8288 Output_data_got_powerpc<size, big_endian>* got
8289 = target->got_section(symtab, layout);
8290 if (!gsym->final_value_is_known()
8291 && (gsym->is_from_dynobj()
8292 || gsym->is_undefined()
8293 || gsym->is_preemptible()))
8294 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
8295 target->rela_dyn_section(layout),
8296 elfcpp::R_POWERPC_DTPREL);
8297 else
8298 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
8299 }
8300 break;
8301
8302 case elfcpp::R_POWERPC_GOT_TPREL16:
8303 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8304 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8305 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8306 {
8307 const bool final = gsym->final_value_is_known();
8308 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8309 if (tls_type == tls::TLSOPT_NONE)
8310 {
8311 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8312 {
8313 Output_data_got_powerpc<size, big_endian>* got
8314 = target->got_section(symtab, layout);
8315 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8316 if (gsym->is_undefined()
8317 || gsym->is_from_dynobj())
8318 {
8319 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8320 elfcpp::R_POWERPC_TPREL);
8321 }
8322 else
8323 {
8324 unsigned int off = got->add_constant(0);
8325 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8326 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8327 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8328 got, off, 0);
8329 }
8330 }
8331 }
8332 else if (tls_type == tls::TLSOPT_TO_LE)
8333 {
8334 // no GOT relocs needed for Local Exec.
8335 }
8336 else
8337 gold_unreachable();
8338 }
8339 break;
8340
8341 default:
8342 unsupported_reloc_global(object, r_type, gsym);
8343 break;
8344 }
8345
8346 if (size == 64
8347 && parameters->options().toc_optimize())
8348 {
8349 if (data_shndx == ppc_object->toc_shndx())
8350 {
8351 bool ok = true;
8352 if (r_type != elfcpp::R_PPC64_ADDR64
8353 || (is_ifunc && target->abiversion() < 2))
8354 ok = false;
8355 else if (parameters->options().output_is_position_independent()
8356 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
8357 ok = false;
8358 if (!ok)
8359 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8360 }
8361
8362 enum {no_check, check_lo, check_ha} insn_check;
8363 switch (r_type)
8364 {
8365 default:
8366 insn_check = no_check;
8367 break;
8368
8369 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8370 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8371 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8372 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8373 case elfcpp::R_POWERPC_GOT16_HA:
8374 case elfcpp::R_PPC64_TOC16_HA:
8375 insn_check = check_ha;
8376 break;
8377
8378 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8379 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8380 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8381 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8382 case elfcpp::R_POWERPC_GOT16_LO:
8383 case elfcpp::R_PPC64_GOT16_LO_DS:
8384 case elfcpp::R_PPC64_TOC16_LO:
8385 case elfcpp::R_PPC64_TOC16_LO_DS:
8386 insn_check = check_lo;
8387 break;
8388 }
8389
8390 section_size_type slen;
8391 const unsigned char* view = NULL;
8392 if (insn_check != no_check)
8393 {
8394 view = ppc_object->section_contents(data_shndx, &slen, false);
8395 section_size_type off =
8396 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8397 if (off < slen)
8398 {
8399 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8400 if (insn_check == check_lo
8401 ? !ok_lo_toc_insn(insn, r_type)
8402 : ((insn & ((0x3f << 26) | 0x1f << 16))
8403 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8404 {
8405 ppc_object->set_no_toc_opt();
8406 gold_warning(_("%s: toc optimization is not supported "
8407 "for %#08x instruction"),
8408 ppc_object->name().c_str(), insn);
8409 }
8410 }
8411 }
8412
8413 switch (r_type)
8414 {
8415 default:
8416 break;
8417 case elfcpp::R_PPC64_TOC16:
8418 case elfcpp::R_PPC64_TOC16_LO:
8419 case elfcpp::R_PPC64_TOC16_HI:
8420 case elfcpp::R_PPC64_TOC16_HA:
8421 case elfcpp::R_PPC64_TOC16_DS:
8422 case elfcpp::R_PPC64_TOC16_LO_DS:
8423 if (gsym->source() == Symbol::FROM_OBJECT
8424 && !gsym->object()->is_dynamic())
8425 {
8426 Powerpc_relobj<size, big_endian>* sym_object
8427 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8428 bool is_ordinary;
8429 unsigned int shndx = gsym->shndx(&is_ordinary);
8430 if (shndx == sym_object->toc_shndx())
8431 {
8432 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8433 Address dst_off = sym->value() + reloc.get_r_addend();
8434 if (dst_off < sym_object->section_size(shndx))
8435 {
8436 bool ok = false;
8437 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8438 ok = true;
8439 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8440 {
8441 // Need to check that the insn is a ld
8442 if (!view)
8443 view = ppc_object->section_contents(data_shndx,
8444 &slen,
8445 false);
8446 section_size_type off =
8447 (convert_to_section_size_type(reloc.get_r_offset())
8448 + (big_endian ? -2 : 3));
8449 if (off < slen
8450 && (view[off] & (0x3f << 2)) == (58u << 2))
8451 ok = true;
8452 }
8453 if (!ok)
8454 sym_object->set_no_toc_opt(dst_off);
8455 }
8456 }
8457 }
8458 break;
8459 }
8460 }
8461
8462 if (size == 32)
8463 {
8464 switch (r_type)
8465 {
8466 case elfcpp::R_PPC_LOCAL24PC:
8467 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8468 gold_error(_("%s: unsupported -mbss-plt code"),
8469 ppc_object->name().c_str());
8470 break;
8471 default:
8472 break;
8473 }
8474 }
8475
8476 switch (r_type)
8477 {
8478 case elfcpp::R_POWERPC_GOT_TLSLD16:
8479 case elfcpp::R_POWERPC_GOT_TLSGD16:
8480 case elfcpp::R_POWERPC_GOT_TPREL16:
8481 case elfcpp::R_POWERPC_GOT_DTPREL16:
8482 case elfcpp::R_POWERPC_GOT16:
8483 case elfcpp::R_PPC64_GOT16_DS:
8484 case elfcpp::R_PPC64_TOC16:
8485 case elfcpp::R_PPC64_TOC16_DS:
8486 ppc_object->set_has_small_toc_reloc();
8487 default:
8488 break;
8489 }
8490 }
8491
8492 // Process relocations for gc.
8493
8494 template<int size, bool big_endian>
8495 void
8496 Target_powerpc<size, big_endian>::gc_process_relocs(
8497 Symbol_table* symtab,
8498 Layout* layout,
8499 Sized_relobj_file<size, big_endian>* object,
8500 unsigned int data_shndx,
8501 unsigned int,
8502 const unsigned char* prelocs,
8503 size_t reloc_count,
8504 Output_section* output_section,
8505 bool needs_special_offset_handling,
8506 size_t local_symbol_count,
8507 const unsigned char* plocal_symbols)
8508 {
8509 typedef Target_powerpc<size, big_endian> Powerpc;
8510 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8511 Classify_reloc;
8512
8513 Powerpc_relobj<size, big_endian>* ppc_object
8514 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8515 if (size == 64)
8516 ppc_object->set_opd_valid();
8517 if (size == 64 && data_shndx == ppc_object->opd_shndx())
8518 {
8519 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
8520 for (p = ppc_object->access_from_map()->begin();
8521 p != ppc_object->access_from_map()->end();
8522 ++p)
8523 {
8524 Address dst_off = p->first;
8525 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8526 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
8527 for (s = p->second.begin(); s != p->second.end(); ++s)
8528 {
8529 Relobj* src_obj = s->first;
8530 unsigned int src_indx = s->second;
8531 symtab->gc()->add_reference(src_obj, src_indx,
8532 ppc_object, dst_indx);
8533 }
8534 p->second.clear();
8535 }
8536 ppc_object->access_from_map()->clear();
8537 ppc_object->process_gc_mark(symtab);
8538 // Don't look at .opd relocs as .opd will reference everything.
8539 return;
8540 }
8541
8542 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
8543 symtab,
8544 layout,
8545 this,
8546 object,
8547 data_shndx,
8548 prelocs,
8549 reloc_count,
8550 output_section,
8551 needs_special_offset_handling,
8552 local_symbol_count,
8553 plocal_symbols);
8554 }
8555
8556 // Handle target specific gc actions when adding a gc reference from
8557 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
8558 // and DST_OFF. For powerpc64, this adds a referenc to the code
8559 // section of a function descriptor.
8560
8561 template<int size, bool big_endian>
8562 void
8563 Target_powerpc<size, big_endian>::do_gc_add_reference(
8564 Symbol_table* symtab,
8565 Relobj* src_obj,
8566 unsigned int src_shndx,
8567 Relobj* dst_obj,
8568 unsigned int dst_shndx,
8569 Address dst_off) const
8570 {
8571 if (size != 64 || dst_obj->is_dynamic())
8572 return;
8573
8574 Powerpc_relobj<size, big_endian>* ppc_object
8575 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
8576 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
8577 {
8578 if (ppc_object->opd_valid())
8579 {
8580 dst_shndx = ppc_object->get_opd_ent(dst_off);
8581 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
8582 }
8583 else
8584 {
8585 // If we haven't run scan_opd_relocs, we must delay
8586 // processing this function descriptor reference.
8587 ppc_object->add_reference(src_obj, src_shndx, dst_off);
8588 }
8589 }
8590 }
8591
8592 // Add any special sections for this symbol to the gc work list.
8593 // For powerpc64, this adds the code section of a function
8594 // descriptor.
8595
8596 template<int size, bool big_endian>
8597 void
8598 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
8599 Symbol_table* symtab,
8600 Symbol* sym) const
8601 {
8602 if (size == 64)
8603 {
8604 Powerpc_relobj<size, big_endian>* ppc_object
8605 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
8606 bool is_ordinary;
8607 unsigned int shndx = sym->shndx(&is_ordinary);
8608 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
8609 {
8610 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
8611 Address dst_off = gsym->value();
8612 if (ppc_object->opd_valid())
8613 {
8614 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8615 symtab->gc()->worklist().push_back(Section_id(ppc_object,
8616 dst_indx));
8617 }
8618 else
8619 ppc_object->add_gc_mark(dst_off);
8620 }
8621 }
8622 }
8623
8624 // For a symbol location in .opd, set LOC to the location of the
8625 // function entry.
8626
8627 template<int size, bool big_endian>
8628 void
8629 Target_powerpc<size, big_endian>::do_function_location(
8630 Symbol_location* loc) const
8631 {
8632 if (size == 64 && loc->shndx != 0)
8633 {
8634 if (loc->object->is_dynamic())
8635 {
8636 Powerpc_dynobj<size, big_endian>* ppc_object
8637 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
8638 if (loc->shndx == ppc_object->opd_shndx())
8639 {
8640 Address dest_off;
8641 Address off = loc->offset - ppc_object->opd_address();
8642 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
8643 loc->offset = dest_off;
8644 }
8645 }
8646 else
8647 {
8648 const Powerpc_relobj<size, big_endian>* ppc_object
8649 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
8650 if (loc->shndx == ppc_object->opd_shndx())
8651 {
8652 Address dest_off;
8653 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
8654 loc->offset = dest_off;
8655 }
8656 }
8657 }
8658 }
8659
8660 // FNOFFSET in section SHNDX in OBJECT is the start of a function
8661 // compiled with -fsplit-stack. The function calls non-split-stack
8662 // code. Change the function to ensure it has enough stack space to
8663 // call some random function.
8664
8665 template<int size, bool big_endian>
8666 void
8667 Target_powerpc<size, big_endian>::do_calls_non_split(
8668 Relobj* object,
8669 unsigned int shndx,
8670 section_offset_type fnoffset,
8671 section_size_type fnsize,
8672 const unsigned char* prelocs,
8673 size_t reloc_count,
8674 unsigned char* view,
8675 section_size_type view_size,
8676 std::string* from,
8677 std::string* to) const
8678 {
8679 // 32-bit not supported.
8680 if (size == 32)
8681 {
8682 // warn
8683 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
8684 prelocs, reloc_count, view, view_size,
8685 from, to);
8686 return;
8687 }
8688
8689 // The function always starts with
8690 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
8691 // addis %r12,%r1,-allocate@ha
8692 // addi %r12,%r12,-allocate@l
8693 // cmpld %r12,%r0
8694 // but note that the addis or addi may be replaced with a nop
8695
8696 unsigned char *entry = view + fnoffset;
8697 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
8698
8699 if ((insn & 0xffff0000) == addis_2_12)
8700 {
8701 /* Skip ELFv2 global entry code. */
8702 entry += 8;
8703 insn = elfcpp::Swap<32, big_endian>::readval(entry);
8704 }
8705
8706 unsigned char *pinsn = entry;
8707 bool ok = false;
8708 const uint32_t ld_private_ss = 0xe80d8fc0;
8709 if (insn == ld_private_ss)
8710 {
8711 int32_t allocate = 0;
8712 while (1)
8713 {
8714 pinsn += 4;
8715 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
8716 if ((insn & 0xffff0000) == addis_12_1)
8717 allocate += (insn & 0xffff) << 16;
8718 else if ((insn & 0xffff0000) == addi_12_1
8719 || (insn & 0xffff0000) == addi_12_12)
8720 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
8721 else if (insn != nop)
8722 break;
8723 }
8724 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
8725 {
8726 int extra = parameters->options().split_stack_adjust_size();
8727 allocate -= extra;
8728 if (allocate >= 0 || extra < 0)
8729 {
8730 object->error(_("split-stack stack size overflow at "
8731 "section %u offset %0zx"),
8732 shndx, static_cast<size_t>(fnoffset));
8733 return;
8734 }
8735 pinsn = entry + 4;
8736 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
8737 if (insn != addis_12_1)
8738 {
8739 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8740 pinsn += 4;
8741 insn = addi_12_12 | (allocate & 0xffff);
8742 if (insn != addi_12_12)
8743 {
8744 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8745 pinsn += 4;
8746 }
8747 }
8748 else
8749 {
8750 insn = addi_12_1 | (allocate & 0xffff);
8751 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8752 pinsn += 4;
8753 }
8754 if (pinsn != entry + 12)
8755 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
8756
8757 ok = true;
8758 }
8759 }
8760
8761 if (!ok)
8762 {
8763 if (!object->has_no_split_stack())
8764 object->error(_("failed to match split-stack sequence at "
8765 "section %u offset %0zx"),
8766 shndx, static_cast<size_t>(fnoffset));
8767 }
8768 }
8769
8770 // Scan relocations for a section.
8771
8772 template<int size, bool big_endian>
8773 void
8774 Target_powerpc<size, big_endian>::scan_relocs(
8775 Symbol_table* symtab,
8776 Layout* layout,
8777 Sized_relobj_file<size, big_endian>* object,
8778 unsigned int data_shndx,
8779 unsigned int sh_type,
8780 const unsigned char* prelocs,
8781 size_t reloc_count,
8782 Output_section* output_section,
8783 bool needs_special_offset_handling,
8784 size_t local_symbol_count,
8785 const unsigned char* plocal_symbols)
8786 {
8787 typedef Target_powerpc<size, big_endian> Powerpc;
8788 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8789 Classify_reloc;
8790
8791 if (!this->plt_localentry0_init_)
8792 {
8793 bool plt_localentry0 = false;
8794 if (size == 64
8795 && this->abiversion() >= 2)
8796 {
8797 if (parameters->options().user_set_plt_localentry())
8798 plt_localentry0 = parameters->options().plt_localentry();
8799 if (plt_localentry0
8800 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
8801 gold_warning(_("--plt-localentry is especially dangerous without "
8802 "ld.so support to detect ABI violations"));
8803 }
8804 this->plt_localentry0_ = plt_localentry0;
8805 this->plt_localentry0_init_ = true;
8806 }
8807
8808 if (sh_type == elfcpp::SHT_REL)
8809 {
8810 gold_error(_("%s: unsupported REL reloc section"),
8811 object->name().c_str());
8812 return;
8813 }
8814
8815 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
8816 symtab,
8817 layout,
8818 this,
8819 object,
8820 data_shndx,
8821 prelocs,
8822 reloc_count,
8823 output_section,
8824 needs_special_offset_handling,
8825 local_symbol_count,
8826 plocal_symbols);
8827 }
8828
8829 // Functor class for processing the global symbol table.
8830 // Removes symbols defined on discarded opd entries.
8831
8832 template<bool big_endian>
8833 class Global_symbol_visitor_opd
8834 {
8835 public:
8836 Global_symbol_visitor_opd()
8837 { }
8838
8839 void
8840 operator()(Sized_symbol<64>* sym)
8841 {
8842 if (sym->has_symtab_index()
8843 || sym->source() != Symbol::FROM_OBJECT
8844 || !sym->in_real_elf())
8845 return;
8846
8847 if (sym->object()->is_dynamic())
8848 return;
8849
8850 Powerpc_relobj<64, big_endian>* symobj
8851 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
8852 if (symobj->opd_shndx() == 0)
8853 return;
8854
8855 bool is_ordinary;
8856 unsigned int shndx = sym->shndx(&is_ordinary);
8857 if (shndx == symobj->opd_shndx()
8858 && symobj->get_opd_discard(sym->value()))
8859 {
8860 sym->set_undefined();
8861 sym->set_visibility(elfcpp::STV_DEFAULT);
8862 sym->set_is_defined_in_discarded_section();
8863 sym->set_symtab_index(-1U);
8864 }
8865 }
8866 };
8867
8868 template<int size, bool big_endian>
8869 void
8870 Target_powerpc<size, big_endian>::define_save_restore_funcs(
8871 Layout* layout,
8872 Symbol_table* symtab)
8873 {
8874 if (size == 64)
8875 {
8876 Output_data_save_res<size, big_endian>* savres
8877 = new Output_data_save_res<size, big_endian>(symtab);
8878 this->savres_section_ = savres;
8879 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8880 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
8881 savres, ORDER_TEXT, false);
8882 }
8883 }
8884
8885 // Sort linker created .got section first (for the header), then input
8886 // sections belonging to files using small model code.
8887
8888 template<bool big_endian>
8889 class Sort_toc_sections
8890 {
8891 public:
8892 bool
8893 operator()(const Output_section::Input_section& is1,
8894 const Output_section::Input_section& is2) const
8895 {
8896 if (!is1.is_input_section() && is2.is_input_section())
8897 return true;
8898 bool small1
8899 = (is1.is_input_section()
8900 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
8901 ->has_small_toc_reloc()));
8902 bool small2
8903 = (is2.is_input_section()
8904 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
8905 ->has_small_toc_reloc()));
8906 return small1 && !small2;
8907 }
8908 };
8909
8910 // Finalize the sections.
8911
8912 template<int size, bool big_endian>
8913 void
8914 Target_powerpc<size, big_endian>::do_finalize_sections(
8915 Layout* layout,
8916 const Input_objects* input_objects,
8917 Symbol_table* symtab)
8918 {
8919 if (parameters->doing_static_link())
8920 {
8921 // At least some versions of glibc elf-init.o have a strong
8922 // reference to __rela_iplt marker syms. A weak ref would be
8923 // better..
8924 if (this->iplt_ != NULL)
8925 {
8926 Reloc_section* rel = this->iplt_->rel_plt();
8927 symtab->define_in_output_data("__rela_iplt_start", NULL,
8928 Symbol_table::PREDEFINED, rel, 0, 0,
8929 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8930 elfcpp::STV_HIDDEN, 0, false, true);
8931 symtab->define_in_output_data("__rela_iplt_end", NULL,
8932 Symbol_table::PREDEFINED, rel, 0, 0,
8933 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8934 elfcpp::STV_HIDDEN, 0, true, true);
8935 }
8936 else
8937 {
8938 symtab->define_as_constant("__rela_iplt_start", NULL,
8939 Symbol_table::PREDEFINED, 0, 0,
8940 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8941 elfcpp::STV_HIDDEN, 0, true, false);
8942 symtab->define_as_constant("__rela_iplt_end", NULL,
8943 Symbol_table::PREDEFINED, 0, 0,
8944 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8945 elfcpp::STV_HIDDEN, 0, true, false);
8946 }
8947 }
8948
8949 if (size == 64)
8950 {
8951 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
8952 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
8953
8954 if (!parameters->options().relocatable())
8955 {
8956 this->define_save_restore_funcs(layout, symtab);
8957
8958 // Annoyingly, we need to make these sections now whether or
8959 // not we need them. If we delay until do_relax then we
8960 // need to mess with the relaxation machinery checkpointing.
8961 this->got_section(symtab, layout);
8962 this->make_brlt_section(layout);
8963
8964 if (parameters->options().toc_sort())
8965 {
8966 Output_section* os = this->got_->output_section();
8967 if (os != NULL && os->input_sections().size() > 1)
8968 std::stable_sort(os->input_sections().begin(),
8969 os->input_sections().end(),
8970 Sort_toc_sections<big_endian>());
8971 }
8972 }
8973 }
8974
8975 // Fill in some more dynamic tags.
8976 Output_data_dynamic* odyn = layout->dynamic_data();
8977 if (odyn != NULL)
8978 {
8979 const Reloc_section* rel_plt = (this->plt_ == NULL
8980 ? NULL
8981 : this->plt_->rel_plt());
8982 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
8983 this->rela_dyn_, true, size == 32);
8984
8985 if (size == 32)
8986 {
8987 if (this->got_ != NULL)
8988 {
8989 this->got_->finalize_data_size();
8990 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
8991 this->got_, this->got_->g_o_t());
8992 }
8993 if (this->has_tls_get_addr_opt_)
8994 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
8995 }
8996 else
8997 {
8998 if (this->glink_ != NULL)
8999 {
9000 this->glink_->finalize_data_size();
9001 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9002 this->glink_,
9003 (this->glink_->pltresolve_size()
9004 - 32));
9005 }
9006 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9007 odyn->add_constant(elfcpp::DT_PPC64_OPT,
9008 ((this->has_localentry0_
9009 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9010 | (this->has_tls_get_addr_opt_
9011 ? elfcpp::PPC64_OPT_TLS : 0)));
9012 }
9013 }
9014
9015 // Emit any relocs we saved in an attempt to avoid generating COPY
9016 // relocs.
9017 if (this->copy_relocs_.any_saved_relocs())
9018 this->copy_relocs_.emit(this->rela_dyn_section(layout));
9019
9020 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9021 p != input_objects->relobj_end();
9022 ++p)
9023 {
9024 Powerpc_relobj<size, big_endian>* ppc_relobj
9025 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9026 if (ppc_relobj->attributes_section_data())
9027 this->merge_object_attributes(ppc_relobj->name().c_str(),
9028 ppc_relobj->attributes_section_data());
9029 }
9030 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9031 p != input_objects->dynobj_end();
9032 ++p)
9033 {
9034 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9035 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9036 if (ppc_dynobj->attributes_section_data())
9037 this->merge_object_attributes(ppc_dynobj->name().c_str(),
9038 ppc_dynobj->attributes_section_data());
9039 }
9040
9041 // Create a .gnu.attributes section if we have merged any attributes
9042 // from inputs.
9043 if (this->attributes_section_data_ != NULL
9044 && this->attributes_section_data_->size() != 0)
9045 {
9046 Output_attributes_section_data* attributes_section
9047 = new Output_attributes_section_data(*this->attributes_section_data_);
9048 layout->add_output_section_data(".gnu.attributes",
9049 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9050 attributes_section, ORDER_INVALID, false);
9051 }
9052 }
9053
9054 // Merge object attributes from input file called NAME with those of the
9055 // output. The input object attributes are in the object pointed by PASD.
9056
9057 template<int size, bool big_endian>
9058 void
9059 Target_powerpc<size, big_endian>::merge_object_attributes(
9060 const char* name,
9061 const Attributes_section_data* pasd)
9062 {
9063 // Return if there is no attributes section data.
9064 if (pasd == NULL)
9065 return;
9066
9067 // Create output object attributes.
9068 if (this->attributes_section_data_ == NULL)
9069 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9070
9071 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9072 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9073 Object_attribute* out_attr
9074 = this->attributes_section_data_->known_attributes(vendor);
9075
9076 const char* err;
9077 const char* first;
9078 const char* second;
9079 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9080 int in_fp = in_attr[tag].int_value() & 0xf;
9081 int out_fp = out_attr[tag].int_value() & 0xf;
9082 if (in_fp != out_fp)
9083 {
9084 err = NULL;
9085 if ((in_fp & 3) == 0)
9086 ;
9087 else if ((out_fp & 3) == 0)
9088 {
9089 out_fp |= in_fp & 3;
9090 out_attr[tag].set_int_value(out_fp);
9091 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9092 this->last_fp_ = name;
9093 }
9094 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9095 {
9096 err = N_("%s uses hard float, %s uses soft float");
9097 first = this->last_fp_;
9098 second = name;
9099 }
9100 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9101 {
9102 err = N_("%s uses hard float, %s uses soft float");
9103 first = name;
9104 second = this->last_fp_;
9105 }
9106 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9107 {
9108 err = N_("%s uses double-precision hard float, "
9109 "%s uses single-precision hard float");
9110 first = this->last_fp_;
9111 second = name;
9112 }
9113 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9114 {
9115 err = N_("%s uses double-precision hard float, "
9116 "%s uses single-precision hard float");
9117 first = name;
9118 second = this->last_fp_;
9119 }
9120
9121 if (err || (in_fp & 0xc) == 0)
9122 ;
9123 else if ((out_fp & 0xc) == 0)
9124 {
9125 out_fp |= in_fp & 0xc;
9126 out_attr[tag].set_int_value(out_fp);
9127 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9128 this->last_ld_ = name;
9129 }
9130 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9131 {
9132 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9133 first = name;
9134 second = this->last_ld_;
9135 }
9136 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9137 {
9138 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9139 first = this->last_ld_;
9140 second = name;
9141 }
9142 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9143 {
9144 err = N_("%s uses IBM long double, %s uses IEEE long double");
9145 first = this->last_ld_;
9146 second = name;
9147 }
9148 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9149 {
9150 err = N_("%s uses IBM long double, %s uses IEEE long double");
9151 first = name;
9152 second = this->last_ld_;
9153 }
9154
9155 if (err)
9156 {
9157 if (parameters->options().warn_mismatch())
9158 gold_error(_(err), first, second);
9159 // Arrange for this attribute to be deleted. It's better to
9160 // say "don't know" about a file than to wrongly claim compliance.
9161 out_attr[tag].set_type(0);
9162 }
9163 }
9164
9165 if (size == 32)
9166 {
9167 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
9168 int in_vec = in_attr[tag].int_value() & 3;
9169 int out_vec = out_attr[tag].int_value() & 3;
9170 if (in_vec != out_vec)
9171 {
9172 err = NULL;
9173 if (in_vec == 0)
9174 ;
9175 else if (out_vec == 0)
9176 {
9177 out_vec = in_vec;
9178 out_attr[tag].set_int_value(out_vec);
9179 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9180 this->last_vec_ = name;
9181 }
9182 // For now, allow generic to transition to AltiVec or SPE
9183 // without a warning. If GCC marked files with their stack
9184 // alignment and used don't-care markings for files which are
9185 // not affected by the vector ABI, we could warn about this
9186 // case too. */
9187 else if (in_vec == 1)
9188 ;
9189 else if (out_vec == 1)
9190 {
9191 out_vec = in_vec;
9192 out_attr[tag].set_int_value(out_vec);
9193 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9194 this->last_vec_ = name;
9195 }
9196 else if (out_vec < in_vec)
9197 {
9198 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9199 first = this->last_vec_;
9200 second = name;
9201 }
9202 else if (out_vec > in_vec)
9203 {
9204 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9205 first = name;
9206 second = this->last_vec_;
9207 }
9208 if (err)
9209 {
9210 if (parameters->options().warn_mismatch())
9211 gold_error(_(err), first, second);
9212 out_attr[tag].set_type(0);
9213 }
9214 }
9215
9216 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
9217 int in_struct = in_attr[tag].int_value() & 3;
9218 int out_struct = out_attr[tag].int_value() & 3;
9219 if (in_struct != out_struct)
9220 {
9221 err = NULL;
9222 if (in_struct == 0 || in_struct == 3)
9223 ;
9224 else if (out_struct == 0)
9225 {
9226 out_struct = in_struct;
9227 out_attr[tag].set_int_value(out_struct);
9228 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9229 this->last_struct_ = name;
9230 }
9231 else if (out_struct < in_struct)
9232 {
9233 err = N_("%s uses r3/r4 for small structure returns, "
9234 "%s uses memory");
9235 first = this->last_struct_;
9236 second = name;
9237 }
9238 else if (out_struct > in_struct)
9239 {
9240 err = N_("%s uses r3/r4 for small structure returns, "
9241 "%s uses memory");
9242 first = name;
9243 second = this->last_struct_;
9244 }
9245 if (err)
9246 {
9247 if (parameters->options().warn_mismatch())
9248 gold_error(_(err), first, second);
9249 out_attr[tag].set_type(0);
9250 }
9251 }
9252 }
9253
9254 // Merge Tag_compatibility attributes and any common GNU ones.
9255 this->attributes_section_data_->merge(name, pasd);
9256 }
9257
9258 // Emit any saved relocs, and mark toc entries using any of these
9259 // relocs as not optimizable.
9260
9261 template<int sh_type, int size, bool big_endian>
9262 void
9263 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
9264 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
9265 {
9266 if (size == 64
9267 && parameters->options().toc_optimize())
9268 {
9269 for (typename Copy_relocs<sh_type, size, big_endian>::
9270 Copy_reloc_entries::iterator p = this->entries_.begin();
9271 p != this->entries_.end();
9272 ++p)
9273 {
9274 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
9275 entry = *p;
9276
9277 // If the symbol is no longer defined in a dynamic object,
9278 // then we emitted a COPY relocation. If it is still
9279 // dynamic then we'll need dynamic relocations and thus
9280 // can't optimize toc entries.
9281 if (entry.sym_->is_from_dynobj())
9282 {
9283 Powerpc_relobj<size, big_endian>* ppc_object
9284 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
9285 if (entry.shndx_ == ppc_object->toc_shndx())
9286 ppc_object->set_no_toc_opt(entry.address_);
9287 }
9288 }
9289 }
9290
9291 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
9292 }
9293
9294 // Return the value to use for a branch relocation.
9295
9296 template<int size, bool big_endian>
9297 bool
9298 Target_powerpc<size, big_endian>::symval_for_branch(
9299 const Symbol_table* symtab,
9300 const Sized_symbol<size>* gsym,
9301 Powerpc_relobj<size, big_endian>* object,
9302 Address *value,
9303 unsigned int *dest_shndx)
9304 {
9305 if (size == 32 || this->abiversion() >= 2)
9306 gold_unreachable();
9307 *dest_shndx = 0;
9308
9309 // If the symbol is defined in an opd section, ie. is a function
9310 // descriptor, use the function descriptor code entry address
9311 Powerpc_relobj<size, big_endian>* symobj = object;
9312 if (gsym != NULL
9313 && (gsym->source() != Symbol::FROM_OBJECT
9314 || gsym->object()->is_dynamic()))
9315 return true;
9316 if (gsym != NULL)
9317 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9318 unsigned int shndx = symobj->opd_shndx();
9319 if (shndx == 0)
9320 return true;
9321 Address opd_addr = symobj->get_output_section_offset(shndx);
9322 if (opd_addr == invalid_address)
9323 return true;
9324 opd_addr += symobj->output_section_address(shndx);
9325 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
9326 {
9327 Address sec_off;
9328 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
9329 if (symtab->is_section_folded(symobj, *dest_shndx))
9330 {
9331 Section_id folded
9332 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
9333 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
9334 *dest_shndx = folded.second;
9335 }
9336 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
9337 if (sec_addr == invalid_address)
9338 return false;
9339
9340 sec_addr += symobj->output_section(*dest_shndx)->address();
9341 *value = sec_addr + sec_off;
9342 }
9343 return true;
9344 }
9345
9346 // Perform a relocation.
9347
9348 template<int size, bool big_endian>
9349 inline bool
9350 Target_powerpc<size, big_endian>::Relocate::relocate(
9351 const Relocate_info<size, big_endian>* relinfo,
9352 unsigned int,
9353 Target_powerpc* target,
9354 Output_section* os,
9355 size_t relnum,
9356 const unsigned char* preloc,
9357 const Sized_symbol<size>* gsym,
9358 const Symbol_value<size>* psymval,
9359 unsigned char* view,
9360 Address address,
9361 section_size_type view_size)
9362 {
9363 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
9364 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
9365 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9366
9367 if (view == NULL)
9368 return true;
9369
9370 if (target->replace_tls_get_addr(gsym))
9371 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
9372
9373 const elfcpp::Rela<size, big_endian> rela(preloc);
9374 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
9375 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
9376 {
9377 case Track_tls::NOT_EXPECTED:
9378 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9379 _("__tls_get_addr call lacks marker reloc"));
9380 break;
9381 case Track_tls::EXPECTED:
9382 // We have already complained.
9383 break;
9384 case Track_tls::SKIP:
9385 if (is_plt16_reloc<size>(r_type)
9386 || r_type == elfcpp::R_POWERPC_PLTSEQ
9387 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
9388 {
9389 Insn* iview = reinterpret_cast<Insn*>(view);
9390 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9391 }
9392 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
9393 {
9394 Insn* iview = reinterpret_cast<Insn*>(view);
9395 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
9396 }
9397 return true;
9398 case Track_tls::NORMAL:
9399 break;
9400 }
9401
9402 // Offset from start of insn to d-field reloc.
9403 const int d_offset = big_endian ? 2 : 0;
9404
9405 Powerpc_relobj<size, big_endian>* const object
9406 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9407 Address value = 0;
9408 bool has_stub_value = false;
9409 bool localentry0 = false;
9410 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
9411 bool has_plt_offset
9412 = (gsym != NULL
9413 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
9414 : object->local_has_plt_offset(r_sym));
9415 if (has_plt_offset
9416 && !is_plt16_reloc<size>(r_type)
9417 && r_type != elfcpp::R_POWERPC_PLTSEQ
9418 && r_type != elfcpp::R_POWERPC_PLTCALL
9419 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
9420 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
9421 && (!psymval->is_ifunc_symbol()
9422 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
9423 {
9424 if (size == 64
9425 && gsym != NULL
9426 && target->abiversion() >= 2
9427 && !parameters->options().output_is_position_independent()
9428 && !is_branch_reloc<size>(r_type))
9429 {
9430 Address off = target->glink_section()->find_global_entry(gsym);
9431 if (off != invalid_address)
9432 {
9433 value = target->glink_section()->global_entry_address() + off;
9434 has_stub_value = true;
9435 }
9436 }
9437 else
9438 {
9439 Stub_table<size, big_endian>* stub_table = NULL;
9440 if (target->stub_tables().size() == 1)
9441 stub_table = target->stub_tables()[0];
9442 if (stub_table == NULL
9443 && !(size == 32
9444 && gsym != NULL
9445 && !parameters->options().output_is_position_independent()
9446 && !is_branch_reloc<size>(r_type)))
9447 stub_table = object->stub_table(relinfo->data_shndx);
9448 if (stub_table == NULL)
9449 {
9450 // This is a ref from a data section to an ifunc symbol,
9451 // or a non-branch reloc for which we always want to use
9452 // one set of stubs for resolving function addresses.
9453 if (target->stub_tables().size() != 0)
9454 stub_table = target->stub_tables()[0];
9455 }
9456 if (stub_table != NULL)
9457 {
9458 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
9459 if (gsym != NULL)
9460 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
9461 rela.get_r_addend());
9462 else
9463 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
9464 rela.get_r_addend());
9465 if (ent != NULL)
9466 {
9467 value = stub_table->stub_address() + ent->off_;
9468 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9469 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
9470 size_t reloc_count = shdr.get_sh_size() / reloc_size;
9471 if (size == 64
9472 && ent->r2save_
9473 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
9474 value += 4;
9475 else if (size == 64
9476 && ent->r2save_
9477 && relnum < reloc_count - 1)
9478 {
9479 Reltype next_rela(preloc + reloc_size);
9480 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
9481 == elfcpp::R_PPC64_TOCSAVE
9482 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
9483 value += 4;
9484 }
9485 localentry0 = ent->localentry0_;
9486 has_stub_value = true;
9487 }
9488 }
9489 }
9490 // We don't care too much about bogus debug references to
9491 // non-local functions, but otherwise there had better be a plt
9492 // call stub or global entry stub as appropriate.
9493 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
9494 }
9495
9496 if (has_plt_offset && is_plt16_reloc<size>(r_type))
9497 {
9498 const Output_data_plt_powerpc<size, big_endian>* plt;
9499 if (gsym)
9500 value = target->plt_off(gsym, &plt);
9501 else
9502 value = target->plt_off(object, r_sym, &plt);
9503 value += plt->address();
9504
9505 if (size == 64)
9506 value -= (target->got_section()->output_section()->address()
9507 + object->toc_base_offset());
9508 else if (parameters->options().output_is_position_independent())
9509 {
9510 if (rela.get_r_addend() >= 32768)
9511 {
9512 unsigned int got2 = object->got2_shndx();
9513 value -= (object->get_output_section_offset(got2)
9514 + object->output_section(got2)->address()
9515 + rela.get_r_addend());
9516 }
9517 else
9518 value -= (target->got_section()->address()
9519 + target->got_section()->g_o_t());
9520 }
9521 }
9522 else if (!has_plt_offset
9523 && (is_plt16_reloc<size>(r_type)
9524 || r_type == elfcpp::R_POWERPC_PLTSEQ
9525 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
9526 {
9527 Insn* iview = reinterpret_cast<Insn*>(view);
9528 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9529 r_type = elfcpp::R_POWERPC_NONE;
9530 }
9531 else if (r_type == elfcpp::R_POWERPC_GOT16
9532 || r_type == elfcpp::R_POWERPC_GOT16_LO
9533 || r_type == elfcpp::R_POWERPC_GOT16_HI
9534 || r_type == elfcpp::R_POWERPC_GOT16_HA
9535 || r_type == elfcpp::R_PPC64_GOT16_DS
9536 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
9537 {
9538 if (gsym != NULL)
9539 {
9540 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
9541 value = gsym->got_offset(GOT_TYPE_STANDARD);
9542 }
9543 else
9544 {
9545 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
9546 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
9547 }
9548 value -= target->got_section()->got_base_offset(object);
9549 }
9550 else if (r_type == elfcpp::R_PPC64_TOC)
9551 {
9552 value = (target->got_section()->output_section()->address()
9553 + object->toc_base_offset());
9554 }
9555 else if (gsym != NULL
9556 && (r_type == elfcpp::R_POWERPC_REL24
9557 || r_type == elfcpp::R_PPC_PLTREL24)
9558 && has_stub_value)
9559 {
9560 if (size == 64)
9561 {
9562 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
9563 Valtype* wv = reinterpret_cast<Valtype*>(view);
9564 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
9565 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
9566 {
9567 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
9568 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
9569 if ((insn & 1) != 0
9570 && (insn2 == nop
9571 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
9572 {
9573 elfcpp::Swap<32, big_endian>::
9574 writeval(wv + 1, ld_2_1 + target->stk_toc());
9575 can_plt_call = true;
9576 }
9577 }
9578 if (!can_plt_call)
9579 {
9580 // If we don't have a branch and link followed by a nop,
9581 // we can't go via the plt because there is no place to
9582 // put a toc restoring instruction.
9583 // Unless we know we won't be returning.
9584 if (strcmp(gsym->name(), "__libc_start_main") == 0)
9585 can_plt_call = true;
9586 }
9587 if (!can_plt_call)
9588 {
9589 // g++ as of 20130507 emits self-calls without a
9590 // following nop. This is arguably wrong since we have
9591 // conflicting information. On the one hand a global
9592 // symbol and on the other a local call sequence, but
9593 // don't error for this special case.
9594 // It isn't possible to cheaply verify we have exactly
9595 // such a call. Allow all calls to the same section.
9596 bool ok = false;
9597 Address code = value;
9598 if (gsym->source() == Symbol::FROM_OBJECT
9599 && gsym->object() == object)
9600 {
9601 unsigned int dest_shndx = 0;
9602 if (target->abiversion() < 2)
9603 {
9604 Address addend = rela.get_r_addend();
9605 code = psymval->value(object, addend);
9606 target->symval_for_branch(relinfo->symtab, gsym, object,
9607 &code, &dest_shndx);
9608 }
9609 bool is_ordinary;
9610 if (dest_shndx == 0)
9611 dest_shndx = gsym->shndx(&is_ordinary);
9612 ok = dest_shndx == relinfo->data_shndx;
9613 }
9614 if (!ok)
9615 {
9616 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9617 _("call lacks nop, can't restore toc; "
9618 "recompile with -fPIC"));
9619 value = code;
9620 }
9621 }
9622 }
9623 }
9624 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9625 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9626 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9627 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9628 {
9629 // First instruction of a global dynamic sequence, arg setup insn.
9630 const bool final = gsym == NULL || gsym->final_value_is_known();
9631 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9632 enum Got_type got_type = GOT_TYPE_STANDARD;
9633 if (tls_type == tls::TLSOPT_NONE)
9634 got_type = GOT_TYPE_TLSGD;
9635 else if (tls_type == tls::TLSOPT_TO_IE)
9636 got_type = GOT_TYPE_TPREL;
9637 if (got_type != GOT_TYPE_STANDARD)
9638 {
9639 if (gsym != NULL)
9640 {
9641 gold_assert(gsym->has_got_offset(got_type));
9642 value = gsym->got_offset(got_type);
9643 }
9644 else
9645 {
9646 gold_assert(object->local_has_got_offset(r_sym, got_type));
9647 value = object->local_got_offset(r_sym, got_type);
9648 }
9649 value -= target->got_section()->got_base_offset(object);
9650 }
9651 if (tls_type == tls::TLSOPT_TO_IE)
9652 {
9653 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9654 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9655 {
9656 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9657 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9658 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
9659 if (size == 32)
9660 insn |= 32 << 26; // lwz
9661 else
9662 insn |= 58 << 26; // ld
9663 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9664 }
9665 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9666 - elfcpp::R_POWERPC_GOT_TLSGD16);
9667 }
9668 else if (tls_type == tls::TLSOPT_TO_LE)
9669 {
9670 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9671 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9672 {
9673 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9674 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9675 insn &= (1 << 26) - (1 << 21); // extract rt
9676 if (size == 32)
9677 insn |= addis_0_2;
9678 else
9679 insn |= addis_0_13;
9680 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9681 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9682 value = psymval->value(object, rela.get_r_addend());
9683 }
9684 else
9685 {
9686 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9687 Insn insn = nop;
9688 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9689 r_type = elfcpp::R_POWERPC_NONE;
9690 }
9691 }
9692 }
9693 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9694 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9695 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9696 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9697 {
9698 // First instruction of a local dynamic sequence, arg setup insn.
9699 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
9700 if (tls_type == tls::TLSOPT_NONE)
9701 {
9702 value = target->tlsld_got_offset();
9703 value -= target->got_section()->got_base_offset(object);
9704 }
9705 else
9706 {
9707 gold_assert(tls_type == tls::TLSOPT_TO_LE);
9708 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9709 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9710 {
9711 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9712 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9713 insn &= (1 << 26) - (1 << 21); // extract rt
9714 if (size == 32)
9715 insn |= addis_0_2;
9716 else
9717 insn |= addis_0_13;
9718 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9719 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9720 value = dtp_offset;
9721 }
9722 else
9723 {
9724 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9725 Insn insn = nop;
9726 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9727 r_type = elfcpp::R_POWERPC_NONE;
9728 }
9729 }
9730 }
9731 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
9732 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
9733 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
9734 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
9735 {
9736 // Accesses relative to a local dynamic sequence address,
9737 // no optimisation here.
9738 if (gsym != NULL)
9739 {
9740 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
9741 value = gsym->got_offset(GOT_TYPE_DTPREL);
9742 }
9743 else
9744 {
9745 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
9746 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
9747 }
9748 value -= target->got_section()->got_base_offset(object);
9749 }
9750 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9751 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9752 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9753 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9754 {
9755 // First instruction of initial exec sequence.
9756 const bool final = gsym == NULL || gsym->final_value_is_known();
9757 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9758 if (tls_type == tls::TLSOPT_NONE)
9759 {
9760 if (gsym != NULL)
9761 {
9762 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
9763 value = gsym->got_offset(GOT_TYPE_TPREL);
9764 }
9765 else
9766 {
9767 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
9768 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
9769 }
9770 value -= target->got_section()->got_base_offset(object);
9771 }
9772 else
9773 {
9774 gold_assert(tls_type == tls::TLSOPT_TO_LE);
9775 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9776 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9777 {
9778 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9779 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9780 insn &= (1 << 26) - (1 << 21); // extract rt from ld
9781 if (size == 32)
9782 insn |= addis_0_2;
9783 else
9784 insn |= addis_0_13;
9785 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9786 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9787 value = psymval->value(object, rela.get_r_addend());
9788 }
9789 else
9790 {
9791 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9792 Insn insn = nop;
9793 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9794 r_type = elfcpp::R_POWERPC_NONE;
9795 }
9796 }
9797 }
9798 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9799 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9800 {
9801 // Second instruction of a global dynamic sequence,
9802 // the __tls_get_addr call
9803 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
9804 const bool final = gsym == NULL || gsym->final_value_is_known();
9805 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9806 if (tls_type != tls::TLSOPT_NONE)
9807 {
9808 if (tls_type == tls::TLSOPT_TO_IE)
9809 {
9810 Insn* iview = reinterpret_cast<Insn*>(view);
9811 Insn insn = add_3_3_13;
9812 if (size == 32)
9813 insn = add_3_3_2;
9814 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9815 r_type = elfcpp::R_POWERPC_NONE;
9816 }
9817 else
9818 {
9819 Insn* iview = reinterpret_cast<Insn*>(view);
9820 Insn insn = addi_3_3;
9821 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9822 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9823 view += d_offset;
9824 value = psymval->value(object, rela.get_r_addend());
9825 }
9826 this->skip_next_tls_get_addr_call();
9827 }
9828 }
9829 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9830 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9831 {
9832 // Second instruction of a local dynamic sequence,
9833 // the __tls_get_addr call
9834 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
9835 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
9836 if (tls_type == tls::TLSOPT_TO_LE)
9837 {
9838 Insn* iview = reinterpret_cast<Insn*>(view);
9839 Insn insn = addi_3_3;
9840 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9841 this->skip_next_tls_get_addr_call();
9842 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9843 view += d_offset;
9844 value = dtp_offset;
9845 }
9846 }
9847 else if (r_type == elfcpp::R_POWERPC_TLS)
9848 {
9849 // Second instruction of an initial exec sequence
9850 const bool final = gsym == NULL || gsym->final_value_is_known();
9851 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9852 if (tls_type == tls::TLSOPT_TO_LE)
9853 {
9854 Insn* iview = reinterpret_cast<Insn*>(view);
9855 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9856 unsigned int reg = size == 32 ? 2 : 13;
9857 insn = at_tls_transform(insn, reg);
9858 gold_assert(insn != 0);
9859 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9860 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9861 view += d_offset;
9862 value = psymval->value(object, rela.get_r_addend());
9863 }
9864 }
9865 else if (!has_stub_value)
9866 {
9867 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
9868 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
9869 {
9870 // PLTCALL without plt entry => convert to direct call
9871 Insn* iview = reinterpret_cast<Insn*>(view);
9872 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9873 insn = (insn & 1) | b;
9874 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9875 if (size == 32)
9876 r_type = elfcpp::R_PPC_PLTREL24;
9877 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
9878 r_type = elfcpp::R_PPC64_REL24_NOTOC;
9879 else
9880 r_type = elfcpp::R_POWERPC_REL24;
9881 }
9882 Address addend = 0;
9883 if (!(size == 32
9884 && (r_type == elfcpp::R_PPC_PLTREL24
9885 || r_type == elfcpp::R_POWERPC_PLT16_LO
9886 || r_type == elfcpp::R_POWERPC_PLT16_HI
9887 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
9888 addend = rela.get_r_addend();
9889 value = psymval->value(object, addend);
9890 if (size == 64 && is_branch_reloc<size>(r_type))
9891 {
9892 if (target->abiversion() >= 2)
9893 {
9894 if (gsym != NULL)
9895 value += object->ppc64_local_entry_offset(gsym);
9896 else
9897 value += object->ppc64_local_entry_offset(r_sym);
9898 }
9899 else
9900 {
9901 unsigned int dest_shndx;
9902 target->symval_for_branch(relinfo->symtab, gsym, object,
9903 &value, &dest_shndx);
9904 }
9905 }
9906 Address max_branch_offset = max_branch_delta<size>(r_type);
9907 if (max_branch_offset != 0
9908 && (value - address + max_branch_offset >= 2 * max_branch_offset
9909 || (size == 64
9910 && r_type == elfcpp::R_PPC64_REL24_NOTOC
9911 && (gsym != NULL
9912 ? object->ppc64_needs_toc(gsym)
9913 : object->ppc64_needs_toc(r_sym)))))
9914 {
9915 Stub_table<size, big_endian>* stub_table
9916 = object->stub_table(relinfo->data_shndx);
9917 if (stub_table != NULL)
9918 {
9919 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
9920 = stub_table->find_long_branch_entry(object, value);
9921 if (ent != NULL)
9922 {
9923 if (ent->save_res_)
9924 value = (value - target->savres_section()->address()
9925 + stub_table->branch_size());
9926 else
9927 value = (stub_table->stub_address() + stub_table->plt_size()
9928 + ent->off_);
9929 has_stub_value = true;
9930 }
9931 }
9932 }
9933 }
9934
9935 switch (r_type)
9936 {
9937 case elfcpp::R_PPC64_REL24_NOTOC:
9938 if (size == 32)
9939 break;
9940 // Fall through.
9941 case elfcpp::R_PPC64_REL64:
9942 case elfcpp::R_POWERPC_REL32:
9943 case elfcpp::R_POWERPC_REL24:
9944 case elfcpp::R_PPC_PLTREL24:
9945 case elfcpp::R_PPC_LOCAL24PC:
9946 case elfcpp::R_POWERPC_REL16:
9947 case elfcpp::R_POWERPC_REL16_LO:
9948 case elfcpp::R_POWERPC_REL16_HI:
9949 case elfcpp::R_POWERPC_REL16_HA:
9950 case elfcpp::R_POWERPC_REL16DX_HA:
9951 case elfcpp::R_PPC64_REL16_HIGH:
9952 case elfcpp::R_PPC64_REL16_HIGHA:
9953 case elfcpp::R_PPC64_REL16_HIGHER:
9954 case elfcpp::R_PPC64_REL16_HIGHERA:
9955 case elfcpp::R_PPC64_REL16_HIGHEST:
9956 case elfcpp::R_PPC64_REL16_HIGHESTA:
9957 case elfcpp::R_POWERPC_REL14:
9958 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9959 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9960 value -= address;
9961 break;
9962
9963 case elfcpp::R_PPC64_TOC16:
9964 case elfcpp::R_PPC64_TOC16_LO:
9965 case elfcpp::R_PPC64_TOC16_HI:
9966 case elfcpp::R_PPC64_TOC16_HA:
9967 case elfcpp::R_PPC64_TOC16_DS:
9968 case elfcpp::R_PPC64_TOC16_LO_DS:
9969 // Subtract the TOC base address.
9970 value -= (target->got_section()->output_section()->address()
9971 + object->toc_base_offset());
9972 break;
9973
9974 case elfcpp::R_POWERPC_SECTOFF:
9975 case elfcpp::R_POWERPC_SECTOFF_LO:
9976 case elfcpp::R_POWERPC_SECTOFF_HI:
9977 case elfcpp::R_POWERPC_SECTOFF_HA:
9978 case elfcpp::R_PPC64_SECTOFF_DS:
9979 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9980 if (os != NULL)
9981 value -= os->address();
9982 break;
9983
9984 case elfcpp::R_PPC64_TPREL16_DS:
9985 case elfcpp::R_PPC64_TPREL16_LO_DS:
9986 case elfcpp::R_PPC64_TPREL16_HIGH:
9987 case elfcpp::R_PPC64_TPREL16_HIGHA:
9988 if (size != 64)
9989 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
9990 break;
9991 // Fall through.
9992 case elfcpp::R_POWERPC_TPREL16:
9993 case elfcpp::R_POWERPC_TPREL16_LO:
9994 case elfcpp::R_POWERPC_TPREL16_HI:
9995 case elfcpp::R_POWERPC_TPREL16_HA:
9996 case elfcpp::R_POWERPC_TPREL:
9997 case elfcpp::R_PPC64_TPREL16_HIGHER:
9998 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9999 case elfcpp::R_PPC64_TPREL16_HIGHEST:
10000 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
10001 // tls symbol values are relative to tls_segment()->vaddr()
10002 value -= tp_offset;
10003 break;
10004
10005 case elfcpp::R_PPC64_DTPREL16_DS:
10006 case elfcpp::R_PPC64_DTPREL16_LO_DS:
10007 case elfcpp::R_PPC64_DTPREL16_HIGHER:
10008 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10009 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10010 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10011 if (size != 64)
10012 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
10013 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
10014 break;
10015 // Fall through.
10016 case elfcpp::R_POWERPC_DTPREL16:
10017 case elfcpp::R_POWERPC_DTPREL16_LO:
10018 case elfcpp::R_POWERPC_DTPREL16_HI:
10019 case elfcpp::R_POWERPC_DTPREL16_HA:
10020 case elfcpp::R_POWERPC_DTPREL:
10021 case elfcpp::R_PPC64_DTPREL16_HIGH:
10022 case elfcpp::R_PPC64_DTPREL16_HIGHA:
10023 // tls symbol values are relative to tls_segment()->vaddr()
10024 value -= dtp_offset;
10025 break;
10026
10027 case elfcpp::R_PPC64_ADDR64_LOCAL:
10028 if (gsym != NULL)
10029 value += object->ppc64_local_entry_offset(gsym);
10030 else
10031 value += object->ppc64_local_entry_offset(r_sym);
10032 break;
10033
10034 default:
10035 break;
10036 }
10037
10038 Insn branch_bit = 0;
10039 switch (r_type)
10040 {
10041 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10042 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10043 branch_bit = 1 << 21;
10044 // Fall through.
10045 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10046 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10047 {
10048 Insn* iview = reinterpret_cast<Insn*>(view);
10049 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10050 insn &= ~(1 << 21);
10051 insn |= branch_bit;
10052 if (this->is_isa_v2)
10053 {
10054 // Set 'a' bit. This is 0b00010 in BO field for branch
10055 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
10056 // for branch on CTR insns (BO == 1a00t or 1a01t).
10057 if ((insn & (0x14 << 21)) == (0x04 << 21))
10058 insn |= 0x02 << 21;
10059 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10060 insn |= 0x08 << 21;
10061 else
10062 break;
10063 }
10064 else
10065 {
10066 // Invert 'y' bit if not the default.
10067 if (static_cast<Signed_address>(value) < 0)
10068 insn ^= 1 << 21;
10069 }
10070 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10071 }
10072 break;
10073
10074 case elfcpp::R_POWERPC_PLT16_HA:
10075 if (size == 32
10076 && !parameters->options().output_is_position_independent())
10077 {
10078 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10079 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10080
10081 // Convert addis to lis.
10082 if ((insn & (0x3f << 26)) == 15u << 26
10083 && (insn & (0x1f << 16)) != 0)
10084 {
10085 insn &= ~(0x1f << 16);
10086 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10087 }
10088 }
10089 break;
10090
10091 default:
10092 break;
10093 }
10094
10095 if (size == 64)
10096 {
10097 switch (r_type)
10098 {
10099 default:
10100 break;
10101
10102 // Multi-instruction sequences that access the GOT/TOC can
10103 // be optimized, eg.
10104 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
10105 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
10106 // and
10107 // addis ra,r2,0; addi rb,ra,x@toc@l;
10108 // to nop; addi rb,r2,x@toc;
10109 // FIXME: the @got sequence shown above is not yet
10110 // optimized. Note that gcc as of 2017-01-07 doesn't use
10111 // the ELF @got relocs except for TLS, instead using the
10112 // PowerOpen variant of a compiler managed GOT (called TOC).
10113 // The PowerOpen TOC sequence equivalent to the first
10114 // example is optimized.
10115 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10116 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10117 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10118 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10119 case elfcpp::R_POWERPC_GOT16_HA:
10120 case elfcpp::R_PPC64_TOC16_HA:
10121 if (parameters->options().toc_optimize())
10122 {
10123 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10124 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10125 if (r_type == elfcpp::R_PPC64_TOC16_HA
10126 && object->make_toc_relative(target, &value))
10127 {
10128 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
10129 == ((15u << 26) | (2 << 16)));
10130 }
10131 if (((insn & ((0x3f << 26) | 0x1f << 16))
10132 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
10133 && value + 0x8000 < 0x10000)
10134 {
10135 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10136 return true;
10137 }
10138 }
10139 break;
10140
10141 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
10142 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
10143 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
10144 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
10145 case elfcpp::R_POWERPC_GOT16_LO:
10146 case elfcpp::R_PPC64_GOT16_LO_DS:
10147 case elfcpp::R_PPC64_TOC16_LO:
10148 case elfcpp::R_PPC64_TOC16_LO_DS:
10149 if (parameters->options().toc_optimize())
10150 {
10151 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10152 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10153 bool changed = false;
10154 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
10155 && object->make_toc_relative(target, &value))
10156 {
10157 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
10158 insn ^= (14u << 26) ^ (58u << 26);
10159 r_type = elfcpp::R_PPC64_TOC16_LO;
10160 changed = true;
10161 }
10162 if (ok_lo_toc_insn(insn, r_type)
10163 && value + 0x8000 < 0x10000)
10164 {
10165 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
10166 {
10167 // Transform addic to addi when we change reg.
10168 insn &= ~((0x3f << 26) | (0x1f << 16));
10169 insn |= (14u << 26) | (2 << 16);
10170 }
10171 else
10172 {
10173 insn &= ~(0x1f << 16);
10174 insn |= 2 << 16;
10175 }
10176 changed = true;
10177 }
10178 if (changed)
10179 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10180 }
10181 break;
10182
10183 case elfcpp::R_POWERPC_TPREL16_HA:
10184 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
10185 {
10186 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10187 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10188 if ((insn & ((0x3f << 26) | 0x1f << 16))
10189 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
10190 ;
10191 else
10192 {
10193 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10194 return true;
10195 }
10196 }
10197 break;
10198
10199 case elfcpp::R_PPC64_TPREL16_LO_DS:
10200 if (size == 32)
10201 // R_PPC_TLSGD, R_PPC_TLSLD
10202 break;
10203 // Fall through.
10204 case elfcpp::R_POWERPC_TPREL16_LO:
10205 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
10206 {
10207 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10208 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10209 insn &= ~(0x1f << 16);
10210 insn |= (size == 32 ? 2 : 13) << 16;
10211 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10212 }
10213 break;
10214
10215 case elfcpp::R_PPC64_ENTRY:
10216 value = (target->got_section()->output_section()->address()
10217 + object->toc_base_offset());
10218 if (value + 0x80008000 <= 0xffffffff
10219 && !parameters->options().output_is_position_independent())
10220 {
10221 Insn* iview = reinterpret_cast<Insn*>(view);
10222 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
10223 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
10224
10225 if ((insn1 & ~0xfffc) == ld_2_12
10226 && insn2 == add_2_2_12)
10227 {
10228 insn1 = lis_2 + ha(value);
10229 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
10230 insn2 = addi_2_2 + l(value);
10231 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
10232 return true;
10233 }
10234 }
10235 else
10236 {
10237 value -= address;
10238 if (value + 0x80008000 <= 0xffffffff)
10239 {
10240 Insn* iview = reinterpret_cast<Insn*>(view);
10241 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
10242 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
10243
10244 if ((insn1 & ~0xfffc) == ld_2_12
10245 && insn2 == add_2_2_12)
10246 {
10247 insn1 = addis_2_12 + ha(value);
10248 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
10249 insn2 = addi_2_2 + l(value);
10250 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
10251 return true;
10252 }
10253 }
10254 }
10255 break;
10256
10257 case elfcpp::R_POWERPC_REL16_LO:
10258 // If we are generating a non-PIC executable, edit
10259 // 0: addis 2,12,.TOC.-0b@ha
10260 // addi 2,2,.TOC.-0b@l
10261 // used by ELFv2 global entry points to set up r2, to
10262 // lis 2,.TOC.@ha
10263 // addi 2,2,.TOC.@l
10264 // if .TOC. is in range. */
10265 if (value + address - 4 + 0x80008000 <= 0xffffffff
10266 && relnum + 1 > 1
10267 && preloc != NULL
10268 && target->abiversion() >= 2
10269 && !parameters->options().output_is_position_independent()
10270 && rela.get_r_addend() == d_offset + 4
10271 && gsym != NULL
10272 && strcmp(gsym->name(), ".TOC.") == 0)
10273 {
10274 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10275 Reltype prev_rela(preloc - reloc_size);
10276 if ((prev_rela.get_r_info()
10277 == elfcpp::elf_r_info<size>(r_sym,
10278 elfcpp::R_POWERPC_REL16_HA))
10279 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
10280 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
10281 {
10282 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10283 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
10284 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
10285
10286 if ((insn1 & 0xffff0000) == addis_2_12
10287 && (insn2 & 0xffff0000) == addi_2_2)
10288 {
10289 insn1 = lis_2 + ha(value + address - 4);
10290 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
10291 insn2 = addi_2_2 + l(value + address - 4);
10292 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
10293 if (relinfo->rr)
10294 {
10295 relinfo->rr->set_strategy(relnum - 1,
10296 Relocatable_relocs::RELOC_SPECIAL);
10297 relinfo->rr->set_strategy(relnum,
10298 Relocatable_relocs::RELOC_SPECIAL);
10299 }
10300 return true;
10301 }
10302 }
10303 }
10304 break;
10305 }
10306 }
10307
10308 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
10309 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
10310 switch (r_type)
10311 {
10312 case elfcpp::R_POWERPC_ADDR32:
10313 case elfcpp::R_POWERPC_UADDR32:
10314 if (size == 64)
10315 overflow = Reloc::CHECK_BITFIELD;
10316 break;
10317
10318 case elfcpp::R_POWERPC_REL32:
10319 case elfcpp::R_POWERPC_REL16DX_HA:
10320 if (size == 64)
10321 overflow = Reloc::CHECK_SIGNED;
10322 break;
10323
10324 case elfcpp::R_POWERPC_UADDR16:
10325 overflow = Reloc::CHECK_BITFIELD;
10326 break;
10327
10328 case elfcpp::R_POWERPC_ADDR16:
10329 // We really should have three separate relocations,
10330 // one for 16-bit data, one for insns with 16-bit signed fields,
10331 // and one for insns with 16-bit unsigned fields.
10332 overflow = Reloc::CHECK_BITFIELD;
10333 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
10334 overflow = Reloc::CHECK_LOW_INSN;
10335 break;
10336
10337 case elfcpp::R_POWERPC_ADDR16_HI:
10338 case elfcpp::R_POWERPC_ADDR16_HA:
10339 case elfcpp::R_POWERPC_GOT16_HI:
10340 case elfcpp::R_POWERPC_GOT16_HA:
10341 case elfcpp::R_POWERPC_PLT16_HI:
10342 case elfcpp::R_POWERPC_PLT16_HA:
10343 case elfcpp::R_POWERPC_SECTOFF_HI:
10344 case elfcpp::R_POWERPC_SECTOFF_HA:
10345 case elfcpp::R_PPC64_TOC16_HI:
10346 case elfcpp::R_PPC64_TOC16_HA:
10347 case elfcpp::R_PPC64_PLTGOT16_HI:
10348 case elfcpp::R_PPC64_PLTGOT16_HA:
10349 case elfcpp::R_POWERPC_TPREL16_HI:
10350 case elfcpp::R_POWERPC_TPREL16_HA:
10351 case elfcpp::R_POWERPC_DTPREL16_HI:
10352 case elfcpp::R_POWERPC_DTPREL16_HA:
10353 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
10354 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10355 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
10356 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10357 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
10358 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10359 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
10360 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10361 case elfcpp::R_POWERPC_REL16_HI:
10362 case elfcpp::R_POWERPC_REL16_HA:
10363 if (size != 32)
10364 overflow = Reloc::CHECK_HIGH_INSN;
10365 break;
10366
10367 case elfcpp::R_POWERPC_REL16:
10368 case elfcpp::R_PPC64_TOC16:
10369 case elfcpp::R_POWERPC_GOT16:
10370 case elfcpp::R_POWERPC_SECTOFF:
10371 case elfcpp::R_POWERPC_TPREL16:
10372 case elfcpp::R_POWERPC_DTPREL16:
10373 case elfcpp::R_POWERPC_GOT_TLSGD16:
10374 case elfcpp::R_POWERPC_GOT_TLSLD16:
10375 case elfcpp::R_POWERPC_GOT_TPREL16:
10376 case elfcpp::R_POWERPC_GOT_DTPREL16:
10377 overflow = Reloc::CHECK_LOW_INSN;
10378 break;
10379
10380 case elfcpp::R_PPC64_REL24_NOTOC:
10381 if (size == 32)
10382 break;
10383 // Fall through.
10384 case elfcpp::R_POWERPC_ADDR24:
10385 case elfcpp::R_POWERPC_ADDR14:
10386 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10387 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10388 case elfcpp::R_PPC64_ADDR16_DS:
10389 case elfcpp::R_POWERPC_REL24:
10390 case elfcpp::R_PPC_PLTREL24:
10391 case elfcpp::R_PPC_LOCAL24PC:
10392 case elfcpp::R_PPC64_TPREL16_DS:
10393 case elfcpp::R_PPC64_DTPREL16_DS:
10394 case elfcpp::R_PPC64_TOC16_DS:
10395 case elfcpp::R_PPC64_GOT16_DS:
10396 case elfcpp::R_PPC64_SECTOFF_DS:
10397 case elfcpp::R_POWERPC_REL14:
10398 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10399 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10400 overflow = Reloc::CHECK_SIGNED;
10401 break;
10402 }
10403
10404 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10405 Insn insn = 0;
10406
10407 if (overflow == Reloc::CHECK_LOW_INSN
10408 || overflow == Reloc::CHECK_HIGH_INSN)
10409 {
10410 insn = elfcpp::Swap<32, big_endian>::readval(iview);
10411
10412 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
10413 overflow = Reloc::CHECK_BITFIELD;
10414 else if (overflow == Reloc::CHECK_LOW_INSN
10415 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
10416 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
10417 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
10418 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
10419 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
10420 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
10421 overflow = Reloc::CHECK_UNSIGNED;
10422 else
10423 overflow = Reloc::CHECK_SIGNED;
10424 }
10425
10426 bool maybe_dq_reloc = false;
10427 typename Powerpc_relocate_functions<size, big_endian>::Status status
10428 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
10429 switch (r_type)
10430 {
10431 case elfcpp::R_POWERPC_NONE:
10432 case elfcpp::R_POWERPC_TLS:
10433 case elfcpp::R_POWERPC_GNU_VTINHERIT:
10434 case elfcpp::R_POWERPC_GNU_VTENTRY:
10435 case elfcpp::R_POWERPC_PLTSEQ:
10436 case elfcpp::R_POWERPC_PLTCALL:
10437 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
10438 case elfcpp::R_PPC64_PLTCALL_NOTOC:
10439 break;
10440
10441 case elfcpp::R_PPC64_ADDR64:
10442 case elfcpp::R_PPC64_REL64:
10443 case elfcpp::R_PPC64_TOC:
10444 case elfcpp::R_PPC64_ADDR64_LOCAL:
10445 Reloc::addr64(view, value);
10446 break;
10447
10448 case elfcpp::R_POWERPC_TPREL:
10449 case elfcpp::R_POWERPC_DTPREL:
10450 if (size == 64)
10451 Reloc::addr64(view, value);
10452 else
10453 status = Reloc::addr32(view, value, overflow);
10454 break;
10455
10456 case elfcpp::R_PPC64_UADDR64:
10457 Reloc::addr64_u(view, value);
10458 break;
10459
10460 case elfcpp::R_POWERPC_ADDR32:
10461 status = Reloc::addr32(view, value, overflow);
10462 break;
10463
10464 case elfcpp::R_POWERPC_REL32:
10465 case elfcpp::R_POWERPC_UADDR32:
10466 status = Reloc::addr32_u(view, value, overflow);
10467 break;
10468
10469 case elfcpp::R_PPC64_REL24_NOTOC:
10470 if (size == 32)
10471 goto unsupp; // R_PPC_EMB_RELSDA
10472 // Fall through.
10473 case elfcpp::R_POWERPC_ADDR24:
10474 case elfcpp::R_POWERPC_REL24:
10475 case elfcpp::R_PPC_PLTREL24:
10476 case elfcpp::R_PPC_LOCAL24PC:
10477 status = Reloc::addr24(view, value, overflow);
10478 break;
10479
10480 case elfcpp::R_POWERPC_GOT_DTPREL16:
10481 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
10482 case elfcpp::R_POWERPC_GOT_TPREL16:
10483 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
10484 if (size == 64)
10485 {
10486 // On ppc64 these are all ds form
10487 maybe_dq_reloc = true;
10488 break;
10489 }
10490 // Fall through.
10491 case elfcpp::R_POWERPC_ADDR16:
10492 case elfcpp::R_POWERPC_REL16:
10493 case elfcpp::R_PPC64_TOC16:
10494 case elfcpp::R_POWERPC_GOT16:
10495 case elfcpp::R_POWERPC_SECTOFF:
10496 case elfcpp::R_POWERPC_TPREL16:
10497 case elfcpp::R_POWERPC_DTPREL16:
10498 case elfcpp::R_POWERPC_GOT_TLSGD16:
10499 case elfcpp::R_POWERPC_GOT_TLSLD16:
10500 case elfcpp::R_POWERPC_ADDR16_LO:
10501 case elfcpp::R_POWERPC_REL16_LO:
10502 case elfcpp::R_PPC64_TOC16_LO:
10503 case elfcpp::R_POWERPC_GOT16_LO:
10504 case elfcpp::R_POWERPC_PLT16_LO:
10505 case elfcpp::R_POWERPC_SECTOFF_LO:
10506 case elfcpp::R_POWERPC_TPREL16_LO:
10507 case elfcpp::R_POWERPC_DTPREL16_LO:
10508 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
10509 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
10510 if (size == 64)
10511 status = Reloc::addr16(view, value, overflow);
10512 else
10513 maybe_dq_reloc = true;
10514 break;
10515
10516 case elfcpp::R_POWERPC_UADDR16:
10517 status = Reloc::addr16_u(view, value, overflow);
10518 break;
10519
10520 case elfcpp::R_PPC64_ADDR16_HIGH:
10521 case elfcpp::R_PPC64_TPREL16_HIGH:
10522 case elfcpp::R_PPC64_DTPREL16_HIGH:
10523 if (size == 32)
10524 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
10525 goto unsupp;
10526 // Fall through.
10527 case elfcpp::R_POWERPC_ADDR16_HI:
10528 case elfcpp::R_POWERPC_REL16_HI:
10529 case elfcpp::R_PPC64_REL16_HIGH:
10530 case elfcpp::R_PPC64_TOC16_HI:
10531 case elfcpp::R_POWERPC_GOT16_HI:
10532 case elfcpp::R_POWERPC_PLT16_HI:
10533 case elfcpp::R_POWERPC_SECTOFF_HI:
10534 case elfcpp::R_POWERPC_TPREL16_HI:
10535 case elfcpp::R_POWERPC_DTPREL16_HI:
10536 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
10537 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
10538 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
10539 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
10540 Reloc::addr16_hi(view, value);
10541 break;
10542
10543 case elfcpp::R_PPC64_ADDR16_HIGHA:
10544 case elfcpp::R_PPC64_TPREL16_HIGHA:
10545 case elfcpp::R_PPC64_DTPREL16_HIGHA:
10546 if (size == 32)
10547 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
10548 goto unsupp;
10549 // Fall through.
10550 case elfcpp::R_POWERPC_ADDR16_HA:
10551 case elfcpp::R_POWERPC_REL16_HA:
10552 case elfcpp::R_PPC64_REL16_HIGHA:
10553 case elfcpp::R_PPC64_TOC16_HA:
10554 case elfcpp::R_POWERPC_GOT16_HA:
10555 case elfcpp::R_POWERPC_PLT16_HA:
10556 case elfcpp::R_POWERPC_SECTOFF_HA:
10557 case elfcpp::R_POWERPC_TPREL16_HA:
10558 case elfcpp::R_POWERPC_DTPREL16_HA:
10559 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10560 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10561 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10562 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10563 Reloc::addr16_ha(view, value);
10564 break;
10565
10566 case elfcpp::R_POWERPC_REL16DX_HA:
10567 status = Reloc::addr16dx_ha(view, value, overflow);
10568 break;
10569
10570 case elfcpp::R_PPC64_DTPREL16_HIGHER:
10571 if (size == 32)
10572 // R_PPC_EMB_NADDR16_LO
10573 goto unsupp;
10574 // Fall through.
10575 case elfcpp::R_PPC64_ADDR16_HIGHER:
10576 case elfcpp::R_PPC64_REL16_HIGHER:
10577 case elfcpp::R_PPC64_TPREL16_HIGHER:
10578 Reloc::addr16_hi2(view, value);
10579 break;
10580
10581 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10582 if (size == 32)
10583 // R_PPC_EMB_NADDR16_HI
10584 goto unsupp;
10585 // Fall through.
10586 case elfcpp::R_PPC64_ADDR16_HIGHERA:
10587 case elfcpp::R_PPC64_REL16_HIGHERA:
10588 case elfcpp::R_PPC64_TPREL16_HIGHERA:
10589 Reloc::addr16_ha2(view, value);
10590 break;
10591
10592 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10593 if (size == 32)
10594 // R_PPC_EMB_NADDR16_HA
10595 goto unsupp;
10596 // Fall through.
10597 case elfcpp::R_PPC64_ADDR16_HIGHEST:
10598 case elfcpp::R_PPC64_REL16_HIGHEST:
10599 case elfcpp::R_PPC64_TPREL16_HIGHEST:
10600 Reloc::addr16_hi3(view, value);
10601 break;
10602
10603 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10604 if (size == 32)
10605 // R_PPC_EMB_SDAI16
10606 goto unsupp;
10607 // Fall through.
10608 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
10609 case elfcpp::R_PPC64_REL16_HIGHESTA:
10610 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
10611 Reloc::addr16_ha3(view, value);
10612 break;
10613
10614 case elfcpp::R_PPC64_DTPREL16_DS:
10615 case elfcpp::R_PPC64_DTPREL16_LO_DS:
10616 if (size == 32)
10617 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
10618 goto unsupp;
10619 // Fall through.
10620 case elfcpp::R_PPC64_TPREL16_DS:
10621 case elfcpp::R_PPC64_TPREL16_LO_DS:
10622 if (size == 32)
10623 // R_PPC_TLSGD, R_PPC_TLSLD
10624 break;
10625 // Fall through.
10626 case elfcpp::R_PPC64_ADDR16_DS:
10627 case elfcpp::R_PPC64_ADDR16_LO_DS:
10628 case elfcpp::R_PPC64_TOC16_DS:
10629 case elfcpp::R_PPC64_TOC16_LO_DS:
10630 case elfcpp::R_PPC64_GOT16_DS:
10631 case elfcpp::R_PPC64_GOT16_LO_DS:
10632 case elfcpp::R_PPC64_PLT16_LO_DS:
10633 case elfcpp::R_PPC64_SECTOFF_DS:
10634 case elfcpp::R_PPC64_SECTOFF_LO_DS:
10635 maybe_dq_reloc = true;
10636 break;
10637
10638 case elfcpp::R_POWERPC_ADDR14:
10639 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10640 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10641 case elfcpp::R_POWERPC_REL14:
10642 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10643 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10644 status = Reloc::addr14(view, value, overflow);
10645 break;
10646
10647 case elfcpp::R_POWERPC_COPY:
10648 case elfcpp::R_POWERPC_GLOB_DAT:
10649 case elfcpp::R_POWERPC_JMP_SLOT:
10650 case elfcpp::R_POWERPC_RELATIVE:
10651 case elfcpp::R_POWERPC_DTPMOD:
10652 case elfcpp::R_PPC64_JMP_IREL:
10653 case elfcpp::R_POWERPC_IRELATIVE:
10654 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10655 _("unexpected reloc %u in object file"),
10656 r_type);
10657 break;
10658
10659 case elfcpp::R_PPC64_TOCSAVE:
10660 if (size == 32)
10661 // R_PPC_EMB_SDA21
10662 goto unsupp;
10663 else
10664 {
10665 Symbol_location loc;
10666 loc.object = relinfo->object;
10667 loc.shndx = relinfo->data_shndx;
10668 loc.offset = rela.get_r_offset();
10669 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
10670 if (p != target->tocsave_loc().end())
10671 {
10672 // If we've generated plt calls using this tocsave, then
10673 // the nop needs to be changed to save r2.
10674 Insn* iview = reinterpret_cast<Insn*>(view);
10675 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
10676 elfcpp::Swap<32, big_endian>::
10677 writeval(iview, std_2_1 + target->stk_toc());
10678 }
10679 }
10680 break;
10681
10682 case elfcpp::R_PPC_EMB_SDA2I16:
10683 case elfcpp::R_PPC_EMB_SDA2REL:
10684 if (size == 32)
10685 goto unsupp;
10686 // R_PPC64_TLSGD, R_PPC64_TLSLD
10687 break;
10688
10689 case elfcpp::R_POWERPC_PLT32:
10690 case elfcpp::R_POWERPC_PLTREL32:
10691 case elfcpp::R_PPC_SDAREL16:
10692 case elfcpp::R_POWERPC_ADDR30:
10693 case elfcpp::R_PPC64_PLT64:
10694 case elfcpp::R_PPC64_PLTREL64:
10695 case elfcpp::R_PPC64_PLTGOT16:
10696 case elfcpp::R_PPC64_PLTGOT16_LO:
10697 case elfcpp::R_PPC64_PLTGOT16_HI:
10698 case elfcpp::R_PPC64_PLTGOT16_HA:
10699 case elfcpp::R_PPC64_PLTGOT16_DS:
10700 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
10701 case elfcpp::R_PPC_TOC16:
10702 default:
10703 unsupp:
10704 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10705 _("unsupported reloc %u"),
10706 r_type);
10707 break;
10708 }
10709
10710 if (maybe_dq_reloc)
10711 {
10712 if (insn == 0)
10713 insn = elfcpp::Swap<32, big_endian>::readval(iview);
10714
10715 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
10716 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
10717 && (insn & 3) == 1))
10718 status = Reloc::addr16_dq(view, value, overflow);
10719 else if (size == 64
10720 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
10721 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
10722 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
10723 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
10724 status = Reloc::addr16_ds(view, value, overflow);
10725 else
10726 status = Reloc::addr16(view, value, overflow);
10727 }
10728
10729 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
10730 && (has_stub_value
10731 || !(gsym != NULL
10732 && gsym->is_undefined()
10733 && is_branch_reloc<size>(r_type))))
10734 {
10735 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10736 _("relocation overflow"));
10737 if (has_stub_value)
10738 gold_info(_("try relinking with a smaller --stub-group-size"));
10739 }
10740
10741 return true;
10742 }
10743
10744 // Relocate section data.
10745
10746 template<int size, bool big_endian>
10747 void
10748 Target_powerpc<size, big_endian>::relocate_section(
10749 const Relocate_info<size, big_endian>* relinfo,
10750 unsigned int sh_type,
10751 const unsigned char* prelocs,
10752 size_t reloc_count,
10753 Output_section* output_section,
10754 bool needs_special_offset_handling,
10755 unsigned char* view,
10756 Address address,
10757 section_size_type view_size,
10758 const Reloc_symbol_changes* reloc_symbol_changes)
10759 {
10760 typedef Target_powerpc<size, big_endian> Powerpc;
10761 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
10762 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
10763 Powerpc_comdat_behavior;
10764 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10765 Classify_reloc;
10766
10767 gold_assert(sh_type == elfcpp::SHT_RELA);
10768
10769 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
10770 Powerpc_comdat_behavior, Classify_reloc>(
10771 relinfo,
10772 this,
10773 prelocs,
10774 reloc_count,
10775 output_section,
10776 needs_special_offset_handling,
10777 view,
10778 address,
10779 view_size,
10780 reloc_symbol_changes);
10781 }
10782
10783 template<int size, bool big_endian>
10784 class Powerpc_scan_relocatable_reloc
10785 {
10786 public:
10787 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10788 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10789 static const int sh_type = elfcpp::SHT_RELA;
10790
10791 // Return the symbol referred to by the relocation.
10792 static inline unsigned int
10793 get_r_sym(const Reltype* reloc)
10794 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
10795
10796 // Return the type of the relocation.
10797 static inline unsigned int
10798 get_r_type(const Reltype* reloc)
10799 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
10800
10801 // Return the strategy to use for a local symbol which is not a
10802 // section symbol, given the relocation type.
10803 inline Relocatable_relocs::Reloc_strategy
10804 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
10805 {
10806 if (r_type == 0 && r_sym == 0)
10807 return Relocatable_relocs::RELOC_DISCARD;
10808 return Relocatable_relocs::RELOC_COPY;
10809 }
10810
10811 // Return the strategy to use for a local symbol which is a section
10812 // symbol, given the relocation type.
10813 inline Relocatable_relocs::Reloc_strategy
10814 local_section_strategy(unsigned int, Relobj*)
10815 {
10816 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
10817 }
10818
10819 // Return the strategy to use for a global symbol, given the
10820 // relocation type, the object, and the symbol index.
10821 inline Relocatable_relocs::Reloc_strategy
10822 global_strategy(unsigned int r_type, Relobj*, unsigned int)
10823 {
10824 if (size == 32
10825 && (r_type == elfcpp::R_PPC_PLTREL24
10826 || r_type == elfcpp::R_POWERPC_PLT16_LO
10827 || r_type == elfcpp::R_POWERPC_PLT16_HI
10828 || r_type == elfcpp::R_POWERPC_PLT16_HA))
10829 return Relocatable_relocs::RELOC_SPECIAL;
10830 return Relocatable_relocs::RELOC_COPY;
10831 }
10832 };
10833
10834 // Scan the relocs during a relocatable link.
10835
10836 template<int size, bool big_endian>
10837 void
10838 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
10839 Symbol_table* symtab,
10840 Layout* layout,
10841 Sized_relobj_file<size, big_endian>* object,
10842 unsigned int data_shndx,
10843 unsigned int sh_type,
10844 const unsigned char* prelocs,
10845 size_t reloc_count,
10846 Output_section* output_section,
10847 bool needs_special_offset_handling,
10848 size_t local_symbol_count,
10849 const unsigned char* plocal_symbols,
10850 Relocatable_relocs* rr)
10851 {
10852 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
10853
10854 gold_assert(sh_type == elfcpp::SHT_RELA);
10855
10856 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
10857 symtab,
10858 layout,
10859 object,
10860 data_shndx,
10861 prelocs,
10862 reloc_count,
10863 output_section,
10864 needs_special_offset_handling,
10865 local_symbol_count,
10866 plocal_symbols,
10867 rr);
10868 }
10869
10870 // Scan the relocs for --emit-relocs.
10871
10872 template<int size, bool big_endian>
10873 void
10874 Target_powerpc<size, big_endian>::emit_relocs_scan(
10875 Symbol_table* symtab,
10876 Layout* layout,
10877 Sized_relobj_file<size, big_endian>* object,
10878 unsigned int data_shndx,
10879 unsigned int sh_type,
10880 const unsigned char* prelocs,
10881 size_t reloc_count,
10882 Output_section* output_section,
10883 bool needs_special_offset_handling,
10884 size_t local_symbol_count,
10885 const unsigned char* plocal_syms,
10886 Relocatable_relocs* rr)
10887 {
10888 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10889 Classify_reloc;
10890 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10891 Emit_relocs_strategy;
10892
10893 gold_assert(sh_type == elfcpp::SHT_RELA);
10894
10895 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10896 symtab,
10897 layout,
10898 object,
10899 data_shndx,
10900 prelocs,
10901 reloc_count,
10902 output_section,
10903 needs_special_offset_handling,
10904 local_symbol_count,
10905 plocal_syms,
10906 rr);
10907 }
10908
10909 // Emit relocations for a section.
10910 // This is a modified version of the function by the same name in
10911 // target-reloc.h. Using relocate_special_relocatable for
10912 // R_PPC_PLTREL24 would require duplication of the entire body of the
10913 // loop, so we may as well duplicate the whole thing.
10914
10915 template<int size, bool big_endian>
10916 void
10917 Target_powerpc<size, big_endian>::relocate_relocs(
10918 const Relocate_info<size, big_endian>* relinfo,
10919 unsigned int sh_type,
10920 const unsigned char* prelocs,
10921 size_t reloc_count,
10922 Output_section* output_section,
10923 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10924 unsigned char*,
10925 Address view_address,
10926 section_size_type,
10927 unsigned char* reloc_view,
10928 section_size_type reloc_view_size)
10929 {
10930 gold_assert(sh_type == elfcpp::SHT_RELA);
10931
10932 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10933 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
10934 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10935 // Offset from start of insn to d-field reloc.
10936 const int d_offset = big_endian ? 2 : 0;
10937
10938 Powerpc_relobj<size, big_endian>* const object
10939 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10940 const unsigned int local_count = object->local_symbol_count();
10941 unsigned int got2_shndx = object->got2_shndx();
10942 Address got2_addend = 0;
10943 if (got2_shndx != 0)
10944 {
10945 got2_addend = object->get_output_section_offset(got2_shndx);
10946 gold_assert(got2_addend != invalid_address);
10947 }
10948
10949 const bool relocatable = parameters->options().relocatable();
10950
10951 unsigned char* pwrite = reloc_view;
10952 bool zap_next = false;
10953 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10954 {
10955 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
10956 if (strategy == Relocatable_relocs::RELOC_DISCARD)
10957 continue;
10958
10959 Reltype reloc(prelocs);
10960 Reltype_write reloc_write(pwrite);
10961
10962 Address offset = reloc.get_r_offset();
10963 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
10964 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10965 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10966 const unsigned int orig_r_sym = r_sym;
10967 typename elfcpp::Elf_types<size>::Elf_Swxword addend
10968 = reloc.get_r_addend();
10969 const Symbol* gsym = NULL;
10970
10971 if (zap_next)
10972 {
10973 // We could arrange to discard these and other relocs for
10974 // tls optimised sequences in the strategy methods, but for
10975 // now do as BFD ld does.
10976 r_type = elfcpp::R_POWERPC_NONE;
10977 zap_next = false;
10978 }
10979
10980 // Get the new symbol index.
10981 Output_section* os = NULL;
10982 if (r_sym < local_count)
10983 {
10984 switch (strategy)
10985 {
10986 case Relocatable_relocs::RELOC_COPY:
10987 case Relocatable_relocs::RELOC_SPECIAL:
10988 if (r_sym != 0)
10989 {
10990 r_sym = object->symtab_index(r_sym);
10991 gold_assert(r_sym != -1U);
10992 }
10993 break;
10994
10995 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
10996 {
10997 // We are adjusting a section symbol. We need to find
10998 // the symbol table index of the section symbol for
10999 // the output section corresponding to input section
11000 // in which this symbol is defined.
11001 gold_assert(r_sym < local_count);
11002 bool is_ordinary;
11003 unsigned int shndx =
11004 object->local_symbol_input_shndx(r_sym, &is_ordinary);
11005 gold_assert(is_ordinary);
11006 os = object->output_section(shndx);
11007 gold_assert(os != NULL);
11008 gold_assert(os->needs_symtab_index());
11009 r_sym = os->symtab_index();
11010 }
11011 break;
11012
11013 default:
11014 gold_unreachable();
11015 }
11016 }
11017 else
11018 {
11019 gsym = object->global_symbol(r_sym);
11020 gold_assert(gsym != NULL);
11021 if (gsym->is_forwarder())
11022 gsym = relinfo->symtab->resolve_forwards(gsym);
11023
11024 gold_assert(gsym->has_symtab_index());
11025 r_sym = gsym->symtab_index();
11026 }
11027
11028 // Get the new offset--the location in the output section where
11029 // this relocation should be applied.
11030 if (static_cast<Address>(offset_in_output_section) != invalid_address)
11031 offset += offset_in_output_section;
11032 else
11033 {
11034 section_offset_type sot_offset =
11035 convert_types<section_offset_type, Address>(offset);
11036 section_offset_type new_sot_offset =
11037 output_section->output_offset(object, relinfo->data_shndx,
11038 sot_offset);
11039 gold_assert(new_sot_offset != -1);
11040 offset = new_sot_offset;
11041 }
11042
11043 // In an object file, r_offset is an offset within the section.
11044 // In an executable or dynamic object, generated by
11045 // --emit-relocs, r_offset is an absolute address.
11046 if (!relocatable)
11047 {
11048 offset += view_address;
11049 if (static_cast<Address>(offset_in_output_section) != invalid_address)
11050 offset -= offset_in_output_section;
11051 }
11052
11053 // Handle the reloc addend based on the strategy.
11054 if (strategy == Relocatable_relocs::RELOC_COPY)
11055 ;
11056 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
11057 {
11058 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
11059 addend = psymval->value(object, addend);
11060 // In a relocatable link, the symbol value is relative to
11061 // the start of the output section. For a non-relocatable
11062 // link, we need to adjust the addend.
11063 if (!relocatable)
11064 {
11065 gold_assert(os != NULL);
11066 addend -= os->address();
11067 }
11068 }
11069 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
11070 {
11071 if (size == 32)
11072 {
11073 if (addend >= 32768)
11074 addend += got2_addend;
11075 }
11076 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
11077 {
11078 r_type = elfcpp::R_POWERPC_ADDR16_HA;
11079 addend -= d_offset;
11080 }
11081 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
11082 {
11083 r_type = elfcpp::R_POWERPC_ADDR16_LO;
11084 addend -= d_offset + 4;
11085 }
11086 }
11087 else
11088 gold_unreachable();
11089
11090 if (!relocatable)
11091 {
11092 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11093 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
11094 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
11095 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
11096 {
11097 // First instruction of a global dynamic sequence,
11098 // arg setup insn.
11099 const bool final = gsym == NULL || gsym->final_value_is_known();
11100 switch (this->optimize_tls_gd(final))
11101 {
11102 case tls::TLSOPT_TO_IE:
11103 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11104 - elfcpp::R_POWERPC_GOT_TLSGD16);
11105 break;
11106 case tls::TLSOPT_TO_LE:
11107 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11108 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11109 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11110 else
11111 {
11112 r_type = elfcpp::R_POWERPC_NONE;
11113 offset -= d_offset;
11114 }
11115 break;
11116 default:
11117 break;
11118 }
11119 }
11120 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11121 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11122 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11123 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
11124 {
11125 // First instruction of a local dynamic sequence,
11126 // arg setup insn.
11127 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
11128 {
11129 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11130 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11131 {
11132 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11133 const Output_section* os = relinfo->layout->tls_segment()
11134 ->first_section();
11135 gold_assert(os != NULL);
11136 gold_assert(os->needs_symtab_index());
11137 r_sym = os->symtab_index();
11138 addend = dtp_offset;
11139 }
11140 else
11141 {
11142 r_type = elfcpp::R_POWERPC_NONE;
11143 offset -= d_offset;
11144 }
11145 }
11146 }
11147 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11148 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11149 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11150 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
11151 {
11152 // First instruction of initial exec sequence.
11153 const bool final = gsym == NULL || gsym->final_value_is_known();
11154 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
11155 {
11156 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11157 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11158 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11159 else
11160 {
11161 r_type = elfcpp::R_POWERPC_NONE;
11162 offset -= d_offset;
11163 }
11164 }
11165 }
11166 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11167 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11168 {
11169 // Second instruction of a global dynamic sequence,
11170 // the __tls_get_addr call
11171 const bool final = gsym == NULL || gsym->final_value_is_known();
11172 switch (this->optimize_tls_gd(final))
11173 {
11174 case tls::TLSOPT_TO_IE:
11175 r_type = elfcpp::R_POWERPC_NONE;
11176 zap_next = true;
11177 break;
11178 case tls::TLSOPT_TO_LE:
11179 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11180 offset += d_offset;
11181 zap_next = true;
11182 break;
11183 default:
11184 break;
11185 }
11186 }
11187 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11188 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11189 {
11190 // Second instruction of a local dynamic sequence,
11191 // the __tls_get_addr call
11192 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
11193 {
11194 const Output_section* os = relinfo->layout->tls_segment()
11195 ->first_section();
11196 gold_assert(os != NULL);
11197 gold_assert(os->needs_symtab_index());
11198 r_sym = os->symtab_index();
11199 addend = dtp_offset;
11200 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11201 offset += d_offset;
11202 zap_next = true;
11203 }
11204 }
11205 else if (r_type == elfcpp::R_POWERPC_TLS)
11206 {
11207 // Second instruction of an initial exec sequence
11208 const bool final = gsym == NULL || gsym->final_value_is_known();
11209 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
11210 {
11211 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11212 offset += d_offset;
11213 }
11214 }
11215 }
11216
11217 reloc_write.put_r_offset(offset);
11218 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
11219 reloc_write.put_r_addend(addend);
11220
11221 pwrite += reloc_size;
11222 }
11223
11224 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
11225 == reloc_view_size);
11226 }
11227
11228 // Return the value to use for a dynamic symbol which requires special
11229 // treatment. This is how we support equality comparisons of function
11230 // pointers across shared library boundaries, as described in the
11231 // processor specific ABI supplement.
11232
11233 template<int size, bool big_endian>
11234 uint64_t
11235 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
11236 {
11237 if (size == 32)
11238 {
11239 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
11240 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11241 p != this->stub_tables_.end();
11242 ++p)
11243 {
11244 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11245 = (*p)->find_plt_call_entry(gsym);
11246 if (ent != NULL)
11247 return (*p)->stub_address() + ent->off_;
11248 }
11249 }
11250 else if (this->abiversion() >= 2)
11251 {
11252 Address off = this->glink_section()->find_global_entry(gsym);
11253 if (off != invalid_address)
11254 return this->glink_section()->global_entry_address() + off;
11255 }
11256 gold_unreachable();
11257 }
11258
11259 // Return the PLT address to use for a local symbol.
11260 template<int size, bool big_endian>
11261 uint64_t
11262 Target_powerpc<size, big_endian>::do_plt_address_for_local(
11263 const Relobj* object,
11264 unsigned int symndx) const
11265 {
11266 if (size == 32)
11267 {
11268 const Sized_relobj<size, big_endian>* relobj
11269 = static_cast<const Sized_relobj<size, big_endian>*>(object);
11270 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11271 p != this->stub_tables_.end();
11272 ++p)
11273 {
11274 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11275 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
11276 if (ent != NULL)
11277 return (*p)->stub_address() + ent->off_;
11278 }
11279 }
11280 gold_unreachable();
11281 }
11282
11283 // Return the PLT address to use for a global symbol.
11284 template<int size, bool big_endian>
11285 uint64_t
11286 Target_powerpc<size, big_endian>::do_plt_address_for_global(
11287 const Symbol* gsym) const
11288 {
11289 if (size == 32)
11290 {
11291 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11292 p != this->stub_tables_.end();
11293 ++p)
11294 {
11295 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11296 = (*p)->find_plt_call_entry(gsym);
11297 if (ent != NULL)
11298 return (*p)->stub_address() + ent->off_;
11299 }
11300 }
11301 else if (this->abiversion() >= 2)
11302 {
11303 Address off = this->glink_section()->find_global_entry(gsym);
11304 if (off != invalid_address)
11305 return this->glink_section()->global_entry_address() + off;
11306 }
11307 gold_unreachable();
11308 }
11309
11310 // Return the offset to use for the GOT_INDX'th got entry which is
11311 // for a local tls symbol specified by OBJECT, SYMNDX.
11312 template<int size, bool big_endian>
11313 int64_t
11314 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
11315 const Relobj* object,
11316 unsigned int symndx,
11317 unsigned int got_indx) const
11318 {
11319 const Powerpc_relobj<size, big_endian>* ppc_object
11320 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
11321 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
11322 {
11323 for (Got_type got_type = GOT_TYPE_TLSGD;
11324 got_type <= GOT_TYPE_TPREL;
11325 got_type = Got_type(got_type + 1))
11326 if (ppc_object->local_has_got_offset(symndx, got_type))
11327 {
11328 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
11329 if (got_type == GOT_TYPE_TLSGD)
11330 off += size / 8;
11331 if (off == got_indx * (size / 8))
11332 {
11333 if (got_type == GOT_TYPE_TPREL)
11334 return -tp_offset;
11335 else
11336 return -dtp_offset;
11337 }
11338 }
11339 }
11340 gold_unreachable();
11341 }
11342
11343 // Return the offset to use for the GOT_INDX'th got entry which is
11344 // for global tls symbol GSYM.
11345 template<int size, bool big_endian>
11346 int64_t
11347 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
11348 Symbol* gsym,
11349 unsigned int got_indx) const
11350 {
11351 if (gsym->type() == elfcpp::STT_TLS)
11352 {
11353 for (Got_type got_type = GOT_TYPE_TLSGD;
11354 got_type <= GOT_TYPE_TPREL;
11355 got_type = Got_type(got_type + 1))
11356 if (gsym->has_got_offset(got_type))
11357 {
11358 unsigned int off = gsym->got_offset(got_type);
11359 if (got_type == GOT_TYPE_TLSGD)
11360 off += size / 8;
11361 if (off == got_indx * (size / 8))
11362 {
11363 if (got_type == GOT_TYPE_TPREL)
11364 return -tp_offset;
11365 else
11366 return -dtp_offset;
11367 }
11368 }
11369 }
11370 gold_unreachable();
11371 }
11372
11373 // The selector for powerpc object files.
11374
11375 template<int size, bool big_endian>
11376 class Target_selector_powerpc : public Target_selector
11377 {
11378 public:
11379 Target_selector_powerpc()
11380 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
11381 size, big_endian,
11382 (size == 64
11383 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
11384 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
11385 (size == 64
11386 ? (big_endian ? "elf64ppc" : "elf64lppc")
11387 : (big_endian ? "elf32ppc" : "elf32lppc")))
11388 { }
11389
11390 virtual Target*
11391 do_instantiate_target()
11392 { return new Target_powerpc<size, big_endian>(); }
11393 };
11394
11395 Target_selector_powerpc<32, true> target_selector_ppc32;
11396 Target_selector_powerpc<32, false> target_selector_ppc32le;
11397 Target_selector_powerpc<64, true> target_selector_ppc64;
11398 Target_selector_powerpc<64, false> target_selector_ppc64le;
11399
11400 // Instantiate these constants for -O0
11401 template<int size, bool big_endian>
11402 const typename Output_data_glink<size, big_endian>::Address
11403 Output_data_glink<size, big_endian>::invalid_address;
11404 template<int size, bool big_endian>
11405 const typename Stub_table<size, big_endian>::Address
11406 Stub_table<size, big_endian>::invalid_address;
11407 template<int size, bool big_endian>
11408 const typename Target_powerpc<size, big_endian>::Address
11409 Target_powerpc<size, big_endian>::invalid_address;
11410
11411 } // End anonymous namespace.
This page took 0.253784 seconds and 5 git commands to generate.