gdb/testsuite:
[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
26#include "elfcpp.h"
27#include "parameters.h"
28#include "reloc.h"
29#include "powerpc.h"
30#include "object.h"
31#include "symtab.h"
32#include "layout.h"
33#include "output.h"
34#include "copy-relocs.h"
35#include "target.h"
36#include "target-reloc.h"
37#include "target-select.h"
38#include "tls.h"
39#include "errors.h"
f345227a 40#include "gc.h"
42cacb20
DE
41
42namespace
43{
44
45using namespace gold;
46
47template<int size, bool big_endian>
48class Output_data_plt_powerpc;
49
cf43a2fe
AM
50template<int size, bool big_endian>
51class Output_data_got_powerpc;
52
53template<int size, bool big_endian>
54class Output_data_glink;
55
56template<int size, bool big_endian>
57class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
58{
59public:
dd93cd0a 60 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
c9269dff 61 typedef typename elfcpp::Elf_types<size>::Elf_Off Offset;
e81fea4d
AM
62 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
63 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 64
cf43a2fe
AM
65 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
66 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
67 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
bfdfa4cd 68 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
cf43a2fe
AM
69 { }
70
71 ~Powerpc_relobj()
72 { }
73
c9269dff 74 // The .got2 section shndx.
cf43a2fe
AM
75 unsigned int
76 got2_shndx() const
77 {
78 if (size == 32)
c9269dff 79 return this->special_;
cf43a2fe
AM
80 else
81 return 0;
82 }
83
c9269dff
AM
84 // The .opd section shndx.
85 unsigned int
86 opd_shndx() const
87 {
88 if (size == 32)
89 return 0;
90 else
91 return this->special_;
92 }
93
94 // Init OPD entry arrays.
95 void
96 init_opd(size_t opd_size)
97 {
98 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 99 this->opd_ent_.resize(count);
c9269dff
AM
100 }
101
102 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
103 unsigned int
104 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
105 {
106 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
107 gold_assert(ndx < this->opd_ent_.size());
108 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 109 if (value != NULL)
bfdfa4cd
AM
110 *value = this->opd_ent_[ndx].off;
111 return this->opd_ent_[ndx].shndx;
c9269dff
AM
112 }
113
114 // Set section and offset of function entry for .opd + R_OFF.
115 void
dd93cd0a 116 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
117 {
118 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
119 gold_assert(ndx < this->opd_ent_.size());
120 this->opd_ent_[ndx].shndx = shndx;
121 this->opd_ent_[ndx].off = value;
122 }
123
124 // Return discard flag for .opd + R_OFF.
125 bool
126 get_opd_discard(Address r_off) const
127 {
128 size_t ndx = this->opd_ent_ndx(r_off);
129 gold_assert(ndx < this->opd_ent_.size());
130 return this->opd_ent_[ndx].discard;
131 }
132
133 // Set discard flag for .opd + R_OFF.
134 void
135 set_opd_discard(Address r_off)
136 {
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 this->opd_ent_[ndx].discard = true;
c9269dff
AM
140 }
141
e81fea4d
AM
142 Access_from*
143 access_from_map()
144 { return &this->access_from_map_; }
145
146 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
147 // section at DST_OFF.
148 void
149 add_reference(Object* src_obj,
150 unsigned int src_indx,
151 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
152 {
153 Section_id src_id(src_obj, src_indx);
154 this->access_from_map_[dst_off].insert(src_id);
155 }
156
157 bool
158 opd_valid() const
159 { return this->opd_valid_; }
160
161 void
162 set_opd_valid()
163 { this->opd_valid_ = true; }
164
c9269dff
AM
165 // Examine .rela.opd to build info about function entry points.
166 void
167 scan_opd_relocs(size_t reloc_count,
168 const unsigned char* prelocs,
169 const unsigned char* plocal_syms);
170
171 void
172 do_read_relocs(Read_relocs_data*);
173
cf43a2fe
AM
174 bool
175 do_find_special_sections(Read_symbols_data* sd);
176
dd93cd0a
AM
177 // Return offset in output GOT section that this object will use
178 // as a TOC pointer. Won't be just a constant with multi-toc support.
179 Address
180 toc_base_offset() const
181 { return 0x8000; }
182
cf43a2fe 183private:
bfdfa4cd
AM
184 struct Opd_ent
185 {
186 unsigned int shndx;
187 bool discard;
188 Offset off;
189 };
190
191 // Return index into opd_ent_ array for .opd entry at OFF.
192 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
193 // apart when the language doesn't use the last 8-byte word, the
194 // environment pointer. Thus dividing the entry section offset by
195 // 16 will give an index into opd_ent_ that works for either layout
196 // of .opd. (It leaves some elements of the vector unused when .opd
197 // entries are spaced 24 bytes apart, but we don't know the spacing
198 // until relocations are processed, and in any case it is possible
199 // for an object to have some entries spaced 16 bytes apart and
200 // others 24 bytes apart.)
c9269dff
AM
201 size_t
202 opd_ent_ndx(size_t off) const
203 { return off >> 4;}
204
205 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
206 unsigned int special_;
bfdfa4cd
AM
207
208 // Set at the start of gc_process_relocs, when we know opd_ent_
209 // vector is valid. The flag could be made atomic and set in
210 // do_read_relocs with memory_order_release and then tested with
211 // memory_order_acquire, potentially resulting in fewer entries in
212 // access_from_map_.
213 bool opd_valid_;
214
c9269dff
AM
215 // The first 8-byte word of an OPD entry gives the address of the
216 // entry point of the function. Relocatable object files have a
bfdfa4cd 217 // relocation on this word. The following vector records the
c9269dff 218 // section and offset specified by these relocations.
bfdfa4cd
AM
219 std::vector<Opd_ent> opd_ent_;
220
e81fea4d 221 // References made to this object's .opd section when running
bfdfa4cd
AM
222 // gc_process_relocs for another object, before the opd_ent_ vector
223 // is valid for this object.
e81fea4d 224 Access_from access_from_map_;
cf43a2fe
AM
225};
226
42cacb20
DE
227template<int size, bool big_endian>
228class Target_powerpc : public Sized_target<size, big_endian>
229{
230 public:
d83ce4e3
AM
231 typedef
232 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 233 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 234 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
c9269dff 235 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
236 // Offset of tp and dtp pointers from start of TLS block.
237 static const Address tp_offset = 0x7000;
238 static const Address dtp_offset = 0x8000;
42cacb20
DE
239
240 Target_powerpc()
241 : Sized_target<size, big_endian>(&powerpc_info),
cf43a2fe 242 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
42cacb20 243 copy_relocs_(elfcpp::R_POWERPC_COPY),
dd93cd0a 244 dynbss_(NULL), tlsld_got_offset_(-1U)
42cacb20
DE
245 {
246 }
247
2e702c99 248 // Process the relocations to determine unreferenced sections for
6d03d481
ST
249 // garbage collection.
250 void
ad0f2072 251 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
252 Layout* layout,
253 Sized_relobj_file<size, big_endian>* object,
254 unsigned int data_shndx,
255 unsigned int sh_type,
256 const unsigned char* prelocs,
257 size_t reloc_count,
258 Output_section* output_section,
259 bool needs_special_offset_handling,
260 size_t local_symbol_count,
261 const unsigned char* plocal_symbols);
6d03d481 262
42cacb20
DE
263 // Scan the relocations to look for symbol adjustments.
264 void
ad0f2072 265 scan_relocs(Symbol_table* symtab,
42cacb20 266 Layout* layout,
6fa2a40b 267 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
268 unsigned int data_shndx,
269 unsigned int sh_type,
270 const unsigned char* prelocs,
271 size_t reloc_count,
272 Output_section* output_section,
273 bool needs_special_offset_handling,
274 size_t local_symbol_count,
275 const unsigned char* plocal_symbols);
921b5322
AM
276
277 // Map input .toc section to output .got section.
278 const char*
279 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
280 {
281 if (size == 64 && strcmp(name, ".toc") == 0)
282 {
283 *plen = 4;
284 return ".got";
285 }
286 return NULL;
287 }
288
42cacb20
DE
289 // Finalize the sections.
290 void
f59f41f3 291 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
292
293 // Return the value to use for a dynamic which requires special
294 // treatment.
295 uint64_t
296 do_dynsym_value(const Symbol*) const;
297
bd73a62d
AM
298 // Return the offset to use for the GOT_INDX'th got entry which is
299 // for a local tls symbol specified by OBJECT, SYMNDX.
300 int64_t
301 do_tls_offset_for_local(const Relobj* object,
302 unsigned int symndx,
303 unsigned int got_indx) const;
304
305 // Return the offset to use for the GOT_INDX'th got entry which is
306 // for global tls symbol GSYM.
307 int64_t
308 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
309
42cacb20
DE
310 // Relocate a section.
311 void
312 relocate_section(const Relocate_info<size, big_endian>*,
313 unsigned int sh_type,
314 const unsigned char* prelocs,
315 size_t reloc_count,
316 Output_section* output_section,
317 bool needs_special_offset_handling,
318 unsigned char* view,
c9269dff 319 Address view_address,
364c7fa5
ILT
320 section_size_type view_size,
321 const Reloc_symbol_changes*);
42cacb20
DE
322
323 // Scan the relocs during a relocatable link.
324 void
ad0f2072 325 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 326 Layout* layout,
6fa2a40b 327 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
328 unsigned int data_shndx,
329 unsigned int sh_type,
330 const unsigned char* prelocs,
331 size_t reloc_count,
332 Output_section* output_section,
333 bool needs_special_offset_handling,
334 size_t local_symbol_count,
335 const unsigned char* plocal_symbols,
336 Relocatable_relocs*);
337
7404fe1b 338 // Emit relocations for a section.
42cacb20 339 void
7404fe1b
AM
340 relocate_relocs(const Relocate_info<size, big_endian>*,
341 unsigned int sh_type,
342 const unsigned char* prelocs,
343 size_t reloc_count,
344 Output_section* output_section,
345 off_t offset_in_output_section,
346 const Relocatable_relocs*,
347 unsigned char*,
348 Address view_address,
349 section_size_type,
350 unsigned char* reloc_view,
351 section_size_type reloc_view_size);
42cacb20
DE
352
353 // Return whether SYM is defined by the ABI.
354 bool
9c2d0ef9 355 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 356 {
cf43a2fe 357 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
358 }
359
360 // Return the size of the GOT section.
361 section_size_type
0e70b911 362 got_size() const
42cacb20
DE
363 {
364 gold_assert(this->got_ != NULL);
365 return this->got_->data_size();
366 }
367
cf43a2fe
AM
368 // Get the PLT section.
369 const Output_data_plt_powerpc<size, big_endian>*
370 plt_section() const
371 {
372 gold_assert(this->plt_ != NULL);
373 return this->plt_;
374 }
375
376 // Get the .glink section.
377 const Output_data_glink<size, big_endian>*
378 glink_section() const
379 {
380 gold_assert(this->glink_ != NULL);
381 return this->glink_;
382 }
383
384 // Get the GOT section.
385 const Output_data_got_powerpc<size, big_endian>*
386 got_section() const
387 {
388 gold_assert(this->got_ != NULL);
389 return this->got_;
390 }
391
cf43a2fe
AM
392 Object*
393 do_make_elf_object(const std::string&, Input_file*, off_t,
394 const elfcpp::Ehdr<size, big_endian>&);
395
0e70b911
CC
396 // Return the number of entries in the GOT.
397 unsigned int
398 got_entry_count() const
399 {
400 if (this->got_ == NULL)
401 return 0;
402 return this->got_size() / (size / 8);
403 }
404
405 // Return the number of entries in the PLT.
406 unsigned int
407 plt_entry_count() const;
408
409 // Return the offset of the first non-reserved PLT entry.
410 unsigned int
411 first_plt_entry_offset() const;
412
413 // Return the size of each PLT entry.
414 unsigned int
415 plt_entry_size() const;
416
e81fea4d
AM
417 // Add any special sections for this symbol to the gc work list.
418 // For powerpc64, this adds the code section of a function
419 // descriptor.
420 void
421 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
422
423 // Handle target specific gc actions when adding a gc reference from
424 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
425 // and DST_OFF. For powerpc64, this adds a referenc to the code
426 // section of a function descriptor.
427 void
428 do_gc_add_reference(Symbol_table* symtab,
429 Object* src_obj,
430 unsigned int src_shndx,
431 Object* dst_obj,
432 unsigned int dst_shndx,
433 Address dst_off) const;
434
42cacb20
DE
435 private:
436
437 // The class which scans relocations.
438 class Scan
439 {
440 public:
bfdfa4cd
AM
441 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
442
42cacb20
DE
443 Scan()
444 : issued_non_pic_error_(false)
445 { }
446
95a2c8d6
RS
447 static inline int
448 get_reference_flags(unsigned int r_type);
449
42cacb20 450 inline void
ad0f2072 451 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 452 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
453 unsigned int data_shndx,
454 Output_section* output_section,
455 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
456 const elfcpp::Sym<size, big_endian>& lsym,
457 bool is_discarded);
42cacb20
DE
458
459 inline void
ad0f2072 460 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 461 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
462 unsigned int data_shndx,
463 Output_section* output_section,
464 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
465 Symbol* gsym);
466
21bb3914
ST
467 inline bool
468 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
469 Target_powerpc* ,
2e702c99 470 Sized_relobj_file<size, big_endian>* ,
21bb3914 471 unsigned int ,
2e702c99
RM
472 Output_section* ,
473 const elfcpp::Rela<size, big_endian>& ,
474 unsigned int ,
475 const elfcpp::Sym<size, big_endian>&)
21bb3914
ST
476 { return false; }
477
478 inline bool
479 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
480 Target_powerpc* ,
2e702c99
RM
481 Sized_relobj_file<size, big_endian>* ,
482 unsigned int ,
483 Output_section* ,
484 const elfcpp::Rela<size,
21bb3914
ST
485 big_endian>& ,
486 unsigned int , Symbol*)
487 { return false; }
488
42cacb20
DE
489 private:
490 static void
6fa2a40b 491 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
492 unsigned int r_type);
493
494 static void
6fa2a40b 495 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
496 unsigned int r_type, Symbol*);
497
498 static void
499 generate_tls_call(Symbol_table* symtab, Layout* layout,
500 Target_powerpc* target);
501
502 void
503 check_non_pic(Relobj*, unsigned int r_type);
504
505 // Whether we have issued an error about a non-PIC compilation.
506 bool issued_non_pic_error_;
507 };
508
3ea0a085
AM
509 Address
510 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
511 Powerpc_relobj<size, big_endian>* object,
512 unsigned int *dest_shndx);
513
42cacb20
DE
514 // The class which implements relocation.
515 class Relocate
516 {
517 public:
dd93cd0a
AM
518 // Use 'at' branch hints when true, 'y' when false.
519 // FIXME maybe: set this with an option.
520 static const bool is_isa_v2 = true;
521
522 enum skip_tls
523 {
524 CALL_NOT_EXPECTED = 0,
525 CALL_EXPECTED = 1,
526 CALL_SKIP = 2
527 };
528
529 Relocate()
530 : call_tls_get_addr_(CALL_NOT_EXPECTED)
531 { }
532
533 ~Relocate()
534 {
535 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
536 {
537 // FIXME: This needs to specify the location somehow.
538 gold_error(_("missing expected __tls_get_addr call"));
539 }
540 }
541
42cacb20
DE
542 // Do a relocation. Return false if the caller should not issue
543 // any warnings about this relocation.
544 inline bool
545 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
031cdbed
ILT
546 Output_section*, size_t relnum,
547 const elfcpp::Rela<size, big_endian>&,
42cacb20
DE
548 unsigned int r_type, const Sized_symbol<size>*,
549 const Symbol_value<size>*,
550 unsigned char*,
551 typename elfcpp::Elf_types<size>::Elf_Addr,
552 section_size_type);
553
dd93cd0a
AM
554 // This is set if we should skip the next reloc, which should be a
555 // call to __tls_get_addr.
556 enum skip_tls call_tls_get_addr_;
42cacb20
DE
557 };
558
559 // A class which returns the size required for a relocation type,
560 // used while scanning relocs during a relocatable link.
561 class Relocatable_size_for_reloc
562 {
563 public:
564 unsigned int
cf43a2fe
AM
565 get_size_for_reloc(unsigned int, Relobj*)
566 {
567 gold_unreachable();
568 return 0;
569 }
42cacb20
DE
570 };
571
dd93cd0a
AM
572 // Optimize the TLS relocation type based on what we know about the
573 // symbol. IS_FINAL is true if the final address of this symbol is
574 // known at link time.
575
576 tls::Tls_optimization
577 optimize_tls_gd(bool is_final)
578 {
579 // If we are generating a shared library, then we can't do anything
580 // in the linker.
581 if (parameters->options().shared())
582 return tls::TLSOPT_NONE;
583
584 if (!is_final)
585 return tls::TLSOPT_TO_IE;
586 return tls::TLSOPT_TO_LE;
587 }
588
589 tls::Tls_optimization
590 optimize_tls_ld()
591 {
592 if (parameters->options().shared())
593 return tls::TLSOPT_NONE;
594
595 return tls::TLSOPT_TO_LE;
596 }
597
598 tls::Tls_optimization
599 optimize_tls_ie(bool is_final)
600 {
601 if (!is_final || parameters->options().shared())
602 return tls::TLSOPT_NONE;
603
604 return tls::TLSOPT_TO_LE;
605 }
cf43a2fe 606
42cacb20 607 // Get the GOT section, creating it if necessary.
cf43a2fe 608 Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
609 got_section(Symbol_table*, Layout*);
610
cf43a2fe
AM
611 // Create glink.
612 void
613 make_glink_section(Layout*);
42cacb20 614
cf43a2fe
AM
615 // Create the PLT section.
616 void
617 make_plt_section(Layout*);
42cacb20
DE
618
619 // Create a PLT entry for a global symbol.
620 void
cf43a2fe
AM
621 make_plt_entry(Layout*, Symbol*,
622 const elfcpp::Rela<size, big_endian>&,
623 const Sized_relobj<size, big_endian>* object);
42cacb20 624
dd93cd0a
AM
625 // Create a GOT entry for local dynamic __tls_get_addr.
626 unsigned int
627 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
628 Sized_relobj_file<size, big_endian>* object);
629
42cacb20 630 unsigned int
dd93cd0a
AM
631 tlsld_got_offset() const
632 {
633 return this->tlsld_got_offset_;
634 }
42cacb20 635
42cacb20
DE
636 // Get the dynamic reloc section, creating it if necessary.
637 Reloc_section*
638 rela_dyn_section(Layout*);
639
42cacb20
DE
640 // Copy a relocation against a global symbol.
641 void
ef9beddf 642 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 643 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
644 unsigned int shndx, Output_section* output_section,
645 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
646 {
647 this->copy_relocs_.copy_reloc(symtab, layout,
648 symtab->get_sized_symbol<size>(sym),
649 object, shndx, output_section,
650 reloc, this->rela_dyn_section(layout));
651 }
652
653 // Information about this specific target which we pass to the
654 // general Target structure.
655 static Target::Target_info powerpc_info;
656
657 // The types of GOT entries needed for this platform.
0e70b911
CC
658 // These values are exposed to the ABI in an incremental link.
659 // Do not renumber existing values without changing the version
660 // number of the .gnu_incremental_inputs section.
42cacb20
DE
661 enum Got_type
662 {
dd93cd0a
AM
663 GOT_TYPE_STANDARD,
664 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
665 GOT_TYPE_DTPREL, // entry for @got@dtprel
666 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
667 };
668
cf43a2fe
AM
669 // The GOT output section.
670 Output_data_got_powerpc<size, big_endian>* got_;
671 // The PLT output section.
42cacb20 672 Output_data_plt_powerpc<size, big_endian>* plt_;
cf43a2fe
AM
673 // The .glink output section.
674 Output_data_glink<size, big_endian>* glink_;
675 // The dynamic reloc output section.
42cacb20
DE
676 Reloc_section* rela_dyn_;
677 // Relocs saved to avoid a COPY reloc.
678 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
679 // Space for variables copied with a COPY reloc.
680 Output_data_space* dynbss_;
dd93cd0a
AM
681 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
682 unsigned int tlsld_got_offset_;
42cacb20
DE
683};
684
685template<>
686Target::Target_info Target_powerpc<32, true>::powerpc_info =
687{
688 32, // size
689 true, // is_big_endian
690 elfcpp::EM_PPC, // machine_code
691 false, // has_make_symbol
692 false, // has_resolve
693 false, // has_code_fill
694 true, // is_default_stack_executable
b3ce541e 695 false, // can_icf_inline_merge_sections
42cacb20
DE
696 '\0', // wrap_char
697 "/usr/lib/ld.so.1", // dynamic_linker
698 0x10000000, // default_text_segment_address
699 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 700 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
701 false, // isolate_execinstr
702 0, // rosegment_gap
8a5e3e08
ILT
703 elfcpp::SHN_UNDEF, // small_common_shndx
704 elfcpp::SHN_UNDEF, // large_common_shndx
705 0, // small_common_section_flags
05a352e6
DK
706 0, // large_common_section_flags
707 NULL, // attributes_section
708 NULL // attributes_vendor
42cacb20
DE
709};
710
711template<>
712Target::Target_info Target_powerpc<32, false>::powerpc_info =
713{
714 32, // size
715 false, // is_big_endian
716 elfcpp::EM_PPC, // machine_code
717 false, // has_make_symbol
718 false, // has_resolve
719 false, // has_code_fill
720 true, // is_default_stack_executable
b3ce541e 721 false, // can_icf_inline_merge_sections
42cacb20
DE
722 '\0', // wrap_char
723 "/usr/lib/ld.so.1", // dynamic_linker
724 0x10000000, // default_text_segment_address
725 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 726 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
727 false, // isolate_execinstr
728 0, // rosegment_gap
8a5e3e08
ILT
729 elfcpp::SHN_UNDEF, // small_common_shndx
730 elfcpp::SHN_UNDEF, // large_common_shndx
731 0, // small_common_section_flags
05a352e6
DK
732 0, // large_common_section_flags
733 NULL, // attributes_section
734 NULL // attributes_vendor
42cacb20
DE
735};
736
737template<>
738Target::Target_info Target_powerpc<64, true>::powerpc_info =
739{
740 64, // size
741 true, // is_big_endian
742 elfcpp::EM_PPC64, // machine_code
743 false, // has_make_symbol
744 false, // has_resolve
745 false, // has_code_fill
746 true, // is_default_stack_executable
b3ce541e 747 false, // can_icf_inline_merge_sections
42cacb20
DE
748 '\0', // wrap_char
749 "/usr/lib/ld.so.1", // dynamic_linker
750 0x10000000, // default_text_segment_address
751 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 752 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
753 false, // isolate_execinstr
754 0, // rosegment_gap
8a5e3e08
ILT
755 elfcpp::SHN_UNDEF, // small_common_shndx
756 elfcpp::SHN_UNDEF, // large_common_shndx
757 0, // small_common_section_flags
05a352e6
DK
758 0, // large_common_section_flags
759 NULL, // attributes_section
760 NULL // attributes_vendor
42cacb20
DE
761};
762
763template<>
764Target::Target_info Target_powerpc<64, false>::powerpc_info =
765{
766 64, // size
767 false, // is_big_endian
768 elfcpp::EM_PPC64, // machine_code
769 false, // has_make_symbol
770 false, // has_resolve
771 false, // has_code_fill
772 true, // is_default_stack_executable
b3ce541e 773 false, // can_icf_inline_merge_sections
42cacb20
DE
774 '\0', // wrap_char
775 "/usr/lib/ld.so.1", // dynamic_linker
776 0x10000000, // default_text_segment_address
777 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 778 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
779 false, // isolate_execinstr
780 0, // rosegment_gap
8a5e3e08
ILT
781 elfcpp::SHN_UNDEF, // small_common_shndx
782 elfcpp::SHN_UNDEF, // large_common_shndx
783 0, // small_common_section_flags
05a352e6
DK
784 0, // large_common_section_flags
785 NULL, // attributes_section
786 NULL // attributes_vendor
42cacb20
DE
787};
788
dd93cd0a
AM
789inline bool
790is_branch_reloc(unsigned int r_type)
791{
792 return (r_type == elfcpp::R_POWERPC_REL24
793 || r_type == elfcpp::R_PPC_PLTREL24
794 || r_type == elfcpp::R_PPC_LOCAL24PC
795 || r_type == elfcpp::R_POWERPC_REL14
796 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
797 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
798 || r_type == elfcpp::R_POWERPC_ADDR24
799 || r_type == elfcpp::R_POWERPC_ADDR14
800 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
801 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
802}
803
804// If INSN is an opcode that may be used with an @tls operand, return
805// the transformed insn for TLS optimisation, otherwise return 0. If
806// REG is non-zero only match an insn with RB or RA equal to REG.
807uint32_t
808at_tls_transform(uint32_t insn, unsigned int reg)
809{
810 if ((insn & (0x3f << 26)) != 31 << 26)
811 return 0;
812
813 unsigned int rtra;
814 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
815 rtra = insn & ((1 << 26) - (1 << 16));
816 else if (((insn >> 16) & 0x1f) == reg)
817 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
818 else
819 return 0;
820
821 if ((insn & (0x3ff << 1)) == 266 << 1)
822 // add -> addi
823 insn = 14 << 26;
824 else if ((insn & (0x1f << 1)) == 23 << 1
825 && ((insn & (0x1f << 6)) < 14 << 6
826 || ((insn & (0x1f << 6)) >= 16 << 6
827 && (insn & (0x1f << 6)) < 24 << 6)))
828 // load and store indexed -> dform
829 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
830 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
831 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
832 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
833 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
834 // lwax -> lwa
835 insn = (58 << 26) | 2;
836 else
837 return 0;
838 insn |= rtra;
839 return insn;
840}
841
842// Modified version of symtab.h class Symbol member
843// Given a direct absolute or pc-relative static relocation against
844// the global symbol, this function returns whether a dynamic relocation
845// is needed.
846
847template<int size>
848bool
849needs_dynamic_reloc(const Symbol* gsym, int flags)
850{
851 // No dynamic relocations in a static link!
852 if (parameters->doing_static_link())
853 return false;
854
855 // A reference to an undefined symbol from an executable should be
856 // statically resolved to 0, and does not need a dynamic relocation.
857 // This matches gnu ld behavior.
858 if (gsym->is_undefined() && !parameters->options().shared())
859 return false;
860
861 // A reference to an absolute symbol does not need a dynamic relocation.
862 if (gsym->is_absolute())
863 return false;
864
865 // An absolute reference within a position-independent output file
866 // will need a dynamic relocation.
867 if ((flags & Symbol::ABSOLUTE_REF)
868 && parameters->options().output_is_position_independent())
869 return true;
870
871 // A function call that can branch to a local PLT entry does not need
872 // a dynamic relocation.
873 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
874 return false;
875
876 // A reference to any PLT entry in a non-position-independent executable
877 // does not need a dynamic relocation.
878 // Except due to having function descriptors on powerpc64 we don't define
879 // functions to their plt code in an executable, so this doesn't apply.
880 if (size == 32
881 && !parameters->options().output_is_position_independent()
882 && gsym->has_plt_offset())
883 return false;
884
885 // A reference to a symbol defined in a dynamic object or to a
886 // symbol that is preemptible will need a dynamic relocation.
887 if (gsym->is_from_dynobj()
888 || gsym->is_undefined()
889 || gsym->is_preemptible())
890 return true;
891
892 // For all other cases, return FALSE.
893 return false;
894}
895
896// Modified version of symtab.h class Symbol member
897// Whether we should use the PLT offset associated with a symbol for
898// a relocation. FLAGS is a set of Reference_flags.
899
900template<int size>
901bool
902use_plt_offset(const Symbol* gsym, int flags)
903{
904 // If the symbol doesn't have a PLT offset, then naturally we
905 // don't want to use it.
906 if (!gsym->has_plt_offset())
907 return false;
908
909 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
910 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
911 return true;
912
913 // If we are going to generate a dynamic relocation, then we will
914 // wind up using that, so no need to use the PLT entry.
915 if (needs_dynamic_reloc<size>(gsym, flags))
916 return false;
917
918 // If the symbol is from a dynamic object, we need to use the PLT
919 // entry.
920 if (gsym->is_from_dynobj())
921 return true;
922
923 // If we are generating a shared object, and gsym symbol is
924 // undefined or preemptible, we need to use the PLT entry.
925 if (parameters->options().shared()
926 && (gsym->is_undefined() || gsym->is_preemptible()))
927 return true;
928
929 // If gsym is a call to a weak undefined symbol, we need to use
930 // the PLT entry; the symbol may be defined by a library loaded
931 // at runtime.
932 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
933 return true;
934
935 // Otherwise we can use the regular definition.
936 return false;
937}
938
42cacb20
DE
939template<int size, bool big_endian>
940class Powerpc_relocate_functions
941{
dd93cd0a 942public:
f4baf0d4 943 enum Overflow_check
dd93cd0a 944 {
f4baf0d4
AM
945 CHECK_NONE,
946 CHECK_SIGNED,
947 CHECK_BITFIELD
dd93cd0a
AM
948 };
949
f4baf0d4 950 enum Status
dd93cd0a 951 {
f4baf0d4
AM
952 STATUS_OK,
953 STATUS_OVERFLOW
954 };
dd93cd0a 955
42cacb20 956private:
c9269dff 957 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff
AM
958 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
959
dd93cd0a
AM
960 template<int valsize>
961 static inline bool
962 has_overflow_signed(Address value)
963 {
964 // limit = 1 << (valsize - 1) without shift count exceeding size of type
965 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
966 limit <<= ((valsize - 1) >> 1);
967 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
968 return value + limit > (limit << 1) - 1;
969 }
970
971 template<int valsize>
972 static inline bool
973 has_overflow_bitfield(Address value)
974 {
975 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
976 limit <<= ((valsize - 1) >> 1);
977 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
978 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
979 }
980
981 template<int valsize>
f4baf0d4
AM
982 static inline Status
983 overflowed(Address value, Overflow_check overflow)
dd93cd0a 984 {
f4baf0d4 985 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
986 {
987 if (has_overflow_signed<valsize>(value))
f4baf0d4 988 return STATUS_OVERFLOW;
dd93cd0a 989 }
f4baf0d4 990 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
991 {
992 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 993 return STATUS_OVERFLOW;
dd93cd0a 994 }
f4baf0d4 995 return STATUS_OK;
dd93cd0a
AM
996 }
997
cf43a2fe 998 // Do a simple RELA relocation
42cacb20 999 template<int valsize>
f4baf0d4
AM
1000 static inline Status
1001 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a
AM
1002 {
1003 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1004 Valtype* wv = reinterpret_cast<Valtype*>(view);
1005 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1006 return overflowed<valsize>(value, overflow);
1007 }
1008
1009 template<int valsize>
f4baf0d4 1010 static inline Status
42cacb20
DE
1011 rela(unsigned char* view,
1012 unsigned int right_shift,
c9269dff
AM
1013 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1014 Address value,
f4baf0d4 1015 Overflow_check overflow)
42cacb20
DE
1016 {
1017 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1018 Valtype* wv = reinterpret_cast<Valtype*>(view);
1019 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
dd93cd0a 1020 Valtype reloc = value >> right_shift;
42cacb20
DE
1021 val &= ~dst_mask;
1022 reloc &= dst_mask;
42cacb20 1023 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
dd93cd0a 1024 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1025 }
1026
cf43a2fe 1027 // Do a simple RELA relocation, unaligned.
42cacb20 1028 template<int valsize>
f4baf0d4
AM
1029 static inline Status
1030 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a
AM
1031 {
1032 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1033 return overflowed<valsize>(value, overflow);
1034 }
1035
1036 template<int valsize>
f4baf0d4 1037 static inline Status
cf43a2fe
AM
1038 rela_ua(unsigned char* view,
1039 unsigned int right_shift,
c9269dff
AM
1040 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1041 Address value,
f4baf0d4 1042 Overflow_check overflow)
42cacb20 1043 {
c9269dff
AM
1044 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1045 Valtype;
dd93cd0a
AM
1046 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1047 Valtype reloc = value >> right_shift;
42cacb20
DE
1048 val &= ~dst_mask;
1049 reloc &= dst_mask;
dd93cd0a
AM
1050 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1051 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1052 }
1053
42cacb20 1054public:
dd93cd0a 1055 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 1056 static inline void
dd93cd0a 1057 addr64(unsigned char* view, Address value)
f4baf0d4 1058 { This::template rela<64>(view, value, CHECK_NONE); }
42cacb20 1059
dd93cd0a 1060 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 1061 static inline void
dd93cd0a 1062 addr64_u(unsigned char* view, Address value)
f4baf0d4 1063 { This::template rela_ua<64>(view, value, CHECK_NONE); }
dd93cd0a
AM
1064
1065 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
1066 static inline Status
1067 addr32(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a
AM
1068 { return This::template rela<32>(view, value, overflow); }
1069
1070 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
1071 static inline Status
1072 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a
AM
1073 { return This::template rela_ua<32>(view, value, overflow); }
1074
1075 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
1076 static inline Status
1077 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1078 {
f4baf0d4
AM
1079 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1080 if (overflow != CHECK_NONE && (value & 3) != 0)
1081 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1082 return stat;
1083 }
42cacb20
DE
1084
1085 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
1086 static inline Status
1087 addr16(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1088 { return This::template rela<16>(view, value, overflow); }
42cacb20 1089
dd93cd0a 1090 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
1091 static inline Status
1092 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1093 { return This::template rela_ua<16>(view, value, overflow); }
42cacb20 1094
dd93cd0a 1095 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1096 static inline Status
1097 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1098 {
f4baf0d4
AM
1099 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1100 if (overflow != CHECK_NONE && (value & 3) != 0)
1101 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1102 return stat;
1103 }
42cacb20 1104
42cacb20
DE
1105 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1106 static inline void
dd93cd0a 1107 addr16_hi(unsigned char* view, Address value)
f4baf0d4 1108 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 1109
c9269dff 1110 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 1111 static inline void
dd93cd0a
AM
1112 addr16_ha(unsigned char* view, Address value)
1113 { This::addr16_hi(view, value + 0x8000); }
42cacb20 1114
dd93cd0a 1115 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 1116 static inline void
dd93cd0a 1117 addr16_hi2(unsigned char* view, Address value)
f4baf0d4 1118 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 1119
dd93cd0a 1120 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 1121 static inline void
dd93cd0a
AM
1122 addr16_ha2(unsigned char* view, Address value)
1123 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 1124
dd93cd0a 1125 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 1126 static inline void
dd93cd0a 1127 addr16_hi3(unsigned char* view, Address value)
f4baf0d4 1128 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 1129
dd93cd0a 1130 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 1131 static inline void
dd93cd0a
AM
1132 addr16_ha3(unsigned char* view, Address value)
1133 { This::addr16_hi3(view, value + 0x8000); }
1134
1135 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1136 static inline Status
1137 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1138 {
f4baf0d4
AM
1139 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1140 if (overflow != CHECK_NONE && (value & 3) != 0)
1141 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1142 return stat;
1143 }
cf43a2fe
AM
1144};
1145
c9269dff
AM
1146// Stash away the index of .got2 or .opd in a relocatable object, if
1147// such a section exists.
cf43a2fe
AM
1148
1149template<int size, bool big_endian>
1150bool
1151Powerpc_relobj<size, big_endian>::do_find_special_sections(
1152 Read_symbols_data* sd)
1153{
c9269dff
AM
1154 const unsigned char* const pshdrs = sd->section_headers->data();
1155 const unsigned char* namesu = sd->section_names->data();
1156 const char* names = reinterpret_cast<const char*>(namesu);
1157 section_size_type names_size = sd->section_names_size;
1158 const unsigned char* s;
1159
1160 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1161 names, names_size, NULL);
1162 if (s != NULL)
1163 {
1164 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1165 this->special_ = ndx;
1166 }
1167 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1168}
1169
1170// Examine .rela.opd to build info about function entry points.
1171
1172template<int size, bool big_endian>
1173void
1174Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1175 size_t reloc_count,
1176 const unsigned char* prelocs,
1177 const unsigned char* plocal_syms)
1178{
1179 if (size == 64)
cf43a2fe 1180 {
c9269dff
AM
1181 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1182 Reltype;
1183 const int reloc_size
1184 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1185 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1186
1187 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 1188 {
c9269dff
AM
1189 Reltype reloc(prelocs);
1190 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1191 = reloc.get_r_info();
1192 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1193 if (r_type == elfcpp::R_PPC64_ADDR64)
1194 {
1195 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1196 typename elfcpp::Elf_types<size>::Elf_Addr value;
1197 bool is_ordinary;
1198 unsigned int shndx;
1199 if (r_sym < this->local_symbol_count())
1200 {
1201 typename elfcpp::Sym<size, big_endian>
1202 lsym(plocal_syms + r_sym * sym_size);
1203 shndx = lsym.get_st_shndx();
1204 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1205 value = lsym.get_st_value();
1206 }
1207 else
1208 shndx = this->symbol_section_and_value(r_sym, &value,
1209 &is_ordinary);
1210 this->set_opd_ent(reloc.get_r_offset(), shndx,
1211 value + reloc.get_r_addend());
1212 }
1213 }
1214 }
1215}
1216
1217template<int size, bool big_endian>
1218void
1219Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1220{
1221 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1222 if (size == 64)
1223 {
1224 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1225 p != rd->relocs.end();
1226 ++p)
1227 {
1228 if (p->data_shndx == this->opd_shndx())
1229 {
1230 this->init_opd(this->section_size(this->opd_shndx()));
1231 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1232 rd->local_symbols->data());
1233 break;
1234 }
cf43a2fe
AM
1235 }
1236 }
cf43a2fe
AM
1237}
1238
1239// Set up PowerPC target specific relobj.
1240
1241template<int size, bool big_endian>
1242Object*
1243Target_powerpc<size, big_endian>::do_make_elf_object(
1244 const std::string& name,
1245 Input_file* input_file,
1246 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1247{
1248 int et = ehdr.get_e_type();
957564c9
AS
1249 // ET_EXEC files are valid input for --just-symbols/-R,
1250 // and we treat them as relocatable objects.
1251 if (et == elfcpp::ET_REL
1252 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
1253 {
1254 Powerpc_relobj<size, big_endian>* obj =
c9269dff 1255 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
1256 obj->setup();
1257 return obj;
1258 }
1259 else if (et == elfcpp::ET_DYN)
1260 {
1261 Sized_dynobj<size, big_endian>* obj =
c9269dff 1262 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
1263 obj->setup();
1264 return obj;
1265 }
1266 else
1267 {
c9269dff 1268 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
1269 return NULL;
1270 }
1271}
1272
1273template<int size, bool big_endian>
1274class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1275{
1276public:
1277 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1278 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1279
1280 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1281 : Output_data_got<size, big_endian>(),
1282 symtab_(symtab), layout_(layout),
1283 header_ent_cnt_(size == 32 ? 3 : 1),
1284 header_index_(size == 32 ? 0x2000 : 0)
1285 {}
1286
1287 class Got_entry;
1288
1289 // Create a new GOT entry and return its offset.
1290 unsigned int
1291 add_got_entry(Got_entry got_entry)
42cacb20 1292 {
cf43a2fe
AM
1293 this->reserve_ent();
1294 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1295 }
42cacb20 1296
cf43a2fe
AM
1297 // Create a pair of new GOT entries and return the offset of the first.
1298 unsigned int
1299 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1300 {
1301 this->reserve_ent(2);
1302 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1303 got_entry_2);
1304 }
42cacb20 1305
dd93cd0a
AM
1306 unsigned int
1307 add_constant_pair(Valtype c1, Valtype c2)
1308 {
1309 this->reserve_ent(2);
1310 unsigned int got_offset = this->add_constant(c1);
1311 this->add_constant(c2);
1312 return got_offset;
1313 }
1314
1315 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
1316 unsigned int
1317 g_o_t() const
1318 {
1319 return this->got_offset(this->header_index_);
42cacb20 1320 }
cf43a2fe 1321
dd93cd0a
AM
1322 // Offset of base used to access the GOT/TOC.
1323 // The got/toc pointer reg will be set to this value.
1324 typename elfcpp::Elf_types<size>::Elf_Off
1325 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1326 {
1327 if (size == 32)
1328 return this->g_o_t();
1329 else
1330 return (this->output_section()->address()
1331 + object->toc_base_offset()
1332 - this->address());
1333 }
1334
cf43a2fe
AM
1335 // Ensure our GOT has a header.
1336 void
1337 set_final_data_size()
1338 {
1339 if (this->header_ent_cnt_ != 0)
1340 this->make_header();
1341 Output_data_got<size, big_endian>::set_final_data_size();
1342 }
1343
1344 // First word of GOT header needs some values that are not
1345 // handled by Output_data_got so poke them in here.
1346 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1347 void
1348 do_write(Output_file* of)
1349 {
6ce78956
AM
1350 this->replace_constant(this->header_index_,
1351 (size == 32
1352 ? this->layout_->dynamic_section()->address()
dd93cd0a 1353 : this->output_section()->address() + 0x8000));
cf43a2fe
AM
1354
1355 Output_data_got<size, big_endian>::do_write(of);
1356 }
1357
1358private:
1359 void
1360 reserve_ent(unsigned int cnt = 1)
1361 {
1362 if (this->header_ent_cnt_ == 0)
1363 return;
1364 if (this->num_entries() + cnt > this->header_index_)
1365 this->make_header();
1366 }
1367
1368 void
1369 make_header()
1370 {
1371 this->header_ent_cnt_ = 0;
1372 this->header_index_ = this->num_entries();
1373 if (size == 32)
1374 {
1375 Output_data_got<size, big_endian>::add_constant(0);
1376 Output_data_got<size, big_endian>::add_constant(0);
1377 Output_data_got<size, big_endian>::add_constant(0);
1378
1379 // Define _GLOBAL_OFFSET_TABLE_ at the header
1380 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1381 Symbol_table::PREDEFINED,
1382 this, this->g_o_t(), 0,
1383 elfcpp::STT_OBJECT,
1384 elfcpp::STB_LOCAL,
1385 elfcpp::STV_HIDDEN,
1386 0, false, false);
1387 }
1388 else
1389 Output_data_got<size, big_endian>::add_constant(0);
1390 }
1391
1392 // Stashed pointers.
1393 Symbol_table* symtab_;
1394 Layout* layout_;
1395
1396 // GOT header size.
1397 unsigned int header_ent_cnt_;
1398 // GOT header index.
1399 unsigned int header_index_;
42cacb20
DE
1400};
1401
1402// Get the GOT section, creating it if necessary.
1403
1404template<int size, bool big_endian>
cf43a2fe 1405Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
1406Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1407 Layout* layout)
1408{
1409 if (this->got_ == NULL)
1410 {
1411 gold_assert(symtab != NULL && layout != NULL);
1412
cf43a2fe
AM
1413 this->got_
1414 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
1415
1416 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1417 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 1418 this->got_, ORDER_DATA, false);
42cacb20
DE
1419 }
1420
1421 return this->got_;
1422}
1423
1424// Get the dynamic reloc section, creating it if necessary.
1425
1426template<int size, bool big_endian>
1427typename Target_powerpc<size, big_endian>::Reloc_section*
1428Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1429{
1430 if (this->rela_dyn_ == NULL)
1431 {
1432 gold_assert(layout != NULL);
1433 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1434 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
1435 elfcpp::SHF_ALLOC, this->rela_dyn_,
1436 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
1437 }
1438 return this->rela_dyn_;
1439}
1440
1441// A class to handle the PLT data.
1442
1443template<int size, bool big_endian>
cf43a2fe 1444class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
1445{
1446 public:
1447 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1448 size, big_endian> Reloc_section;
1449
cf43a2fe 1450 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
42cacb20
DE
1451
1452 // Add an entry to the PLT.
cf43a2fe
AM
1453 void
1454 add_entry(Symbol*);
42cacb20
DE
1455
1456 // Return the .rela.plt section data.
cf43a2fe
AM
1457 const Reloc_section*
1458 rel_plt() const
1459 {
42cacb20
DE
1460 return this->rel_;
1461 }
1462
0e70b911
CC
1463 // Return the number of PLT entries.
1464 unsigned int
1465 entry_count() const
d83ce4e3
AM
1466 {
1467 return ((this->current_data_size() - initial_plt_entry_size)
1468 / plt_entry_size);
1469 }
0e70b911
CC
1470
1471 // Return the offset of the first non-reserved PLT entry.
1472 static unsigned int
1473 first_plt_entry_offset()
cf43a2fe 1474 { return initial_plt_entry_size; }
0e70b911
CC
1475
1476 // Return the size of a PLT entry.
1477 static unsigned int
1478 get_plt_entry_size()
cf43a2fe 1479 { return plt_entry_size; }
0e70b911 1480
42cacb20 1481 protected:
42cacb20 1482 void
cf43a2fe 1483 do_adjust_output_section(Output_section* os)
42cacb20 1484 {
cf43a2fe 1485 os->set_entsize(0);
42cacb20
DE
1486 }
1487
6ce78956
AM
1488 // Write to a map file.
1489 void
1490 do_print_to_mapfile(Mapfile* mapfile) const
1491 { mapfile->print_output_data(this, _("** PLT")); }
1492
cf43a2fe
AM
1493 private:
1494 // The size of an entry in the PLT.
1495 static const int plt_entry_size = size == 32 ? 4 : 24;
1496 // The size of the first reserved entry.
1497 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1498
42cacb20
DE
1499 // Write out the PLT data.
1500 void
1501 do_write(Output_file*);
1502
1503 // The reloc section.
1504 Reloc_section* rel_;
cf43a2fe
AM
1505 // Allows access to .glink for do_write.
1506 Target_powerpc<size, big_endian>* targ_;
42cacb20
DE
1507};
1508
cf43a2fe 1509// Create the PLT section.
42cacb20
DE
1510
1511template<int size, bool big_endian>
d83ce4e3
AM
1512Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1513 Layout* layout,
1514 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
1515 : Output_section_data_build(size == 32 ? 4 : 8),
1516 targ_(targ)
42cacb20
DE
1517{
1518 this->rel_ = new Reloc_section(false);
1519 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
1520 elfcpp::SHF_ALLOC, this->rel_,
1521 ORDER_DYNAMIC_PLT_RELOCS, false);
42cacb20
DE
1522}
1523
42cacb20
DE
1524// Add an entry to the PLT.
1525
1526template<int size, bool big_endian>
1527void
1528Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1529{
cf43a2fe
AM
1530 if (!gsym->has_plt_offset())
1531 {
1532 off_t off = this->current_data_size();
1533
1534 if (off == 0)
1535 off += initial_plt_entry_size;
1536 gsym->set_plt_offset(off);
1537 gsym->set_needs_dynsym_entry();
1538 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1539 off += plt_entry_size;
1540 this->set_current_data_size(off);
1541 }
42cacb20
DE
1542}
1543
dd93cd0a
AM
1544static const uint32_t add_0_11_11 = 0x7c0b5a14;
1545static const uint32_t add_3_3_2 = 0x7c631214;
1546static const uint32_t add_3_3_13 = 0x7c636a14;
1547static const uint32_t add_11_0_11 = 0x7d605a14;
1548static const uint32_t add_12_2_11 = 0x7d825a14;
1549static const uint32_t addi_11_11 = 0x396b0000;
1550static const uint32_t addi_12_12 = 0x398c0000;
1551static const uint32_t addi_2_2 = 0x38420000;
1552static const uint32_t addi_3_2 = 0x38620000;
1553static const uint32_t addi_3_3 = 0x38630000;
1554static const uint32_t addis_0_2 = 0x3c020000;
1555static const uint32_t addis_0_13 = 0x3c0d0000;
c9269dff
AM
1556static const uint32_t addis_11_11 = 0x3d6b0000;
1557static const uint32_t addis_11_30 = 0x3d7e0000;
1558static const uint32_t addis_12_12 = 0x3d8c0000;
dd93cd0a
AM
1559static const uint32_t addis_12_2 = 0x3d820000;
1560static const uint32_t addis_3_2 = 0x3c620000;
1561static const uint32_t addis_3_13 = 0x3c6d0000;
c9269dff
AM
1562static const uint32_t b = 0x48000000;
1563static const uint32_t bcl_20_31 = 0x429f0005;
1564static const uint32_t bctr = 0x4e800420;
1565static const uint32_t blrl = 0x4e800021;
dd93cd0a
AM
1566static const uint32_t cror_15_15_15 = 0x4def7b82;
1567static const uint32_t cror_31_31_31 = 0x4ffffb82;
1568static const uint32_t ld_11_12 = 0xe96c0000;
1569static const uint32_t ld_11_2 = 0xe9620000;
1570static const uint32_t ld_2_1 = 0xe8410000;
1571static const uint32_t ld_2_11 = 0xe84b0000;
1572static const uint32_t ld_2_12 = 0xe84c0000;
1573static const uint32_t ld_2_2 = 0xe8420000;
1574static const uint32_t li_0_0 = 0x38000000;
1575static const uint32_t lis_0_0 = 0x3c000000;
c9269dff
AM
1576static const uint32_t lis_11 = 0x3d600000;
1577static const uint32_t lis_12 = 0x3d800000;
c9269dff
AM
1578static const uint32_t lwz_0_12 = 0x800c0000;
1579static const uint32_t lwz_11_11 = 0x816b0000;
1580static const uint32_t lwz_11_30 = 0x817e0000;
1581static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 1582static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 1583static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 1584static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff
AM
1585static const uint32_t mflr_12 = 0x7d8802a6;
1586static const uint32_t mtctr_0 = 0x7c0903a6;
1587static const uint32_t mtctr_11 = 0x7d6903a6;
1588static const uint32_t mtlr_0 = 0x7c0803a6;
c9269dff 1589static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 1590static const uint32_t nop = 0x60000000;
c9269dff 1591static const uint32_t ori_0_0_0 = 0x60000000;
dd93cd0a
AM
1592static const uint32_t std_2_1 = 0xf8410000;
1593static const uint32_t sub_11_11_12 = 0x7d6c5850;
42cacb20
DE
1594
1595// Write out the PLT.
1596
1597template<int size, bool big_endian>
1598void
1599Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1600{
cf43a2fe
AM
1601 if (size == 32)
1602 {
1603 const off_t offset = this->offset();
1604 const section_size_type oview_size
1605 = convert_to_section_size_type(this->data_size());
1606 unsigned char* const oview = of->get_output_view(offset, oview_size);
1607 unsigned char* pov = oview;
1608 unsigned char* endpov = oview + oview_size;
1609
1610 // The address the .glink branch table
1611 const Output_data_glink<size, big_endian>* glink
1612 = this->targ_->glink_section();
1613 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1614 = glink->address() + glink->pltresolve();
1615
1616 while (pov < endpov)
1617 {
1618 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1619 pov += 4;
1620 branch_tab += 4;
1621 }
1622
1623 of->write_output_view(offset, oview_size, oview);
1624 }
1625}
1626
1627// Create the PLT section.
1628
1629template<int size, bool big_endian>
1630void
1631Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1632{
1633 if (this->plt_ == NULL)
1634 {
1635 if (this->glink_ == NULL)
1636 make_glink_section(layout);
1637
1638 // Ensure that .rela.dyn always appears before .rela.plt This is
1639 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1640 // needs to include .rela.plt in it's range.
1641 this->rela_dyn_section(layout);
1642
1643 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1644 layout->add_output_section_data(".plt",
1645 (size == 32
1646 ? elfcpp::SHT_PROGBITS
1647 : elfcpp::SHT_NOBITS),
1648 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1649 this->plt_,
1650 (size == 32
1651 ? ORDER_SMALL_DATA
1652 : ORDER_SMALL_BSS),
1653 false);
1654 }
1655}
1656
1657// A class to handle .glink.
1658
1659template<int size, bool big_endian>
1660class Output_data_glink : public Output_section_data
1661{
1662 public:
c9269dff
AM
1663 static const int pltresolve_size = 16*4;
1664
cf43a2fe
AM
1665 Output_data_glink(Target_powerpc<size, big_endian>*);
1666
1667 // Add an entry
1668 void
1669 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1670 const Sized_relobj<size, big_endian>*);
cf43a2fe
AM
1671
1672 unsigned int
1673 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1674 const Sized_relobj<size, big_endian>*) const;
cf43a2fe
AM
1675
1676 unsigned int
1677 glink_entry_size() const
1678 {
1679 if (size == 32)
1680 return 4 * 4;
1681 else
1682 // FIXME: We should be using multiple glink sections for
1683 // stubs to support > 33M applications.
1684 return 8 * 4;
1685 }
1686
1687 off_t
1688 pltresolve() const
1689 {
1690 return this->pltresolve_;
1691 }
1692
6ce78956
AM
1693 protected:
1694 // Write to a map file.
1695 void
1696 do_print_to_mapfile(Mapfile* mapfile) const
1697 { mapfile->print_output_data(this, _("** glink")); }
1698
cf43a2fe 1699 private:
cf43a2fe
AM
1700 void
1701 set_final_data_size();
1702
1703 // Write out .glink
1704 void
1705 do_write(Output_file*);
1706
d1a8cabd 1707 class Glink_sym_ent
cf43a2fe 1708 {
d1a8cabd
AM
1709 public:
1710 Glink_sym_ent(const Symbol* sym,
cf43a2fe 1711 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1712 const Sized_relobj<size, big_endian>* object)
c9269dff 1713 : sym_(sym), addend_(0), object_(0)
cf43a2fe
AM
1714 {
1715 if (size != 32)
1716 this->addend_ = reloc.get_r_addend();
d1a8cabd
AM
1717 else if (parameters->options().output_is_position_independent()
1718 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1719 == elfcpp::R_PPC_PLTREL24))
cf43a2fe 1720 {
d1a8cabd 1721 this->addend_ = reloc.get_r_addend();
cf43a2fe 1722 if (this->addend_ != 0)
d1a8cabd 1723 this->object_ = object;
cf43a2fe
AM
1724 }
1725 }
1726
cf43a2fe
AM
1727 bool operator==(const Glink_sym_ent& that) const
1728 {
1729 return (this->sym_ == that.sym_
1730 && this->object_ == that.object_
d1a8cabd 1731 && this->addend_ == that.addend_);
cf43a2fe 1732 }
c9269dff
AM
1733
1734 const Symbol* sym_;
1735 unsigned int addend_;
1736 const Sized_relobj<size, big_endian>* object_;
cf43a2fe
AM
1737 };
1738
d1a8cabd 1739 class Glink_sym_ent_hash
cf43a2fe 1740 {
d1a8cabd 1741 public:
cf43a2fe
AM
1742 size_t operator()(const Glink_sym_ent& ent) const
1743 {
1744 return (reinterpret_cast<uintptr_t>(ent.sym_)
1745 ^ reinterpret_cast<uintptr_t>(ent.object_)
cf43a2fe
AM
1746 ^ ent.addend_);
1747 }
1748 };
1749
d1a8cabd 1750 // Map sym/object/addend to index.
cf43a2fe
AM
1751 typedef Unordered_map<Glink_sym_ent, unsigned int,
1752 Glink_sym_ent_hash> Glink_entries;
1753 Glink_entries glink_entries_;
1754
1755 // Offset of pltresolve stub (actually, branch table for 32-bit)
1756 off_t pltresolve_;
1757
1758 // Allows access to .got and .plt for do_write.
1759 Target_powerpc<size, big_endian>* targ_;
1760};
1761
1762// Create the glink section.
1763
1764template<int size, bool big_endian>
d83ce4e3
AM
1765Output_data_glink<size, big_endian>::Output_data_glink(
1766 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
1767 : Output_section_data(16),
1768 pltresolve_(0), targ_(targ)
1769{
1770}
1771
1772// Add an entry to glink, if we do not already have one for this
d1a8cabd 1773// sym/object/addend combo.
cf43a2fe
AM
1774
1775template<int size, bool big_endian>
1776void
d83ce4e3
AM
1777Output_data_glink<size, big_endian>::add_entry(
1778 const Symbol* gsym,
1779 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1780 const Sized_relobj<size, big_endian>* object)
cf43a2fe 1781{
d1a8cabd 1782 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe 1783 unsigned int indx = this->glink_entries_.size();
d83ce4e3 1784 this->glink_entries_.insert(std::make_pair(ent, indx));
cf43a2fe
AM
1785}
1786
1787template<int size, bool big_endian>
1788unsigned int
d83ce4e3
AM
1789Output_data_glink<size, big_endian>::find_entry(
1790 const Symbol* gsym,
1791 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1792 const Sized_relobj<size, big_endian>* object) const
cf43a2fe 1793{
d1a8cabd 1794 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe
AM
1795 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1796 gold_assert(p != this->glink_entries_.end());
1797 return p->second;
1798}
1799
1800template<int size, bool big_endian>
1801void
1802Output_data_glink<size, big_endian>::set_final_data_size()
1803{
1804 unsigned int count = this->glink_entries_.size();
1805 off_t total = count;
1806
1807 if (count != 0)
1808 {
1809 if (size == 32)
1810 {
1811 total *= 16;
1812 this->pltresolve_ = total;
1813
1814 // space for branch table
1815 total += 4 * (count - 1);
1816
1817 total += -total & 15;
1818 total += this->pltresolve_size;
1819 }
1820 else
1821 {
1822 total *= 32;
1823 this->pltresolve_ = total;
1824 total += this->pltresolve_size;
1825
1826 // space for branch table
1827 total += 8 * count;
1828 if (count > 0x8000)
1829 total += 4 * (count - 0x8000);
1830 }
1831 }
1832
1833 this->set_data_size(total);
1834}
1835
1836static inline uint32_t
1837l(uint32_t a)
1838{
1839 return a & 0xffff;
1840}
1841
1842static inline uint32_t
1843hi(uint32_t a)
1844{
1845 return l(a >> 16);
1846}
1847
1848static inline uint32_t
1849ha(uint32_t a)
1850{
1851 return hi(a + 0x8000);
1852}
1853
1854template<bool big_endian>
1855static inline void
c9269dff 1856write_insn(unsigned char* p, uint32_t v)
cf43a2fe
AM
1857{
1858 elfcpp::Swap<32, big_endian>::writeval(p, v);
1859}
1860
1861// Write out .glink.
1862
1863template<int size, bool big_endian>
1864void
1865Output_data_glink<size, big_endian>::do_write(Output_file* of)
1866{
1867 const off_t off = this->offset();
42cacb20
DE
1868 const section_size_type oview_size =
1869 convert_to_section_size_type(this->data_size());
cf43a2fe 1870 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 1871 unsigned char* p;
42cacb20 1872
cf43a2fe 1873 // The base address of the .plt section.
dd93cd0a
AM
1874 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1875 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 1876
dd93cd0a
AM
1877 const Output_data_got_powerpc<size, big_endian>* got
1878 = this->targ_->got_section();
cf43a2fe
AM
1879
1880 if (size == 64)
1881 {
dd93cd0a 1882 Address got_os_addr = got->output_section()->address();
c9269dff 1883
cf43a2fe
AM
1884 // Write out call stubs.
1885 typename Glink_entries::const_iterator g;
1886 for (g = this->glink_entries_.begin();
1887 g != this->glink_entries_.end();
1888 ++g)
1889 {
dd93cd0a
AM
1890 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1891 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1892 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1893 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1894 Address pltoff = plt_addr - got_addr;
cf43a2fe
AM
1895
1896 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1897 gold_error(_("%s: linkage table error against `%s'"),
1898 g->first.object_->name().c_str(),
1899 g->first.sym_->demangled_name().c_str());
1900
1901 p = oview + g->second * this->glink_entry_size();
1902 if (ha(pltoff) != 0)
1903 {
1904 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1905 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1906 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1907 if (ha(pltoff + 16) != ha(pltoff))
1908 {
1909 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1910 pltoff = 0;
1911 }
1912 write_insn<big_endian>(p, mtctr_11), p += 4;
1913 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1914 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1915 write_insn<big_endian>(p, bctr), p += 4;
1916 }
1917 else
1918 {
1919 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1920 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1921 if (ha(pltoff + 16) != ha(pltoff))
1922 {
1923 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1924 pltoff = 0;
1925 }
1926 write_insn<big_endian>(p, mtctr_11), p += 4;
1927 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1928 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1929 write_insn<big_endian>(p, bctr), p += 4;
1930 }
1931 }
1932
1933 // Write pltresolve stub.
1934 p = oview + this->pltresolve_;
dd93cd0a
AM
1935 Address after_bcl = this->address() + this->pltresolve_ + 16;
1936 Address pltoff = plt_base - after_bcl;
cf43a2fe
AM
1937
1938 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1939
1940 write_insn<big_endian>(p, mflr_12), p += 4;
1941 write_insn<big_endian>(p, bcl_20_31), p += 4;
1942 write_insn<big_endian>(p, mflr_11), p += 4;
1943 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1944 write_insn<big_endian>(p, mtlr_12), p += 4;
1945 write_insn<big_endian>(p, add_12_2_11), p += 4;
1946 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1947 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1948 write_insn<big_endian>(p, mtctr_11), p += 4;
1949 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1950 write_insn<big_endian>(p, bctr), p += 4;
1951 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1952 write_insn<big_endian>(p, nop), p += 4;
1953
1954 // Write lazy link call stubs.
1955 uint32_t indx = 0;
1956 while (p < oview + oview_size)
1957 {
1958 if (indx < 0x8000)
1959 {
1960 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1961 }
1962 else
1963 {
1964 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1965 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1966 }
c9269dff 1967 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
cf43a2fe
AM
1968 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1969 indx++;
1970 }
1971 }
1972 else
1973 {
dd93cd0a
AM
1974 // The address of _GLOBAL_OFFSET_TABLE_.
1975 Address g_o_t = got->address() + got->g_o_t();
c9269dff 1976
cf43a2fe
AM
1977 // Write out call stubs.
1978 typename Glink_entries::const_iterator g;
1979 for (g = this->glink_entries_.begin();
1980 g != this->glink_entries_.end();
1981 ++g)
1982 {
dd93cd0a
AM
1983 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1984 Address got_addr;
1985 const Address invalid_address = static_cast<Address>(-1);
cf43a2fe
AM
1986
1987 p = oview + g->second * this->glink_entry_size();
1988 if (parameters->options().output_is_position_independent())
1989 {
d1a8cabd
AM
1990 const Powerpc_relobj<size, big_endian>* object = static_cast
1991 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1992 if (object != NULL)
1993 {
1994 unsigned int got2 = object->got2_shndx();
c9269dff
AM
1995 got_addr = g->first.object_->get_output_section_offset(got2);
1996 gold_assert(got_addr != invalid_address);
1997 got_addr += (g->first.object_->output_section(got2)->address()
1998 + g->first.addend_);
d1a8cabd 1999 }
cf43a2fe
AM
2000 else
2001 got_addr = g_o_t;
2002
dd93cd0a 2003 Address pltoff = plt_addr - got_addr;
cf43a2fe
AM
2004 if (ha(pltoff) == 0)
2005 {
2006 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2007 write_insn<big_endian>(p + 4, mtctr_11);
2008 write_insn<big_endian>(p + 8, bctr);
2009 }
2010 else
2011 {
2012 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2013 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2014 write_insn<big_endian>(p + 8, mtctr_11);
2015 write_insn<big_endian>(p + 12, bctr);
2016 }
2017 }
2018 else
2019 {
2020 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2021 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2022 write_insn<big_endian>(p + 8, mtctr_11);
2023 write_insn<big_endian>(p + 12, bctr);
2024 }
2025 }
42cacb20 2026
cf43a2fe
AM
2027 // Write out pltresolve branch table.
2028 p = oview + this->pltresolve_;
2029 unsigned int the_end = oview_size - this->pltresolve_size;
c9269dff 2030 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
2031 while (p < end_p - 8 * 4)
2032 write_insn<big_endian>(p, b + end_p - p), p += 4;
2033 while (p < end_p)
2034 write_insn<big_endian>(p, nop), p += 4;
42cacb20 2035
cf43a2fe
AM
2036 // Write out pltresolve call stub.
2037 if (parameters->options().output_is_position_independent())
42cacb20 2038 {
dd93cd0a
AM
2039 Address res0_off = this->pltresolve_;
2040 Address after_bcl_off = the_end + 12;
2041 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe
AM
2042
2043 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2044 write_insn<big_endian>(p + 4, mflr_0);
2045 write_insn<big_endian>(p + 8, bcl_20_31);
2046 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2047 write_insn<big_endian>(p + 16, mflr_12);
2048 write_insn<big_endian>(p + 20, mtlr_0);
2049 write_insn<big_endian>(p + 24, sub_11_11_12);
2050
dd93cd0a 2051 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe
AM
2052
2053 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2054 if (ha(got_bcl) == ha(got_bcl + 4))
2055 {
2056 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2057 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2058 }
2059 else
2060 {
2061 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2062 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2063 }
2064 write_insn<big_endian>(p + 40, mtctr_0);
2065 write_insn<big_endian>(p + 44, add_0_11_11);
2066 write_insn<big_endian>(p + 48, add_11_0_11);
2067 write_insn<big_endian>(p + 52, bctr);
2068 write_insn<big_endian>(p + 56, nop);
2069 write_insn<big_endian>(p + 60, nop);
42cacb20 2070 }
cf43a2fe 2071 else
42cacb20 2072 {
dd93cd0a 2073 Address res0 = this->pltresolve_ + this->address();
cf43a2fe
AM
2074
2075 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2076 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2077 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2078 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2079 else
2080 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2081 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2082 write_insn<big_endian>(p + 16, mtctr_0);
2083 write_insn<big_endian>(p + 20, add_0_11_11);
2084 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2085 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2086 else
2087 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2088 write_insn<big_endian>(p + 28, add_11_0_11);
2089 write_insn<big_endian>(p + 32, bctr);
2090 write_insn<big_endian>(p + 36, nop);
2091 write_insn<big_endian>(p + 40, nop);
2092 write_insn<big_endian>(p + 44, nop);
2093 write_insn<big_endian>(p + 48, nop);
2094 write_insn<big_endian>(p + 52, nop);
2095 write_insn<big_endian>(p + 56, nop);
2096 write_insn<big_endian>(p + 60, nop);
42cacb20 2097 }
cf43a2fe 2098 p += 64;
42cacb20
DE
2099 }
2100
cf43a2fe
AM
2101 of->write_output_view(off, oview_size, oview);
2102}
2103
2104// Create the glink section.
42cacb20 2105
cf43a2fe
AM
2106template<int size, bool big_endian>
2107void
2108Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2109{
2110 if (this->glink_ == NULL)
2111 {
2112 this->glink_ = new Output_data_glink<size, big_endian>(this);
2113 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2114 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2115 this->glink_, ORDER_TEXT, false);
2116 }
42cacb20
DE
2117}
2118
2119// Create a PLT entry for a global symbol.
2120
2121template<int size, bool big_endian>
2122void
d83ce4e3
AM
2123Target_powerpc<size, big_endian>::make_plt_entry(
2124 Layout* layout,
2125 Symbol* gsym,
2126 const elfcpp::Rela<size, big_endian>& reloc,
2127 const Sized_relobj<size, big_endian>* object)
42cacb20 2128{
42cacb20 2129 if (this->plt_ == NULL)
cf43a2fe 2130 this->make_plt_section(layout);
42cacb20 2131
cf43a2fe 2132 this->plt_->add_entry(gsym);
612a8d3d 2133
d1a8cabd 2134 this->glink_->add_entry(gsym, reloc, object);
42cacb20
DE
2135}
2136
0e70b911
CC
2137// Return the number of entries in the PLT.
2138
2139template<int size, bool big_endian>
2140unsigned int
2141Target_powerpc<size, big_endian>::plt_entry_count() const
2142{
2143 if (this->plt_ == NULL)
2144 return 0;
2145 return this->plt_->entry_count();
2146}
2147
2148// Return the offset of the first non-reserved PLT entry.
2149
2150template<int size, bool big_endian>
2151unsigned int
2152Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2153{
2154 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2155}
2156
2157// Return the size of each PLT entry.
2158
2159template<int size, bool big_endian>
2160unsigned int
2161Target_powerpc<size, big_endian>::plt_entry_size() const
2162{
2163 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2164}
2165
dd93cd0a 2166// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
2167
2168template<int size, bool big_endian>
2169unsigned int
dd93cd0a 2170Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
2171 Symbol_table* symtab,
2172 Layout* layout,
2173 Sized_relobj_file<size, big_endian>* object)
42cacb20 2174{
dd93cd0a 2175 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
2176 {
2177 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2178 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
2179 Output_data_got_powerpc<size, big_endian>* got
2180 = this->got_section(symtab, layout);
2181 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
2182 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2183 got_offset, 0);
dd93cd0a 2184 this->tlsld_got_offset_ = got_offset;
42cacb20 2185 }
dd93cd0a 2186 return this->tlsld_got_offset_;
42cacb20
DE
2187}
2188
95a2c8d6
RS
2189// Get the Reference_flags for a particular relocation.
2190
2191template<int size, bool big_endian>
2192int
d83ce4e3 2193Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
95a2c8d6
RS
2194{
2195 switch (r_type)
2196 {
2197 case elfcpp::R_POWERPC_NONE:
2198 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2199 case elfcpp::R_POWERPC_GNU_VTENTRY:
2200 case elfcpp::R_PPC64_TOC:
2201 // No symbol reference.
2202 return 0;
2203
dd93cd0a
AM
2204 case elfcpp::R_PPC64_ADDR64:
2205 case elfcpp::R_PPC64_UADDR64:
2206 case elfcpp::R_POWERPC_ADDR32:
2207 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 2208 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 2209 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
2210 case elfcpp::R_POWERPC_ADDR16_LO:
2211 case elfcpp::R_POWERPC_ADDR16_HI:
2212 case elfcpp::R_POWERPC_ADDR16_HA:
95a2c8d6
RS
2213 return Symbol::ABSOLUTE_REF;
2214
dd93cd0a
AM
2215 case elfcpp::R_POWERPC_ADDR24:
2216 case elfcpp::R_POWERPC_ADDR14:
2217 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2218 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2219 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2220
2221 case elfcpp::R_POWERPC_REL32:
95a2c8d6 2222 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
2223 case elfcpp::R_POWERPC_REL16:
2224 case elfcpp::R_POWERPC_REL16_LO:
2225 case elfcpp::R_POWERPC_REL16_HI:
2226 case elfcpp::R_POWERPC_REL16_HA:
95a2c8d6
RS
2227 return Symbol::RELATIVE_REF;
2228
dd93cd0a 2229 case elfcpp::R_POWERPC_REL24:
95a2c8d6 2230 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
2231 case elfcpp::R_POWERPC_REL14:
2232 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2233 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
95a2c8d6
RS
2234 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2235
2236 case elfcpp::R_POWERPC_GOT16:
2237 case elfcpp::R_POWERPC_GOT16_LO:
2238 case elfcpp::R_POWERPC_GOT16_HI:
2239 case elfcpp::R_POWERPC_GOT16_HA:
2240 case elfcpp::R_PPC64_TOC16:
2241 case elfcpp::R_PPC64_TOC16_LO:
2242 case elfcpp::R_PPC64_TOC16_HI:
2243 case elfcpp::R_PPC64_TOC16_HA:
2244 case elfcpp::R_PPC64_TOC16_DS:
2245 case elfcpp::R_PPC64_TOC16_LO_DS:
2246 // Absolute in GOT.
2247 return Symbol::ABSOLUTE_REF;
2248
2249 case elfcpp::R_POWERPC_GOT_TPREL16:
2250 case elfcpp::R_POWERPC_TLS:
2251 return Symbol::TLS_REF;
2252
2253 case elfcpp::R_POWERPC_COPY:
2254 case elfcpp::R_POWERPC_GLOB_DAT:
2255 case elfcpp::R_POWERPC_JMP_SLOT:
2256 case elfcpp::R_POWERPC_RELATIVE:
2257 case elfcpp::R_POWERPC_DTPMOD:
2258 default:
2259 // Not expected. We will give an error later.
2260 return 0;
2261 }
2262}
2263
42cacb20
DE
2264// Report an unsupported relocation against a local symbol.
2265
2266template<int size, bool big_endian>
2267void
2268Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
2269 Sized_relobj_file<size, big_endian>* object,
2270 unsigned int r_type)
42cacb20
DE
2271{
2272 gold_error(_("%s: unsupported reloc %u against local symbol"),
2273 object->name().c_str(), r_type);
2274}
2275
2276// We are about to emit a dynamic relocation of type R_TYPE. If the
2277// dynamic linker does not support it, issue an error.
2278
2279template<int size, bool big_endian>
2280void
2281Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2282 unsigned int r_type)
2283{
2284 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2285
2286 // These are the relocation types supported by glibc for both 32-bit
2287 // and 64-bit powerpc.
2288 switch (r_type)
2289 {
3ea0a085 2290 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
2291 case elfcpp::R_POWERPC_RELATIVE:
2292 case elfcpp::R_POWERPC_GLOB_DAT:
2293 case elfcpp::R_POWERPC_DTPMOD:
2294 case elfcpp::R_POWERPC_DTPREL:
2295 case elfcpp::R_POWERPC_TPREL:
2296 case elfcpp::R_POWERPC_JMP_SLOT:
2297 case elfcpp::R_POWERPC_COPY:
3ea0a085 2298 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 2299 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 2300 case elfcpp::R_POWERPC_UADDR32:
42cacb20 2301 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
2302 case elfcpp::R_POWERPC_ADDR16:
2303 case elfcpp::R_POWERPC_UADDR16:
2304 case elfcpp::R_POWERPC_ADDR16_LO:
2305 case elfcpp::R_POWERPC_ADDR16_HI:
2306 case elfcpp::R_POWERPC_ADDR16_HA:
2307 case elfcpp::R_POWERPC_ADDR14:
2308 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2309 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2310 case elfcpp::R_POWERPC_REL32:
42cacb20 2311 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
2312 case elfcpp::R_POWERPC_TPREL16:
2313 case elfcpp::R_POWERPC_TPREL16_LO:
2314 case elfcpp::R_POWERPC_TPREL16_HI:
2315 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
2316 return;
2317
2318 default:
2319 break;
2320 }
2321
2322 if (size == 64)
2323 {
2324 switch (r_type)
2325 {
2326 // These are the relocation types supported only on 64-bit.
2327 case elfcpp::R_PPC64_ADDR64:
42cacb20 2328 case elfcpp::R_PPC64_UADDR64:
3ea0a085 2329 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 2330 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 2331 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
2332 case elfcpp::R_PPC64_ADDR16_HIGHER:
2333 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2334 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2335 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 2336 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
2337 case elfcpp::R_POWERPC_ADDR30:
2338 case elfcpp::R_PPC64_TPREL16_DS:
2339 case elfcpp::R_PPC64_TPREL16_LO_DS:
2340 case elfcpp::R_PPC64_TPREL16_HIGHER:
2341 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2342 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2343 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
2344 return;
2345
2346 default:
2347 break;
2348 }
2349 }
2350 else
2351 {
2352 switch (r_type)
2353 {
2354 // These are the relocation types supported only on 32-bit.
3ea0a085
AM
2355 // ??? glibc ld.so doesn't need to support these.
2356 case elfcpp::R_POWERPC_DTPREL16:
2357 case elfcpp::R_POWERPC_DTPREL16_LO:
2358 case elfcpp::R_POWERPC_DTPREL16_HI:
2359 case elfcpp::R_POWERPC_DTPREL16_HA:
2360 return;
42cacb20
DE
2361
2362 default:
2363 break;
2364 }
2365 }
2366
2367 // This prevents us from issuing more than one error per reloc
2368 // section. But we can still wind up issuing more than one
2369 // error per object file.
2370 if (this->issued_non_pic_error_)
2371 return;
33aea2fd 2372 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
2373 object->error(_("requires unsupported dynamic reloc; "
2374 "recompile with -fPIC"));
2375 this->issued_non_pic_error_ = true;
2376 return;
2377}
2378
2379// Scan a relocation for a local symbol.
2380
2381template<int size, bool big_endian>
2382inline void
2383Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
2384 Symbol_table* symtab,
2385 Layout* layout,
2386 Target_powerpc<size, big_endian>* target,
2387 Sized_relobj_file<size, big_endian>* object,
2388 unsigned int data_shndx,
2389 Output_section* output_section,
2390 const elfcpp::Rela<size, big_endian>& reloc,
2391 unsigned int r_type,
bfdfa4cd
AM
2392 const elfcpp::Sym<size, big_endian>& /* lsym */,
2393 bool is_discarded)
42cacb20 2394{
dd93cd0a
AM
2395 Powerpc_relobj<size, big_endian>* ppc_object
2396 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2397
bfdfa4cd
AM
2398 if (is_discarded)
2399 {
2400 if (size == 64
2401 && data_shndx == ppc_object->opd_shndx()
2402 && r_type == elfcpp::R_PPC64_ADDR64)
2403 ppc_object->set_opd_discard(reloc.get_r_offset());
2404 return;
2405 }
2406
42cacb20
DE
2407 switch (r_type)
2408 {
2409 case elfcpp::R_POWERPC_NONE:
2410 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2411 case elfcpp::R_POWERPC_GNU_VTENTRY:
6ce78956 2412 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 2413 case elfcpp::R_PPC_EMB_MRKREF:
7404fe1b 2414 case elfcpp::R_POWERPC_TLS:
dd93cd0a
AM
2415 break;
2416
2417 case elfcpp::R_PPC64_TOC:
2418 {
2419 Output_data_got_powerpc<size, big_endian>* got
2420 = target->got_section(symtab, layout);
2421 if (parameters->options().output_is_position_independent())
2422 {
bfdfa4cd
AM
2423 Address off = reloc.get_r_offset();
2424 if (size == 64
2425 && data_shndx == ppc_object->opd_shndx()
2426 && ppc_object->get_opd_discard(off - 8))
2427 break;
2428
dd93cd0a 2429 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 2430 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
2431 rela_dyn->add_output_section_relative(got->output_section(),
2432 elfcpp::R_POWERPC_RELATIVE,
2433 output_section,
bfdfa4cd
AM
2434 object, data_shndx, off,
2435 symobj->toc_base_offset());
dd93cd0a
AM
2436 }
2437 }
42cacb20
DE
2438 break;
2439
2440 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 2441 case elfcpp::R_PPC64_UADDR64:
42cacb20 2442 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
2443 case elfcpp::R_POWERPC_UADDR32:
2444 case elfcpp::R_POWERPC_ADDR24:
c9269dff 2445 case elfcpp::R_POWERPC_ADDR16:
42cacb20 2446 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
2447 case elfcpp::R_POWERPC_ADDR16_HI:
2448 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a
AM
2449 case elfcpp::R_POWERPC_UADDR16:
2450 case elfcpp::R_PPC64_ADDR16_HIGHER:
2451 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2452 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2453 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2454 case elfcpp::R_PPC64_ADDR16_DS:
2455 case elfcpp::R_PPC64_ADDR16_LO_DS:
2456 case elfcpp::R_POWERPC_ADDR14:
2457 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2458 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
2459 // If building a shared library (or a position-independent
2460 // executable), we need to create a dynamic relocation for
2461 // this location.
2462 if (parameters->options().output_is_position_independent())
2e702c99
RM
2463 {
2464 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20 2465
dd93cd0a
AM
2466 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2467 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99
RM
2468 {
2469 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
2470 rela_dyn->add_local_relative(object, r_sym,
2471 elfcpp::R_POWERPC_RELATIVE,
2472 output_section, data_shndx,
2473 reloc.get_r_offset(),
2474 reloc.get_r_addend(), false);
2e702c99
RM
2475 }
2476 else
2477 {
dd93cd0a 2478 check_non_pic(object, r_type);
42cacb20 2479 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
2480 rela_dyn->add_local(object, r_sym, r_type, output_section,
2481 data_shndx, reloc.get_r_offset(),
2482 reloc.get_r_addend());
2e702c99
RM
2483 }
2484 }
42cacb20
DE
2485 break;
2486
3ea0a085 2487 case elfcpp::R_PPC64_REL64:
dd93cd0a 2488 case elfcpp::R_POWERPC_REL32:
42cacb20
DE
2489 case elfcpp::R_POWERPC_REL24:
2490 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a 2491 case elfcpp::R_POWERPC_REL16:
6ce78956 2492 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 2493 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 2494 case elfcpp::R_POWERPC_REL16_HA:
dd93cd0a
AM
2495 case elfcpp::R_POWERPC_REL14:
2496 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2497 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2498 case elfcpp::R_POWERPC_SECTOFF:
2499 case elfcpp::R_POWERPC_TPREL16:
2500 case elfcpp::R_POWERPC_DTPREL16:
2501 case elfcpp::R_POWERPC_SECTOFF_LO:
2502 case elfcpp::R_POWERPC_TPREL16_LO:
2503 case elfcpp::R_POWERPC_DTPREL16_LO:
2504 case elfcpp::R_POWERPC_SECTOFF_HI:
2505 case elfcpp::R_POWERPC_TPREL16_HI:
2506 case elfcpp::R_POWERPC_DTPREL16_HI:
2507 case elfcpp::R_POWERPC_SECTOFF_HA:
2508 case elfcpp::R_POWERPC_TPREL16_HA:
2509 case elfcpp::R_POWERPC_DTPREL16_HA:
2510 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2511 case elfcpp::R_PPC64_TPREL16_HIGHER:
2512 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2513 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2514 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2515 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2516 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2517 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2518 case elfcpp::R_PPC64_TPREL16_DS:
2519 case elfcpp::R_PPC64_TPREL16_LO_DS:
2520 case elfcpp::R_PPC64_DTPREL16_DS:
2521 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2522 case elfcpp::R_PPC64_SECTOFF_DS:
2523 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2524 case elfcpp::R_PPC64_TLSGD:
2525 case elfcpp::R_PPC64_TLSLD:
42cacb20
DE
2526 break;
2527
2528 case elfcpp::R_POWERPC_GOT16:
2529 case elfcpp::R_POWERPC_GOT16_LO:
2530 case elfcpp::R_POWERPC_GOT16_HI:
2531 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
2532 case elfcpp::R_PPC64_GOT16_DS:
2533 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 2534 {
c9269dff 2535 // The symbol requires a GOT entry.
dd93cd0a
AM
2536 Output_data_got_powerpc<size, big_endian>* got
2537 = target->got_section(symtab, layout);
2538 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20
DE
2539
2540 // If we are generating a shared object, we need to add a
2541 // dynamic relocation for this symbol's GOT entry.
2542 if (parameters->options().output_is_position_independent())
2543 {
2544 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2545 {
2546 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2547 unsigned int off;
2548
2549 off = got->add_constant(0);
2550 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2551 rela_dyn->add_local_relative(object, r_sym,
2552 elfcpp::R_POWERPC_RELATIVE,
397b129b 2553 got, off, 0, false);
42cacb20 2554 }
2e702c99 2555 }
42cacb20
DE
2556 else
2557 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2558 }
2559 break;
2560
cf43a2fe
AM
2561 case elfcpp::R_PPC64_TOC16:
2562 case elfcpp::R_PPC64_TOC16_LO:
2563 case elfcpp::R_PPC64_TOC16_HI:
2564 case elfcpp::R_PPC64_TOC16_HA:
2565 case elfcpp::R_PPC64_TOC16_DS:
2566 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
2567 // We need a GOT section.
2568 target->got_section(symtab, layout);
2569 break;
2570
dd93cd0a
AM
2571 case elfcpp::R_POWERPC_GOT_TLSGD16:
2572 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2573 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2574 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2575 {
2576 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2577 if (tls_type == tls::TLSOPT_NONE)
2578 {
2579 Output_data_got_powerpc<size, big_endian>* got
2580 = target->got_section(symtab, layout);
2581 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
2582 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2583 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
2584 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
2585 }
2586 else if (tls_type == tls::TLSOPT_TO_LE)
2587 {
2588 // no GOT relocs needed for Local Exec.
2589 }
2590 else
2591 gold_unreachable();
2592 }
42cacb20
DE
2593 break;
2594
dd93cd0a
AM
2595 case elfcpp::R_POWERPC_GOT_TLSLD16:
2596 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2597 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2598 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2599 {
2600 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2601 if (tls_type == tls::TLSOPT_NONE)
2602 target->tlsld_got_offset(symtab, layout, object);
2603 else if (tls_type == tls::TLSOPT_TO_LE)
2604 {
2605 // no GOT relocs needed for Local Exec.
7404fe1b
AM
2606 if (parameters->options().emit_relocs())
2607 {
2608 Output_section* os = layout->tls_segment()->first_section();
2609 gold_assert(os != NULL);
2610 os->set_needs_symtab_index();
2611 }
dd93cd0a
AM
2612 }
2613 else
2614 gold_unreachable();
2615 }
42cacb20 2616 break;
42cacb20 2617
dd93cd0a
AM
2618 case elfcpp::R_POWERPC_GOT_DTPREL16:
2619 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2620 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2621 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2622 {
2623 Output_data_got_powerpc<size, big_endian>* got
2624 = target->got_section(symtab, layout);
2625 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 2626 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
2627 }
2628 break;
42cacb20 2629
dd93cd0a
AM
2630 case elfcpp::R_POWERPC_GOT_TPREL16:
2631 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2632 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2633 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2634 {
2635 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2636 if (tls_type == tls::TLSOPT_NONE)
2637 {
2638 Output_data_got_powerpc<size, big_endian>* got
2639 = target->got_section(symtab, layout);
2640 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 2641 got->add_local_tls(object, r_sym, GOT_TYPE_TPREL);
dd93cd0a
AM
2642 }
2643 else if (tls_type == tls::TLSOPT_TO_LE)
2644 {
2645 // no GOT relocs needed for Local Exec.
2646 }
2647 else
2648 gold_unreachable();
2649 }
2650 break;
2651
2652 default:
2653 unsupported_reloc_local(object, r_type);
2654 break;
2655 }
2656}
2657
2658// Report an unsupported relocation against a global symbol.
2659
2660template<int size, bool big_endian>
2661void
2662Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2663 Sized_relobj_file<size, big_endian>* object,
2664 unsigned int r_type,
2665 Symbol* gsym)
2666{
42cacb20
DE
2667 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2668 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2669}
2670
2671// Scan a relocation for a global symbol.
2672
2673template<int size, bool big_endian>
2674inline void
2675Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
2676 Symbol_table* symtab,
2677 Layout* layout,
2678 Target_powerpc<size, big_endian>* target,
2679 Sized_relobj_file<size, big_endian>* object,
2680 unsigned int data_shndx,
2681 Output_section* output_section,
2682 const elfcpp::Rela<size, big_endian>& reloc,
2683 unsigned int r_type,
2684 Symbol* gsym)
42cacb20 2685{
dd93cd0a
AM
2686 Powerpc_relobj<size, big_endian>* ppc_object
2687 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2688
42cacb20
DE
2689 switch (r_type)
2690 {
2691 case elfcpp::R_POWERPC_NONE:
2692 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2693 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 2694 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a 2695 case elfcpp::R_PPC_EMB_MRKREF:
7404fe1b 2696 case elfcpp::R_POWERPC_TLS:
dd93cd0a
AM
2697 break;
2698
2699 case elfcpp::R_PPC64_TOC:
2700 {
2701 Output_data_got_powerpc<size, big_endian>* got
2702 = target->got_section(symtab, layout);
2703 if (parameters->options().output_is_position_independent())
2704 {
bfdfa4cd
AM
2705 Address off = reloc.get_r_offset();
2706 if (size == 64
2707 && data_shndx == ppc_object->opd_shndx()
2708 && ppc_object->get_opd_discard(off - 8))
2709 break;
2710
dd93cd0a
AM
2711 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2712 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2713 if (data_shndx != ppc_object->opd_shndx())
2714 symobj = static_cast
2715 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2716 rela_dyn->add_output_section_relative(got->output_section(),
2717 elfcpp::R_POWERPC_RELATIVE,
2718 output_section,
bfdfa4cd 2719 object, data_shndx, off,
dd93cd0a
AM
2720 symobj->toc_base_offset());
2721 }
2722 }
42cacb20
DE
2723 break;
2724
c9269dff 2725 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd
AM
2726 if (size == 64
2727 && data_shndx == ppc_object->opd_shndx()
2728 && (gsym->is_defined_in_discarded_section()
2729 || gsym->object() != object))
2730 {
2731 ppc_object->set_opd_discard(reloc.get_r_offset());
2732 break;
2733 }
2734 // Fall thru
dd93cd0a 2735 case elfcpp::R_PPC64_UADDR64:
c9269dff 2736 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
2737 case elfcpp::R_POWERPC_UADDR32:
2738 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
2739 case elfcpp::R_POWERPC_ADDR16:
2740 case elfcpp::R_POWERPC_ADDR16_LO:
2741 case elfcpp::R_POWERPC_ADDR16_HI:
2742 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a
AM
2743 case elfcpp::R_POWERPC_UADDR16:
2744 case elfcpp::R_PPC64_ADDR16_HIGHER:
2745 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2746 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2747 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2748 case elfcpp::R_PPC64_ADDR16_DS:
2749 case elfcpp::R_PPC64_ADDR16_LO_DS:
2750 case elfcpp::R_POWERPC_ADDR14:
2751 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2752 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 2753 {
c9269dff
AM
2754 // Make a PLT entry if necessary.
2755 if (gsym->needs_plt_entry())
2756 {
cf43a2fe 2757 target->make_plt_entry(layout, gsym, reloc, 0);
2e702c99
RM
2758 // Since this is not a PC-relative relocation, we may be
2759 // taking the address of a function. In that case we need to
2760 // set the entry in the dynamic symbol table to the address of
2761 // the PLT entry.
cf43a2fe
AM
2762 if (size == 32
2763 && gsym->is_from_dynobj() && !parameters->options().shared())
2e702c99 2764 gsym->set_needs_dynsym_value();
c9269dff
AM
2765 }
2766 // Make a dynamic relocation if necessary.
dd93cd0a 2767 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
c9269dff
AM
2768 {
2769 if (gsym->may_need_copy_reloc())
2770 {
2771 target->copy_reloc(symtab, layout, object,
2772 data_shndx, output_section, gsym, reloc);
2773 }
dd93cd0a
AM
2774 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2775 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2776 && (gsym->can_use_relative_reloc(false)
2777 || data_shndx == ppc_object->opd_shndx()))
2e702c99
RM
2778 {
2779 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2780 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
42cacb20
DE
2781 output_section, object,
2782 data_shndx, reloc.get_r_offset(),
13cf9988 2783 reloc.get_r_addend(), false);
2e702c99
RM
2784 }
2785 else
2786 {
2787 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20 2788 check_non_pic(object, r_type);
dd93cd0a
AM
2789 rela_dyn->add_global(gsym, r_type, output_section,
2790 object, data_shndx,
2791 reloc.get_r_offset(),
2792 reloc.get_r_addend());
2e702c99
RM
2793 }
2794 }
42cacb20
DE
2795 }
2796 break;
2797
cf43a2fe 2798 case elfcpp::R_PPC_PLTREL24:
42cacb20 2799 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
2800 if (gsym->needs_plt_entry()
2801 || (!gsym->final_value_is_known()
2802 && (gsym->is_undefined()
2803 || gsym->is_from_dynobj()
2804 || gsym->is_preemptible())))
2805 target->make_plt_entry(layout, gsym, reloc, object);
2806 // Fall thru
42cacb20 2807
3ea0a085 2808 case elfcpp::R_PPC64_REL64:
dd93cd0a 2809 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
2810 // Make a dynamic relocation if necessary.
2811 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2812 {
2813 if (gsym->may_need_copy_reloc())
2814 {
2815 target->copy_reloc(symtab, layout, object,
2816 data_shndx, output_section, gsym,
2817 reloc);
2818 }
2819 else
2820 {
2821 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2822 check_non_pic(object, r_type);
2823 rela_dyn->add_global(gsym, r_type, output_section, object,
2824 data_shndx, reloc.get_r_offset(),
2825 reloc.get_r_addend());
2826 }
2827 }
2828 break;
2829
6ce78956
AM
2830 case elfcpp::R_POWERPC_REL16:
2831 case elfcpp::R_POWERPC_REL16_LO:
2832 case elfcpp::R_POWERPC_REL16_HI:
2833 case elfcpp::R_POWERPC_REL16_HA:
dd93cd0a
AM
2834 case elfcpp::R_POWERPC_REL14:
2835 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2836 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2837 case elfcpp::R_POWERPC_SECTOFF:
2838 case elfcpp::R_POWERPC_TPREL16:
2839 case elfcpp::R_POWERPC_DTPREL16:
2840 case elfcpp::R_POWERPC_SECTOFF_LO:
2841 case elfcpp::R_POWERPC_TPREL16_LO:
2842 case elfcpp::R_POWERPC_DTPREL16_LO:
2843 case elfcpp::R_POWERPC_SECTOFF_HI:
2844 case elfcpp::R_POWERPC_TPREL16_HI:
2845 case elfcpp::R_POWERPC_DTPREL16_HI:
2846 case elfcpp::R_POWERPC_SECTOFF_HA:
2847 case elfcpp::R_POWERPC_TPREL16_HA:
2848 case elfcpp::R_POWERPC_DTPREL16_HA:
2849 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2850 case elfcpp::R_PPC64_TPREL16_HIGHER:
2851 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2852 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2853 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2854 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2855 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2856 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2857 case elfcpp::R_PPC64_TPREL16_DS:
2858 case elfcpp::R_PPC64_TPREL16_LO_DS:
2859 case elfcpp::R_PPC64_DTPREL16_DS:
2860 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2861 case elfcpp::R_PPC64_SECTOFF_DS:
2862 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2863 case elfcpp::R_PPC64_TLSGD:
2864 case elfcpp::R_PPC64_TLSLD:
cf43a2fe
AM
2865 break;
2866
42cacb20
DE
2867 case elfcpp::R_POWERPC_GOT16:
2868 case elfcpp::R_POWERPC_GOT16_LO:
2869 case elfcpp::R_POWERPC_GOT16_HI:
2870 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
2871 case elfcpp::R_PPC64_GOT16_DS:
2872 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 2873 {
c9269dff
AM
2874 // The symbol requires a GOT entry.
2875 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
2876
2877 got = target->got_section(symtab, layout);
2e702c99
RM
2878 if (gsym->final_value_is_known())
2879 got->add_global(gsym, GOT_TYPE_STANDARD);
2880 else
2881 {
2882 // If this symbol is not fully resolved, we need to add a
2883 // dynamic relocation for it.
2884 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2885 if (gsym->is_from_dynobj()
2886 || gsym->is_undefined()
2887 || gsym->is_preemptible())
2888 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
83896202 2889 elfcpp::R_POWERPC_GLOB_DAT);
2e702c99
RM
2890 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2891 {
42cacb20
DE
2892 unsigned int off = got->add_constant(0);
2893
2894 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2895 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
13cf9988 2896 got, off, 0, false);
42cacb20 2897 }
2e702c99 2898 }
42cacb20
DE
2899 }
2900 break;
2901
cf43a2fe
AM
2902 case elfcpp::R_PPC64_TOC16:
2903 case elfcpp::R_PPC64_TOC16_LO:
2904 case elfcpp::R_PPC64_TOC16_HI:
2905 case elfcpp::R_PPC64_TOC16_HA:
2906 case elfcpp::R_PPC64_TOC16_DS:
2907 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
2908 // We need a GOT section.
2909 target->got_section(symtab, layout);
2910 break;
2911
dd93cd0a
AM
2912 case elfcpp::R_POWERPC_GOT_TLSGD16:
2913 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2914 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2915 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2916 {
2917 const bool final = gsym->final_value_is_known();
2918 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2919 if (tls_type == tls::TLSOPT_NONE)
2920 {
2921 Output_data_got_powerpc<size, big_endian>* got
2922 = target->got_section(symtab, layout);
2923 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2924 target->rela_dyn_section(layout),
2925 elfcpp::R_POWERPC_DTPMOD,
2926 elfcpp::R_POWERPC_DTPREL);
2927 }
2928 else if (tls_type == tls::TLSOPT_TO_IE)
2929 {
2930 Output_data_got_powerpc<size, big_endian>* got
2931 = target->got_section(symtab, layout);
2932 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2933 target->rela_dyn_section(layout),
2934 elfcpp::R_POWERPC_TPREL);
2935 }
2936 else if (tls_type == tls::TLSOPT_TO_LE)
2937 {
2938 // no GOT relocs needed for Local Exec.
2939 }
2940 else
2941 gold_unreachable();
2942 }
42cacb20
DE
2943 break;
2944
dd93cd0a
AM
2945 case elfcpp::R_POWERPC_GOT_TLSLD16:
2946 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2947 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2948 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2949 {
2950 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2951 if (tls_type == tls::TLSOPT_NONE)
2952 target->tlsld_got_offset(symtab, layout, object);
2953 else if (tls_type == tls::TLSOPT_TO_LE)
2954 {
2955 // no GOT relocs needed for Local Exec.
7404fe1b
AM
2956 if (parameters->options().emit_relocs())
2957 {
2958 Output_section* os = layout->tls_segment()->first_section();
2959 gold_assert(os != NULL);
2960 os->set_needs_symtab_index();
2961 }
dd93cd0a
AM
2962 }
2963 else
2964 gold_unreachable();
2965 }
2966 break;
2967
2968 case elfcpp::R_POWERPC_GOT_DTPREL16:
2969 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2970 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2971 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2972 {
2973 Output_data_got_powerpc<size, big_endian>* got
2974 = target->got_section(symtab, layout);
bd73a62d
AM
2975 if (!gsym->final_value_is_known()
2976 && (gsym->is_from_dynobj()
2977 || gsym->is_undefined()
2978 || gsym->is_preemptible()))
2979 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2980 target->rela_dyn_section(layout),
2981 elfcpp::R_POWERPC_DTPREL);
2982 else
2983 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
2984 }
2985 break;
2986
2987 case elfcpp::R_POWERPC_GOT_TPREL16:
2988 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2989 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2990 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2991 {
2992 const bool final = gsym->final_value_is_known();
2993 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2994 if (tls_type == tls::TLSOPT_NONE)
2995 {
2996 Output_data_got_powerpc<size, big_endian>* got
2997 = target->got_section(symtab, layout);
bd73a62d
AM
2998 if (!gsym->final_value_is_known()
2999 && (gsym->is_from_dynobj()
3000 || gsym->is_undefined()
3001 || gsym->is_preemptible()))
3002 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
3003 target->rela_dyn_section(layout),
3004 elfcpp::R_POWERPC_TPREL);
3005 else
3006 got->add_global_tls(gsym, GOT_TYPE_TPREL);
dd93cd0a
AM
3007 }
3008 else if (tls_type == tls::TLSOPT_TO_LE)
3009 {
3010 // no GOT relocs needed for Local Exec.
3011 }
3012 else
3013 gold_unreachable();
3014 }
42cacb20
DE
3015 break;
3016
3017 default:
3018 unsupported_reloc_global(object, r_type, gsym);
3019 break;
3020 }
3021}
3022
6d03d481
ST
3023// Process relocations for gc.
3024
3025template<int size, bool big_endian>
3026void
3027Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
3028 Symbol_table* symtab,
3029 Layout* layout,
3030 Sized_relobj_file<size, big_endian>* object,
3031 unsigned int data_shndx,
3032 unsigned int,
3033 const unsigned char* prelocs,
3034 size_t reloc_count,
3035 Output_section* output_section,
3036 bool needs_special_offset_handling,
3037 size_t local_symbol_count,
3038 const unsigned char* plocal_symbols)
6d03d481
ST
3039{
3040 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 3041 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
e81fea4d
AM
3042 Powerpc_relobj<size, big_endian>* ppc_object
3043 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3044 if (size == 64)
3045 ppc_object->set_opd_valid();
3046 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3047 {
3048 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3049 for (p = ppc_object->access_from_map()->begin();
3050 p != ppc_object->access_from_map()->end();
3051 ++p)
3052 {
3053 Address dst_off = p->first;
3054 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3055 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3056 for (s = p->second.begin(); s != p->second.end(); ++s)
3057 {
3058 Object* src_obj = s->first;
3059 unsigned int src_indx = s->second;
3060 symtab->gc()->add_reference(src_obj, src_indx,
3061 ppc_object, dst_indx);
3062 }
3063 p->second.clear();
3064 }
3065 ppc_object->access_from_map()->clear();
3066 // Don't look at .opd relocs as .opd will reference everything.
3067 return;
3068 }
6d03d481 3069
41cbeecc 3070 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3ff2ccb0 3071 typename Target_powerpc::Relocatable_size_for_reloc>(
6d03d481
ST
3072 symtab,
3073 layout,
3074 this,
3075 object,
3076 data_shndx,
3077 prelocs,
3078 reloc_count,
3079 output_section,
3080 needs_special_offset_handling,
3081 local_symbol_count,
3082 plocal_symbols);
3083}
3084
e81fea4d
AM
3085// Handle target specific gc actions when adding a gc reference from
3086// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3087// and DST_OFF. For powerpc64, this adds a referenc to the code
3088// section of a function descriptor.
3089
3090template<int size, bool big_endian>
3091void
3092Target_powerpc<size, big_endian>::do_gc_add_reference(
3093 Symbol_table* symtab,
3094 Object* src_obj,
3095 unsigned int src_shndx,
3096 Object* dst_obj,
3097 unsigned int dst_shndx,
3098 Address dst_off) const
3099{
3100 Powerpc_relobj<size, big_endian>* ppc_object
3101 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3102 if (size == 64 && dst_shndx == ppc_object->opd_shndx())
3103 {
3104 if (ppc_object->opd_valid())
3105 {
3106 dst_shndx = ppc_object->get_opd_ent(dst_off);
3107 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3108 }
3109 else
3110 {
3111 // If we haven't run scan_opd_relocs, we must delay
3112 // processing this function descriptor reference.
3113 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3114 }
3115 }
3116}
3117
3118// Add any special sections for this symbol to the gc work list.
3119// For powerpc64, this adds the code section of a function
3120// descriptor.
3121
3122template<int size, bool big_endian>
3123void
3124Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3125 Symbol_table* symtab,
3126 Symbol* sym) const
3127{
3128 if (size == 64)
3129 {
3130 Powerpc_relobj<size, big_endian>* ppc_object
3131 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3132 bool is_ordinary;
3133 unsigned int shndx = sym->shndx(&is_ordinary);
3134 if (is_ordinary && shndx == ppc_object->opd_shndx())
3135 {
3136 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
3137 Address dst_off = gsym->value();
3138 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3139 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
3140 }
3141 }
3142}
3143
42cacb20
DE
3144// Scan relocations for a section.
3145
3146template<int size, bool big_endian>
3147void
3148Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
3149 Symbol_table* symtab,
3150 Layout* layout,
3151 Sized_relobj_file<size, big_endian>* object,
3152 unsigned int data_shndx,
3153 unsigned int sh_type,
3154 const unsigned char* prelocs,
3155 size_t reloc_count,
3156 Output_section* output_section,
3157 bool needs_special_offset_handling,
3158 size_t local_symbol_count,
3159 const unsigned char* plocal_symbols)
42cacb20
DE
3160{
3161 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 3162 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
42cacb20
DE
3163
3164 if (sh_type == elfcpp::SHT_REL)
3165 {
3166 gold_error(_("%s: unsupported REL reloc section"),
3167 object->name().c_str());
3168 return;
3169 }
3170
cf43a2fe
AM
3171 if (size == 32)
3172 {
3173 static Output_data_space* sdata;
3174
3175 // Define _SDA_BASE_ at the start of the .sdata section.
3176 if (sdata == NULL)
3177 {
3178 // layout->find_output_section(".sdata") == NULL
3179 sdata = new Output_data_space(4, "** sdata");
3180 Output_section* os
3181 = layout->add_output_section_data(".sdata", 0,
3182 elfcpp::SHF_ALLOC
3183 | elfcpp::SHF_WRITE,
3184 sdata, ORDER_SMALL_DATA, false);
3185 symtab->define_in_output_data("_SDA_BASE_", NULL,
3186 Symbol_table::PREDEFINED,
3187 os, 32768, 0, elfcpp::STT_OBJECT,
3188 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
3189 0, false, false);
3190 }
3191 }
42cacb20 3192
2ea97941 3193 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
42cacb20
DE
3194 symtab,
3195 layout,
3196 this,
3197 object,
3198 data_shndx,
3199 prelocs,
3200 reloc_count,
3201 output_section,
3202 needs_special_offset_handling,
3203 local_symbol_count,
3204 plocal_symbols);
3205}
3206
3207// Finalize the sections.
3208
3209template<int size, bool big_endian>
3210void
d5b40221
DK
3211Target_powerpc<size, big_endian>::do_finalize_sections(
3212 Layout* layout,
f59f41f3
DK
3213 const Input_objects*,
3214 Symbol_table*)
42cacb20
DE
3215{
3216 // Fill in some more dynamic tags.
ea715a34
ILT
3217 const Reloc_section* rel_plt = (this->plt_ == NULL
3218 ? NULL
3219 : this->plt_->rel_plt());
3220 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
612a8d3d 3221 this->rela_dyn_, true, size == 32);
42cacb20 3222
c9269dff 3223 Output_data_dynamic* odyn = layout->dynamic_data();
cf43a2fe
AM
3224 if (size == 32)
3225 {
dd93cd0a
AM
3226 if (this->got_ != NULL)
3227 {
3228 this->got_->finalize_data_size();
3229 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3230 this->got_, this->got_->g_o_t());
3231 }
cf43a2fe 3232 }
c9269dff
AM
3233 else
3234 {
dd93cd0a
AM
3235 if (this->glink_ != NULL)
3236 {
3237 this->glink_->finalize_data_size();
3238 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3239 this->glink_,
3240 (this->glink_->pltresolve()
3241 + this->glink_->pltresolve_size - 32));
3242 }
c9269dff 3243 }
cf43a2fe 3244
42cacb20
DE
3245 // Emit any relocs we saved in an attempt to avoid generating COPY
3246 // relocs.
3247 if (this->copy_relocs_.any_saved_relocs())
3248 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3249}
3250
3ea0a085
AM
3251// Return the value to use for a branch relocation.
3252
3253template<int size, bool big_endian>
3254typename elfcpp::Elf_types<size>::Elf_Addr
3255Target_powerpc<size, big_endian>::symval_for_branch(
3256 Address value,
3257 const Sized_symbol<size>* gsym,
3258 Powerpc_relobj<size, big_endian>* object,
3259 unsigned int *dest_shndx)
3260{
3261 *dest_shndx = 0;
3262 if (size == 32)
3263 return value;
3264
3265 // If the symbol is defined in an opd section, ie. is a function
3266 // descriptor, use the function descriptor code entry address
3267 Powerpc_relobj<size, big_endian>* symobj = object;
3268 if (gsym != NULL)
3269 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3270 unsigned int shndx = symobj->opd_shndx();
3271 if (shndx == 0)
3272 return value;
3273 Address opd_addr = symobj->get_output_section_offset(shndx);
3274 gold_assert(opd_addr != invalid_address);
3275 opd_addr += symobj->output_section(shndx)->address();
3276 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3277 {
3278 Address sec_off;
e81fea4d 3279 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
3ea0a085
AM
3280 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3281 gold_assert(sec_addr != invalid_address);
3282 sec_addr += symobj->output_section(*dest_shndx)->address();
3283 value = sec_addr + sec_off;
3284 }
3285 return value;
3286}
3287
42cacb20
DE
3288// Perform a relocation.
3289
3290template<int size, bool big_endian>
3291inline bool
3292Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3
AM
3293 const Relocate_info<size, big_endian>* relinfo,
3294 Target_powerpc* target,
3295 Output_section* os,
3296 size_t relnum,
3297 const elfcpp::Rela<size, big_endian>& rela,
3298 unsigned int r_type,
3299 const Sized_symbol<size>* gsym,
3300 const Symbol_value<size>* psymval,
3301 unsigned char* view,
c9269dff
AM
3302 Address address,
3303 section_size_type view_size)
42cacb20 3304{
dd93cd0a
AM
3305
3306 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3307 || r_type == elfcpp::R_PPC_PLTREL24)
3308 && gsym != NULL
3309 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3310 enum skip_tls last_tls = this->call_tls_get_addr_;
3311 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3312 if (is_tls_call)
3313 {
3314 if (last_tls == CALL_NOT_EXPECTED)
3315 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3316 _("__tls_get_addr call lacks marker reloc"));
3317 else if (last_tls == CALL_SKIP)
3318 return false;
3319 }
3320 else if (last_tls != CALL_NOT_EXPECTED)
3321 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3322 _("missing expected __tls_get_addr call"));
3323
42cacb20 3324 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
dd93cd0a 3325 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3ea0a085
AM
3326 Powerpc_relobj<size, big_endian>* const object
3327 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a
AM
3328 Address value = 0;
3329 bool has_plt_value = false;
3330 if (gsym != NULL
3331 && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3332 {
3333 const Output_data_glink<size, big_endian>* glink
3334 = target->glink_section();
3335 unsigned int glink_index = glink->find_entry(gsym, rela, object);
3336 value = glink->address() + glink_index * glink->glink_entry_size();
3337 has_plt_value = true;
3338 }
cf43a2fe
AM
3339
3340 if (r_type == elfcpp::R_POWERPC_GOT16
3341 || r_type == elfcpp::R_POWERPC_GOT16_LO
3342 || r_type == elfcpp::R_POWERPC_GOT16_HI
3343 || r_type == elfcpp::R_POWERPC_GOT16_HA
3344 || r_type == elfcpp::R_PPC64_GOT16_DS
3345 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 3346 {
cf43a2fe
AM
3347 if (gsym != NULL)
3348 {
3349 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3350 value = gsym->got_offset(GOT_TYPE_STANDARD);
3351 }
3352 else
3353 {
3354 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3355 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3356 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3357 }
dd93cd0a 3358 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
3359 }
3360 else if (r_type == elfcpp::R_PPC64_TOC)
3361 {
c9269dff 3362 value = (target->got_section()->output_section()->address()
dd93cd0a 3363 + object->toc_base_offset());
cf43a2fe
AM
3364 }
3365 else if (gsym != NULL
3366 && (r_type == elfcpp::R_POWERPC_REL24
3367 || r_type == elfcpp::R_PPC_PLTREL24)
dd93cd0a 3368 && has_plt_value)
cf43a2fe 3369 {
c9269dff
AM
3370 if (size == 64)
3371 {
3372 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3373 Valtype* wv = reinterpret_cast<Valtype*>(view);
3374 bool can_plt_call = false;
3375 if (rela.get_r_offset() + 8 <= view_size)
3376 {
3ea0a085 3377 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 3378 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
3379 if ((insn & 1) != 0
3380 && (insn2 == nop
3381 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff
AM
3382 {
3383 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3384 can_plt_call = true;
3385 }
3386 }
3387 if (!can_plt_call)
3ea0a085
AM
3388 {
3389 // If we don't have a branch and link followed by a nop,
3390 // we can't go via the plt because there is no place to
3391 // put a toc restoring instruction.
3392 // Unless we know we won't be returning.
3393 if (strcmp(gsym->name(), "__libc_start_main") == 0)
3394 can_plt_call = true;
3395 }
3396 if (!can_plt_call)
3397 {
3398 // This is not an error in one special case: A self
3399 // call. It isn't possible to cheaply verify we have
3400 // such a call so just check for a call to the same
3401 // section.
3402 bool ok = false;
3403 if (gsym->source() == Symbol::FROM_OBJECT
3404 && gsym->object() == object)
3405 {
3406 Address addend = rela.get_r_addend();
3407 unsigned int dest_shndx;
3408 value = psymval->value(object, addend);
3409 value = target->symval_for_branch(value, gsym, object,
3410 &dest_shndx);
3411 bool is_ordinary;
3412 if (dest_shndx == 0)
3413 dest_shndx = gsym->shndx(&is_ordinary);
3414 ok = dest_shndx == relinfo->data_shndx;
3415 }
3416 if (!ok)
3417 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3418 _("call lacks nop, can't restore toc; "
3419 "recompile with -fPIC"));
3420 }
c9269dff 3421 }
cf43a2fe 3422 }
dd93cd0a
AM
3423 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3424 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3425 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3426 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3427 {
3428 // First instruction of a global dynamic sequence, arg setup insn.
3429 const bool final = gsym == NULL || gsym->final_value_is_known();
3430 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3431 enum Got_type got_type = GOT_TYPE_STANDARD;
3432 if (tls_type == tls::TLSOPT_NONE)
3433 got_type = GOT_TYPE_TLSGD;
3434 else if (tls_type == tls::TLSOPT_TO_IE)
3435 got_type = GOT_TYPE_TPREL;
3436 if (got_type != GOT_TYPE_STANDARD)
3437 {
3438 if (gsym != NULL)
3439 {
3440 gold_assert(gsym->has_got_offset(got_type));
3441 value = gsym->got_offset(got_type);
3442 }
3443 else
3444 {
3445 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3446 gold_assert(object->local_has_got_offset(r_sym, got_type));
3447 value = object->local_got_offset(r_sym, got_type);
3448 }
3449 value -= target->got_section()->got_base_offset(object);
3450 }
3451 if (tls_type == tls::TLSOPT_TO_IE)
3452 {
3453 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3454 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3455 {
3456 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3457 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3458 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3459 if (size == 32)
3460 insn |= 32 << 26; // lwz
3461 else
3462 insn |= 58 << 26; // ld
3463 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3464 }
3465 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3466 - elfcpp::R_POWERPC_GOT_TLSGD16);
3467 }
3468 else if (tls_type == tls::TLSOPT_TO_LE)
3469 {
3470 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3471 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3472 {
3473 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3474 Insn insn = addis_3_13;
3475 if (size == 32)
3476 insn = addis_3_2;
3477 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3478 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3479 value = psymval->value(object, rela.get_r_addend());
3480 }
3481 else
3482 {
3483 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3484 Insn insn = nop;
3485 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3486 r_type = elfcpp::R_POWERPC_NONE;
3487 }
3488 }
3489 }
3490 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3491 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3492 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3493 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3494 {
3495 // First instruction of a local dynamic sequence, arg setup insn.
3496 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3497 if (tls_type == tls::TLSOPT_NONE)
3498 {
3499 value = target->tlsld_got_offset();
3500 value -= target->got_section()->got_base_offset(object);
3501 }
3502 else
3503 {
3504 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3505 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3506 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3507 {
3508 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3509 Insn insn = addis_3_13;
3510 if (size == 32)
3511 insn = addis_3_2;
3512 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3513 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 3514 value = dtp_offset;
dd93cd0a
AM
3515 }
3516 else
3517 {
3518 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3519 Insn insn = nop;
3520 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3521 r_type = elfcpp::R_POWERPC_NONE;
3522 }
3523 }
3524 }
3525 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3526 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3527 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3528 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3529 {
3530 // Accesses relative to a local dynamic sequence address,
3531 // no optimisation here.
3532 if (gsym != NULL)
3533 {
3534 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3535 value = gsym->got_offset(GOT_TYPE_DTPREL);
3536 }
3537 else
3538 {
3539 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3540 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3541 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3542 }
3543 value -= target->got_section()->got_base_offset(object);
3544 }
3545 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3546 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3547 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3548 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3549 {
3550 // First instruction of initial exec sequence.
3551 const bool final = gsym == NULL || gsym->final_value_is_known();
3552 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3553 if (tls_type == tls::TLSOPT_NONE)
3554 {
3555 if (gsym != NULL)
3556 {
3557 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3558 value = gsym->got_offset(GOT_TYPE_TPREL);
3559 }
3560 else
3561 {
3562 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3563 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3564 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3565 }
3566 value -= target->got_section()->got_base_offset(object);
3567 }
3568 else
3569 {
3570 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3571 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3572 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3573 {
3574 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3575 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3576 insn &= (1 << 26) - (1 << 21); // extract rt from ld
3577 if (size == 32)
3578 insn |= addis_0_2;
3579 else
3580 insn |= addis_0_13;
3581 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3582 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3583 value = psymval->value(object, rela.get_r_addend());
3584 }
3585 else
3586 {
3587 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3588 Insn insn = nop;
3589 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3590 r_type = elfcpp::R_POWERPC_NONE;
3591 }
3592 }
3593 }
3594 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3595 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3596 {
3597 // Second instruction of a global dynamic sequence,
3598 // the __tls_get_addr call
3599 this->call_tls_get_addr_ = CALL_EXPECTED;
3600 const bool final = gsym == NULL || gsym->final_value_is_known();
3601 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3602 if (tls_type != tls::TLSOPT_NONE)
3603 {
3604 if (tls_type == tls::TLSOPT_TO_IE)
3605 {
3606 Insn* iview = reinterpret_cast<Insn*>(view);
3607 Insn insn = add_3_3_13;
3608 if (size == 32)
3609 insn = add_3_3_2;
3610 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3611 r_type = elfcpp::R_POWERPC_NONE;
3612 }
3613 else
3614 {
3615 Insn* iview = reinterpret_cast<Insn*>(view);
3616 Insn insn = addi_3_3;
3617 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3618 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3619 view += 2 * big_endian;
3620 value = psymval->value(object, rela.get_r_addend());
3621 }
3622 this->call_tls_get_addr_ = CALL_SKIP;
3623 }
3624 }
3625 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3626 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3627 {
3628 // Second instruction of a local dynamic sequence,
3629 // the __tls_get_addr call
3630 this->call_tls_get_addr_ = CALL_EXPECTED;
3631 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3632 if (tls_type == tls::TLSOPT_TO_LE)
3633 {
3634 Insn* iview = reinterpret_cast<Insn*>(view);
3635 Insn insn = addi_3_3;
3636 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3637 this->call_tls_get_addr_ = CALL_SKIP;
3638 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3639 view += 2 * big_endian;
7404fe1b 3640 value = dtp_offset;
dd93cd0a
AM
3641 }
3642 }
3643 else if (r_type == elfcpp::R_POWERPC_TLS)
3644 {
3645 // Second instruction of an initial exec sequence
3646 const bool final = gsym == NULL || gsym->final_value_is_known();
3647 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3648 if (tls_type == tls::TLSOPT_TO_LE)
3649 {
3650 Insn* iview = reinterpret_cast<Insn*>(view);
3651 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3652 unsigned int reg = size == 32 ? 2 : 13;
3653 insn = at_tls_transform(insn, reg);
3654 gold_assert(insn != 0);
3655 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3656 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3657 view += 2 * big_endian;
3658 value = psymval->value(object, rela.get_r_addend());
3659 }
3660 }
cf43a2fe
AM
3661 else
3662 {
dd93cd0a 3663 Address addend = 0;
3ea0a085 3664 unsigned int dest_shndx;
cf43a2fe
AM
3665 if (r_type != elfcpp::R_PPC_PLTREL24)
3666 addend = rela.get_r_addend();
dd93cd0a
AM
3667 if (size == 64 || !has_plt_value)
3668 value = psymval->value(object, addend);
3669 if (size == 64 && is_branch_reloc(r_type))
3ea0a085 3670 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
42cacb20
DE
3671 }
3672
42cacb20
DE
3673 switch (r_type)
3674 {
dd93cd0a
AM
3675 case elfcpp::R_PPC64_REL64:
3676 case elfcpp::R_POWERPC_REL32:
3677 case elfcpp::R_POWERPC_REL24:
3678 case elfcpp::R_PPC_PLTREL24:
3679 case elfcpp::R_PPC_LOCAL24PC:
3680 case elfcpp::R_POWERPC_REL16:
3681 case elfcpp::R_POWERPC_REL16_LO:
3682 case elfcpp::R_POWERPC_REL16_HI:
3683 case elfcpp::R_POWERPC_REL16_HA:
3684 case elfcpp::R_POWERPC_REL14:
3685 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3686 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3687 value -= address;
3688 break;
3689
42cacb20
DE
3690 case elfcpp::R_PPC64_TOC16:
3691 case elfcpp::R_PPC64_TOC16_LO:
3692 case elfcpp::R_PPC64_TOC16_HI:
3693 case elfcpp::R_PPC64_TOC16_HA:
3694 case elfcpp::R_PPC64_TOC16_DS:
3695 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 3696 // Subtract the TOC base address.
c9269dff 3697 value -= (target->got_section()->output_section()->address()
dd93cd0a 3698 + object->toc_base_offset());
42cacb20
DE
3699 break;
3700
cf43a2fe
AM
3701 case elfcpp::R_POWERPC_SECTOFF:
3702 case elfcpp::R_POWERPC_SECTOFF_LO:
3703 case elfcpp::R_POWERPC_SECTOFF_HI:
3704 case elfcpp::R_POWERPC_SECTOFF_HA:
3705 case elfcpp::R_PPC64_SECTOFF_DS:
3706 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3707 if (os != NULL)
3708 value -= os->address();
42cacb20
DE
3709 break;
3710
dd93cd0a
AM
3711 case elfcpp::R_PPC64_TPREL16_DS:
3712 case elfcpp::R_PPC64_TPREL16_LO_DS:
3713 if (size != 64)
3714 // R_PPC_TLSGD and R_PPC_TLSLD
3715 break;
3716 case elfcpp::R_POWERPC_TPREL16:
3717 case elfcpp::R_POWERPC_TPREL16_LO:
3718 case elfcpp::R_POWERPC_TPREL16_HI:
3719 case elfcpp::R_POWERPC_TPREL16_HA:
3720 case elfcpp::R_POWERPC_TPREL:
3721 case elfcpp::R_PPC64_TPREL16_HIGHER:
3722 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3723 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3724 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3725 // tls symbol values are relative to tls_segment()->vaddr()
3726 value -= tp_offset;
3727 break;
3728
3729 case elfcpp::R_PPC64_DTPREL16_DS:
3730 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3731 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3732 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3733 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3734 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3735 if (size != 64)
3736 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3737 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3738 break;
3739 case elfcpp::R_POWERPC_DTPREL16:
3740 case elfcpp::R_POWERPC_DTPREL16_LO:
3741 case elfcpp::R_POWERPC_DTPREL16_HI:
3742 case elfcpp::R_POWERPC_DTPREL16_HA:
3743 case elfcpp::R_POWERPC_DTPREL:
3744 // tls symbol values are relative to tls_segment()->vaddr()
3745 value -= dtp_offset;
3746 break;
3747
42cacb20
DE
3748 default:
3749 break;
3750 }
3751
dd93cd0a 3752 Insn branch_bit = 0;
42cacb20
DE
3753 switch (r_type)
3754 {
dd93cd0a
AM
3755 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3756 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3757 branch_bit = 1 << 21;
3758 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3759 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3760 {
3761 Insn* iview = reinterpret_cast<Insn*>(view);
3762 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3763 insn &= ~(1 << 21);
3764 insn |= branch_bit;
3765 if (this->is_isa_v2)
3766 {
3767 // Set 'a' bit. This is 0b00010 in BO field for branch
3768 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3769 // for branch on CTR insns (BO == 1a00t or 1a01t).
3770 if ((insn & (0x14 << 21)) == (0x04 << 21))
3771 insn |= 0x02 << 21;
3772 else if ((insn & (0x14 << 21)) == (0x10 << 21))
3773 insn |= 0x08 << 21;
3774 else
3775 break;
3776 }
3777 else
3778 {
3779 // Invert 'y' bit if not the default.
3780 if (static_cast<Signed_address>(value) < 0)
3781 insn ^= 1 << 21;
3782 }
3783 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3784 }
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
f4baf0d4 3791 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
dd93cd0a
AM
3792 switch (r_type)
3793 {
3794 case elfcpp::R_POWERPC_ADDR32:
3795 case elfcpp::R_POWERPC_UADDR32:
3796 if (size == 64)
f4baf0d4 3797 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
3798 break;
3799
3800 case elfcpp::R_POWERPC_REL32:
dd93cd0a 3801 if (size == 64)
f4baf0d4 3802 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
3803 break;
3804
3805 case elfcpp::R_POWERPC_ADDR24:
3806 case elfcpp::R_POWERPC_ADDR16:
3807 case elfcpp::R_POWERPC_UADDR16:
3808 case elfcpp::R_PPC64_ADDR16_DS:
3809 case elfcpp::R_POWERPC_ADDR14:
3810 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3811 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
f4baf0d4 3812 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
3813 break;
3814
3815 case elfcpp::R_POWERPC_REL24:
42cacb20 3816 case elfcpp::R_PPC_PLTREL24:
cf43a2fe 3817 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
3818 case elfcpp::R_POWERPC_REL16:
3819 case elfcpp::R_PPC64_TOC16:
3820 case elfcpp::R_POWERPC_GOT16:
3821 case elfcpp::R_POWERPC_SECTOFF:
3822 case elfcpp::R_POWERPC_TPREL16:
3823 case elfcpp::R_POWERPC_DTPREL16:
3824 case elfcpp::R_PPC64_TPREL16_DS:
3825 case elfcpp::R_PPC64_DTPREL16_DS:
3826 case elfcpp::R_PPC64_TOC16_DS:
3827 case elfcpp::R_PPC64_GOT16_DS:
3828 case elfcpp::R_PPC64_SECTOFF_DS:
3829 case elfcpp::R_POWERPC_REL14:
3830 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3831 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3832 case elfcpp::R_POWERPC_GOT_TLSGD16:
3833 case elfcpp::R_POWERPC_GOT_TLSLD16:
3834 case elfcpp::R_POWERPC_GOT_TPREL16:
3835 case elfcpp::R_POWERPC_GOT_DTPREL16:
f4baf0d4 3836 overflow = Reloc::CHECK_SIGNED;
42cacb20 3837 break;
dd93cd0a 3838 }
42cacb20 3839
3ea0a085 3840 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 3841 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
3842 switch (r_type)
3843 {
3844 case elfcpp::R_POWERPC_NONE:
3845 case elfcpp::R_POWERPC_TLS:
3846 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3847 case elfcpp::R_POWERPC_GNU_VTENTRY:
3848 case elfcpp::R_PPC_EMB_MRKREF:
42cacb20
DE
3849 break;
3850
3851 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 3852 case elfcpp::R_PPC64_REL64:
cf43a2fe 3853 case elfcpp::R_PPC64_TOC:
dd93cd0a
AM
3854 Reloc::addr64(view, value);
3855 break;
3856
3857 case elfcpp::R_POWERPC_TPREL:
3858 case elfcpp::R_POWERPC_DTPREL:
3859 if (size == 64)
3860 Reloc::addr64(view, value);
3861 else
3ea0a085 3862 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
3863 break;
3864
3865 case elfcpp::R_PPC64_UADDR64:
3866 Reloc::addr64_u(view, value);
42cacb20
DE
3867 break;
3868
3869 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a 3870 case elfcpp::R_POWERPC_REL32:
3ea0a085 3871 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
3872 break;
3873
3874 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 3875 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
3876 break;
3877
3878 case elfcpp::R_POWERPC_ADDR24:
3879 case elfcpp::R_POWERPC_REL24:
3880 case elfcpp::R_PPC_PLTREL24:
3881 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 3882 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
3883 break;
3884
dd93cd0a
AM
3885 case elfcpp::R_POWERPC_GOT_DTPREL16:
3886 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3887 if (size == 64)
3888 {
3ea0a085 3889 status = Reloc::addr16_ds(view, value, overflow);
dd93cd0a
AM
3890 break;
3891 }
cf43a2fe 3892 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 3893 case elfcpp::R_POWERPC_REL16:
cf43a2fe 3894 case elfcpp::R_PPC64_TOC16:
42cacb20 3895 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 3896 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
3897 case elfcpp::R_POWERPC_TPREL16:
3898 case elfcpp::R_POWERPC_DTPREL16:
3899 case elfcpp::R_POWERPC_GOT_TLSGD16:
3900 case elfcpp::R_POWERPC_GOT_TLSLD16:
3901 case elfcpp::R_POWERPC_GOT_TPREL16:
cf43a2fe 3902 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 3903 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 3904 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 3905 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe 3906 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
3907 case elfcpp::R_POWERPC_TPREL16_LO:
3908 case elfcpp::R_POWERPC_DTPREL16_LO:
3909 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3910 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3911 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3ea0a085 3912 status = Reloc::addr16(view, value, overflow);
dd93cd0a
AM
3913 break;
3914
3915 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 3916 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
3917 break;
3918
cf43a2fe 3919 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 3920 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 3921 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 3922 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe 3923 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
3924 case elfcpp::R_POWERPC_TPREL16_HI:
3925 case elfcpp::R_POWERPC_DTPREL16_HI:
3926 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3927 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3928 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3929 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3930 Reloc::addr16_hi(view, value);
42cacb20
DE
3931 break;
3932
cf43a2fe 3933 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 3934 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 3935 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 3936 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe 3937 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
3938 case elfcpp::R_POWERPC_TPREL16_HA:
3939 case elfcpp::R_POWERPC_DTPREL16_HA:
3940 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3941 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3942 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3943 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3944 Reloc::addr16_ha(view, value);
42cacb20
DE
3945 break;
3946
dd93cd0a
AM
3947 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3948 if (size == 32)
3949 // R_PPC_EMB_NADDR16_LO
3950 goto unsupp;
3951 case elfcpp::R_PPC64_ADDR16_HIGHER:
3952 case elfcpp::R_PPC64_TPREL16_HIGHER:
3953 Reloc::addr16_hi2(view, value);
42cacb20
DE
3954 break;
3955
dd93cd0a
AM
3956 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3957 if (size == 32)
3958 // R_PPC_EMB_NADDR16_HI
3959 goto unsupp;
3960 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3961 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3962 Reloc::addr16_ha2(view, value);
42cacb20
DE
3963 break;
3964
dd93cd0a
AM
3965 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3966 if (size == 32)
3967 // R_PPC_EMB_NADDR16_HA
3968 goto unsupp;
3969 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3970 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3971 Reloc::addr16_hi3(view, value);
42cacb20
DE
3972 break;
3973
dd93cd0a
AM
3974 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3975 if (size == 32)
3976 // R_PPC_EMB_SDAI16
3977 goto unsupp;
3978 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3979 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3980 Reloc::addr16_ha3(view, value);
3981 break;
3982
3983 case elfcpp::R_PPC64_DTPREL16_DS:
3984 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3985 if (size == 32)
3986 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3987 goto unsupp;
3988 case elfcpp::R_PPC64_TPREL16_DS:
3989 case elfcpp::R_PPC64_TPREL16_LO_DS:
3990 if (size == 32)
3991 // R_PPC_TLSGD, R_PPC_TLSLD
3992 break;
cf43a2fe
AM
3993 case elfcpp::R_PPC64_ADDR16_DS:
3994 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
3995 case elfcpp::R_PPC64_TOC16_DS:
3996 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
3997 case elfcpp::R_PPC64_GOT16_DS:
3998 case elfcpp::R_PPC64_GOT16_LO_DS:
3999 case elfcpp::R_PPC64_SECTOFF_DS:
4000 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3ea0a085 4001 status = Reloc::addr16_ds(view, value, overflow);
dd93cd0a
AM
4002 break;
4003
4004 case elfcpp::R_POWERPC_ADDR14:
4005 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4006 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4007 case elfcpp::R_POWERPC_REL14:
4008 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4009 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 4010 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
4011 break;
4012
4013 case elfcpp::R_POWERPC_COPY:
4014 case elfcpp::R_POWERPC_GLOB_DAT:
4015 case elfcpp::R_POWERPC_JMP_SLOT:
4016 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 4017 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
4018 case elfcpp::R_PPC64_JMP_IREL:
4019 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
4020 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4021 _("unexpected reloc %u in object file"),
4022 r_type);
4023 break;
4024
dd93cd0a
AM
4025 case elfcpp::R_PPC_EMB_SDA21:
4026 if (size == 32)
4027 goto unsupp;
4028 else
4029 {
4030 // R_PPC64_TOCSAVE. For the time being this can be ignored.
4031 }
4032 break;
4033
4034 case elfcpp::R_PPC_EMB_SDA2I16:
4035 case elfcpp::R_PPC_EMB_SDA2REL:
4036 if (size == 32)
4037 goto unsupp;
4038 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
4039 break;
4040
dd93cd0a
AM
4041 case elfcpp::R_POWERPC_PLT32:
4042 case elfcpp::R_POWERPC_PLTREL32:
4043 case elfcpp::R_POWERPC_PLT16_LO:
4044 case elfcpp::R_POWERPC_PLT16_HI:
4045 case elfcpp::R_POWERPC_PLT16_HA:
4046 case elfcpp::R_PPC_SDAREL16:
4047 case elfcpp::R_POWERPC_ADDR30:
4048 case elfcpp::R_PPC64_PLT64:
4049 case elfcpp::R_PPC64_PLTREL64:
4050 case elfcpp::R_PPC64_PLTGOT16:
4051 case elfcpp::R_PPC64_PLTGOT16_LO:
4052 case elfcpp::R_PPC64_PLTGOT16_HI:
4053 case elfcpp::R_PPC64_PLTGOT16_HA:
4054 case elfcpp::R_PPC64_PLT16_LO_DS:
4055 case elfcpp::R_PPC64_PLTGOT16_DS:
4056 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
4057 case elfcpp::R_PPC_EMB_RELSEC16:
4058 case elfcpp::R_PPC_EMB_RELST_LO:
4059 case elfcpp::R_PPC_EMB_RELST_HI:
4060 case elfcpp::R_PPC_EMB_RELST_HA:
4061 case elfcpp::R_PPC_EMB_BIT_FLD:
4062 case elfcpp::R_PPC_EMB_RELSDA:
4063 case elfcpp::R_PPC_TOC16:
42cacb20 4064 default:
dd93cd0a 4065 unsupp:
42cacb20
DE
4066 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4067 _("unsupported reloc %u"),
4068 r_type);
4069 break;
4070 }
f4baf0d4 4071 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
3ea0a085
AM
4072 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4073 _("relocation overflow"));
42cacb20
DE
4074
4075 return true;
4076}
4077
42cacb20
DE
4078// Relocate section data.
4079
4080template<int size, bool big_endian>
4081void
4082Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
4083 const Relocate_info<size, big_endian>* relinfo,
4084 unsigned int sh_type,
4085 const unsigned char* prelocs,
4086 size_t reloc_count,
4087 Output_section* output_section,
4088 bool needs_special_offset_handling,
4089 unsigned char* view,
c9269dff 4090 Address address,
d83ce4e3
AM
4091 section_size_type view_size,
4092 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
4093{
4094 typedef Target_powerpc<size, big_endian> Powerpc;
4095 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
4096
4097 gold_assert(sh_type == elfcpp::SHT_RELA);
4098
bfdfa4cd
AM
4099 unsigned char *opd_rel = NULL;
4100 Powerpc_relobj<size, big_endian>* const object
4101 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4102 if (size == 64
4103 && relinfo->data_shndx == object->opd_shndx())
4104 {
4105 // Rewrite opd relocs, omitting those for discarded sections
4106 // to silence gold::relocate_section errors.
4107 const int reloc_size
4108 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4109 opd_rel = new unsigned char[reloc_count * reloc_size];
4110 const unsigned char* rrel = prelocs;
4111 unsigned char* wrel = opd_rel;
4112
4113 for (size_t i = 0;
4114 i < reloc_count;
4115 ++i, rrel += reloc_size, wrel += reloc_size)
4116 {
4117 typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
4118 reloc(rrel);
4119 typename elfcpp::Elf_types<size>::Elf_WXword r_info
4120 = reloc.get_r_info();
4121 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4122 Address r_off = reloc.get_r_offset();
4123 if (r_type == elfcpp::R_PPC64_TOC)
4124 r_off -= 8;
4125 bool is_discarded = object->get_opd_discard(r_off);
4126
4127 // Reloc number is reported in some errors, so keep all relocs.
4128 if (is_discarded)
4129 memset(wrel, 0, reloc_size);
4130 else
4131 memcpy(wrel, rrel, reloc_size);
4132 }
4133 prelocs = opd_rel;
4134 }
4135
42cacb20 4136 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
d83ce4e3 4137 Powerpc_relocate>(
42cacb20
DE
4138 relinfo,
4139 this,
4140 prelocs,
4141 reloc_count,
4142 output_section,
4143 needs_special_offset_handling,
4144 view,
4145 address,
364c7fa5
ILT
4146 view_size,
4147 reloc_symbol_changes);
bfdfa4cd
AM
4148
4149 if (opd_rel != NULL)
4150 delete[] opd_rel;
42cacb20
DE
4151}
4152
cf43a2fe 4153class Powerpc_scan_relocatable_reloc
42cacb20 4154{
cf43a2fe
AM
4155public:
4156 // Return the strategy to use for a local symbol which is not a
4157 // section symbol, given the relocation type.
4158 inline Relocatable_relocs::Reloc_strategy
4159 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
4160 {
4161 if (r_type == 0 && r_sym == 0)
4162 return Relocatable_relocs::RELOC_DISCARD;
4163 return Relocatable_relocs::RELOC_COPY;
4164 }
4165
4166 // Return the strategy to use for a local symbol which is a section
4167 // symbol, given the relocation type.
4168 inline Relocatable_relocs::Reloc_strategy
4169 local_section_strategy(unsigned int, Relobj*)
4170 {
4171 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
4172 }
4173
4174 // Return the strategy to use for a global symbol, given the
4175 // relocation type, the object, and the symbol index.
4176 inline Relocatable_relocs::Reloc_strategy
4177 global_strategy(unsigned int r_type, Relobj*, unsigned int)
4178 {
4179 if (r_type == elfcpp::R_PPC_PLTREL24)
4180 return Relocatable_relocs::RELOC_SPECIAL;
4181 return Relocatable_relocs::RELOC_COPY;
4182 }
4183};
42cacb20
DE
4184
4185// Scan the relocs during a relocatable link.
4186
4187template<int size, bool big_endian>
4188void
4189Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
4190 Symbol_table* symtab,
4191 Layout* layout,
4192 Sized_relobj_file<size, big_endian>* object,
4193 unsigned int data_shndx,
4194 unsigned int sh_type,
4195 const unsigned char* prelocs,
4196 size_t reloc_count,
4197 Output_section* output_section,
4198 bool needs_special_offset_handling,
4199 size_t local_symbol_count,
4200 const unsigned char* plocal_symbols,
4201 Relocatable_relocs* rr)
42cacb20
DE
4202{
4203 gold_assert(sh_type == elfcpp::SHT_RELA);
4204
42cacb20 4205 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
d83ce4e3 4206 Powerpc_scan_relocatable_reloc>(
42cacb20
DE
4207 symtab,
4208 layout,
4209 object,
4210 data_shndx,
4211 prelocs,
4212 reloc_count,
4213 output_section,
4214 needs_special_offset_handling,
4215 local_symbol_count,
4216 plocal_symbols,
4217 rr);
4218}
4219
7404fe1b 4220// Emit relocations for a section.
dd93cd0a
AM
4221// This is a modified version of the function by the same name in
4222// target-reloc.h. Using relocate_special_relocatable for
4223// R_PPC_PLTREL24 would require duplication of the entire body of the
4224// loop, so we may as well duplicate the whole thing.
42cacb20
DE
4225
4226template<int size, bool big_endian>
4227void
7404fe1b 4228Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
4229 const Relocate_info<size, big_endian>* relinfo,
4230 unsigned int sh_type,
4231 const unsigned char* prelocs,
4232 size_t reloc_count,
4233 Output_section* output_section,
4234 off_t offset_in_output_section,
4235 const Relocatable_relocs* rr,
cf43a2fe 4236 unsigned char*,
dd93cd0a 4237 Address view_address,
cf43a2fe 4238 section_size_type,
42cacb20
DE
4239 unsigned char* reloc_view,
4240 section_size_type reloc_view_size)
4241{
4242 gold_assert(sh_type == elfcpp::SHT_RELA);
4243
cf43a2fe
AM
4244 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
4245 Reltype;
4246 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
4247 Reltype_write;
4248 const int reloc_size
4249 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
cf43a2fe
AM
4250
4251 Powerpc_relobj<size, big_endian>* const object
4252 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4253 const unsigned int local_count = object->local_symbol_count();
4254 unsigned int got2_shndx = object->got2_shndx();
c9269dff 4255 Address got2_addend = 0;
cf43a2fe 4256 if (got2_shndx != 0)
c9269dff
AM
4257 {
4258 got2_addend = object->get_output_section_offset(got2_shndx);
4259 gold_assert(got2_addend != invalid_address);
4260 }
cf43a2fe
AM
4261
4262 unsigned char* pwrite = reloc_view;
7404fe1b 4263 bool zap_next = false;
cf43a2fe
AM
4264 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4265 {
4266 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4267 if (strategy == Relocatable_relocs::RELOC_DISCARD)
4268 continue;
4269
4270 Reltype reloc(prelocs);
4271 Reltype_write reloc_write(pwrite);
4272
7404fe1b 4273 Address offset = reloc.get_r_offset();
cf43a2fe 4274 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
4275 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4276 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4277 const unsigned int orig_r_sym = r_sym;
4278 typename elfcpp::Elf_types<size>::Elf_Swxword addend
4279 = reloc.get_r_addend();
4280 const Symbol* gsym = NULL;
4281
4282 if (zap_next)
4283 {
4284 // We could arrange to discard these and other relocs for
4285 // tls optimised sequences in the strategy methods, but for
4286 // now do as BFD ld does.
4287 r_type = elfcpp::R_POWERPC_NONE;
4288 zap_next = false;
4289 }
cf43a2fe
AM
4290
4291 // Get the new symbol index.
cf43a2fe
AM
4292 if (r_sym < local_count)
4293 {
4294 switch (strategy)
4295 {
4296 case Relocatable_relocs::RELOC_COPY:
4297 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 4298 if (r_sym != 0)
dd93cd0a 4299 {
7404fe1b
AM
4300 r_sym = object->symtab_index(r_sym);
4301 gold_assert(r_sym != -1U);
dd93cd0a 4302 }
cf43a2fe
AM
4303 break;
4304
4305 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4306 {
4307 // We are adjusting a section symbol. We need to find
4308 // the symbol table index of the section symbol for
4309 // the output section corresponding to input section
4310 // in which this symbol is defined.
4311 gold_assert(r_sym < local_count);
4312 bool is_ordinary;
4313 unsigned int shndx =
4314 object->local_symbol_input_shndx(r_sym, &is_ordinary);
4315 gold_assert(is_ordinary);
4316 Output_section* os = object->output_section(shndx);
4317 gold_assert(os != NULL);
4318 gold_assert(os->needs_symtab_index());
7404fe1b 4319 r_sym = os->symtab_index();
cf43a2fe
AM
4320 }
4321 break;
4322
4323 default:
4324 gold_unreachable();
4325 }
4326 }
4327 else
4328 {
7404fe1b 4329 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
4330 gold_assert(gsym != NULL);
4331 if (gsym->is_forwarder())
4332 gsym = relinfo->symtab->resolve_forwards(gsym);
4333
4334 gold_assert(gsym->has_symtab_index());
7404fe1b 4335 r_sym = gsym->symtab_index();
cf43a2fe
AM
4336 }
4337
4338 // Get the new offset--the location in the output section where
4339 // this relocation should be applied.
cf43a2fe 4340 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 4341 offset += offset_in_output_section;
cf43a2fe
AM
4342 else
4343 {
c9269dff
AM
4344 section_offset_type sot_offset =
4345 convert_types<section_offset_type, Address>(offset);
cf43a2fe 4346 section_offset_type new_sot_offset =
c9269dff
AM
4347 output_section->output_offset(object, relinfo->data_shndx,
4348 sot_offset);
cf43a2fe 4349 gold_assert(new_sot_offset != -1);
7404fe1b 4350 offset = new_sot_offset;
cf43a2fe
AM
4351 }
4352
dd93cd0a
AM
4353 // In an object file, r_offset is an offset within the section.
4354 // In an executable or dynamic object, generated by
4355 // --emit-relocs, r_offset is an absolute address.
7404fe1b 4356 if (!parameters->options().relocatable())
dd93cd0a 4357 {
7404fe1b 4358 offset += view_address;
dd93cd0a 4359 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 4360 offset -= offset_in_output_section;
dd93cd0a
AM
4361 }
4362
cf43a2fe 4363 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
4364 if (strategy == Relocatable_relocs::RELOC_COPY)
4365 ;
4366 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4367 {
7404fe1b 4368 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
cf43a2fe
AM
4369 addend = psymval->value(object, addend);
4370 }
4371 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4372 {
4373 if (addend >= 32768)
4374 addend += got2_addend;
4375 }
4376 else
4377 gold_unreachable();
4378
7404fe1b
AM
4379 if (!parameters->options().relocatable())
4380 {
4381 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4382 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4383 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4384 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4385 {
4386 // First instruction of a global dynamic sequence,
4387 // arg setup insn.
4388 const bool final = gsym == NULL || gsym->final_value_is_known();
4389 switch (this->optimize_tls_gd(final))
4390 {
4391 case tls::TLSOPT_TO_IE:
4392 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4393 - elfcpp::R_POWERPC_GOT_TLSGD16);
4394 break;
4395 case tls::TLSOPT_TO_LE:
4396 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4397 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4398 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4399 else
4400 {
4401 r_type = elfcpp::R_POWERPC_NONE;
4402 offset -= 2 * big_endian;
4403 }
4404 break;
4405 default:
4406 break;
4407 }
4408 }
4409 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4410 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4411 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4412 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4413 {
4414 // First instruction of a local dynamic sequence,
4415 // arg setup insn.
4416 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4417 {
4418 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4419 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4420 {
4421 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4422 const Output_section* os = relinfo->layout->tls_segment()
4423 ->first_section();
4424 gold_assert(os != NULL);
4425 gold_assert(os->needs_symtab_index());
4426 r_sym = os->symtab_index();
4427 addend = dtp_offset;
4428 }
4429 else
4430 {
4431 r_type = elfcpp::R_POWERPC_NONE;
4432 offset -= 2 * big_endian;
4433 }
4434 }
4435 }
4436 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4437 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4438 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4439 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4440 {
4441 // First instruction of initial exec sequence.
4442 const bool final = gsym == NULL || gsym->final_value_is_known();
4443 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4444 {
4445 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4446 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4447 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4448 else
4449 {
4450 r_type = elfcpp::R_POWERPC_NONE;
4451 offset -= 2 * big_endian;
4452 }
4453 }
4454 }
4455 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4456 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4457 {
4458 // Second instruction of a global dynamic sequence,
4459 // the __tls_get_addr call
4460 const bool final = gsym == NULL || gsym->final_value_is_known();
4461 switch (this->optimize_tls_gd(final))
4462 {
4463 case tls::TLSOPT_TO_IE:
4464 r_type = elfcpp::R_POWERPC_NONE;
4465 zap_next = true;
4466 break;
4467 case tls::TLSOPT_TO_LE:
4468 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4469 offset += 2 * big_endian;
4470 zap_next = true;
4471 break;
4472 default:
4473 break;
4474 }
4475 }
4476 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4477 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4478 {
4479 // Second instruction of a local dynamic sequence,
4480 // the __tls_get_addr call
4481 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4482 {
4483 const Output_section* os = relinfo->layout->tls_segment()
4484 ->first_section();
4485 gold_assert(os != NULL);
4486 gold_assert(os->needs_symtab_index());
4487 r_sym = os->symtab_index();
4488 addend = dtp_offset;
4489 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4490 offset += 2 * big_endian;
4491 zap_next = true;
4492 }
4493 }
4494 else if (r_type == elfcpp::R_POWERPC_TLS)
4495 {
4496 // Second instruction of an initial exec sequence
4497 const bool final = gsym == NULL || gsym->final_value_is_known();
4498 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4499 {
4500 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4501 offset += 2 * big_endian;
4502 }
4503 }
4504 }
4505
4506 reloc_write.put_r_offset(offset);
4507 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4508 reloc_write.put_r_addend(addend);
cf43a2fe
AM
4509
4510 pwrite += reloc_size;
4511 }
4512
4513 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4514 == reloc_view_size);
42cacb20
DE
4515}
4516
4517// Return the value to use for a dynamic which requires special
4518// treatment. This is how we support equality comparisons of function
4519// pointers across shared library boundaries, as described in the
4520// processor specific ABI supplement.
4521
4522template<int size, bool big_endian>
4523uint64_t
4524Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4525{
cf43a2fe
AM
4526 if (size == 32)
4527 {
4528 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4529 return this->plt_section()->address() + gsym->plt_offset();
4530 }
4531 else
4532 gold_unreachable();
42cacb20
DE
4533}
4534
bd73a62d
AM
4535// Return the offset to use for the GOT_INDX'th got entry which is
4536// for a local tls symbol specified by OBJECT, SYMNDX.
4537template<int size, bool big_endian>
4538int64_t
4539Target_powerpc<size, big_endian>::do_tls_offset_for_local(
4540 const Relobj* object,
4541 unsigned int symndx,
4542 unsigned int got_indx) const
4543{
4544 const Powerpc_relobj<size, big_endian>* ppc_object
4545 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
4546 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
4547 {
4548 for (Got_type got_type = GOT_TYPE_TLSGD;
4549 got_type <= GOT_TYPE_TPREL;
4550 got_type = Got_type(got_type + 1))
4551 if (ppc_object->local_has_got_offset(symndx, got_type))
4552 {
4553 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
4554 if (got_type == GOT_TYPE_TLSGD)
4555 off += size / 8;
4556 if (off == got_indx * (size / 8))
4557 {
4558 if (got_type == GOT_TYPE_TPREL)
4559 return -tp_offset;
4560 else
4561 return -dtp_offset;
4562 }
4563 }
4564 }
4565 gold_unreachable();
4566}
4567
4568// Return the offset to use for the GOT_INDX'th got entry which is
4569// for global tls symbol GSYM.
4570template<int size, bool big_endian>
4571int64_t
4572Target_powerpc<size, big_endian>::do_tls_offset_for_global(
4573 Symbol* gsym,
4574 unsigned int got_indx) const
4575{
4576 if (gsym->type() == elfcpp::STT_TLS)
4577 {
4578 for (Got_type got_type = GOT_TYPE_TLSGD;
4579 got_type <= GOT_TYPE_TPREL;
4580 got_type = Got_type(got_type + 1))
4581 if (gsym->has_got_offset(got_type))
4582 {
4583 unsigned int off = gsym->got_offset(got_type);
4584 if (got_type == GOT_TYPE_TLSGD)
4585 off += size / 8;
4586 if (off == got_indx * (size / 8))
4587 {
4588 if (got_type == GOT_TYPE_TPREL)
4589 return -tp_offset;
4590 else
4591 return -dtp_offset;
4592 }
4593 }
4594 }
4595 gold_unreachable();
4596}
4597
42cacb20
DE
4598// The selector for powerpc object files.
4599
4600template<int size, bool big_endian>
4601class Target_selector_powerpc : public Target_selector
4602{
4603public:
4604 Target_selector_powerpc()
4605 : Target_selector(elfcpp::EM_NONE, size, big_endian,
03ef7571
ILT
4606 (size == 64
4607 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4608 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4609 (size == 64
4610 ? (big_endian ? "elf64ppc" : "elf64lppc")
4611 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
4612 { }
4613
2e702c99
RM
4614 virtual Target*
4615 do_recognize(Input_file*, off_t, int machine, int, int)
42cacb20
DE
4616 {
4617 switch (size)
4618 {
4619 case 64:
2ea97941 4620 if (machine != elfcpp::EM_PPC64)
42cacb20
DE
4621 return NULL;
4622 break;
4623
4624 case 32:
2ea97941 4625 if (machine != elfcpp::EM_PPC)
42cacb20
DE
4626 return NULL;
4627 break;
4628
4629 default:
4630 return NULL;
4631 }
4632
7f055c20 4633 return this->instantiate_target();
42cacb20
DE
4634 }
4635
2e702c99
RM
4636 virtual Target*
4637 do_instantiate_target()
7f055c20 4638 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
4639};
4640
4641Target_selector_powerpc<32, true> target_selector_ppc32;
4642Target_selector_powerpc<32, false> target_selector_ppc32le;
4643Target_selector_powerpc<64, true> target_selector_ppc64;
4644Target_selector_powerpc<64, false> target_selector_ppc64le;
4645
4646} // End anonymous namespace.
This page took 0.501272 seconds and 4 git commands to generate.