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