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