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