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