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