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