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