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