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