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