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