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