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