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