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