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