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