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