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