]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/s390.cc
[GOLD] PowerPC64: Don't pretend to support multi-toc
[thirdparty/binutils-gdb.git] / gold / s390.cc
CommitLineData
e79a4bad
MK
1// s390.cc -- s390 target support for gold.
2
250d07de 3// Copyright (C) 2015-2021 Free Software Foundation, Inc.
e79a4bad
MK
4// Written by Marcin Kościelnicki <koriakin@0x04.net>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
25#include <cstring>
26
27#include "elfcpp.h"
28#include "dwarf.h"
29#include "parameters.h"
30#include "reloc.h"
31#include "s390.h"
32#include "object.h"
33#include "symtab.h"
34#include "layout.h"
35#include "output.h"
36#include "copy-relocs.h"
37#include "target.h"
38#include "target-reloc.h"
39#include "target-select.h"
40#include "tls.h"
41#include "gc.h"
42#include "icf.h"
43
44namespace
45{
46
47using namespace gold;
48
49// A class to handle the .got.plt section.
50
51template<int size>
52class Output_data_got_plt_s390 : public Output_section_data_build
53{
54 public:
55 Output_data_got_plt_s390(Layout* layout)
56 : Output_section_data_build(size/8),
57 layout_(layout)
58 { }
59
60 Output_data_got_plt_s390(Layout* layout, off_t data_size)
61 : Output_section_data_build(data_size, size/8),
62 layout_(layout)
63 { }
64
65 protected:
66 // Write out the PLT data.
67 void
68 do_write(Output_file*);
69
70 // Write to a map file.
71 void
72 do_print_to_mapfile(Mapfile* mapfile) const
73 { mapfile->print_output_data(this, "** GOT PLT"); }
74
75 private:
76 // A pointer to the Layout class, so that we can find the .dynamic
77 // section when we write out the GOT PLT section.
78 Layout* layout_;
79};
80
81// A class to handle the PLT data.
82
83template<int size>
84class Output_data_plt_s390 : public Output_section_data
85{
86 public:
87 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, true>
88 Reloc_section;
89
90 Output_data_plt_s390(Layout* layout,
91 Output_data_got<size, true>* got,
92 Output_data_got_plt_s390<size>* got_plt,
93 Output_data_space* got_irelative)
94 : Output_section_data(4), layout_(layout),
95 irelative_rel_(NULL), got_(got), got_plt_(got_plt),
96 got_irelative_(got_irelative), count_(0),
97 irelative_count_(0), free_list_()
98 { this->init(layout); }
99
100 Output_data_plt_s390(Layout* layout,
101 Output_data_got<size, true>* got,
102 Output_data_got_plt_s390<size>* got_plt,
103 Output_data_space* got_irelative,
104 unsigned int plt_count)
105 : Output_section_data((plt_count + 1) * plt_entry_size,
106 4, false),
107 layout_(layout), irelative_rel_(NULL), got_(got),
108 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
109 irelative_count_(0), free_list_()
110 {
111 this->init(layout);
112
113 // Initialize the free list and reserve the first entry.
114 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
115 this->free_list_.remove(0, plt_entry_size);
116 }
117
118 // Initialize the PLT section.
119 void
120 init(Layout* layout);
121
122 // Add an entry to the PLT.
123 void
124 add_entry(Symbol_table*, Layout*, Symbol* gsym);
125
126 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
127 unsigned int
128 add_local_ifunc_entry(Symbol_table*, Layout*,
129 Sized_relobj_file<size, true>*, unsigned int);
130
131 // Add the relocation for a PLT entry.
132 void
133 add_relocation(Symbol_table*, Layout*, Symbol*, unsigned int);
134
135 // Return the .rela.plt section data.
136 Reloc_section*
137 rela_plt()
138 { return this->rel_; }
139
140 // Return where the IRELATIVE relocations should go in the PLT
141 // relocations.
142 Reloc_section*
143 rela_irelative(Symbol_table*, Layout*);
144
145 // Return whether we created a section for IRELATIVE relocations.
146 bool
147 has_irelative_section() const
148 { return this->irelative_rel_ != NULL; }
149
150 // Return the number of PLT entries.
151 unsigned int
152 entry_count() const
153 { return this->count_ + this->irelative_count_; }
154
155 // Return the offset of the first non-reserved PLT entry.
156 unsigned int
157 first_plt_entry_offset()
158 { return plt_entry_size; }
159
160 // Return the size of a PLT entry.
161 unsigned int
162 get_plt_entry_size() const
163 { return plt_entry_size; }
164
165 // Reserve a slot in the PLT for an existing symbol in an incremental update.
166 void
167 reserve_slot(unsigned int plt_index)
168 {
169 this->free_list_.remove((plt_index + 1) * plt_entry_size,
170 (plt_index + 2) * plt_entry_size);
171 }
172
173 // Return the PLT address to use for a global symbol.
174 uint64_t
175 address_for_global(const Symbol*);
176
177 // Return the PLT address to use for a local symbol.
178 uint64_t
179 address_for_local(const Relobj*, unsigned int symndx);
180
181 // Add .eh_frame information for the PLT.
182 void
183 add_eh_frame(Layout* layout)
184 {
185 (void)layout;
186 layout->add_eh_frame_for_plt(this,
187 plt_eh_frame_cie,
188 plt_eh_frame_cie_size,
189 plt_eh_frame_fde,
190 plt_eh_frame_fde_size);
191 }
192
193 protected:
194 // Fill in the first PLT entry.
195 void
196 fill_first_plt_entry(unsigned char* pov,
197 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
198 typename elfcpp::Elf_types<size>::Elf_Addr plt_address);
199
200 // Fill in a normal PLT entry. Returns the offset into the entry that
201 // should be the initial GOT slot value.
202 unsigned int
203 fill_plt_entry(unsigned char* pov,
204 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
205 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
206 unsigned int got_offset,
207 unsigned int plt_offset,
208 unsigned int plt_rel_offset);
209
210 void
211 do_adjust_output_section(Output_section* os);
212
213 // Write to a map file.
214 void
215 do_print_to_mapfile(Mapfile* mapfile) const
216 { mapfile->print_output_data(this, _("** PLT")); }
217
218 private:
219 // Set the final size.
220 void
221 set_final_data_size();
222
223 // Write out the PLT data.
224 void
225 do_write(Output_file*);
226
227 // A pointer to the Layout class, so that we can find the .dynamic
228 // section when we write out the GOT PLT section.
229 Layout* layout_;
230 // The reloc section.
231 Reloc_section* rel_;
232 // The IRELATIVE relocs, if necessary. These must follow the
233 // regular PLT relocations.
234 Reloc_section* irelative_rel_;
235 // The .got section.
236 Output_data_got<size, true>* got_;
237 // The .got.plt section.
238 Output_data_got_plt_s390<size>* got_plt_;
239 // The part of the .got.plt section used for IRELATIVE relocs.
240 Output_data_space* got_irelative_;
241 // The number of PLT entries.
242 unsigned int count_;
243 // Number of PLT entries with R_TILEGX_IRELATIVE relocs. These
244 // follow the regular PLT entries.
245 unsigned int irelative_count_;
246 // List of available regions within the section, for incremental
247 // update links.
248 Free_list free_list_;
249
250 // The size of an entry in the PLT.
251 static const int plt_entry_size = 0x20;
252 // The first entry in the PLT.
253 static const unsigned char first_plt_entry_32_abs[plt_entry_size];
254 static const unsigned char first_plt_entry_32_pic[plt_entry_size];
255 static const unsigned char first_plt_entry_64[plt_entry_size];
256 // Other entries in the PLT for an executable.
257 static const unsigned char plt_entry_32_abs[plt_entry_size];
258 static const unsigned char plt_entry_32_pic12[plt_entry_size];
259 static const unsigned char plt_entry_32_pic16[plt_entry_size];
260 static const unsigned char plt_entry_32_pic[plt_entry_size];
261 static const unsigned char plt_entry_64[plt_entry_size];
262
263 // The .eh_frame unwind information for the PLT.
264 static const int plt_eh_frame_cie_size = 12;
265 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
266 static const int plt_eh_frame_fde_size = 12;
267 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
268};
269
270
271template<int size>
272class Target_s390 : public Sized_target<size, true>
273{
274 public:
275 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, true> Reloc_section;
276
277 Target_s390()
278 : Sized_target<size, true>(&s390_info),
279 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
280 global_offset_table_(NULL), rela_dyn_(NULL),
281 rela_irelative_(NULL), copy_relocs_(elfcpp::R_390_COPY),
282 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
283 layout_(NULL)
284 { }
285
286 // Scan the relocations to look for symbol adjustments.
287 void
288 gc_process_relocs(Symbol_table* symtab,
289 Layout* layout,
290 Sized_relobj_file<size, true>* object,
291 unsigned int data_shndx,
292 unsigned int sh_type,
293 const unsigned char* prelocs,
294 size_t reloc_count,
295 Output_section* output_section,
296 bool needs_special_offset_handling,
297 size_t local_symbol_count,
298 const unsigned char* plocal_symbols);
299
300 // Scan the relocations to look for symbol adjustments.
301 void
302 scan_relocs(Symbol_table* symtab,
303 Layout* layout,
304 Sized_relobj_file<size, true>* object,
305 unsigned int data_shndx,
306 unsigned int sh_type,
307 const unsigned char* prelocs,
308 size_t reloc_count,
309 Output_section* output_section,
310 bool needs_special_offset_handling,
311 size_t local_symbol_count,
312 const unsigned char* plocal_symbols);
313
314 // Finalize the sections.
315 void
316 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
317
318 // Return the value to use for a dynamic which requires special
319 // treatment.
320 uint64_t
321 do_dynsym_value(const Symbol*) const;
322
323 // Relocate a section.
324 void
325 relocate_section(const Relocate_info<size, true>*,
326 unsigned int sh_type,
327 const unsigned char* prelocs,
328 size_t reloc_count,
329 Output_section* output_section,
330 bool needs_special_offset_handling,
331 unsigned char* view,
332 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
333 section_size_type view_size,
334 const Reloc_symbol_changes*);
335
336 // Scan the relocs during a relocatable link.
337 void
338 scan_relocatable_relocs(Symbol_table* symtab,
339 Layout* layout,
340 Sized_relobj_file<size, true>* object,
341 unsigned int data_shndx,
342 unsigned int sh_type,
343 const unsigned char* prelocs,
344 size_t reloc_count,
345 Output_section* output_section,
346 bool needs_special_offset_handling,
347 size_t local_symbol_count,
348 const unsigned char* plocal_symbols,
349 Relocatable_relocs*);
350
4d625b70
CC
351 // Scan the relocs for --emit-relocs.
352 void
353 emit_relocs_scan(Symbol_table* symtab,
354 Layout* layout,
355 Sized_relobj_file<size, true>* object,
356 unsigned int data_shndx,
357 unsigned int sh_type,
358 const unsigned char* prelocs,
359 size_t reloc_count,
360 Output_section* output_section,
361 bool needs_special_offset_handling,
362 size_t local_symbol_count,
363 const unsigned char* plocal_syms,
364 Relocatable_relocs* rr);
365
e79a4bad
MK
366 // Return a string used to fill a code section with nops.
367 std::string
368 do_code_fill(section_size_type length) const;
369
370 // Emit relocations for a section.
371 void
372 relocate_relocs(
373 const Relocate_info<size, true>*,
374 unsigned int sh_type,
375 const unsigned char* prelocs,
376 size_t reloc_count,
377 Output_section* output_section,
378 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
e79a4bad
MK
379 unsigned char* view,
380 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
381 section_size_type view_size,
382 unsigned char* reloc_view,
383 section_size_type reloc_view_size);
384
385 // Return whether SYM is defined by the ABI.
386 bool
387 do_is_defined_by_abi(const Symbol* sym) const
388 { return strcmp(sym->name(), "__tls_get_offset") == 0; }
389
390 // Return the PLT address to use for a global symbol.
391 uint64_t
392 do_plt_address_for_global(const Symbol* gsym) const
393 { return this->plt_section()->address_for_global(gsym); }
394
395 uint64_t
396 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
397 { return this->plt_section()->address_for_local(relobj, symndx); }
398
399 // Return the offset to use for the GOT_INDX'th got entry which is
400 // for a local tls symbol specified by OBJECT, SYMNDX.
401 int64_t
402 do_tls_offset_for_local(const Relobj* object,
403 unsigned int symndx,
e4d49a0f
AM
404 unsigned int got_indx,
405 uint64_t addend) const;
e79a4bad
MK
406
407 // Return the offset to use for the GOT_INDX'th got entry which is
408 // for global tls symbol GSYM.
409 int64_t
e4d49a0f
AM
410 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx,
411 uint64_t addend) const;
e79a4bad
MK
412
413 // This function should be defined in targets that can use relocation
414 // types to determine (implemented in local_reloc_may_be_function_pointer
415 // and global_reloc_may_be_function_pointer)
416 // if a function's pointer is taken. ICF uses this in safe mode to only
417 // fold those functions whose pointer is defintely not taken.
418 bool
419 do_can_check_for_function_pointers() const
420 { return true; }
421
2b63aca3
MK
422 // Return whether SYM is call to a non-split function.
423 bool
424 do_is_call_to_non_split(const Symbol* sym, const unsigned char* preloc,
425 const unsigned char* view,
426 section_size_type view_size) const;
427
428 // Adjust -fsplit-stack code which calls non-split-stack code.
429 void
430 do_calls_non_split(Relobj* object, unsigned int shndx,
431 section_offset_type fnoffset, section_size_type fnsize,
432 const unsigned char* prelocs, size_t reloc_count,
433 unsigned char* view, section_size_type view_size,
434 std::string* from, std::string* to) const;
435
e79a4bad
MK
436 // Return the size of the GOT section.
437 section_size_type
438 got_size() const
439 {
440 gold_assert(this->got_ != NULL);
441 return this->got_->data_size();
442 }
443
444 // Return the number of entries in the GOT.
445 unsigned int
446 got_entry_count() const
447 {
448 if (this->got_ == NULL)
449 return 0;
450 return this->got_size() / (size / 8);
451 }
452
453 // Return the number of entries in the PLT.
454 unsigned int
455 plt_entry_count() const;
456
457 // Return the offset of the first non-reserved PLT entry.
458 unsigned int
459 first_plt_entry_offset() const;
460
461 // Return the size of each PLT entry.
462 unsigned int
463 plt_entry_size() const;
464
465 // Create the GOT section for an incremental update.
466 Output_data_got_base*
467 init_got_plt_for_update(Symbol_table* symtab,
468 Layout* layout,
469 unsigned int got_count,
470 unsigned int plt_count);
471
472 // Reserve a GOT entry for a local symbol, and regenerate any
473 // necessary dynamic relocations.
474 void
475 reserve_local_got_entry(unsigned int got_index,
476 Sized_relobj<size, true>* obj,
477 unsigned int r_sym,
478 unsigned int got_type);
479
480 // Reserve a GOT entry for a global symbol, and regenerate any
481 // necessary dynamic relocations.
482 void
483 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
484 unsigned int got_type);
485
486 // Register an existing PLT entry for a global symbol.
487 void
488 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
489 Symbol* gsym);
490
491 // Force a COPY relocation for a given symbol.
492 void
493 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
494
495 // Apply an incremental relocation.
496 void
497 apply_relocation(const Relocate_info<size, true>* relinfo,
498 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
499 unsigned int r_type,
500 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
501 const Symbol* gsym,
502 unsigned char* view,
503 typename elfcpp::Elf_types<size>::Elf_Addr address,
504 section_size_type view_size);
505
506 private:
507
508 // The class which scans relocations.
509 class Scan
510 {
511 public:
512 Scan()
513 : issued_non_pic_error_(false)
514 { }
515
516 static inline int
517 get_reference_flags(unsigned int r_type);
518
519 inline void
520 local(Symbol_table* symtab, Layout* layout, Target_s390* target,
521 Sized_relobj_file<size, true>* object,
522 unsigned int data_shndx,
523 Output_section* output_section,
524 const elfcpp::Rela<size, true>& reloc, unsigned int r_type,
525 const elfcpp::Sym<size, true>& lsym,
526 bool is_discarded);
527
528 inline void
529 global(Symbol_table* symtab, Layout* layout, Target_s390* target,
530 Sized_relobj_file<size, true>* object,
531 unsigned int data_shndx,
532 Output_section* output_section,
533 const elfcpp::Rela<size, true>& reloc, unsigned int r_type,
534 Symbol* gsym);
535
536 inline bool
537 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
538 Target_s390* target,
539 Sized_relobj_file<size, true>* object,
540 unsigned int data_shndx,
541 Output_section* output_section,
542 const elfcpp::Rela<size, true>& reloc,
543 unsigned int r_type,
544 const elfcpp::Sym<size, true>& lsym);
545
546 inline bool
547 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
548 Target_s390* target,
549 Sized_relobj_file<size, true>* object,
550 unsigned int data_shndx,
551 Output_section* output_section,
552 const elfcpp::Rela<size, true>& reloc,
553 unsigned int r_type,
554 Symbol* gsym);
555
556 private:
557 static void
558 unsupported_reloc_local(Sized_relobj_file<size, true>*,
559 unsigned int r_type);
560
561 static void
562 unsupported_reloc_global(Sized_relobj_file<size, true>*,
563 unsigned int r_type, Symbol*);
564
565 void
566 check_non_pic(Relobj*, unsigned int r_type);
567
568 inline bool
569 possible_function_pointer_reloc(unsigned int r_type);
570
571 bool
572 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, true>*,
573 unsigned int r_type);
574
575 // Whether we have issued an error about a non-PIC compilation.
576 bool issued_non_pic_error_;
577 };
578
579 // The class which implements relocation.
580 class Relocate
581 {
582 public:
583 // Do a relocation. Return false if the caller should not issue
584 // any warnings about this relocation.
585 inline bool
91a65d2f
AM
586 relocate(const Relocate_info<size, true>*, unsigned int,
587 Target_s390*, Output_section*, size_t, const unsigned char*,
588 const Sized_symbol<size>*, const Symbol_value<size>*,
e79a4bad
MK
589 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
590 section_size_type);
591
592 private:
593 // Do a TLS relocation.
594 inline typename elfcpp::Elf_types<size>::Elf_Addr
595 relocate_tls(const Relocate_info<size, true>*, Target_s390*,
596 size_t relnum, const elfcpp::Rela<size, true>&,
597 unsigned int r_type, const Sized_symbol<size>*,
598 const Symbol_value<size>*,
599 unsigned char*, section_size_type);
600
601 // Do a TLS General-Dynamic to Initial-Exec transition.
602 inline void
603 tls_gd_to_ie(const Relocate_info<size, true>*, size_t relnum,
604 const elfcpp::Rela<size, true>&,
605 unsigned char* view,
606 section_size_type view_size);
607
608 // Do a TLS General-Dynamic to Local-Exec transition.
609 inline void
610 tls_gd_to_le(const Relocate_info<size, true>*, size_t relnum,
611 const elfcpp::Rela<size, true>&,
612 unsigned char* view,
613 section_size_type view_size);
614
615 // Do a TLS Local-Dynamic to Local-Exec transition.
616 inline void
617 tls_ld_to_le(const Relocate_info<size, true>*, size_t relnum,
618 const elfcpp::Rela<size, true>&,
619 unsigned char* view,
620 section_size_type view_size);
621
622 // Do a TLS Initial-Exec to Local-Exec transition.
623 static inline void
624 tls_ie_to_le(const Relocate_info<size, true>*, size_t relnum,
625 const elfcpp::Rela<size, true>&,
626 unsigned char* view,
627 section_size_type view_size);
628 };
629
e79a4bad
MK
630 // Adjust TLS relocation type based on the options and whether this
631 // is a local symbol.
632 static tls::Tls_optimization
633 optimize_tls_reloc(bool is_final, int r_type);
634
635 // Get the GOT section.
636 const Output_data_got<size, true>*
637 got_section() const
638 {
639 gold_assert(this->got_ != NULL);
640 return this->got_;
641 }
642
643 // Get the GOT section, creating it if necessary.
644 Output_data_got<size, true>*
645 got_section(Symbol_table*, Layout*);
646
647 typename elfcpp::Elf_types<size>::Elf_Addr
648 got_address() const
649 {
650 gold_assert(this->got_ != NULL);
651 return this->got_plt_->address();
652 }
653
654 typename elfcpp::Elf_types<size>::Elf_Addr
655 got_main_offset() const
656 {
657 gold_assert(this->got_ != NULL);
658 return this->got_->address() - this->got_address();
659 }
660
661 // Create the PLT section.
662 void
663 make_plt_section(Symbol_table* symtab, Layout* layout);
664
665 // Create a PLT entry for a global symbol.
666 void
667 make_plt_entry(Symbol_table*, Layout*, Symbol*);
668
669 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
670 void
671 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
672 Sized_relobj_file<size, true>* relobj,
673 unsigned int local_sym_index);
674
675 // Create a GOT entry for the TLS module index.
676 unsigned int
677 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
678 Sized_relobj_file<size, true>* object);
679
680 // Get the PLT section.
681 Output_data_plt_s390<size>*
682 plt_section() const
683 {
684 gold_assert(this->plt_ != NULL);
685 return this->plt_;
686 }
687
688 // Get the dynamic reloc section, creating it if necessary.
689 Reloc_section*
690 rela_dyn_section(Layout*);
691
692 // Get the section to use for IRELATIVE relocations.
693 Reloc_section*
694 rela_irelative_section(Layout*);
695
696 // Add a potential copy relocation.
697 void
698 copy_reloc(Symbol_table* symtab, Layout* layout,
699 Sized_relobj_file<size, true>* object,
700 unsigned int shndx, Output_section* output_section,
701 Symbol* sym, const elfcpp::Rela<size, true>& reloc)
702 {
859d7987 703 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
e79a4bad
MK
704 this->copy_relocs_.copy_reloc(symtab, layout,
705 symtab->get_sized_symbol<size>(sym),
706 object, shndx, output_section,
859d7987
CC
707 r_type, reloc.get_r_offset(),
708 reloc.get_r_addend(),
709 this->rela_dyn_section(layout));
e79a4bad
MK
710 }
711
2b63aca3
MK
712 // A function for targets to call. Return whether BYTES/LEN matches
713 // VIEW/VIEW_SIZE at OFFSET. Like the one in Target, but takes
714 // an unsigned char * parameter.
715 bool
716 match_view_u(const unsigned char* view, section_size_type view_size,
717 section_offset_type offset, const unsigned char* bytes, size_t len) const
718 {
719 return this->match_view(view, view_size, offset,
720 reinterpret_cast<const char*>(bytes), len);
721 }
722
e79a4bad
MK
723 // Information about this specific target which we pass to the
724 // general Target structure.
725 static Target::Target_info s390_info;
726
727 // The types of GOT entries needed for this platform.
728 // These values are exposed to the ABI in an incremental link.
729 // Do not renumber existing values without changing the version
730 // number of the .gnu_incremental_inputs section.
731 enum Got_type
732 {
733 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
734 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
735 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
736 };
737
738 // The GOT section.
739 Output_data_got<size, true>* got_;
740 // The PLT section.
741 Output_data_plt_s390<size>* plt_;
742 // The GOT PLT section.
743 Output_data_got_plt_s390<size>* got_plt_;
744 // The GOT section for IRELATIVE relocations.
745 Output_data_space* got_irelative_;
746 // The _GLOBAL_OFFSET_TABLE_ symbol.
747 Symbol* global_offset_table_;
748 // The dynamic reloc section.
749 Reloc_section* rela_dyn_;
750 // The section to use for IRELATIVE relocs.
751 Reloc_section* rela_irelative_;
752 // Relocs saved to avoid a COPY reloc.
753 Copy_relocs<elfcpp::SHT_RELA, size, true> copy_relocs_;
754 // Offset of the GOT entry for the TLS module index.
755 unsigned int got_mod_index_offset_;
756 // True if the _TLS_MODULE_BASE_ symbol has been defined.
757 bool tls_base_symbol_defined_;
758 // For use in do_tls_offset_for_*
759 Layout *layout_;
2b63aca3
MK
760
761 // Code sequences for -fsplit-stack matching.
2b63aca3
MK
762 static const unsigned char ss_code_bras_8[];
763 static const unsigned char ss_code_l_basr[];
764 static const unsigned char ss_code_a_basr[];
2b63aca3
MK
765 static const unsigned char ss_code_larl[];
766 static const unsigned char ss_code_brasl[];
767 static const unsigned char ss_code_jg[];
768 static const unsigned char ss_code_jgl[];
769
770 // Variable code sequence matchers for -fsplit-stack.
40d85a7f
MK
771 bool ss_match_st_r14(unsigned char* view,
772 section_size_type view_size,
773 section_offset_type *offset) const;
774 bool ss_match_l_r14(unsigned char* view,
775 section_size_type view_size,
776 section_offset_type *offset) const;
2b63aca3
MK
777 bool ss_match_mcount(unsigned char* view,
778 section_size_type view_size,
779 section_offset_type *offset) const;
40d85a7f
MK
780 bool ss_match_ear(unsigned char* view,
781 section_size_type view_size,
782 section_offset_type *offset) const;
783 bool ss_match_c(unsigned char* view,
784 section_size_type view_size,
785 section_offset_type *offset) const;
2b63aca3
MK
786 bool ss_match_l(unsigned char* view,
787 section_size_type view_size,
788 section_offset_type *offset,
789 int *guard_reg) const;
790 bool ss_match_ahi(unsigned char* view,
791 section_size_type view_size,
792 section_offset_type *offset,
793 int guard_reg,
794 uint32_t *arg) const;
795 bool ss_match_alfi(unsigned char* view,
796 section_size_type view_size,
797 section_offset_type *offset,
798 int guard_reg,
799 uint32_t *arg) const;
800 bool ss_match_cr(unsigned char* view,
801 section_size_type view_size,
802 section_offset_type *offset,
803 int guard_reg) const;
e79a4bad
MK
804};
805
806template<>
807Target::Target_info Target_s390<32>::s390_info =
808{
809 32, // size
810 true, // is_big_endian
811 elfcpp::EM_S390, // machine_code
812 false, // has_make_symbol
813 false, // has_resolve
814 true, // has_code_fill
815 true, // is_default_stack_executable
816 true, // can_icf_inline_merge_sections
817 '\0', // wrap_char
818 "/lib/ld.so.1", // dynamic_linker
819 0x00400000, // default_text_segment_address
820 4 * 1024, // abi_pagesize (overridable by -z max-page-size)
821 4 * 1024, // common_pagesize (overridable by -z common-page-size)
822 false, // isolate_execinstr
823 0, // rosegment_gap
824 elfcpp::SHN_UNDEF, // small_common_shndx
825 elfcpp::SHN_UNDEF, // large_common_shndx
826 0, // small_common_section_flags
827 0, // large_common_section_flags
828 NULL, // attributes_section
829 NULL, // attributes_vendor
830 "_start", // entry_symbol_name
831 32, // hash_entry_size
bce5a025 832 elfcpp::SHT_PROGBITS, // unwind_section_type
e79a4bad
MK
833};
834
835template<>
836Target::Target_info Target_s390<64>::s390_info =
837{
838 64, // size
839 true, // is_big_endian
840 elfcpp::EM_S390, // machine_code
841 false, // has_make_symbol
842 false, // has_resolve
843 true, // has_code_fill
844 true, // is_default_stack_executable
845 true, // can_icf_inline_merge_sections
846 '\0', // wrap_char
847 "/lib/ld64.so.1", // dynamic_linker
848 0x80000000ll, // default_text_segment_address
849 4 * 1024, // abi_pagesize (overridable by -z max-page-size)
850 4 * 1024, // common_pagesize (overridable by -z common-page-size)
851 false, // isolate_execinstr
852 0, // rosegment_gap
853 elfcpp::SHN_UNDEF, // small_common_shndx
854 elfcpp::SHN_UNDEF, // large_common_shndx
855 0, // small_common_section_flags
856 0, // large_common_section_flags
857 NULL, // attributes_section
858 NULL, // attributes_vendor
859 "_start", // entry_symbol_name
860 64, // hash_entry_size
bce5a025 861 elfcpp::SHT_PROGBITS, // unwind_section_type
e79a4bad
MK
862};
863
864template<int size>
865class S390_relocate_functions
866{
867public:
868 enum Overflow_check
869 {
870 CHECK_NONE,
871 CHECK_SIGNED,
872 CHECK_UNSIGNED,
873 CHECK_BITFIELD,
874 CHECK_LOW_INSN,
875 CHECK_HIGH_INSN
876 };
877
878 enum Status
879 {
880 STATUS_OK,
881 STATUS_OVERFLOW
882 };
883
884private:
885 typedef S390_relocate_functions<size> This;
886 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
887
888 template<int valsize>
889 static inline bool
890 has_overflow_signed(Address value)
891 {
892 // limit = 1 << (valsize - 1) without shift count exceeding size of type
893 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
894 limit <<= ((valsize - 1) >> 1);
895 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
896 return value + limit > (limit << 1) - 1;
897 }
898
899 template<int valsize>
900 static inline bool
901 has_overflow_unsigned(Address value)
902 {
903 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
904 limit <<= ((valsize - 1) >> 1);
905 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
906 return value > (limit << 1) - 1;
907 }
908
909 template<int fieldsize>
910 static inline void
911 rela(unsigned char* view, Address mask, Address value)
912 {
913 typedef typename elfcpp::Swap<fieldsize, true>::Valtype Valtype;
914 Valtype* wv = reinterpret_cast<Valtype*>(view);
915 Valtype val = elfcpp::Swap<fieldsize, true>::readval(view);
916 val &= ~mask;
917 value &= mask;
918 elfcpp::Swap<fieldsize, true>::writeval(wv, val | value);
919 }
920
921public:
922 // R_390_12, R_390_GOT12, R_390_GOTPLT12, R_390_GOTIE12
923 static inline Status
924 rela12(unsigned char* view, Address value)
925 {
926 if (This::template has_overflow_unsigned<12>(value))
927 return STATUS_OVERFLOW;
928 This::template rela<16>(view, 0x0fff, value);
929 return STATUS_OK;
930 }
931
932 // R_390_16, R_390_GOT16, R_390_GOTPLT16, R_390_GOTOFF16, R_390_PLTOFF16
933 static inline Status
934 rela16(unsigned char* view, Address value)
935 {
936 if (This::template has_overflow_signed<16>(value))
937 return STATUS_OVERFLOW;
938 This::template rela<16>(view, 0xffff, value);
939 return STATUS_OK;
940 }
941
942 // R_390_20, R_390_GOT20, R_390_GOTPLT20, R_390_GOTIE20
943 static inline Status
944 rela20(unsigned char* view, Address value)
945 {
946 if (This::template has_overflow_signed<20>(value))
947 return STATUS_OVERFLOW;
948 This::template rela<16>(view, 0x0fff, value);
949 This::template rela<16>(view + 2, 0xff00, value >> (12 - 8));
950 return STATUS_OK;
951 }
952
953 // R_390_PC12DBL, R_390_PLT12DBL
954 static inline Status
955 pcrela12dbl(unsigned char* view, Address value, Address address)
956 {
957 value -= address;
958 if ((value & 1) != 0)
959 return STATUS_OVERFLOW;
960 if (This::template has_overflow_signed<13>(value))
961 return STATUS_OVERFLOW;
962 value >>= 1;
963 This::template rela<16>(view, 0x0fff, value);
964 return STATUS_OK;
965 }
966
967 // R_390_PC16DBL, R_390_PLT16DBL
968 static inline Status
969 pcrela16dbl(unsigned char* view, Address value, Address address)
970 {
971 value -= address;
972 if ((value & 1) != 0)
973 return STATUS_OVERFLOW;
974 if (This::template has_overflow_signed<17>(value))
975 return STATUS_OVERFLOW;
976 value >>= 1;
977 This::template rela<16>(view, 0xffff, value);
978 return STATUS_OK;
979 }
980
981 // R_390_PC24DBL, R_390_PLT24DBL
982 static inline Status
983 pcrela24dbl(unsigned char* view, Address value, Address address)
984 {
985 value -= address;
986 if ((value & 1) != 0)
987 return STATUS_OVERFLOW;
988 if (This::template has_overflow_signed<25>(value))
989 return STATUS_OVERFLOW;
990 value >>= 1;
991 // Swap doesn't take 24-bit fields well...
992 This::template rela<8>(view, 0xff, value >> 16);
993 This::template rela<16>(view + 1, 0xffff, value);
994 return STATUS_OK;
995 }
996
997 // R_390_PC32DBL, R_390_PLT32DBL, R_390_GOTPCDBL, R_390_GOTENT, R_390_GOTPLTENT
998 static inline Status
999 pcrela32dbl(unsigned char* view, Address value, Address address)
1000 {
1001 Address reloc = value - address;
1002 if ((reloc & 1) != 0)
1003 {
1004 gold_warning(_("R_390_PC32DBL target misaligned at %llx"), (long long)address);
1005 // Wait for a fix for https://sourceware.org/bugzilla/show_bug.cgi?id=18960
1006 // return STATUS_OVERFLOW;
1007 }
1008 if (This::template has_overflow_signed<33>(reloc))
1009 return STATUS_OVERFLOW;
1010 reloc >>= 1;
1011 if (value < address && size == 32)
1012 reloc |= 0x80000000;
1013 This::template rela<32>(view, 0xffffffff, reloc);
1014 return STATUS_OK;
1015 }
1016
1017};
1018
1019// Initialize the PLT section.
1020
1021template<int size>
1022void
1023Output_data_plt_s390<size>::init(Layout* layout)
1024{
1025 this->rel_ = new Reloc_section(false);
1026 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1027 elfcpp::SHF_ALLOC, this->rel_,
1028 ORDER_DYNAMIC_PLT_RELOCS, false);
1029}
1030
1031template<int size>
1032void
1033Output_data_plt_s390<size>::do_adjust_output_section(Output_section* os)
1034{
1035 os->set_entsize(plt_entry_size);
1036}
1037
1038// Add an entry to the PLT.
1039
1040template<int size>
1041void
1042Output_data_plt_s390<size>::add_entry(Symbol_table* symtab, Layout* layout,
1043 Symbol* gsym)
1044{
1045 gold_assert(!gsym->has_plt_offset());
1046
1047 unsigned int plt_index;
1048 off_t plt_offset;
1049 section_offset_type got_offset;
1050
1051 unsigned int* pcount;
1052 unsigned int offset;
1053 unsigned int reserved;
1054 Output_section_data_build* got;
1055 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1056 && gsym->can_use_relative_reloc(false))
1057 {
1058 pcount = &this->irelative_count_;
1059 offset = 0;
1060 reserved = 0;
1061 got = this->got_irelative_;
1062 }
1063 else
1064 {
1065 pcount = &this->count_;
1066 offset = 1;
1067 reserved = 3;
1068 got = this->got_plt_;
1069 }
1070
1071 if (!this->is_data_size_valid())
1072 {
1073 // Note that when setting the PLT offset for a non-IRELATIVE
1074 // entry we skip the initial reserved PLT entry.
1075 plt_index = *pcount + offset;
1076 plt_offset = plt_index * plt_entry_size;
1077
1078 ++*pcount;
1079
1080 got_offset = (plt_index - offset + reserved) * size / 8;
1081 gold_assert(got_offset == got->current_data_size());
1082
1083 // Every PLT entry needs a GOT entry which points back to the PLT
1084 // entry (this will be changed by the dynamic linker, normally
1085 // lazily when the function is called).
1086 got->set_current_data_size(got_offset + size / 8);
1087 }
1088 else
1089 {
1090 // FIXME: This is probably not correct for IRELATIVE relocs.
1091
1092 // For incremental updates, find an available slot.
1093 plt_offset = this->free_list_.allocate(plt_entry_size,
1094 plt_entry_size, 0);
1095 if (plt_offset == -1)
1096 gold_fallback(_("out of patch space (PLT);"
1097 " relink with --incremental-full"));
1098
1099 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1100 // can be calculated from the PLT index, adjusting for the three
1101 // reserved entries at the beginning of the GOT.
1102 plt_index = plt_offset / plt_entry_size - 1;
1103 got_offset = (plt_index - offset + reserved) * size / 8;
1104 }
1105
1106 gsym->set_plt_offset(plt_offset);
1107
1108 // Every PLT entry needs a reloc.
1109 this->add_relocation(symtab, layout, gsym, got_offset);
1110
1111 // Note that we don't need to save the symbol. The contents of the
1112 // PLT are independent of which symbols are used. The symbols only
1113 // appear in the relocations.
1114}
1115
1116// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1117// the PLT offset.
1118
1119template<int size>
1120unsigned int
1121Output_data_plt_s390<size>::add_local_ifunc_entry(
1122 Symbol_table* symtab,
1123 Layout* layout,
1124 Sized_relobj_file<size, true>* relobj,
1125 unsigned int local_sym_index)
1126{
1127 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
1128 ++this->irelative_count_;
1129
1130 section_offset_type got_offset = this->got_irelative_->current_data_size();
1131
1132 // Every PLT entry needs a GOT entry which points back to the PLT
1133 // entry.
1134 this->got_irelative_->set_current_data_size(got_offset + size / 8);
1135
1136 // Every PLT entry needs a reloc.
1137 Reloc_section* rela = this->rela_irelative(symtab, layout);
1138 rela->add_symbolless_local_addend(relobj, local_sym_index,
1139 elfcpp::R_390_IRELATIVE,
1140 this->got_irelative_, got_offset, 0);
1141
1142 return plt_offset;
1143}
1144
1145// Add the relocation for a PLT entry.
1146
1147template<int size>
1148void
1149Output_data_plt_s390<size>::add_relocation(Symbol_table* symtab,
1150 Layout* layout,
1151 Symbol* gsym,
1152 unsigned int got_offset)
1153{
1154 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1155 && gsym->can_use_relative_reloc(false))
1156 {
1157 Reloc_section* rela = this->rela_irelative(symtab, layout);
1158 rela->add_symbolless_global_addend(gsym, elfcpp::R_390_IRELATIVE,
1159 this->got_irelative_, got_offset, 0);
1160 }
1161 else
1162 {
1163 gsym->set_needs_dynsym_entry();
1164 this->rel_->add_global(gsym, elfcpp::R_390_JMP_SLOT, this->got_plt_,
1165 got_offset, 0);
1166 }
1167}
1168
1169// Return where the IRELATIVE relocations should go in the PLT. These
1170// follow the JUMP_SLOT and the TLSDESC relocations.
1171
1172template<int size>
1173typename Output_data_plt_s390<size>::Reloc_section*
1174Output_data_plt_s390<size>::rela_irelative(Symbol_table* symtab,
1175 Layout* layout)
1176{
1177 if (this->irelative_rel_ == NULL)
1178 {
1179 this->irelative_rel_ = new Reloc_section(false);
1180 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1181 elfcpp::SHF_ALLOC, this->irelative_rel_,
1182 ORDER_DYNAMIC_PLT_RELOCS, false);
1183 gold_assert(this->irelative_rel_->output_section()
1184 == this->rel_->output_section());
1185
1186 if (parameters->doing_static_link())
1187 {
1188 // A statically linked executable will only have a .rela.plt
1189 // section to hold R_390_IRELATIVE relocs for
1190 // STT_GNU_IFUNC symbols. The library will use these
1191 // symbols to locate the IRELATIVE relocs at program startup
1192 // time.
1193 symtab->define_in_output_data("__rela_iplt_start", NULL,
1194 Symbol_table::PREDEFINED,
1195 this->irelative_rel_, 0, 0,
1196 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1197 elfcpp::STV_HIDDEN, 0, false, true);
1198 symtab->define_in_output_data("__rela_iplt_end", NULL,
1199 Symbol_table::PREDEFINED,
1200 this->irelative_rel_, 0, 0,
1201 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1202 elfcpp::STV_HIDDEN, 0, true, true);
1203 }
1204 }
1205 return this->irelative_rel_;
1206}
1207
1208// Return the PLT address to use for a global symbol.
1209
1210template<int size>
1211uint64_t
1212Output_data_plt_s390<size>::address_for_global(const Symbol* gsym)
1213{
1214 uint64_t offset = 0;
1215 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1216 && gsym->can_use_relative_reloc(false))
1217 offset = (this->count_ + 1) * plt_entry_size;
1218 return this->address() + offset + gsym->plt_offset();
1219}
1220
1221// Return the PLT address to use for a local symbol. These are always
1222// IRELATIVE relocs.
1223
1224template<int size>
1225uint64_t
1226Output_data_plt_s390<size>::address_for_local(const Relobj* object,
1227 unsigned int r_sym)
1228{
1229 return (this->address()
1230 + (this->count_ + 1) * plt_entry_size
1231 + object->local_plt_offset(r_sym));
1232}
1233
1234// Set the final size.
1235template<int size>
1236void
1237Output_data_plt_s390<size>::set_final_data_size()
1238{
1239 unsigned int count = this->count_ + this->irelative_count_;
1240 this->set_data_size((count + 1) * plt_entry_size);
1241}
1242
1243template<int size>
1244const unsigned char
1245Output_data_plt_s390<size>::first_plt_entry_32_abs[plt_entry_size] =
1246{
1247 0x50, 0x10, 0xf0, 0x1c, // st %r1, 28(%r15)
1248 0x0d, 0x10, // basr %r1, %r0
1249 0x58, 0x10, 0x10, 0x12, // l %r1, 18(%r1)
1250 0xd2, 0x03, 0xf0, 0x18, 0x10, 0x04, // mvc 24(4,%r15), 4(%r1)
1251 0x58, 0x10, 0x10, 0x08, // l %r1, 8(%r1)
1252 0x07, 0xf1, // br %r1
1253 0x00, 0x00, // padding
1254 0x00, 0x00, 0x00, 0x00, // _GLOBAL_OFFSET_TABLE_ (to fill)
1255 0x00, 0x00, 0x00, 0x00, // padding
1256};
1257
1258template<int size>
1259const unsigned char
1260Output_data_plt_s390<size>::first_plt_entry_32_pic[plt_entry_size] =
1261{
1262 0x50, 0x10, 0xf0, 0x1c, // st %r1, 28(%r15)
1263 0x58, 0x10, 0xc0, 0x04, // l %r1, 4(%r12)
1264 0x50, 0x10, 0xf0, 0x18, // st %r1, 24(%r15)
1265 0x58, 0x10, 0xc0, 0x08, // l %r1, 8(%r12)
1266 0x07, 0xf1, // br %r1
1267 0x00, 0x00, // padding
1268 0x00, 0x00, 0x00, 0x00, // padding
1269 0x00, 0x00, 0x00, 0x00, // padding
1270 0x00, 0x00, 0x00, 0x00, // padding
1271};
1272
1273template<int size>
1274const unsigned char
1275Output_data_plt_s390<size>::first_plt_entry_64[plt_entry_size] =
1276{
1277 0xe3, 0x10, 0xf0, 0x38, 0x00, 0x24, // stg %r1, 56(%r15)
1278 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, // larl %r1, _GLOBAL_OFFSET_TABLE_ (to fill)
1279 0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08, // mvc 48(8,%r15), 8(%r1)
1280 0xe3, 0x10, 0x10, 0x10, 0x00, 0x04, // lg %r1, 16(%r1)
1281 0x07, 0xf1, // br %r1
1282 0x07, 0x00, // nopr
1283 0x07, 0x00, // nopr
1284 0x07, 0x00, // nopr
1285};
1286
1287template<int size>
1288void
1289Output_data_plt_s390<size>::fill_first_plt_entry(
1290 unsigned char* pov,
1291 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1292 typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
1293{
1294 if (size == 64)
1295 {
1296 memcpy(pov, first_plt_entry_64, plt_entry_size);
1297 S390_relocate_functions<size>::pcrela32dbl(pov + 8, got_address, (plt_address + 6));
1298 }
1299 else if (!parameters->options().output_is_position_independent())
1300 {
1301 memcpy(pov, first_plt_entry_32_abs, plt_entry_size);
1302 elfcpp::Swap<32, true>::writeval(pov + 24, got_address);
1303 }
1304 else
1305 {
1306 memcpy(pov, first_plt_entry_32_pic, plt_entry_size);
1307 }
1308}
1309
1310template<int size>
1311const unsigned char
1312Output_data_plt_s390<size>::plt_entry_32_abs[plt_entry_size] =
1313{
1314 // first part
1315 0x0d, 0x10, // basr %r1, %r0
1316 0x58, 0x10, 0x10, 0x16, // l %r1, 22(%r1)
1317 0x58, 0x10, 0x10, 0x00, // l %r1, 0(%r1)
1318 0x07, 0xf1, // br %r1
1319 // second part
1320 0x0d, 0x10, // basr %r1, %r0
1321 0x58, 0x10, 0x10, 0x0e, // l %r1, 14(%r1)
1322 0xa7, 0xf4, 0x00, 0x00, // j first_plt_entry (to fill)
1323 0x00, 0x00, // padding
1324 0x00, 0x00, 0x00, 0x00, // _GLOBAL_OFFSET_TABLE_+sym@gotplt (to fill)
1325 0x00, 0x00, 0x00, 0x00, // offset of relocation in .rela.plt (to fill)
1326};
1327
1328template<int size>
1329const unsigned char
1330Output_data_plt_s390<size>::plt_entry_32_pic12[plt_entry_size] =
1331{
1332 // first part
1333 0x58, 0x10, 0xc0, 0x00, // l %r1, sym@gotplt(%r12) (to fill)
1334 0x07, 0xf1, // br %r1
1335 0x00, 0x00, // padding
1336 0x00, 0x00, 0x00, 0x00, // padding
1337 // second part
1338 0x0d, 0x10, // basr %r1, %r0
1339 0x58, 0x10, 0x10, 0x0e, // l %r1, 14(%r1)
1340 0xa7, 0xf4, 0x00, 0x00, // j first_plt_entry (to fill)
1341 0x00, 0x00, // padding
1342 0x00, 0x00, 0x00, 0x00, // padding
1343 0x00, 0x00, 0x00, 0x00, // offset of relocation in .rela.plt (to fill)
1344};
1345
1346template<int size>
1347const unsigned char
1348Output_data_plt_s390<size>::plt_entry_32_pic16[plt_entry_size] =
1349{
1350 // first part
1351 0xa7, 0x18, 0x00, 0x00, // lhi %r1, sym@gotplt (to fill)
1352 0x58, 0x11, 0xc0, 0x00, // l %r1, 0(%r1, %r12)
1353 0x07, 0xf1, // br %r1
1354 0x00, 0x00, // padding
1355 // second part
1356 0x0d, 0x10, // basr %r1, %r0
1357 0x58, 0x10, 0x10, 0x0e, // l %r1, 14(%r1)
1358 0xa7, 0xf4, 0x00, 0x00, // j first_plt_entry (to fill)
1359 0x00, 0x00, // padding
1360 0x00, 0x00, 0x00, 0x00, // padding
1361 0x00, 0x00, 0x00, 0x00, // offset of relocation in .rela.plt (to fill)
1362};
1363
1364template<int size>
1365const unsigned char
1366Output_data_plt_s390<size>::plt_entry_32_pic[plt_entry_size] =
1367{
1368 // first part
1369 0x0d, 0x10, // basr %r1, %r0
1370 0x58, 0x10, 0x10, 0x16, // l %r1, 22(%r1)
1371 0x58, 0x11, 0xc0, 0x00, // l %r1, 0(%r1, %r12)
1372 0x07, 0xf1, // br %r1
1373 // second part
1374 0x0d, 0x10, // basr %r1, %r0
1375 0x58, 0x10, 0x10, 0x0e, // l %r1, 14(%r1)
1376 0xa7, 0xf4, 0x00, 0x00, // j first_plt_entry (to fill)
1377 0x00, 0x00, // padding
1378 0x00, 0x00, 0x00, 0x00, // sym@gotplt (to fill)
1379 0x00, 0x00, 0x00, 0x00, // offset of relocation in .rela.plt (to fill)
1380};
1381
1382template<int size>
1383const unsigned char
1384Output_data_plt_s390<size>::plt_entry_64[plt_entry_size] =
1385{
1386 // first part
1387 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, // larl %r1, _GLOBAL_OFFSET_TABLE_+off (to fill)
1388 0xe3, 0x10, 0x10, 0x00, 0x00, 0x04, // lg %r1, 0(%r1)
1389 0x07, 0xf1, // br %r1
1390 // second part
1391 0x0d, 0x10, // basr %r1, %r0
1392 0xe3, 0x10, 0x10, 0x0c, 0x00, 0x14, // lgf %r1, 12(%r1)
1393 0xc0, 0xf4, 0x00, 0x00, 0x00, 0x00, // jg first_plt_entry (to fill)
1394 0x00, 0x00, 0x00, 0x00, // offset of relocation in .rela.plt (to fill)
1395};
1396
1397template<int size>
1398unsigned int
1399Output_data_plt_s390<size>::fill_plt_entry(
1400 unsigned char* pov,
1401 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
1402 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
1403 unsigned int got_offset,
1404 unsigned int plt_offset,
1405 unsigned int plt_rel_offset)
1406{
1407 if (size == 64)
1408 {
1409 memcpy(pov, plt_entry_64, plt_entry_size);
1410 S390_relocate_functions<size>::pcrela32dbl(pov + 2, got_address + got_offset, plt_address + plt_offset);
1411 S390_relocate_functions<size>::pcrela32dbl(pov + 24, plt_address, plt_address + plt_offset + 22);
1412 }
1413 else
1414 {
1415 if (!parameters->options().output_is_position_independent())
1416 {
1417 memcpy(pov, plt_entry_32_abs, plt_entry_size);
1418 elfcpp::Swap<32, true>::writeval(pov + 24, got_address + got_offset);
1419 }
1420 else
1421 {
1422 if (got_offset < 0x1000)
1423 {
1424 memcpy(pov, plt_entry_32_pic12, plt_entry_size);
1425 S390_relocate_functions<size>::rela12(pov + 2, got_offset);
1426 }
1427 else if (got_offset < 0x8000)
1428 {
1429 memcpy(pov, plt_entry_32_pic16, plt_entry_size);
1430 S390_relocate_functions<size>::rela16(pov + 2, got_offset);
1431 }
1432 else
1433 {
1434 memcpy(pov, plt_entry_32_pic, plt_entry_size);
1435 elfcpp::Swap<32, true>::writeval(pov + 24, got_offset);
1436 }
1437 }
1438 typename elfcpp::Elf_types<size>::Elf_Addr target = plt_address;
1439 if (plt_offset >= 0x10000)
1440 {
1441 // Would overflow pcrela16dbl - aim at the farthest previous jump
1442 // we can reach.
1443 if (plt_offset > 0x10000)
1444 {
1445 // Use the full range of pcrel16dbl.
1446 target = plt_address + plt_offset - 0x10000 + 18;
1447 }
1448 else
1449 {
1450 // if plt_offset is exactly 0x10000, the above would aim at 18th byte
1451 // of first_plt_entry, which doesn't have the jump back like the others.
1452 // Aim at the next entry instead.
1453 target = plt_address + plt_offset - 0xffe0 + 18;
1454 }
1455 }
1456 S390_relocate_functions<size>::pcrela16dbl(pov + 20, target, plt_address + plt_offset + 18);
1457 }
1458 elfcpp::Swap<32, true>::writeval(pov + 28, plt_rel_offset);
1459 if (size == 64)
1460 return 14;
1461 else
1462 return 12;
1463}
1464
1465// The .eh_frame unwind information for the PLT.
1466
1467template<>
1468const unsigned char
1469Output_data_plt_s390<32>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1470{
1471 1, // CIE version.
1472 'z', // Augmentation: augmentation size included.
1473 'R', // Augmentation: FDE encoding included.
1474 '\0', // End of augmentation string.
1475 1, // Code alignment factor.
1476 0x7c, // Data alignment factor.
1477 14, // Return address column.
1478 1, // Augmentation size.
1479 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1480 | elfcpp::DW_EH_PE_sdata4),
1481 elfcpp::DW_CFA_def_cfa, 15, 0x60, // DW_CFA_def_cfa: r15 ofs 0x60.
1482};
1483
1484template<>
1485const unsigned char
1486Output_data_plt_s390<64>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1487{
1488 1, // CIE version.
1489 'z', // Augmentation: augmentation size included.
1490 'R', // Augmentation: FDE encoding included.
1491 '\0', // End of augmentation string.
1492 1, // Code alignment factor.
1493 0x78, // Data alignment factor.
1494 14, // Return address column.
1495 1, // Augmentation size.
1496 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1497 | elfcpp::DW_EH_PE_sdata4),
1498 elfcpp::DW_CFA_def_cfa, 15, 0xa0, // DW_CFA_def_cfa: r15 ofs 0xa0.
1499};
1500
1501template<int size>
1502const unsigned char
1503Output_data_plt_s390<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1504{
1505 0, 0, 0, 0, // Replaced with offset to .plt.
1506 0, 0, 0, 0, // Replaced with size of .plt.
1507 0, // Augmentation size.
1508 elfcpp::DW_CFA_nop,
1509 elfcpp::DW_CFA_nop,
1510 elfcpp::DW_CFA_nop
1511};
1512
1513// Write out the PLT. This uses the hand-coded instructions above,
1514// and adjusts them as needed.
1515
1516template<int size>
1517void
1518Output_data_plt_s390<size>::do_write(Output_file* of)
1519{
1520 const off_t offset = this->offset();
1521 const section_size_type oview_size =
1522 convert_to_section_size_type(this->data_size());
1523 unsigned char* const oview = of->get_output_view(offset, oview_size);
1524
1525 const off_t got_file_offset = this->got_plt_->offset();
1526 gold_assert(parameters->incremental_update()
1527 || (got_file_offset + this->got_plt_->data_size()
1528 == this->got_irelative_->offset()));
1529 const section_size_type got_size =
1530 convert_to_section_size_type(this->got_plt_->data_size()
1531 + this->got_irelative_->data_size());
1532 unsigned char* const got_view = of->get_output_view(got_file_offset,
1533 got_size);
1534
1535 unsigned char* pov = oview;
1536
1537 // The base address of the .plt section.
1538 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1539 // The base address of the PLT portion of the .got section,
1540 // which is where the GOT pointer will point, and where the
1541 // three reserved GOT entries are located.
1542 typename elfcpp::Elf_types<size>::Elf_Addr got_address
1543 = this->got_plt_->address();
1544
1545 this->fill_first_plt_entry(pov, got_address, plt_address);
1546 pov += this->get_plt_entry_size();
1547
1548 unsigned char* got_pov = got_view;
1549
1550 const int rel_size = elfcpp::Elf_sizes<size>::rela_size;
1551
1552 unsigned int plt_offset = this->get_plt_entry_size();
1553 unsigned int plt_rel_offset = 0;
1554 unsigned int got_offset = 3 * size / 8;
1555 const unsigned int count = this->count_ + this->irelative_count_;
1556 // The first three entries in the GOT are reserved, and are written
1557 // by Output_data_got_plt_s390::do_write.
1558 got_pov += 3 * size / 8;
1559
1560 for (unsigned int plt_index = 0;
1561 plt_index < count;
1562 ++plt_index,
1563 pov += plt_entry_size,
1564 got_pov += size / 8,
1565 plt_offset += plt_entry_size,
1566 plt_rel_offset += rel_size,
1567 got_offset += size / 8)
1568 {
1569 // Set and adjust the PLT entry itself.
1570 unsigned int lazy_offset = this->fill_plt_entry(pov,
1571 got_address, plt_address,
1572 got_offset, plt_offset,
1573 plt_rel_offset);
1574
1575 // Set the entry in the GOT.
1576 elfcpp::Swap<size, true>::writeval(got_pov,
1577 plt_address + plt_offset + lazy_offset);
1578 }
1579
1580 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1581 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1582
1583 of->write_output_view(offset, oview_size, oview);
1584 of->write_output_view(got_file_offset, got_size, got_view);
1585}
1586
1587// Get the GOT section, creating it if necessary.
1588
1589template<int size>
1590Output_data_got<size, true>*
1591Target_s390<size>::got_section(Symbol_table* symtab, Layout* layout)
1592{
1593 if (this->got_ == NULL)
1594 {
1595 gold_assert(symtab != NULL && layout != NULL);
1596
1597 // When using -z now, we can treat .got as a relro section.
1598 // Without -z now, it is modified after program startup by lazy
1599 // PLT relocations.
1600 bool is_got_relro = parameters->options().now();
1601 Output_section_order got_order = (is_got_relro
1602 ? ORDER_RELRO_LAST
1603 : ORDER_DATA);
1604
1605 // The old GNU linker creates a .got.plt section. We just
1606 // create another set of data in the .got section. Note that we
1607 // always create a PLT if we create a GOT, although the PLT
1608 // might be empty.
1609 this->got_plt_ = new Output_data_got_plt_s390<size>(layout);
1610 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1611 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
1612 this->got_plt_, got_order, is_got_relro);
1613
1614 // The first three entries are reserved.
1615 this->got_plt_->set_current_data_size(3 * size / 8);
1616
1617 // If there are any IRELATIVE relocations, they get GOT entries
1618 // in .got.plt after the jump slot entries.
1619 this->got_irelative_ = new Output_data_space(size / 8, "** GOT IRELATIVE PLT");
1620 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1621 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
1622 this->got_irelative_,
1623 got_order, is_got_relro);
1624
1625 // Unlike some targets (.e.g x86), S/390 does not use separate .got and
1626 // .got.plt sections in output. The output .got section contains both
1627 // PLT and non-PLT GOT entries.
1628 this->got_ = new Output_data_got<size, true>();
1629
1630 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1631 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
1632 this->got_, got_order, is_got_relro);
1633
1634 // Define _GLOBAL_OFFSET_TABLE_ at the start of the GOT.
1635 this->global_offset_table_ =
1636 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1637 Symbol_table::PREDEFINED,
1638 this->got_plt_,
1639 0, 0, elfcpp::STT_OBJECT,
1640 elfcpp::STB_LOCAL,
1641 elfcpp::STV_HIDDEN, 0,
1642 false, false);
1643
1644 }
1645 return this->got_;
1646}
1647
1648// Get the dynamic reloc section, creating it if necessary.
1649
1650template<int size>
1651typename Target_s390<size>::Reloc_section*
1652Target_s390<size>::rela_dyn_section(Layout* layout)
1653{
1654 if (this->rela_dyn_ == NULL)
1655 {
1656 gold_assert(layout != NULL);
1657 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1658 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1659 elfcpp::SHF_ALLOC, this->rela_dyn_,
1660 ORDER_DYNAMIC_RELOCS, false);
1661 }
1662 return this->rela_dyn_;
1663}
1664
1665// Get the section to use for IRELATIVE relocs, creating it if
1666// necessary. These go in .rela.dyn, but only after all other dynamic
1667// relocations. They need to follow the other dynamic relocations so
1668// that they can refer to global variables initialized by those
1669// relocs.
1670
1671template<int size>
1672typename Target_s390<size>::Reloc_section*
1673Target_s390<size>::rela_irelative_section(Layout* layout)
1674{
1675 if (this->rela_irelative_ == NULL)
1676 {
1677 // Make sure we have already created the dynamic reloc section.
1678 this->rela_dyn_section(layout);
1679 this->rela_irelative_ = new Reloc_section(false);
1680 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1681 elfcpp::SHF_ALLOC, this->rela_irelative_,
1682 ORDER_DYNAMIC_RELOCS, false);
1683 gold_assert(this->rela_dyn_->output_section()
1684 == this->rela_irelative_->output_section());
1685 }
1686 return this->rela_irelative_;
1687}
1688
1689// Write the first three reserved words of the .got.plt section.
1690// The remainder of the section is written while writing the PLT
1691// in Output_data_plt_s390::do_write.
1692
1693template<int size>
1694void
1695Output_data_got_plt_s390<size>::do_write(Output_file* of)
1696{
1697 // The first entry in the GOT is the address of the .dynamic section
1698 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1699 // We saved space for them when we created the section in
1700 // Target_x86_64::got_section.
1701 const off_t got_file_offset = this->offset();
1702 gold_assert(this->data_size() >= 3 * size / 8);
1703 unsigned char* const got_view =
1704 of->get_output_view(got_file_offset, 3 * size / 8);
1705 Output_section* dynamic = this->layout_->dynamic_section();
1706 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1707 elfcpp::Swap<size, true>::writeval(got_view, dynamic_addr);
1708 memset(got_view + size / 8, 0, 2 * size / 8);
1709 of->write_output_view(got_file_offset, 3 * size / 8, got_view);
1710}
1711
1712// Create the PLT section.
1713
1714template<int size>
1715void
1716Target_s390<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
1717{
1718 if (this->plt_ == NULL)
1719 {
1720 // Create the GOT sections first.
1721 this->got_section(symtab, layout);
1722
1723 // Ensure that .rela.dyn always appears before .rela.plt This is
1724 // necessary due to how, on 32-bit S/390 and some other targets,
1725 // .rela.dyn needs to include .rela.plt in it's range.
1726 this->rela_dyn_section(layout);
1727
1728 this->plt_ = new Output_data_plt_s390<size>(layout,
1729 this->got_, this->got_plt_, this->got_irelative_);
1730
1731 // Add unwind information if requested.
1732 if (parameters->options().ld_generated_unwind_info())
1733 this->plt_->add_eh_frame(layout);
1734
1735 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1736 (elfcpp::SHF_ALLOC
1737 | elfcpp::SHF_EXECINSTR),
1738 this->plt_, ORDER_PLT, false);
1739
1740 // Make the sh_info field of .rela.plt point to .plt.
1741 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1742 rela_plt_os->set_info_section(this->plt_->output_section());
1743 }
1744}
1745
1746// Create a PLT entry for a global symbol.
1747
1748template<int size>
1749void
1750Target_s390<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1751 Symbol* gsym)
1752{
1753 if (gsym->has_plt_offset())
1754 return;
1755
1756 if (this->plt_ == NULL)
1757 this->make_plt_section(symtab, layout);
1758
1759 this->plt_->add_entry(symtab, layout, gsym);
1760}
1761
1762// Make a PLT entry for a local STT_GNU_IFUNC symbol.
1763
1764template<int size>
1765void
1766Target_s390<size>::make_local_ifunc_plt_entry(
1767 Symbol_table* symtab, Layout* layout,
1768 Sized_relobj_file<size, true>* relobj,
1769 unsigned int local_sym_index)
1770{
1771 if (relobj->local_has_plt_offset(local_sym_index))
1772 return;
1773 if (this->plt_ == NULL)
1774 this->make_plt_section(symtab, layout);
1775 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1776 relobj,
1777 local_sym_index);
1778 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1779}
1780
1781// Return the number of entries in the PLT.
1782
1783template<int size>
1784unsigned int
1785Target_s390<size>::plt_entry_count() const
1786{
1787 if (this->plt_ == NULL)
1788 return 0;
1789 return this->plt_->entry_count();
1790}
1791
1792// Return the offset of the first non-reserved PLT entry.
1793
1794template<int size>
1795unsigned int
1796Target_s390<size>::first_plt_entry_offset() const
1797{
1798 return this->plt_->first_plt_entry_offset();
1799}
1800
1801// Return the size of each PLT entry.
1802
1803template<int size>
1804unsigned int
1805Target_s390<size>::plt_entry_size() const
1806{
1807 return this->plt_->get_plt_entry_size();
1808}
1809
1810// Create the GOT and PLT sections for an incremental update.
1811
1812template<int size>
1813Output_data_got_base*
1814Target_s390<size>::init_got_plt_for_update(Symbol_table* symtab,
1815 Layout* layout,
1816 unsigned int got_count,
1817 unsigned int plt_count)
1818{
1819 gold_assert(this->got_ == NULL);
1820
1821 // Add the three reserved entries.
1822 this->got_plt_ = new Output_data_got_plt_s390<size>(layout, (plt_count + 3) * size / 8);
1823 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1824 (elfcpp::SHF_ALLOC
1825 | elfcpp::SHF_WRITE),
1826 this->got_plt_, ORDER_NON_RELRO_FIRST,
1827 false);
1828
1829 // If there are any IRELATIVE relocations, they get GOT entries in
1830 // .got.plt after the jump slot entries.
1831 this->got_irelative_ = new Output_data_space(0, size / 8, "** GOT IRELATIVE PLT");
1832 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1833 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1834 this->got_irelative_,
1835 ORDER_NON_RELRO_FIRST, false);
1836
1837 this->got_ = new Output_data_got<size, true>(got_count * size / 8);
1838 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1839 (elfcpp::SHF_ALLOC
1840 | elfcpp::SHF_WRITE),
1841 this->got_, ORDER_RELRO_LAST,
1842 true);
1843
1844 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1845 this->global_offset_table_ =
1846 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1847 Symbol_table::PREDEFINED,
1848 this->got_plt_,
1849 0, 0, elfcpp::STT_OBJECT,
1850 elfcpp::STB_LOCAL,
1851 elfcpp::STV_HIDDEN, 0,
1852 false, false);
1853
1854 // Create the PLT section.
1855 this->plt_ = new Output_data_plt_s390<size>(layout,
1856 this->got_, this->got_plt_, this->got_irelative_, plt_count);
1857
1858 // Add unwind information if requested.
1859 if (parameters->options().ld_generated_unwind_info())
1860 this->plt_->add_eh_frame(layout);
1861
1862 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1863 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1864 this->plt_, ORDER_PLT, false);
1865
1866 // Make the sh_info field of .rela.plt point to .plt.
1867 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1868 rela_plt_os->set_info_section(this->plt_->output_section());
1869
1870 // Create the rela_dyn section.
1871 this->rela_dyn_section(layout);
1872
1873 return this->got_;
1874}
1875
1876// Reserve a GOT entry for a local symbol, and regenerate any
1877// necessary dynamic relocations.
1878
1879template<int size>
1880void
1881Target_s390<size>::reserve_local_got_entry(
1882 unsigned int got_index,
1883 Sized_relobj<size, true>* obj,
1884 unsigned int r_sym,
1885 unsigned int got_type)
1886{
1887 unsigned int got_offset = got_index * size / 8;
1888 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1889
1890 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1891 switch (got_type)
1892 {
1893 case GOT_TYPE_STANDARD:
1894 if (parameters->options().output_is_position_independent())
1895 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_390_RELATIVE,
1896 this->got_, got_offset, 0, false);
1897 break;
1898 case GOT_TYPE_TLS_OFFSET:
1899 rela_dyn->add_local(obj, r_sym, elfcpp::R_390_TLS_TPOFF,
1900 this->got_, got_offset, 0);
1901 break;
1902 case GOT_TYPE_TLS_PAIR:
1903 this->got_->reserve_slot(got_index + 1);
1904 rela_dyn->add_local(obj, r_sym, elfcpp::R_390_TLS_DTPMOD,
1905 this->got_, got_offset, 0);
1906 break;
1907 default:
1908 gold_unreachable();
1909 }
1910}
1911
1912// Reserve a GOT entry for a global symbol, and regenerate any
1913// necessary dynamic relocations.
1914
1915template<int size>
1916void
1917Target_s390<size>::reserve_global_got_entry(unsigned int got_index,
1918 Symbol* gsym,
1919 unsigned int got_type)
1920{
1921 unsigned int got_offset = got_index * size / 8;
1922 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1923
1924 this->got_->reserve_global(got_index, gsym, got_type);
1925 switch (got_type)
1926 {
1927 case GOT_TYPE_STANDARD:
1928 if (!gsym->final_value_is_known())
1929 {
1930 if (gsym->is_from_dynobj()
1931 || gsym->is_undefined()
1932 || gsym->is_preemptible()
1933 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1934 rela_dyn->add_global(gsym, elfcpp::R_390_GLOB_DAT,
1935 this->got_, got_offset, 0);
1936 else
1937 rela_dyn->add_global_relative(gsym, elfcpp::R_390_RELATIVE,
1938 this->got_, got_offset, 0, false);
1939 }
1940 break;
1941 case GOT_TYPE_TLS_OFFSET:
1942 rela_dyn->add_global_relative(gsym, elfcpp::R_390_TLS_TPOFF,
1943 this->got_, got_offset, 0, false);
1944 break;
1945 case GOT_TYPE_TLS_PAIR:
1946 this->got_->reserve_slot(got_index + 1);
1947 rela_dyn->add_global_relative(gsym, elfcpp::R_390_TLS_DTPMOD,
1948 this->got_, got_offset, 0, false);
1949 rela_dyn->add_global_relative(gsym, elfcpp::R_390_TLS_DTPOFF,
1950 this->got_, got_offset + size / 8, 0, false);
1951 break;
1952 default:
1953 gold_unreachable();
1954 }
1955}
1956
1957// Register an existing PLT entry for a global symbol.
1958
1959template<int size>
1960void
1961Target_s390<size>::register_global_plt_entry(Symbol_table* symtab,
1962 Layout* layout,
1963 unsigned int plt_index,
1964 Symbol* gsym)
1965{
1966 gold_assert(this->plt_ != NULL);
1967 gold_assert(!gsym->has_plt_offset());
1968
1969 this->plt_->reserve_slot(plt_index);
1970
1971 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1972
1973 unsigned int got_offset = (plt_index + 3) * size / 8;
1974 this->plt_->add_relocation(symtab, layout, gsym, got_offset);
1975}
1976
1977// Force a COPY relocation for a given symbol.
1978
1979template<int size>
1980void
1981Target_s390<size>::emit_copy_reloc(
1982 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1983{
1984 this->copy_relocs_.emit_copy_reloc(symtab,
1985 symtab->get_sized_symbol<size>(sym),
1986 os,
1987 offset,
1988 this->rela_dyn_section(NULL));
1989}
1990
1991// Create a GOT entry for the TLS module index.
1992
1993template<int size>
1994unsigned int
1995Target_s390<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1996 Sized_relobj_file<size, true>* object)
1997{
1998 if (this->got_mod_index_offset_ == -1U)
1999 {
2000 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2001 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2002 Output_data_got<size, true>* got = this->got_section(symtab, layout);
2003 unsigned int got_offset = got->add_constant(0);
2004 rela_dyn->add_local(object, 0, elfcpp::R_390_TLS_DTPMOD, got,
2005 got_offset, 0);
2006 got->add_constant(0);
2007 this->got_mod_index_offset_ = got_offset;
2008 }
2009 return this->got_mod_index_offset_;
2010}
2011
2012// Optimize the TLS relocation type based on what we know about the
2013// symbol. IS_FINAL is true if the final address of this symbol is
2014// known at link time.
2015
2016template<int size>
2017tls::Tls_optimization
2018Target_s390<size>::optimize_tls_reloc(bool is_final, int r_type)
2019{
2020 // If we are generating a shared library, then we can't do anything
2021 // in the linker.
2022 if (parameters->options().shared())
2023 return tls::TLSOPT_NONE;
2024
2025 switch (r_type)
2026 {
2027 case elfcpp::R_390_TLS_GD32:
2028 case elfcpp::R_390_TLS_GD64:
2029 case elfcpp::R_390_TLS_GDCALL:
2030 // These are General-Dynamic which permits fully general TLS
2031 // access. Since we know that we are generating an executable,
2032 // we can convert this to Initial-Exec. If we also know that
2033 // this is a local symbol, we can further switch to Local-Exec.
2034 if (is_final)
2035 return tls::TLSOPT_TO_LE;
2036 return tls::TLSOPT_TO_IE;
2037
2038 case elfcpp::R_390_TLS_LDM32:
2039 case elfcpp::R_390_TLS_LDM64:
2040 case elfcpp::R_390_TLS_LDO32:
2041 case elfcpp::R_390_TLS_LDO64:
2042 case elfcpp::R_390_TLS_LDCALL:
2043 // This is Local-Dynamic, which refers to a local symbol in the
2044 // dynamic TLS block. Since we know that we generating an
2045 // executable, we can switch to Local-Exec.
2046 return tls::TLSOPT_TO_LE;
2047
2048 case elfcpp::R_390_TLS_IE32:
2049 case elfcpp::R_390_TLS_IE64:
2050 case elfcpp::R_390_TLS_GOTIE32:
2051 case elfcpp::R_390_TLS_GOTIE64:
2052 case elfcpp::R_390_TLS_LOAD:
2053 // These are Initial-Exec relocs which get the thread offset
2054 // from the GOT. If we know that we are linking against the
2055 // local symbol, we can switch to Local-Exec, which links the
2056 // thread offset into the instruction.
2057 if (is_final)
2058 return tls::TLSOPT_TO_LE;
2059 return tls::TLSOPT_NONE;
2060
2061 case elfcpp::R_390_TLS_GOTIE12:
2062 case elfcpp::R_390_TLS_IEENT:
2063 case elfcpp::R_390_TLS_GOTIE20:
2064 // These are Initial-Exec, but cannot be optimized.
2065 return tls::TLSOPT_NONE;
2066
2067 case elfcpp::R_390_TLS_LE32:
2068 case elfcpp::R_390_TLS_LE64:
2069 // When we already have Local-Exec, there is nothing further we
2070 // can do.
2071 return tls::TLSOPT_NONE;
2072
2073 default:
2074 gold_unreachable();
2075 }
2076}
2077
2078// Get the Reference_flags for a particular relocation.
2079
2080template<int size>
2081int
2082Target_s390<size>::Scan::get_reference_flags(unsigned int r_type)
2083{
2084 switch (r_type)
2085 {
2086 case elfcpp::R_390_NONE:
2087 case elfcpp::R_390_GNU_VTINHERIT:
2088 case elfcpp::R_390_GNU_VTENTRY:
2089 case elfcpp::R_390_GOTPC:
2090 case elfcpp::R_390_GOTPCDBL:
2091 // No symbol reference.
2092 return 0;
2093
2094 case elfcpp::R_390_64:
2095 case elfcpp::R_390_32:
2096 case elfcpp::R_390_20:
2097 case elfcpp::R_390_16:
2098 case elfcpp::R_390_12:
2099 case elfcpp::R_390_8:
2100 return Symbol::ABSOLUTE_REF;
2101
2102 case elfcpp::R_390_PC12DBL:
2103 case elfcpp::R_390_PC16:
2104 case elfcpp::R_390_PC16DBL:
2105 case elfcpp::R_390_PC24DBL:
2106 case elfcpp::R_390_PC32:
2107 case elfcpp::R_390_PC32DBL:
2108 case elfcpp::R_390_PC64:
2109 case elfcpp::R_390_GOTOFF16:
2110 case elfcpp::R_390_GOTOFF32:
2111 case elfcpp::R_390_GOTOFF64:
2112 return Symbol::RELATIVE_REF;
2113
2114 case elfcpp::R_390_PLT12DBL:
2115 case elfcpp::R_390_PLT16DBL:
2116 case elfcpp::R_390_PLT24DBL:
2117 case elfcpp::R_390_PLT32:
2118 case elfcpp::R_390_PLT32DBL:
2119 case elfcpp::R_390_PLT64:
2120 case elfcpp::R_390_PLTOFF16:
2121 case elfcpp::R_390_PLTOFF32:
2122 case elfcpp::R_390_PLTOFF64:
2123 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2124
2125 case elfcpp::R_390_GOT12:
2126 case elfcpp::R_390_GOT16:
2127 case elfcpp::R_390_GOT20:
2128 case elfcpp::R_390_GOT32:
2129 case elfcpp::R_390_GOT64:
2130 case elfcpp::R_390_GOTENT:
2131 case elfcpp::R_390_GOTPLT12:
2132 case elfcpp::R_390_GOTPLT16:
2133 case elfcpp::R_390_GOTPLT20:
2134 case elfcpp::R_390_GOTPLT32:
2135 case elfcpp::R_390_GOTPLT64:
2136 case elfcpp::R_390_GOTPLTENT:
2137 // Absolute in GOT.
2138 return Symbol::ABSOLUTE_REF;
2139
2140 case elfcpp::R_390_TLS_GD32: // Global-dynamic
2141 case elfcpp::R_390_TLS_GD64:
2142 case elfcpp::R_390_TLS_GDCALL:
2143 case elfcpp::R_390_TLS_LDM32: // Local-dynamic
2144 case elfcpp::R_390_TLS_LDM64:
2145 case elfcpp::R_390_TLS_LDO32:
2146 case elfcpp::R_390_TLS_LDO64:
2147 case elfcpp::R_390_TLS_LDCALL:
2148 case elfcpp::R_390_TLS_IE32: // Initial-exec
2149 case elfcpp::R_390_TLS_IE64:
2150 case elfcpp::R_390_TLS_IEENT:
2151 case elfcpp::R_390_TLS_GOTIE12:
2152 case elfcpp::R_390_TLS_GOTIE20:
2153 case elfcpp::R_390_TLS_GOTIE32:
2154 case elfcpp::R_390_TLS_GOTIE64:
2155 case elfcpp::R_390_TLS_LOAD:
2156 case elfcpp::R_390_TLS_LE32: // Local-exec
2157 case elfcpp::R_390_TLS_LE64:
2158 return Symbol::TLS_REF;
2159
2160 case elfcpp::R_390_COPY:
2161 case elfcpp::R_390_GLOB_DAT:
2162 case elfcpp::R_390_JMP_SLOT:
2163 case elfcpp::R_390_RELATIVE:
2164 case elfcpp::R_390_IRELATIVE:
2165 case elfcpp::R_390_TLS_TPOFF:
2166 case elfcpp::R_390_TLS_DTPOFF:
2167 case elfcpp::R_390_TLS_DTPMOD:
2168 default:
2169 // Not expected. We will give an error later.
2170 return 0;
2171 }
2172}
2173
2174// Report an unsupported relocation against a local symbol.
2175
2176template<int size>
2177void
2178Target_s390<size>::Scan::unsupported_reloc_local(
2179 Sized_relobj_file<size, true>* object,
2180 unsigned int r_type)
2181{
2182 gold_error(_("%s: unsupported reloc %u against local symbol"),
2183 object->name().c_str(), r_type);
2184}
2185
2186// We are about to emit a dynamic relocation of type R_TYPE. If the
2187// dynamic linker does not support it, issue an error.
2188
2189template<int size>
2190void
2191Target_s390<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
2192{
2193 gold_assert(r_type != elfcpp::R_390_NONE);
2194
2195 if (size == 64)
2196 {
2197 switch (r_type)
2198 {
2199 // These are the relocation types supported by glibc for s390 64-bit.
2200 case elfcpp::R_390_RELATIVE:
2201 case elfcpp::R_390_IRELATIVE:
2202 case elfcpp::R_390_COPY:
2203 case elfcpp::R_390_GLOB_DAT:
2204 case elfcpp::R_390_JMP_SLOT:
2205 case elfcpp::R_390_TLS_DTPMOD:
2206 case elfcpp::R_390_TLS_DTPOFF:
2207 case elfcpp::R_390_TLS_TPOFF:
2208 case elfcpp::R_390_8:
2209 case elfcpp::R_390_16:
2210 case elfcpp::R_390_32:
2211 case elfcpp::R_390_64:
2212 case elfcpp::R_390_PC16:
2213 case elfcpp::R_390_PC16DBL:
2214 case elfcpp::R_390_PC32:
2215 case elfcpp::R_390_PC32DBL:
2216 case elfcpp::R_390_PC64:
2217 return;
2218
2219 default:
2220 break;
2221 }
2222 }
2223 else
2224 {
2225 switch (r_type)
2226 {
2227 // These are the relocation types supported by glibc for s390 32-bit.
2228 case elfcpp::R_390_RELATIVE:
2229 case elfcpp::R_390_IRELATIVE:
2230 case elfcpp::R_390_COPY:
2231 case elfcpp::R_390_GLOB_DAT:
2232 case elfcpp::R_390_JMP_SLOT:
2233 case elfcpp::R_390_TLS_DTPMOD:
2234 case elfcpp::R_390_TLS_DTPOFF:
2235 case elfcpp::R_390_TLS_TPOFF:
2236 case elfcpp::R_390_8:
2237 case elfcpp::R_390_16:
2238 case elfcpp::R_390_32:
2239 case elfcpp::R_390_PC16:
2240 case elfcpp::R_390_PC16DBL:
2241 case elfcpp::R_390_PC32:
2242 case elfcpp::R_390_PC32DBL:
2243 return;
2244
2245 default:
2246 break;
2247 }
2248 }
2249
2250 // This prevents us from issuing more than one error per reloc
2251 // section. But we can still wind up issuing more than one
2252 // error per object file.
2253 if (this->issued_non_pic_error_)
2254 return;
2255 gold_assert(parameters->options().output_is_position_independent());
2256 object->error(_("requires unsupported dynamic reloc; "
2257 "recompile with -fPIC"));
2258 this->issued_non_pic_error_ = true;
2259 return;
2260}
2261
2262// Return whether we need to make a PLT entry for a relocation of the
2263// given type against a STT_GNU_IFUNC symbol.
2264
2265template<int size>
2266bool
2267Target_s390<size>::Scan::reloc_needs_plt_for_ifunc(
2268 Sized_relobj_file<size, true>* object,
2269 unsigned int r_type)
2270{
2271 int flags = Scan::get_reference_flags(r_type);
2272 if (flags & Symbol::TLS_REF)
2273 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2274 object->name().c_str(), r_type);
2275 return flags != 0;
2276}
2277
2278// Scan a relocation for a local symbol.
2279
2280template<int size>
2281inline void
2282Target_s390<size>::Scan::local(Symbol_table* symtab,
2283 Layout* layout,
2284 Target_s390<size>* target,
2285 Sized_relobj_file<size, true>* object,
2286 unsigned int data_shndx,
2287 Output_section* output_section,
2288 const elfcpp::Rela<size, true>& reloc,
2289 unsigned int r_type,
2290 const elfcpp::Sym<size, true>& lsym,
2291 bool is_discarded)
2292{
2293 if (is_discarded)
2294 return;
2295
2296 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2297 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2298
2299 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2300 {
2301 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2302 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2303 }
2304
2305 switch (r_type)
2306 {
2307 case elfcpp::R_390_NONE:
2308 case elfcpp::R_390_GNU_VTINHERIT:
2309 case elfcpp::R_390_GNU_VTENTRY:
2310 break;
2311
2312 case elfcpp::R_390_64:
2313 // If building a shared library (or a position-independent
2314 // executable), we need to create a dynamic relocation for this
2315 // location. The relocation applied at link time will apply the
2316 // link-time value, so we flag the location with an
2317 // R_390_RELATIVE relocation so the dynamic loader can
2318 // relocate it easily.
2319 if (parameters->options().output_is_position_independent() && size == 64)
2320 {
2321 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2322 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2323 rela_dyn->add_local_relative(object, r_sym,
2324 elfcpp::R_390_RELATIVE,
2325 output_section, data_shndx,
2326 reloc.get_r_offset(),
2327 reloc.get_r_addend(), is_ifunc);
2328 }
2329 break;
2330
2331 case elfcpp::R_390_32:
2332 case elfcpp::R_390_20:
2333 case elfcpp::R_390_16:
2334 case elfcpp::R_390_12:
2335 case elfcpp::R_390_8:
2336 if (parameters->options().output_is_position_independent())
2337 {
2338 if (size == 32 && r_type == elfcpp::R_390_32)
2339 {
2340 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2341 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2342 rela_dyn->add_local_relative(object, r_sym,
2343 elfcpp::R_390_RELATIVE,
2344 output_section, data_shndx,
2345 reloc.get_r_offset(),
2346 reloc.get_r_addend(), is_ifunc);
2347 break;
2348 }
2349
2350 check_non_pic(object, r_type);
2351
2352 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2353 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2354 if (lsym.get_st_type() != elfcpp::STT_SECTION)
2355 rela_dyn->add_local(object, r_sym, r_type, output_section,
2356 data_shndx, reloc.get_r_offset(),
2357 reloc.get_r_addend());
2358 else
2359 {
2360 gold_assert(lsym.get_st_value() == 0);
2361 unsigned int shndx = lsym.get_st_shndx();
2362 bool is_ordinary;
2363 shndx = object->adjust_sym_shndx(r_sym, shndx,
2364 &is_ordinary);
2365 if (!is_ordinary)
2366 object->error(_("section symbol %u has bad shndx %u"),
2367 r_sym, shndx);
2368 else
2369 rela_dyn->add_local_section(object, shndx,
2370 r_type, output_section,
2371 data_shndx, reloc.get_r_offset(),
2372 reloc.get_r_addend());
2373 }
2374 }
2375 break;
2376
2377 case elfcpp::R_390_PC12DBL:
2378 case elfcpp::R_390_PC16:
2379 case elfcpp::R_390_PC16DBL:
2380 case elfcpp::R_390_PC24DBL:
2381 case elfcpp::R_390_PC32:
2382 case elfcpp::R_390_PC32DBL:
2383 case elfcpp::R_390_PC64:
2384 break;
2385
2386 case elfcpp::R_390_PLT12DBL:
2387 case elfcpp::R_390_PLT16DBL:
2388 case elfcpp::R_390_PLT24DBL:
2389 case elfcpp::R_390_PLT32:
2390 case elfcpp::R_390_PLT32DBL:
2391 case elfcpp::R_390_PLT64:
2392 // Since we know this is a local symbol, we can handle this as a
2393 // PC32 reloc.
2394 break;
2395
2396 case elfcpp::R_390_GOTPC:
2397 case elfcpp::R_390_GOTPCDBL:
2398 case elfcpp::R_390_GOTOFF16:
2399 case elfcpp::R_390_GOTOFF32:
2400 case elfcpp::R_390_GOTOFF64:
2401 case elfcpp::R_390_PLTOFF16:
2402 case elfcpp::R_390_PLTOFF32:
2403 case elfcpp::R_390_PLTOFF64:
2404 // We need a GOT section.
2405 target->got_section(symtab, layout);
2406 // For PLTOFF*, we'd normally want a PLT section, but since we
2407 // know this is a local symbol, no PLT is needed.
2408 break;
2409
2410 case elfcpp::R_390_GOT12:
2411 case elfcpp::R_390_GOT16:
2412 case elfcpp::R_390_GOT20:
2413 case elfcpp::R_390_GOT32:
2414 case elfcpp::R_390_GOT64:
2415 case elfcpp::R_390_GOTENT:
2416 case elfcpp::R_390_GOTPLT12:
2417 case elfcpp::R_390_GOTPLT16:
2418 case elfcpp::R_390_GOTPLT20:
2419 case elfcpp::R_390_GOTPLT32:
2420 case elfcpp::R_390_GOTPLT64:
2421 case elfcpp::R_390_GOTPLTENT:
2422 {
2423 // The symbol requires a GOT section.
2424 Output_data_got<size, true>* got = target->got_section(symtab, layout);
2425
2426 // The symbol requires a GOT entry.
2427 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2428
2429 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2430 // lets function pointers compare correctly with shared
2431 // libraries. Otherwise we would need an IRELATIVE reloc.
2432 bool is_new;
2433 if (is_ifunc)
2434 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2435 else
2436 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2437 if (is_new)
2438 {
2439 // If we are generating a shared object, we need to add a
2440 // dynamic relocation for this symbol's GOT entry.
2441 if (parameters->options().output_is_position_independent())
2442 {
2443 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2444 unsigned int got_offset =
2445 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2446 rela_dyn->add_local_relative(object, r_sym,
2447 elfcpp::R_390_RELATIVE,
2448 got, got_offset, 0, is_ifunc);
2449 }
2450 }
2451 // For GOTPLT*, we'd normally want a PLT section, but since
2452 // we know this is a local symbol, no PLT is needed.
2453 }
2454 break;
2455
2456 case elfcpp::R_390_COPY:
2457 case elfcpp::R_390_GLOB_DAT:
2458 case elfcpp::R_390_JMP_SLOT:
2459 case elfcpp::R_390_RELATIVE:
2460 case elfcpp::R_390_IRELATIVE:
2461 // These are outstanding tls relocs, which are unexpected when linking
2462 case elfcpp::R_390_TLS_TPOFF:
2463 case elfcpp::R_390_TLS_DTPOFF:
2464 case elfcpp::R_390_TLS_DTPMOD:
2465 gold_error(_("%s: unexpected reloc %u in object file"),
2466 object->name().c_str(), r_type);
2467 break;
2468
2469 // These are initial tls relocs, which are expected when linking
2470 case elfcpp::R_390_TLS_GD32: // Global-dynamic
2471 case elfcpp::R_390_TLS_GD64:
2472 case elfcpp::R_390_TLS_GDCALL:
2473 case elfcpp::R_390_TLS_LDM32: // Local-dynamic
2474 case elfcpp::R_390_TLS_LDM64:
2475 case elfcpp::R_390_TLS_LDO32:
2476 case elfcpp::R_390_TLS_LDO64:
2477 case elfcpp::R_390_TLS_LDCALL:
2478 case elfcpp::R_390_TLS_IE32: // Initial-exec
2479 case elfcpp::R_390_TLS_IE64:
2480 case elfcpp::R_390_TLS_IEENT:
2481 case elfcpp::R_390_TLS_GOTIE12:
2482 case elfcpp::R_390_TLS_GOTIE20:
2483 case elfcpp::R_390_TLS_GOTIE32:
2484 case elfcpp::R_390_TLS_GOTIE64:
2485 case elfcpp::R_390_TLS_LOAD:
2486 case elfcpp::R_390_TLS_LE32: // Local-exec
2487 case elfcpp::R_390_TLS_LE64:
2488 {
2489 bool output_is_shared = parameters->options().shared();
2490 const tls::Tls_optimization optimized_type
2491 = Target_s390<size>::optimize_tls_reloc(!output_is_shared,
2492 r_type);
2493 switch (r_type)
2494 {
2495 case elfcpp::R_390_TLS_GD32: // General-dynamic
2496 case elfcpp::R_390_TLS_GD64:
2497 case elfcpp::R_390_TLS_GDCALL:
2498 if (optimized_type == tls::TLSOPT_NONE)
2499 {
2500 // Create a pair of GOT entries for the module index and
2501 // dtv-relative offset.
2502 Output_data_got<size, true>* got
2503 = target->got_section(symtab, layout);
2504 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2505 unsigned int shndx = lsym.get_st_shndx();
2506 bool is_ordinary;
2507 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2508 if (!is_ordinary)
2509 object->error(_("local symbol %u has bad shndx %u"),
2510 r_sym, shndx);
2511 else
2512 got->add_local_pair_with_rel(object, r_sym,
2513 shndx,
2514 GOT_TYPE_TLS_PAIR,
2515 target->rela_dyn_section(layout),
2516 elfcpp::R_390_TLS_DTPMOD);
2517 }
2518 else if (optimized_type != tls::TLSOPT_TO_LE)
2519 unsupported_reloc_local(object, r_type);
2520 break;
2521
2522 case elfcpp::R_390_TLS_LDM32: // Local-dynamic
2523 case elfcpp::R_390_TLS_LDM64:
2524 case elfcpp::R_390_TLS_LDCALL:
2525 if (optimized_type == tls::TLSOPT_NONE)
2526 {
2527 // Create a GOT entry for the module index.
2528 target->got_mod_index_entry(symtab, layout, object);
2529 }
2530 else if (optimized_type != tls::TLSOPT_TO_LE)
2531 unsupported_reloc_local(object, r_type);
2532 break;
2533
2534 case elfcpp::R_390_TLS_LDO32:
2535 case elfcpp::R_390_TLS_LDO64:
2536 break;
2537
2538 case elfcpp::R_390_TLS_IE32: // Initial-exec
2539 case elfcpp::R_390_TLS_IE64:
2540 // These two involve an absolute address
2541 if (parameters->options().shared()
2542 && optimized_type == tls::TLSOPT_NONE)
2543 {
2544 if ((size == 32 && r_type == elfcpp::R_390_TLS_IE32) ||
2545 (size == 64 && r_type == elfcpp::R_390_TLS_IE64))
2546 {
2547 // We need to create a dynamic relocation.
2548 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2549 unsigned int r_sym =
2550 elfcpp::elf_r_sym<size>(reloc.get_r_info());
2551 rela_dyn->add_local_relative(object, r_sym,
2552 elfcpp::R_390_RELATIVE,
2553 output_section, data_shndx,
2554 reloc.get_r_offset(),
2555 reloc.get_r_addend(), false);
2556 }
2557 else
2558 {
2559 unsupported_reloc_local(object, r_type);
2560 }
2561 }
d8e90251 2562 // Fall through.
e79a4bad
MK
2563 case elfcpp::R_390_TLS_IEENT:
2564 case elfcpp::R_390_TLS_GOTIE12:
2565 case elfcpp::R_390_TLS_GOTIE20:
2566 case elfcpp::R_390_TLS_GOTIE32:
2567 case elfcpp::R_390_TLS_GOTIE64:
2568 case elfcpp::R_390_TLS_LOAD:
2569 layout->set_has_static_tls();
2570 if (optimized_type == tls::TLSOPT_NONE)
2571 {
2572 if (!output_is_shared)
2573 {
2574 // We're making an executable, and the symbol is local, but
2575 // we cannot optimize to LE. Make a const GOT entry instead.
2576 Output_data_got<size, true>* got
2577 = target->got_section(symtab, layout);
2578 unsigned int r_sym
2579 = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2580 got->add_local_plt(object, r_sym, GOT_TYPE_TLS_OFFSET);
2581 }
2582 else
2583 {
2584 // Create a GOT entry for the tp-relative offset.
2585 Output_data_got<size, true>* got
2586 = target->got_section(symtab, layout);
2587 unsigned int r_sym
2588 = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2589 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
2590 target->rela_dyn_section(layout),
2591 elfcpp::R_390_TLS_TPOFF);
2592 }
2593 }
2594 else if (optimized_type != tls::TLSOPT_TO_LE)
2595 unsupported_reloc_local(object, r_type);
2596 break;
2597
2598 case elfcpp::R_390_TLS_LE32: // Local-exec
2599 case elfcpp::R_390_TLS_LE64:
2600 layout->set_has_static_tls();
2601 if (output_is_shared)
2602 {
2603 // We need to create a dynamic relocation.
2604 if ((size == 32 && r_type == elfcpp::R_390_TLS_LE32) ||
2605 (size == 64 && r_type == elfcpp::R_390_TLS_LE64))
2606 {
2607 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2608 unsigned int r_sym
2609 = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2610 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2611 rela_dyn->add_local(object, r_sym, elfcpp::R_390_TLS_TPOFF,
2612 output_section, data_shndx,
2613 reloc.get_r_offset(),
2614 reloc.get_r_addend());
2615 }
2616 else
2617 {
2618 unsupported_reloc_local(object, r_type);
2619 }
2620 }
2621 break;
2622
2623 default:
2624 gold_unreachable();
2625 }
2626 }
2627 break;
2628
2629 default:
2630 gold_error(_("%s: unsupported reloc %u against local symbol"),
2631 object->name().c_str(), r_type);
2632 break;
2633 }
2634}
2635
2636// Scan a relocation for a global symbol.
2637
2638template<int size>
2639inline void
2640Target_s390<size>::Scan::global(Symbol_table* symtab,
2641 Layout* layout,
2642 Target_s390<size>* target,
2643 Sized_relobj_file<size, true>* object,
2644 unsigned int data_shndx,
2645 Output_section* output_section,
2646 const elfcpp::Rela<size, true>& reloc,
2647 unsigned int r_type,
2648 Symbol* gsym)
2649{
2650 // A STT_GNU_IFUNC symbol may require a PLT entry.
2651 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2652 && this->reloc_needs_plt_for_ifunc(object, r_type))
2653 target->make_plt_entry(symtab, layout, gsym);
2654
2655 switch (r_type)
2656 {
2657 case elfcpp::R_390_NONE:
2658 case elfcpp::R_390_GNU_VTINHERIT:
2659 case elfcpp::R_390_GNU_VTENTRY:
2660 break;
2661
2662 case elfcpp::R_390_64:
2663 case elfcpp::R_390_32:
2664 case elfcpp::R_390_20:
2665 case elfcpp::R_390_16:
2666 case elfcpp::R_390_12:
2667 case elfcpp::R_390_8:
2668 {
2669 // Make a PLT entry if necessary.
2670 if (gsym->needs_plt_entry())
2671 {
2672 target->make_plt_entry(symtab, layout, gsym);
2673 // Since this is not a PC-relative relocation, we may be
2674 // taking the address of a function. In that case we need to
2675 // set the entry in the dynamic symbol table to the address of
2676 // the PLT entry.
2677 if (gsym->is_from_dynobj() && !parameters->options().shared())
2678 gsym->set_needs_dynsym_value();
2679 }
2680 // Make a dynamic relocation if necessary.
2681 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2682 {
2683 if (!parameters->options().output_is_position_independent()
2684 && gsym->may_need_copy_reloc())
2685 {
2686 target->copy_reloc(symtab, layout, object,
2687 data_shndx, output_section, gsym, reloc);
2688 }
2689 else if (((size == 64 && r_type == elfcpp::R_390_64)
2690 || (size == 32 && r_type == elfcpp::R_390_32))
2691 && gsym->type() == elfcpp::STT_GNU_IFUNC
2692 && gsym->can_use_relative_reloc(false)
2693 && !gsym->is_from_dynobj()
2694 && !gsym->is_undefined()
2695 && !gsym->is_preemptible())
2696 {
2697 // Use an IRELATIVE reloc for a locally defined
2698 // STT_GNU_IFUNC symbol. This makes a function
2699 // address in a PIE executable match the address in a
2700 // shared library that it links against.
2701 Reloc_section* rela_dyn =
2702 target->rela_irelative_section(layout);
2703 unsigned int r_type = elfcpp::R_390_IRELATIVE;
2704 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2705 output_section, object,
2706 data_shndx,
2707 reloc.get_r_offset(),
2708 reloc.get_r_addend());
2709 }
2710 else if (((size == 64 && r_type == elfcpp::R_390_64)
2711 || (size == 32 && r_type == elfcpp::R_390_32))
2712 && gsym->can_use_relative_reloc(false))
2713 {
2714 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2715 rela_dyn->add_global_relative(gsym, elfcpp::R_390_RELATIVE,
2716 output_section, object,
2717 data_shndx,
2718 reloc.get_r_offset(),
2719 reloc.get_r_addend(), false);
2720 }
2721 else
2722 {
2723 check_non_pic(object, r_type);
2724 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2725 rela_dyn->add_global(gsym, r_type, output_section, object,
2726 data_shndx, reloc.get_r_offset(),
2727 reloc.get_r_addend());
2728 }
2729 }
2730 }
2731 break;
2732
2733 case elfcpp::R_390_PC12DBL:
2734 case elfcpp::R_390_PC16:
2735 case elfcpp::R_390_PC16DBL:
2736 case elfcpp::R_390_PC24DBL:
2737 case elfcpp::R_390_PC32:
2738 case elfcpp::R_390_PC32DBL:
2739 case elfcpp::R_390_PC64:
2740 {
2741 // Make a PLT entry if necessary.
2742 if (gsym->needs_plt_entry())
2743 {
2744 target->make_plt_entry(symtab, layout, gsym);
2745 // larl is often used to take address of a function. Aim the
2746 // symbol at the PLT entry.
2747 if (gsym->is_from_dynobj() && !parameters->options().shared())
2748 gsym->set_needs_dynsym_value();
2749 }
2750 // Make a dynamic relocation if necessary.
2751 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2752 {
2753 if (parameters->options().output_is_executable()
2754 && gsym->may_need_copy_reloc())
2755 {
2756 target->copy_reloc(symtab, layout, object,
2757 data_shndx, output_section, gsym, reloc);
2758 }
2759 else
2760 {
2761 check_non_pic(object, r_type);
2762 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2763 rela_dyn->add_global(gsym, r_type, output_section, object,
2764 data_shndx, reloc.get_r_offset(),
2765 reloc.get_r_addend());
2766 }
2767 }
2768 }
2769 break;
2770
2771 case elfcpp::R_390_PLT12DBL:
2772 case elfcpp::R_390_PLT16DBL:
2773 case elfcpp::R_390_PLT24DBL:
2774 case elfcpp::R_390_PLT32:
2775 case elfcpp::R_390_PLT32DBL:
2776 case elfcpp::R_390_PLT64:
2777 // If the symbol is fully resolved, this is just a PC32 reloc.
2778 // Otherwise we need a PLT entry.
2779 if (gsym->final_value_is_known())
2780 break;
2781 // If building a shared library, we can also skip the PLT entry
2782 // if the symbol is defined in the output file and is protected
2783 // or hidden.
2784 if (gsym->is_defined()
2785 && !gsym->is_from_dynobj()
2786 && !gsym->is_preemptible())
2787 break;
2788 target->make_plt_entry(symtab, layout, gsym);
2789 break;
2790
2791 case elfcpp::R_390_GOTPC:
2792 case elfcpp::R_390_GOTPCDBL:
2793 case elfcpp::R_390_GOTOFF16:
2794 case elfcpp::R_390_GOTOFF32:
2795 case elfcpp::R_390_GOTOFF64:
2796 case elfcpp::R_390_PLTOFF16:
2797 case elfcpp::R_390_PLTOFF32:
2798 case elfcpp::R_390_PLTOFF64:
2799 // We need a GOT section.
2800 target->got_section(symtab, layout);
2801 // For PLTOFF*, we also need a PLT entry (but only if the
2802 // symbol is not fully resolved).
2803 if ((r_type == elfcpp::R_390_PLTOFF16
2804 || r_type == elfcpp::R_390_PLTOFF32
2805 || r_type == elfcpp::R_390_PLTOFF64)
2806 && !gsym->final_value_is_known())
2807 target->make_plt_entry(symtab, layout, gsym);
2808 break;
2809
2810 case elfcpp::R_390_GOT12:
2811 case elfcpp::R_390_GOT16:
2812 case elfcpp::R_390_GOT20:
2813 case elfcpp::R_390_GOT32:
2814 case elfcpp::R_390_GOT64:
2815 case elfcpp::R_390_GOTENT:
2816 case elfcpp::R_390_GOTPLT12:
2817 case elfcpp::R_390_GOTPLT16:
2818 case elfcpp::R_390_GOTPLT20:
2819 case elfcpp::R_390_GOTPLT32:
2820 case elfcpp::R_390_GOTPLT64:
2821 case elfcpp::R_390_GOTPLTENT:
2822 {
2823 // The symbol requires a GOT entry.
2824 Output_data_got<size, true>* got = target->got_section(symtab, layout);
2825
2826 if (gsym->final_value_is_known())
2827 {
2828 // For a STT_GNU_IFUNC symbol we want the PLT address.
2829 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2830 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2831 else
2832 got->add_global(gsym, GOT_TYPE_STANDARD);
2833 }
2834 else
2835 {
2836 // If this symbol is not fully resolved, we need to add a
2837 // dynamic relocation for it.
2838 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2839
2840 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2841 //
2842 // 1) The symbol may be defined in some other module.
2843 //
2844 // 2) We are building a shared library and this is a
2845 // protected symbol; using GLOB_DAT means that the dynamic
2846 // linker can use the address of the PLT in the main
2847 // executable when appropriate so that function address
2848 // comparisons work.
2849 //
2850 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2851 // code, again so that function address comparisons work.
2852 if (gsym->is_from_dynobj()
2853 || gsym->is_undefined()
2854 || gsym->is_preemptible()
2855 || (gsym->visibility() == elfcpp::STV_PROTECTED
2856 && parameters->options().shared())
2857 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2858 && parameters->options().output_is_position_independent()))
2859 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2860 elfcpp::R_390_GLOB_DAT);
2861 else
2862 {
2863 // For a STT_GNU_IFUNC symbol we want to write the PLT
2864 // offset into the GOT, so that function pointer
2865 // comparisons work correctly.
2866 bool is_new;
2867 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2868 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2869 else
2870 {
2871 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2872 // Tell the dynamic linker to use the PLT address
2873 // when resolving relocations.
2874 if (gsym->is_from_dynobj()
2875 && !parameters->options().shared())
2876 gsym->set_needs_dynsym_value();
2877 }
2878 if (is_new)
2879 {
2880 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2881 rela_dyn->add_global_relative(gsym,
2882 elfcpp::R_390_RELATIVE,
2883 got, got_off, 0, false);
2884 }
2885 }
2886 }
2887 }
2888 break;
2889
2890 case elfcpp::R_390_COPY:
2891 case elfcpp::R_390_GLOB_DAT:
2892 case elfcpp::R_390_JMP_SLOT:
2893 case elfcpp::R_390_RELATIVE:
2894 case elfcpp::R_390_IRELATIVE:
2895 // These are outstanding tls relocs, which are unexpected when linking
2896 case elfcpp::R_390_TLS_TPOFF:
2897 case elfcpp::R_390_TLS_DTPOFF:
2898 case elfcpp::R_390_TLS_DTPMOD:
2899 gold_error(_("%s: unexpected reloc %u in object file"),
2900 object->name().c_str(), r_type);
2901 break;
2902
2903 // These are initial tls relocs, which are expected for global()
2904 case elfcpp::R_390_TLS_GD32: // Global-dynamic
2905 case elfcpp::R_390_TLS_GD64:
2906 case elfcpp::R_390_TLS_GDCALL:
2907 case elfcpp::R_390_TLS_LDM32: // Local-dynamic
2908 case elfcpp::R_390_TLS_LDM64:
2909 case elfcpp::R_390_TLS_LDO32:
2910 case elfcpp::R_390_TLS_LDO64:
2911 case elfcpp::R_390_TLS_LDCALL:
2912 case elfcpp::R_390_TLS_IE32: // Initial-exec
2913 case elfcpp::R_390_TLS_IE64:
2914 case elfcpp::R_390_TLS_IEENT:
2915 case elfcpp::R_390_TLS_GOTIE12:
2916 case elfcpp::R_390_TLS_GOTIE20:
2917 case elfcpp::R_390_TLS_GOTIE32:
2918 case elfcpp::R_390_TLS_GOTIE64:
2919 case elfcpp::R_390_TLS_LOAD:
2920 case elfcpp::R_390_TLS_LE32: // Local-exec
2921 case elfcpp::R_390_TLS_LE64:
2922 {
2923 // For the optimizable Initial-Exec model, we can treat undef symbols
2924 // as final when building an executable.
2925 const bool is_final = (gsym->final_value_is_known() ||
2926 ((r_type == elfcpp::R_390_TLS_IE32 ||
2927 r_type == elfcpp::R_390_TLS_IE64 ||
2928 r_type == elfcpp::R_390_TLS_GOTIE32 ||
2929 r_type == elfcpp::R_390_TLS_GOTIE64) &&
2930 gsym->is_undefined() &&
2931 parameters->options().output_is_executable()));
2932 const tls::Tls_optimization optimized_type
2933 = Target_s390<size>::optimize_tls_reloc(is_final, r_type);
2934 switch (r_type)
2935 {
2936 case elfcpp::R_390_TLS_GD32: // General-dynamic
2937 case elfcpp::R_390_TLS_GD64:
2938 case elfcpp::R_390_TLS_GDCALL:
2939 if (optimized_type == tls::TLSOPT_NONE)
2940 {
2941 // Create a pair of GOT entries for the module index and
2942 // dtv-relative offset.
2943 Output_data_got<size, true>* got
2944 = target->got_section(symtab, layout);
2945 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2946 target->rela_dyn_section(layout),
2947 elfcpp::R_390_TLS_DTPMOD,
2948 elfcpp::R_390_TLS_DTPOFF);
2949 }
2950 else if (optimized_type == tls::TLSOPT_TO_IE)
2951 {
2952 // Create a GOT entry for the tp-relative offset.
2953 Output_data_got<size, true>* got
2954 = target->got_section(symtab, layout);
2955 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2956 target->rela_dyn_section(layout),
2957 elfcpp::R_390_TLS_TPOFF);
2958 }
2959 else if (optimized_type != tls::TLSOPT_TO_LE)
2960 unsupported_reloc_global(object, r_type, gsym);
2961 break;
2962
2963 case elfcpp::R_390_TLS_LDM32: // Local-dynamic
2964 case elfcpp::R_390_TLS_LDM64:
2965 case elfcpp::R_390_TLS_LDCALL:
2966 if (optimized_type == tls::TLSOPT_NONE)
2967 {
2968 // Create a GOT entry for the module index.
2969 target->got_mod_index_entry(symtab, layout, object);
2970 }
2971 else if (optimized_type != tls::TLSOPT_TO_LE)
2972 unsupported_reloc_global(object, r_type, gsym);
2973 break;
2974
2975 case elfcpp::R_390_TLS_LDO32:
2976 case elfcpp::R_390_TLS_LDO64:
2977 break;
2978
2979 case elfcpp::R_390_TLS_IE32: // Initial-exec
2980 case elfcpp::R_390_TLS_IE64:
2981 // These two involve an absolute address
2982 if (parameters->options().shared())
2983 {
2984 if ((size == 32 && r_type == elfcpp::R_390_TLS_IE32) ||
2985 (size == 64 && r_type == elfcpp::R_390_TLS_IE64))
2986 {
2987 // We need to create a dynamic relocation.
2988 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2989 rela_dyn->add_global_relative(gsym, elfcpp::R_390_RELATIVE,
2990 output_section, object,
2991 data_shndx,
2992 reloc.get_r_offset(),
2993 reloc.get_r_addend(), false);
2994 }
2995 else
2996 {
2997 unsupported_reloc_global(object, r_type, gsym);
2998 }
2999 }
d8e90251 3000 // Fall through.
e79a4bad
MK
3001 case elfcpp::R_390_TLS_IEENT:
3002 case elfcpp::R_390_TLS_GOTIE12:
3003 case elfcpp::R_390_TLS_GOTIE20:
3004 case elfcpp::R_390_TLS_GOTIE32:
3005 case elfcpp::R_390_TLS_GOTIE64:
3006 case elfcpp::R_390_TLS_LOAD:
3007 layout->set_has_static_tls();
3008 if (optimized_type == tls::TLSOPT_NONE)
3009 {
3010 if (is_final && !parameters->options().shared())
3011 {
3012 // We're making an executable, and the symbol is local, but
3013 // we cannot optimize to LE. Make a const GOT entry instead.
3014 Output_data_got<size, true>* got
3015 = target->got_section(symtab, layout);
3016 got->add_global_plt(gsym, GOT_TYPE_TLS_OFFSET);
3017 }
3018 else
3019 {
3020 // Create a GOT entry for the tp-relative offset.
3021 Output_data_got<size, true>* got
3022 = target->got_section(symtab, layout);
3023 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3024 target->rela_dyn_section(layout),
3025 elfcpp::R_390_TLS_TPOFF);
3026 }
3027 }
3028 else if (optimized_type != tls::TLSOPT_TO_LE)
3029 unsupported_reloc_global(object, r_type, gsym);
3030 break;
3031
3032 case elfcpp::R_390_TLS_LE32: // Local-exec
3033 case elfcpp::R_390_TLS_LE64:
3034 layout->set_has_static_tls();
3035 if (parameters->options().shared())
3036 {
3037 // We need to create a dynamic relocation.
3038 if ((size == 32 && r_type == elfcpp::R_390_TLS_LE32) ||
3039 (size == 64 && r_type == elfcpp::R_390_TLS_LE64))
3040 {
3041 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3042 rela_dyn->add_global(gsym, elfcpp::R_390_TLS_TPOFF,
3043 output_section, object,
3044 data_shndx, reloc.get_r_offset(),
3045 reloc.get_r_addend());
3046 }
3047 else
3048 {
3049 unsupported_reloc_global(object, r_type, gsym);
3050 }
3051 }
3052 break;
3053
3054 default:
3055 gold_unreachable();
3056 }
3057 }
3058 break;
3059
3060 default:
3061 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3062 object->name().c_str(), r_type,
3063 gsym->demangled_name().c_str());
3064 break;
3065 }
3066}
3067
3068
3069// Report an unsupported relocation against a global symbol.
3070
3071template<int size>
3072void
3073Target_s390<size>::Scan::unsupported_reloc_global(
3074 Sized_relobj_file<size, true>* object,
3075 unsigned int r_type,
3076 Symbol* gsym)
3077{
3078 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3079 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3080}
3081
3082// Returns true if this relocation type could be that of a function pointer.
3083template<int size>
3084inline bool
3085Target_s390<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
3086{
3087 switch (r_type)
3088 {
3089 case elfcpp::R_390_32:
3090 case elfcpp::R_390_64:
3091 case elfcpp::R_390_PC32DBL: // could be used by larl insn
3092 case elfcpp::R_390_GOT12:
3093 case elfcpp::R_390_GOT16:
3094 case elfcpp::R_390_GOT20:
3095 case elfcpp::R_390_GOT32:
3096 case elfcpp::R_390_GOT64:
3097 case elfcpp::R_390_GOTENT:
3098 case elfcpp::R_390_GOTOFF16:
3099 case elfcpp::R_390_GOTOFF32:
3100 case elfcpp::R_390_GOTOFF64:
3101 return true;
3102 }
3103 return false;
3104}
3105
3106// For safe ICF, scan a relocation for a local symbol to check if it
3107// corresponds to a function pointer being taken. In that case mark
3108// the function whose pointer was taken as not foldable.
3109
3110template<int size>
3111inline bool
3112Target_s390<size>::Scan::local_reloc_may_be_function_pointer(
3113 Symbol_table* ,
3114 Layout* ,
3115 Target_s390<size>* ,
3116 Sized_relobj_file<size, true>* ,
3117 unsigned int ,
3118 Output_section* ,
3119 const elfcpp::Rela<size, true>& ,
3120 unsigned int r_type,
3121 const elfcpp::Sym<size, true>&)
3122{
3123 // When building a shared library, do not fold any local symbols.
3124 return (parameters->options().shared()
3125 || possible_function_pointer_reloc(r_type));
3126}
3127
3128// For safe ICF, scan a relocation for a global symbol to check if it
3129// corresponds to a function pointer being taken. In that case mark
3130// the function whose pointer was taken as not foldable.
3131
3132template<int size>
3133inline bool
3134Target_s390<size>::Scan::global_reloc_may_be_function_pointer(
3135 Symbol_table*,
3136 Layout* ,
3137 Target_s390<size>* ,
3138 Sized_relobj_file<size, true>* ,
3139 unsigned int ,
3140 Output_section* ,
3141 const elfcpp::Rela<size, true>& ,
3142 unsigned int r_type,
3143 Symbol* gsym)
3144{
3145 // When building a shared library, do not fold symbols whose visibility
3146 // is hidden, internal or protected.
3147 return ((parameters->options().shared()
3148 && (gsym->visibility() == elfcpp::STV_INTERNAL
3149 || gsym->visibility() == elfcpp::STV_PROTECTED
3150 || gsym->visibility() == elfcpp::STV_HIDDEN))
3151 || possible_function_pointer_reloc(r_type));
3152}
3153
3154template<int size>
3155void
3156Target_s390<size>::gc_process_relocs(Symbol_table* symtab,
3157 Layout* layout,
3158 Sized_relobj_file<size, true>* object,
3159 unsigned int data_shndx,
3160 unsigned int sh_type,
3161 const unsigned char* prelocs,
3162 size_t reloc_count,
3163 Output_section* output_section,
3164 bool needs_special_offset_handling,
3165 size_t local_symbol_count,
3166 const unsigned char* plocal_symbols)
3167{
4d625b70
CC
3168 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
3169 Classify_reloc;
e79a4bad
MK
3170
3171 if (sh_type == elfcpp::SHT_REL)
3172 return;
3173
4d625b70 3174 gold::gc_process_relocs<size, true, Target_s390<size>, Scan, Classify_reloc>(
e79a4bad
MK
3175 symtab,
3176 layout,
3177 this,
3178 object,
3179 data_shndx,
3180 prelocs,
3181 reloc_count,
3182 output_section,
3183 needs_special_offset_handling,
3184 local_symbol_count,
3185 plocal_symbols);
3186}
3187
3188// Perform a relocation.
3189
3190template<int size>
3191inline bool
3192Target_s390<size>::Relocate::relocate(
3193 const Relocate_info<size, true>* relinfo,
91a65d2f 3194 unsigned int,
e79a4bad
MK
3195 Target_s390<size>* target,
3196 Output_section*,
3197 size_t relnum,
91a65d2f 3198 const unsigned char* preloc,
e79a4bad
MK
3199 const Sized_symbol<size>* gsym,
3200 const Symbol_value<size>* psymval,
3201 unsigned char* view,
3202 typename elfcpp::Elf_types<size>::Elf_Addr address,
3203 section_size_type view_size)
3204{
3205 if (view == NULL)
3206 return true;
3207
91a65d2f
AM
3208 const elfcpp::Rela<size, true> rela(preloc);
3209 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
e79a4bad
MK
3210 const Sized_relobj_file<size, true>* object = relinfo->object;
3211
3212 // Pick the value to use for symbols defined in the PLT.
3213 Symbol_value<size> symval;
3214 if (gsym != NULL
3215 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3216 {
3217 symval.set_output_value(target->plt_address_for_global(gsym));
3218 psymval = &symval;
3219 }
3220 else if (gsym == NULL && psymval->is_ifunc_symbol())
3221 {
3222 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3223 if (object->local_has_plt_offset(r_sym))
3224 {
3225 symval.set_output_value(target->plt_address_for_local(object, r_sym));
3226 psymval = &symval;
3227 }
3228 }
3229
3230 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3231
3232 typename elfcpp::Elf_types<size>::Elf_Addr value = 0;
3233
3234 switch (r_type)
3235 {
3236 case elfcpp::R_390_PLT64:
3237 case elfcpp::R_390_PLT32:
3238 case elfcpp::R_390_PLT32DBL:
3239 case elfcpp::R_390_PLT24DBL:
3240 case elfcpp::R_390_PLT16DBL:
3241 case elfcpp::R_390_PLT12DBL:
3242 gold_assert(gsym == NULL
3243 || gsym->has_plt_offset()
3244 || gsym->final_value_is_known()
3245 || (gsym->is_defined()
3246 && !gsym->is_from_dynobj()
3247 && !gsym->is_preemptible()));
d8e90251 3248 // Fall through.
e79a4bad
MK
3249 case elfcpp::R_390_8:
3250 case elfcpp::R_390_12:
3251 case elfcpp::R_390_16:
3252 case elfcpp::R_390_20:
3253 case elfcpp::R_390_32:
3254 case elfcpp::R_390_64:
3255 case elfcpp::R_390_PC16:
3256 case elfcpp::R_390_PC32:
3257 case elfcpp::R_390_PC64:
3258 case elfcpp::R_390_PC32DBL:
3259 case elfcpp::R_390_PC24DBL:
3260 case elfcpp::R_390_PC16DBL:
3261 case elfcpp::R_390_PC12DBL:
3262 value = psymval->value(object, addend);
3263 break;
3264
3265 case elfcpp::R_390_GOTPC:
3266 case elfcpp::R_390_GOTPCDBL:
3267 gold_assert(gsym != NULL);
3268 value = target->got_address() + addend;
3269 break;
3270
3271 case elfcpp::R_390_PLTOFF64:
3272 case elfcpp::R_390_PLTOFF32:
3273 case elfcpp::R_390_PLTOFF16:
3274 gold_assert(gsym == NULL
3275 || gsym->has_plt_offset()
3276 || gsym->final_value_is_known());
d8e90251 3277 // Fall through.
e79a4bad
MK
3278 case elfcpp::R_390_GOTOFF64:
3279 case elfcpp::R_390_GOTOFF32:
3280 case elfcpp::R_390_GOTOFF16:
3281 value = (psymval->value(object, addend)
3282 - target->got_address());
3283 break;
3284
3285 case elfcpp::R_390_GOT12:
3286 case elfcpp::R_390_GOT16:
3287 case elfcpp::R_390_GOT20:
3288 case elfcpp::R_390_GOT32:
3289 case elfcpp::R_390_GOT64:
3290 case elfcpp::R_390_GOTENT:
3291 case elfcpp::R_390_GOTPLT12:
3292 case elfcpp::R_390_GOTPLT16:
3293 case elfcpp::R_390_GOTPLT20:
3294 case elfcpp::R_390_GOTPLT32:
3295 case elfcpp::R_390_GOTPLT64:
3296 case elfcpp::R_390_GOTPLTENT:
3297 {
3298 unsigned int got_offset = 0;
3299 if (gsym != NULL)
3300 {
3301 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3302 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
3303 }
3304 else
3305 {
3306 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3307 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3308 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3309 }
3310 value = got_offset + target->got_main_offset() + addend;
3311 }
3312 break;
3313
3314 // These are initial tls relocs, which are expected when linking
3315 case elfcpp::R_390_TLS_LOAD:
3316 case elfcpp::R_390_TLS_GDCALL: // Global-dynamic
3317 case elfcpp::R_390_TLS_GD32:
3318 case elfcpp::R_390_TLS_GD64:
3319 case elfcpp::R_390_TLS_LDCALL: // Local-dynamic
3320 case elfcpp::R_390_TLS_LDM32:
3321 case elfcpp::R_390_TLS_LDM64:
3322 case elfcpp::R_390_TLS_LDO32:
3323 case elfcpp::R_390_TLS_LDO64:
3324 case elfcpp::R_390_TLS_GOTIE12: // Initial-exec
3325 case elfcpp::R_390_TLS_GOTIE20:
3326 case elfcpp::R_390_TLS_GOTIE32:
3327 case elfcpp::R_390_TLS_GOTIE64:
3328 case elfcpp::R_390_TLS_IE32:
3329 case elfcpp::R_390_TLS_IE64:
3330 case elfcpp::R_390_TLS_IEENT:
3331 case elfcpp::R_390_TLS_LE32: // Local-exec
3332 case elfcpp::R_390_TLS_LE64:
3333 value = this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3334 view, view_size);
3335 break;
3336
3337 default:
3338 break;
3339 }
3340
3341 typename S390_relocate_functions<size>::Status status
3342 = S390_relocate_functions<size>::STATUS_OK;
3343
3344 switch (r_type)
3345 {
3346 case elfcpp::R_390_NONE:
3347 case elfcpp::R_390_GNU_VTINHERIT:
3348 case elfcpp::R_390_GNU_VTENTRY:
3349 case elfcpp::R_390_TLS_GDCALL:
3350 case elfcpp::R_390_TLS_LDCALL:
3351 case elfcpp::R_390_TLS_LOAD:
3352 break;
3353
3354 case elfcpp::R_390_64:
3355 case elfcpp::R_390_GOT64:
3356 case elfcpp::R_390_GOTPLT64:
3357 case elfcpp::R_390_PLTOFF64:
3358 case elfcpp::R_390_GOTOFF64:
3359 case elfcpp::R_390_TLS_GD64:
3360 case elfcpp::R_390_TLS_LDM64:
3361 case elfcpp::R_390_TLS_LDO64:
3362 case elfcpp::R_390_TLS_GOTIE64:
3363 case elfcpp::R_390_TLS_IE64:
3364 case elfcpp::R_390_TLS_LE64:
3365 Relocate_functions<size, true>::rela64(view, value, 0);
3366 break;
3367
3368 case elfcpp::R_390_32:
3369 case elfcpp::R_390_GOT32:
3370 case elfcpp::R_390_GOTPLT32:
3371 case elfcpp::R_390_PLTOFF32:
3372 case elfcpp::R_390_GOTOFF32:
3373 case elfcpp::R_390_TLS_GD32:
3374 case elfcpp::R_390_TLS_LDM32:
3375 case elfcpp::R_390_TLS_LDO32:
3376 case elfcpp::R_390_TLS_GOTIE32:
3377 case elfcpp::R_390_TLS_IE32:
3378 case elfcpp::R_390_TLS_LE32:
3379 Relocate_functions<size, true>::rela32(view, value, 0);
3380 break;
3381
3382 case elfcpp::R_390_20:
3383 case elfcpp::R_390_GOT20:
3384 case elfcpp::R_390_GOTPLT20:
3385 case elfcpp::R_390_TLS_GOTIE20:
3386 status = S390_relocate_functions<size>::rela20(view, value);
3387 break;
3388
3389 case elfcpp::R_390_16:
3390 case elfcpp::R_390_GOT16:
3391 case elfcpp::R_390_GOTPLT16:
3392 case elfcpp::R_390_PLTOFF16:
3393 case elfcpp::R_390_GOTOFF16:
3394 status = S390_relocate_functions<size>::rela16(view, value);
3395 break;
3396
3397 case elfcpp::R_390_12:
3398 case elfcpp::R_390_GOT12:
3399 case elfcpp::R_390_GOTPLT12:
3400 case elfcpp::R_390_TLS_GOTIE12:
3401 status = S390_relocate_functions<size>::rela12(view, value);
3402 break;
3403
3404 case elfcpp::R_390_8:
3405 Relocate_functions<size, true>::rela8(view, value, 0);
3406 break;
3407
3408 case elfcpp::R_390_PC16:
3409 Relocate_functions<size, true>::pcrela16(view, value, 0,
3410 address);
3411 break;
3412
3413 case elfcpp::R_390_PLT64:
3414 case elfcpp::R_390_PC64:
3415 Relocate_functions<size, true>::pcrela64(view, value, 0, address);
3416 break;
3417
3418 case elfcpp::R_390_PLT32:
3419 case elfcpp::R_390_PC32:
3420 case elfcpp::R_390_GOTPC:
3421 Relocate_functions<size, true>::pcrela32(view, value, 0, address);
3422 break;
3423
3424 case elfcpp::R_390_PLT32DBL:
3425 case elfcpp::R_390_PC32DBL:
3426 case elfcpp::R_390_GOTPCDBL:
3427 status = S390_relocate_functions<size>::pcrela32dbl(view, value, address);
3428 break;
3429
3430 case elfcpp::R_390_PLT24DBL:
3431 case elfcpp::R_390_PC24DBL:
3432 status = S390_relocate_functions<size>::pcrela24dbl(view, value, address);
3433 break;
3434
3435 case elfcpp::R_390_PLT16DBL:
3436 case elfcpp::R_390_PC16DBL:
3437 status = S390_relocate_functions<size>::pcrela16dbl(view, value, address);
3438 break;
3439
3440 case elfcpp::R_390_PLT12DBL:
3441 case elfcpp::R_390_PC12DBL:
3442 status = S390_relocate_functions<size>::pcrela12dbl(view, value, address);
3443 break;
3444
3445 case elfcpp::R_390_GOTENT:
3446 case elfcpp::R_390_GOTPLTENT:
3447 case elfcpp::R_390_TLS_IEENT:
3448 value += target->got_address();
3449 status = S390_relocate_functions<size>::pcrela32dbl(view, value, address);
3450 break;
3451
3452 case elfcpp::R_390_COPY:
3453 case elfcpp::R_390_GLOB_DAT:
3454 case elfcpp::R_390_JMP_SLOT:
3455 case elfcpp::R_390_RELATIVE:
3456 case elfcpp::R_390_IRELATIVE:
3457 // These are outstanding tls relocs, which are unexpected when linking
3458 case elfcpp::R_390_TLS_TPOFF:
3459 case elfcpp::R_390_TLS_DTPMOD:
3460 case elfcpp::R_390_TLS_DTPOFF:
3461 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3462 _("unexpected reloc %u in object file"),
3463 r_type);
3464 break;
3465
3466 default:
3467 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3468 _("unsupported reloc %u"),
3469 r_type);
3470 break;
3471 }
3472
3473 if (status != S390_relocate_functions<size>::STATUS_OK)
3474 {
3475 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3476 _("relocation overflow"));
3477 }
3478
3479 return true;
3480}
3481
3482// Perform a TLS relocation.
3483
3484template<int size>
3485inline typename elfcpp::Elf_types<size>::Elf_Addr
3486Target_s390<size>::Relocate::relocate_tls(
3487 const Relocate_info<size, true>* relinfo,
3488 Target_s390<size>* target,
3489 size_t relnum,
3490 const elfcpp::Rela<size, true>& rela,
3491 unsigned int r_type,
3492 const Sized_symbol<size>* gsym,
3493 const Symbol_value<size>* psymval,
3494 unsigned char* view,
3495 section_size_type view_size)
3496{
3497 Output_segment* tls_segment = relinfo->layout->tls_segment();
3498
3499 const Sized_relobj_file<size, true>* object = relinfo->object;
3500 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3501 elfcpp::Shdr<size, true> data_shdr(relinfo->data_shdr);
3502 bool is_allocatable = (data_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0;
3503
3504 typename elfcpp::Elf_types<size>::Elf_Addr value
3505 = psymval->value(relinfo->object, addend);
3506
3507 const bool is_final = (gsym == NULL
3508 ? !parameters->options().shared()
3509 : gsym->final_value_is_known());
3510 tls::Tls_optimization optimized_type
3511 = Target_s390<size>::optimize_tls_reloc(is_final, r_type);
3512 switch (r_type)
3513 {
3514 case elfcpp::R_390_TLS_GDCALL: // Global-dynamic marker
3515 if (optimized_type == tls::TLSOPT_TO_LE)
3516 {
3517 if (tls_segment == NULL)
3518 {
3519 gold_assert(parameters->errors()->error_count() > 0
3520 || issue_undefined_symbol_error(gsym));
3521 return 0;
3522 }
3523 this->tls_gd_to_le(relinfo, relnum, rela, view, view_size);
3524 break;
3525 }
3526 else
3527 {
3528 if (optimized_type == tls::TLSOPT_TO_IE)
3529 {
3530 this->tls_gd_to_ie(relinfo, relnum, rela, view, view_size);
3531 break;
3532 }
3533 else if (optimized_type == tls::TLSOPT_NONE)
3534 {
3535 break;
3536 }
3537 }
3538 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3539 _("unsupported reloc %u"), r_type);
3540 break;
3541
3542 case elfcpp::R_390_TLS_GD32: // Global-dynamic
3543 case elfcpp::R_390_TLS_GD64:
3544 if (optimized_type == tls::TLSOPT_TO_LE)
3545 {
3546 if (tls_segment == NULL)
3547 {
3548 gold_assert(parameters->errors()->error_count() > 0
3549 || issue_undefined_symbol_error(gsym));
3550 return 0;
3551 }
3552 return value - tls_segment->memsz();
3553 }
3554 else
3555 {
3556 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3557 ? GOT_TYPE_TLS_OFFSET
3558 : GOT_TYPE_TLS_PAIR);
3559 if (gsym != NULL)
3560 {
3561 gold_assert(gsym->has_got_offset(got_type));
3562 return (gsym->got_offset(got_type)
3563 + target->got_main_offset()
3564 + addend);
3565 }
3566 else
3567 {
3568 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3569 gold_assert(object->local_has_got_offset(r_sym, got_type));
3570 return (object->local_got_offset(r_sym, got_type)
3571 + target->got_main_offset()
3572 + addend);
3573 }
3574 }
3575 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3576 _("unsupported reloc %u"), r_type);
3577 break;
3578
3579 case elfcpp::R_390_TLS_LDCALL: // Local-dynamic marker
3580 // This is a marker relocation. If the sequence is being turned to LE,
3581 // we modify the instruction, otherwise the instruction is untouched.
3582 if (optimized_type == tls::TLSOPT_TO_LE)
3583 {
3584 if (tls_segment == NULL)
3585 {
3586 gold_assert(parameters->errors()->error_count() > 0
3587 || issue_undefined_symbol_error(gsym));
3588 return 0;
3589 }
3590 this->tls_ld_to_le(relinfo, relnum, rela, view, view_size);
3591 break;
3592 }
3593 else if (optimized_type == tls::TLSOPT_NONE)
3594 {
3595 break;
3596 }
3597 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3598 _("unsupported reloc %u"), r_type);
3599 break;
3600
3601 case elfcpp::R_390_TLS_LDM32: // Local-dynamic module
3602 case elfcpp::R_390_TLS_LDM64:
3603 if (optimized_type == tls::TLSOPT_TO_LE)
3604 {
3605 if (tls_segment == NULL)
3606 {
3607 gold_assert(parameters->errors()->error_count() > 0
3608 || issue_undefined_symbol_error(gsym));
3609 return 0;
3610 }
3611 // Doesn't matter what we fill it with - it's going to be unused.
3612 return 0;
3613 }
3614 else if (optimized_type == tls::TLSOPT_NONE)
3615 {
3616 // Relocate the field with the offset of the GOT entry for
3617 // the module index.
3618 return (target->got_mod_index_entry(NULL, NULL, NULL)
3619 + addend
3620 + target->got_main_offset());
3621 }
3622 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3623 _("unsupported reloc %u"), r_type);
3624 break;
3625
3626 case elfcpp::R_390_TLS_LDO32: // Local-dynamic offset
3627 case elfcpp::R_390_TLS_LDO64:
3628 // This relocation type is used in debugging information.
3629 // In that case we need to not optimize the value. If the
3630 // section is not allocatable, then we assume we should not
3631 // optimize this reloc.
3632 if (optimized_type == tls::TLSOPT_TO_LE && is_allocatable)
3633 {
3634 if (tls_segment == NULL)
3635 {
3636 gold_assert(parameters->errors()->error_count() > 0
3637 || issue_undefined_symbol_error(gsym));
3638 return 0;
3639 }
3640 value -= tls_segment->memsz();
3641 }
3642 return value;
3643
3644 case elfcpp::R_390_TLS_LOAD: // Initial-exec marker
3645 // This is a marker relocation. If the sequence is being turned to LE,
3646 // we modify the instruction, otherwise the instruction is untouched.
3647 if (gsym != NULL
3648 && gsym->is_undefined()
3649 && parameters->options().output_is_executable())
3650 {
3651 Target_s390<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3652 rela, view,
3653 view_size);
3654 break;
3655 }
3656 else if (optimized_type == tls::TLSOPT_TO_LE)
3657 {
3658 if (tls_segment == NULL)
3659 {
3660 gold_assert(parameters->errors()->error_count() > 0
3661 || issue_undefined_symbol_error(gsym));
3662 return 0;
3663 }
3664 Target_s390<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3665 rela, view,
3666 view_size);
3667 break;
3668 }
3669 else if (optimized_type == tls::TLSOPT_NONE)
3670 {
3671 break;
3672 }
3673 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3674 _("unsupported reloc type %u"),
3675 r_type);
3676 break;
3677
3678 case elfcpp::R_390_TLS_GOTIE12: // Initial-exec, not optimizable
3679 case elfcpp::R_390_TLS_GOTIE20:
3680 case elfcpp::R_390_TLS_IEENT:
3681 case elfcpp::R_390_TLS_GOTIE32: // Initial-exec, optimizable
3682 case elfcpp::R_390_TLS_GOTIE64:
3683 case elfcpp::R_390_TLS_IE32:
3684 case elfcpp::R_390_TLS_IE64:
3685 if (gsym != NULL
3686 && gsym->is_undefined()
3687 && parameters->options().output_is_executable()
3688 // These three cannot be optimized to LE, no matter what
3689 && r_type != elfcpp::R_390_TLS_GOTIE12
3690 && r_type != elfcpp::R_390_TLS_GOTIE20
3691 && r_type != elfcpp::R_390_TLS_IEENT)
3692 {
3693 return value;
3694 }
3695 else if (optimized_type == tls::TLSOPT_TO_LE)
3696 {
3697 if (tls_segment == NULL)
3698 {
3699 gold_assert(parameters->errors()->error_count() > 0
3700 || issue_undefined_symbol_error(gsym));
3701 return 0;
3702 }
3703 return value - tls_segment->memsz();
3704 }
3705 else if (optimized_type == tls::TLSOPT_NONE)
3706 {
3707 // Relocate the field with the offset of the GOT entry for
3708 // the tp-relative offset of the symbol.
3709 unsigned int got_offset;
3710 if (gsym != NULL)
3711 {
3712 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3713 got_offset = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
3714 }
3715 else
3716 {
3717 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3718 gold_assert(object->local_has_got_offset(r_sym,
3719 GOT_TYPE_TLS_OFFSET));
3720 got_offset = object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
3721 }
3722 got_offset += target->got_main_offset();
3723 if (r_type == elfcpp::R_390_TLS_IE32
3724 || r_type == elfcpp::R_390_TLS_IE64)
3725 return target->got_address() + got_offset + addend;
3726 else
3727 return got_offset + addend;
3728 }
3729 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3730 _("unsupported reloc type %u"),
3731 r_type);
3732 break;
3733
3734 case elfcpp::R_390_TLS_LE32: // Local-exec
3735 case elfcpp::R_390_TLS_LE64:
3736 if (tls_segment == NULL)
3737 {
3738 gold_assert(parameters->errors()->error_count() > 0
3739 || issue_undefined_symbol_error(gsym));
3740 return 0;
3741 }
3742 return value - tls_segment->memsz();
3743 }
3744 return 0;
3745}
3746
3747// Do a relocation in which we convert a TLS General-Dynamic to an
3748// Initial-Exec.
3749
3750template<int size>
3751inline void
3752Target_s390<size>::Relocate::tls_gd_to_ie(
3753 const Relocate_info<size, true>* relinfo,
3754 size_t relnum,
3755 const elfcpp::Rela<size, true>& rela,
3756 unsigned char* view,
3757 section_size_type view_size)
3758{
3759 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3760 if (view[0] == 0x4d)
3761 {
3762 // bas, don't care about details
3763 // Change to l %r2, 0(%r2, %r12)
3764 view[0] = 0x58;
3765 view[1] = 0x22;
3766 view[2] = 0xc0;
3767 view[3] = 0x00;
3768 return;
3769 }
3770 else if (view[0] == 0xc0)
3771 {
3772 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3773 // brasl %r14, __tls_get_offset@plt
3774 if (view[1] == 0xe5)
3775 {
3776 // Change to l/lg %r2, 0(%r2, %r12)
3777 // There was a PLT32DBL reloc at the last 4 bytes, overwrite its result.
3778 if (size == 32)
3779 {
3780 // l
3781 view[0] = 0x58;
3782 view[1] = 0x22;
3783 view[2] = 0xc0;
3784 view[3] = 0x00;
3785 // nop
3786 view[4] = 0x07;
3787 view[5] = 0x07;
3788 }
3789 else
3790 {
3791 // lg
3792 view[0] = 0xe3;
3793 view[1] = 0x22;
3794 view[2] = 0xc0;
3795 view[3] = 0;
3796 view[4] = 0;
3797 view[5] = 0x04;
3798 }
3799 return;
3800 }
3801 }
3802 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3803 _("unsupported op for GD to IE"));
3804}
3805
3806// Do a relocation in which we convert a TLS General-Dynamic to a
3807// Local-Exec.
3808
3809template<int size>
3810inline void
3811Target_s390<size>::Relocate::tls_gd_to_le(
3812 const Relocate_info<size, true>* relinfo,
3813 size_t relnum,
3814 const elfcpp::Rela<size, true>& rela,
3815 unsigned char* view,
3816 section_size_type view_size)
3817{
3818 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3819 if (view[0] == 0x0d)
3820 {
3821 // basr, change to nop
3822 view[0] = 0x07;
3823 view[1] = 0x07;
3824 }
3825 else if (view[0] == 0x4d)
3826 {
3827 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3828 // bas, don't care about details, change to nop
3829 view[0] = 0x47;
3830 view[1] = 0;
3831 view[2] = 0;
3832 view[3] = 0;
3833 return;
3834 }
3835 else if (view[0] == 0xc0)
3836 {
3837 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3838 // brasl %r14, __tls_get_offset@plt
3839 if (view[1] == 0xe5)
3840 {
3841 // Change to nop jump. There was a PLT32DBL reloc at the last
3842 // 4 bytes, overwrite its result.
3843 view[1] = 0x04;
3844 view[2] = 0;
3845 view[3] = 0;
3846 view[4] = 0;
3847 view[5] = 0;
3848 return;
3849 }
3850 }
3851 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3852 _("unsupported op for GD to LE"));
3853}
3854
3855template<int size>
3856inline void
3857Target_s390<size>::Relocate::tls_ld_to_le(
3858 const Relocate_info<size, true>* relinfo,
3859 size_t relnum,
3860 const elfcpp::Rela<size, true>& rela,
3861 unsigned char* view,
3862 section_size_type view_size)
3863{
3864 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3865
3866 if (view[0] == 0x0d)
3867 {
3868 // basr, change to nop
3869 view[0] = 0x07;
3870 view[1] = 0x07;
3871 }
3872 else if (view[0] == 0x4d)
3873 {
3874 // bas, don't care about details, change to nop
3875 view[0] = 0x47;
3876 view[1] = 0;
3877 view[2] = 0;
3878 view[3] = 0;
3879 return;
3880 }
3881 else if (view[0] == 0xc0)
3882 {
3883 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3884 // brasl %r14, __tls_get_offset@plt
3885 if (view[1] == 0xe5)
3886 {
3887 // Change to nop jump. There was a PLT32DBL reloc at the last
3888 // 4 bytes, overwrite its result.
3889 view[1] = 0x04;
3890 view[2] = 0;
3891 view[3] = 0;
3892 view[4] = 0;
3893 view[5] = 0;
3894 return;
3895 }
3896 }
3897 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3898 _("unsupported op for LD to LE"));
3899}
3900
3901// Do a relocation in which we convert a TLS Initial-Exec to a
3902// Local-Exec.
3903
3904template<int size>
3905inline void
3906Target_s390<size>::Relocate::tls_ie_to_le(
3907 const Relocate_info<size, true>* relinfo,
3908 size_t relnum,
3909 const elfcpp::Rela<size, true>& rela,
3910 unsigned char* view,
3911 section_size_type view_size)
3912{
3913 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3914
3915 if (view[0] == 0x58)
3916 {
3917 // l %rX, 0(%rY) or l %rX, 0(%rY, %r12)
3918 if ((view[2] & 0x0f) != 0 || view[3] != 0)
3919 goto err;
3920 int rx = view[1] >> 4 & 0xf;
3921 int ry = view[1] & 0xf;
3922 int rz = view[2] >> 4 & 0xf;
3923 if (rz == 0)
3924 {
3925 }
3926 else if (ry == 0)
3927 {
3928 ry = rz;
3929 }
3930 else if (rz == 12)
3931 {
3932 }
3933 else if (ry == 12)
3934 {
3935 ry = rz;
3936 }
3937 else
3938 goto err;
3939 // to lr %rX, $rY
3940 view[0] = 0x18;
3941 view[1] = rx << 4 | ry;
3942 // and insert a nop
3943 view[2] = 0x07;
3944 view[3] = 0x00;
3945 }
3946 else if (view[0] == 0xe3)
3947 {
3948 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3949 // lg %rX, 0(%rY) or lg %rX, 0(%rY, %r12)
3950 if ((view[2] & 0x0f) != 0 ||
3951 view[3] != 0 ||
3952 view[4] != 0 ||
3953 view[5] != 0x04)
3954 goto err;
3955 int rx = view[1] >> 4 & 0xf;
3956 int ry = view[1] & 0xf;
3957 int rz = view[2] >> 4 & 0xf;
3958 if (rz == 0)
3959 {
3960 }
3961 else if (ry == 0)
3962 {
3963 ry = rz;
3964 }
3965 else if (rz == 12)
3966 {
3967 }
3968 else if (ry == 12)
3969 {
3970 ry = rz;
3971 }
3972 else
3973 goto err;
3974 // to sllg %rX, $rY, 0
3975 view[0] = 0xeb;
3976 view[1] = rx << 4 | ry;
3977 view[2] = 0x00;
3978 view[3] = 0x00;
3979 view[4] = 0x00;
3980 view[5] = 0x0d;
3981 }
3982 else
3983 {
3984err:
3985 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3986 _("unsupported op for IE to LE"));
3987 }
3988}
3989
3990// Scan relocations for a section.
3991
3992template<int size>
3993void
3994Target_s390<size>::scan_relocs(Symbol_table* symtab,
3995 Layout* layout,
3996 Sized_relobj_file<size, true>* object,
3997 unsigned int data_shndx,
3998 unsigned int sh_type,
3999 const unsigned char* prelocs,
4000 size_t reloc_count,
4001 Output_section* output_section,
4002 bool needs_special_offset_handling,
4003 size_t local_symbol_count,
4004 const unsigned char* plocal_symbols)
4005{
4d625b70
CC
4006 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
4007 Classify_reloc;
4008
e79a4bad
MK
4009 if (sh_type == elfcpp::SHT_REL)
4010 {
4011 gold_error(_("%s: unsupported REL reloc section"),
4012 object->name().c_str());
4013 return;
4014 }
4015
4d625b70 4016 gold::scan_relocs<size, true, Target_s390<size>, Scan, Classify_reloc>(
e79a4bad
MK
4017 symtab,
4018 layout,
4019 this,
4020 object,
4021 data_shndx,
4022 prelocs,
4023 reloc_count,
4024 output_section,
4025 needs_special_offset_handling,
4026 local_symbol_count,
4027 plocal_symbols);
4028}
4029
4030// Finalize the sections.
4031
4032template<int size>
4033void
4034Target_s390<size>::do_finalize_sections(
4035 Layout* layout,
4036 const Input_objects*,
4037 Symbol_table* symtab)
4038{
4039 const Reloc_section* rel_plt = (this->plt_ == NULL
4040 ? NULL
4041 : this->plt_->rela_plt());
4042 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4043 this->rela_dyn_, true, size == 32);
4044
4045 this->layout_ = layout;
4046
4047 // Emit any relocs we saved in an attempt to avoid generating COPY
4048 // relocs.
4049 if (this->copy_relocs_.any_saved_relocs())
4050 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4051
4052 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4053 // the .got section.
4054 Symbol* sym = this->global_offset_table_;
4055 if (sym != NULL)
4056 {
4057 uint64_t data_size = this->got_->current_data_size();
4058 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4059 }
4060
4061 if (parameters->doing_static_link()
4062 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4063 {
4064 // If linking statically, make sure that the __rela_iplt symbols
4065 // were defined if necessary, even if we didn't create a PLT.
4066 static const Define_symbol_in_segment syms[] =
4067 {
4068 {
4069 "__rela_iplt_start", // name
4070 elfcpp::PT_LOAD, // segment_type
4071 elfcpp::PF_W, // segment_flags_set
4072 elfcpp::PF(0), // segment_flags_clear
4073 0, // value
4074 0, // size
4075 elfcpp::STT_NOTYPE, // type
4076 elfcpp::STB_GLOBAL, // binding
4077 elfcpp::STV_HIDDEN, // visibility
4078 0, // nonvis
4079 Symbol::SEGMENT_START, // offset_from_base
4080 true // only_if_ref
4081 },
4082 {
4083 "__rela_iplt_end", // name
4084 elfcpp::PT_LOAD, // segment_type
4085 elfcpp::PF_W, // segment_flags_set
4086 elfcpp::PF(0), // segment_flags_clear
4087 0, // value
4088 0, // size
4089 elfcpp::STT_NOTYPE, // type
4090 elfcpp::STB_GLOBAL, // binding
4091 elfcpp::STV_HIDDEN, // visibility
4092 0, // nonvis
4093 Symbol::SEGMENT_START, // offset_from_base
4094 true // only_if_ref
4095 }
4096 };
4097
4098 symtab->define_symbols(layout, 2, syms,
4099 layout->script_options()->saw_sections_clause());
4100 }
4101}
4102
4d625b70 4103// Scan the relocs during a relocatable link.
e79a4bad
MK
4104
4105template<int size>
4d625b70
CC
4106void
4107Target_s390<size>::scan_relocatable_relocs(
4108 Symbol_table* symtab,
4109 Layout* layout,
4110 Sized_relobj_file<size, true>* object,
4111 unsigned int data_shndx,
4112 unsigned int sh_type,
4113 const unsigned char* prelocs,
4114 size_t reloc_count,
4115 Output_section* output_section,
4116 bool needs_special_offset_handling,
4117 size_t local_symbol_count,
4118 const unsigned char* plocal_symbols,
4119 Relocatable_relocs* rr)
e79a4bad 4120{
4d625b70
CC
4121 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
4122 Classify_reloc;
4123 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
4124 Scan_relocatable_relocs;
e79a4bad 4125
4d625b70 4126 gold_assert(sh_type == elfcpp::SHT_RELA);
e79a4bad 4127
4d625b70
CC
4128 gold::scan_relocatable_relocs<size, true, Scan_relocatable_relocs>(
4129 symtab,
4130 layout,
4131 object,
4132 data_shndx,
4133 prelocs,
4134 reloc_count,
4135 output_section,
4136 needs_special_offset_handling,
4137 local_symbol_count,
4138 plocal_symbols,
4139 rr);
e79a4bad
MK
4140}
4141
4d625b70 4142// Scan the relocs for --emit-relocs.
e79a4bad
MK
4143
4144template<int size>
4145void
4d625b70 4146Target_s390<size>::emit_relocs_scan(
e79a4bad
MK
4147 Symbol_table* symtab,
4148 Layout* layout,
4149 Sized_relobj_file<size, true>* object,
4150 unsigned int data_shndx,
4151 unsigned int sh_type,
4152 const unsigned char* prelocs,
4153 size_t reloc_count,
4154 Output_section* output_section,
4155 bool needs_special_offset_handling,
4156 size_t local_symbol_count,
4d625b70 4157 const unsigned char* plocal_syms,
e79a4bad
MK
4158 Relocatable_relocs* rr)
4159{
4d625b70
CC
4160 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
4161 Classify_reloc;
4162 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
4163 Emit_relocs_strategy;
e79a4bad 4164
4d625b70 4165 gold_assert(sh_type == elfcpp::SHT_RELA);
e79a4bad 4166
4d625b70 4167 gold::scan_relocatable_relocs<size, true, Emit_relocs_strategy>(
e79a4bad
MK
4168 symtab,
4169 layout,
4170 object,
4171 data_shndx,
4172 prelocs,
4173 reloc_count,
4174 output_section,
4175 needs_special_offset_handling,
4176 local_symbol_count,
4d625b70 4177 plocal_syms,
e79a4bad
MK
4178 rr);
4179}
4180
4181// Relocate a section during a relocatable link.
4182
4183template<int size>
4184void
4185Target_s390<size>::relocate_relocs(
4186 const Relocate_info<size, true>* relinfo,
4187 unsigned int sh_type,
4188 const unsigned char* prelocs,
4189 size_t reloc_count,
4190 Output_section* output_section,
4191 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
e79a4bad
MK
4192 unsigned char* view,
4193 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4194 section_size_type view_size,
4195 unsigned char* reloc_view,
4196 section_size_type reloc_view_size)
4197{
4d625b70
CC
4198 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
4199 Classify_reloc;
4200
e79a4bad
MK
4201 gold_assert(sh_type == elfcpp::SHT_RELA);
4202
4d625b70 4203 gold::relocate_relocs<size, true, Classify_reloc>(
e79a4bad
MK
4204 relinfo,
4205 prelocs,
4206 reloc_count,
4207 output_section,
4208 offset_in_output_section,
e79a4bad
MK
4209 view,
4210 view_address,
4211 view_size,
4212 reloc_view,
4213 reloc_view_size);
4214}
4215
4216// Return the offset to use for the GOT_INDX'th got entry which is
4217// for a local tls symbol specified by OBJECT, SYMNDX.
4218template<int size>
4219int64_t
4220Target_s390<size>::do_tls_offset_for_local(
4221 const Relobj*,
4222 unsigned int,
e4d49a0f
AM
4223 unsigned int,
4224 uint64_t) const
e79a4bad
MK
4225{
4226 // The only way we can get called is when IEENT/GOTIE12/GOTIE20
4227 // couldn't be optimised to LE.
4228 Output_segment* tls_segment = layout_->tls_segment();
4229 return -tls_segment->memsz();
4230}
4231
4232// Return the offset to use for the GOT_INDX'th got entry which is
4233// for global tls symbol GSYM.
4234template<int size>
4235int64_t
4236Target_s390<size>::do_tls_offset_for_global(
4237 Symbol*,
e4d49a0f
AM
4238 unsigned int,
4239 uint64_t) const
e79a4bad
MK
4240{
4241 Output_segment* tls_segment = layout_->tls_segment();
4242 return -tls_segment->memsz();
4243}
4244
4245// Return the value to use for a dynamic which requires special
4246// treatment. This is how we support equality comparisons of function
4247// pointers across shared library boundaries, as described in the
4248// processor specific ABI supplement.
4249
4250template<int size>
4251uint64_t
4252Target_s390<size>::do_dynsym_value(const Symbol* gsym) const
4253{
4254 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4255 return this->plt_address_for_global(gsym);
4256}
4257
4258// Return a string used to fill a code section with nops to take up
4259// the specified length.
4260
4261template<int size>
4262std::string
4263Target_s390<size>::do_code_fill(section_size_type length) const
4264{
4265 if (length & 1)
4266 gold_warning(_("S/390 code fill of odd length requested"));
4267 return std::string(length, static_cast<char>(0x07));
4268}
4269
2b63aca3
MK
4270// Return whether SYM should be treated as a call to a non-split
4271// function. We don't want that to be true of a larl instruction
4272// that merely loads its address.
4273
4274template<int size>
4275bool
4276Target_s390<size>::do_is_call_to_non_split(const Symbol* sym,
4277 const unsigned char* preloc,
4278 const unsigned char* view,
4279 section_size_type view_size) const
4280{
4281 if (sym->type() != elfcpp::STT_FUNC)
4282 return false;
4283 typename Reloc_types<elfcpp::SHT_RELA, size, true>::Reloc reloc(preloc);
4284 typename elfcpp::Elf_types<size>::Elf_WXword r_info
4285 = reloc.get_r_info();
4286 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4287 section_offset_type offset = reloc.get_r_offset();
4288 switch (r_type)
4289 {
4290 // PLT refs always involve calling the function.
4291 case elfcpp::R_390_PLT12DBL:
4292 case elfcpp::R_390_PLT16DBL:
4293 case elfcpp::R_390_PLT24DBL:
4294 case elfcpp::R_390_PLT32:
4295 case elfcpp::R_390_PLT32DBL:
4296 case elfcpp::R_390_PLT64:
4297 case elfcpp::R_390_PLTOFF16:
4298 case elfcpp::R_390_PLTOFF32:
4299 case elfcpp::R_390_PLTOFF64:
4300 // Could be used for calls for -msmall-exec.
4301 case elfcpp::R_390_PC16DBL:
4302 return true;
4303
4304 // Tricky case. When used in a brasl, jg, and other branch instructions,
4305 // it's a call or a sibcall. However, when used in larl, it only loads
4306 // the function's address - not a call.
4307 case elfcpp::R_390_PC32DBL:
4308 {
4309 if (offset < 2
4310 || offset + 4 > static_cast<section_offset_type>(view_size))
4311 {
4312 // Should not happen.
4313 gold_error(_("instruction with PC32DBL not wholly within section"));
4314 return false;
4315 }
4316
4317 uint8_t op0 = view[offset-2];
4318 uint8_t op1 = view[offset-1] & 0xf;
4319
4320 // LARL
4321 if (op0 == 0xc0 && op1 == 0)
4322 return false;
4323
4324 // Otherwise, it's either a call instruction, a branch instruction
4325 // (used as a sibcall), or a data manipulation instruction (which
4326 // has no business being used on a function, and can be ignored).
4327 return true;
4328 }
4329
4330 // Otherwise, it's probably not a call.
4331 default:
4332 return false;
4333 }
4334}
4335
4336// Code sequences to match below.
4337
2b63aca3
MK
4338template<int size>
4339const unsigned char
4340Target_s390<size>::ss_code_bras_8[] = {
4341 0xa7, 0x15, 0x00, 0x06, // bras %r1, .+0xc
4342};
4343
4344template<int size>
4345const unsigned char
4346Target_s390<size>::ss_code_l_basr[] = {
4347 0x58, 0xe0, 0x10, 0x00, // l %r14, 0(%r1)
4348 0x58, 0x10, 0x10, 0x04, // l %r1, 4(%r1)
4349 0x0d, 0xee, // basr %r14, %r14
4350};
4351
4352template<int size>
4353const unsigned char
4354Target_s390<size>::ss_code_a_basr[] = {
4355 0x18, 0xe1, // lr %r14, %r1
4356 0x5a, 0xe0, 0x10, 0x00, // a %r14, 0(%r1)
4357 0x5a, 0x10, 0x10, 0x04, // a %r1, 4(%r1)
4358 0x0d, 0xee, // basr %r14, %r14
4359};
4360
2b63aca3
MK
4361template<int size>
4362const unsigned char
4363Target_s390<size>::ss_code_larl[] = {
4364 0xc0, 0x10, // larl %r1, ...
4365};
4366
4367template<int size>
4368const unsigned char
4369Target_s390<size>::ss_code_brasl[] = {
4370 0xc0, 0xe5, // brasl %r14, ...
4371};
4372
4373template<int size>
4374const unsigned char
4375Target_s390<size>::ss_code_jg[] = {
4376 0xc0, 0xf4, // jg ...
4377};
4378
4379template<int size>
4380const unsigned char
4381Target_s390<size>::ss_code_jgl[] = {
4382 0xc0, 0x44, // jgl ...
4383};
4384
40d85a7f
MK
4385template<>
4386bool
4387Target_s390<32>::ss_match_st_r14(unsigned char* view,
4388 section_size_type view_size,
4389 section_offset_type *offset) const
4390{
4391 static const unsigned char ss_code_st_r14[] = {
4392 0x50, 0xe0, 0xf0, 0x04, // st %r14, 4(%r15)
4393 };
4394 if (!this->match_view_u(view, view_size, *offset, ss_code_st_r14,
4395 sizeof ss_code_st_r14))
4396 return false;
4397 *offset += sizeof ss_code_st_r14;
4398 return true;
4399}
4400
4401template<>
4402bool
4403Target_s390<64>::ss_match_st_r14(unsigned char* view,
4404 section_size_type view_size,
4405 section_offset_type *offset) const
4406{
4407 static const unsigned char ss_code_st_r14[] = {
4408 0xe3, 0xe0, 0xf0, 0x08, 0x00, 0x24 // stg %r14, 8(%r15)
4409 };
4410 if (!this->match_view_u(view, view_size, *offset, ss_code_st_r14,
4411 sizeof ss_code_st_r14))
4412 return false;
4413 *offset += sizeof ss_code_st_r14;
4414 return true;
4415}
4416
4417template<>
4418bool
4419Target_s390<32>::ss_match_l_r14(unsigned char* view,
4420 section_size_type view_size,
4421 section_offset_type *offset) const
4422{
4423 static const unsigned char ss_code_l_r14[] = {
4424 0x58, 0xe0, 0xf0, 0x04, // l %r14, 4(%r15)
4425 };
4426 if (!this->match_view_u(view, view_size, *offset, ss_code_l_r14,
4427 sizeof ss_code_l_r14))
4428 return false;
4429 *offset += sizeof ss_code_l_r14;
4430 return true;
4431}
4432
4433template<>
4434bool
4435Target_s390<64>::ss_match_l_r14(unsigned char* view,
4436 section_size_type view_size,
4437 section_offset_type *offset) const
4438{
4439 static const unsigned char ss_code_l_r14[] = {
4440 0xe3, 0xe0, 0xf0, 0x08, 0x00, 0x04 // lg %r14, 8(%r15)
4441 };
4442 if (!this->match_view_u(view, view_size, *offset, ss_code_l_r14,
4443 sizeof ss_code_l_r14))
4444 return false;
4445 *offset += sizeof ss_code_l_r14;
4446 return true;
4447}
4448
2b63aca3
MK
4449template<int size>
4450bool
4451Target_s390<size>::ss_match_mcount(unsigned char* view,
4452 section_size_type view_size,
4453 section_offset_type *offset) const
4454{
4455 // Match the mcount call sequence.
4456 section_offset_type myoff = *offset;
4457
4458 // First, look for the store instruction saving %r14.
40d85a7f 4459 if (!this->ss_match_st_r14(view, view_size, &myoff))
2b63aca3 4460 return false;
2b63aca3
MK
4461
4462 // Now, param load and the actual call.
4463 if (this->match_view_u(view, view_size, myoff, ss_code_larl,
4464 sizeof ss_code_larl))
4465 {
4466 myoff += sizeof ss_code_larl + 4;
4467
4468 // After larl, expect a brasl.
4469 if (!this->match_view_u(view, view_size, myoff, ss_code_brasl,
4470 sizeof ss_code_brasl))
4471 return false;
4472 myoff += sizeof ss_code_brasl + 4;
4473 }
4474 else if (size == 32 &&
4475 this->match_view_u(view, view_size, myoff, ss_code_bras_8,
4476 sizeof ss_code_bras_8))
4477 {
4478 // The bras skips over a block of 8 bytes, loading its address
4479 // to %r1.
4480 myoff += sizeof ss_code_bras_8 + 8;
4481
4482 // Now, there are two sequences used for actual load and call,
4483 // absolute and PIC.
4484 if (this->match_view_u(view, view_size, myoff, ss_code_l_basr,
4485 sizeof ss_code_l_basr))
4486 myoff += sizeof ss_code_l_basr;
4487 else if (this->match_view_u(view, view_size, myoff, ss_code_a_basr,
4488 sizeof ss_code_a_basr))
4489 myoff += sizeof ss_code_a_basr;
4490 else
4491 return false;
4492 }
4493 else
4494 return false;
4495
4496 // Finally, a load bringing %r14 back.
40d85a7f 4497 if (!this->ss_match_l_r14(view, view_size, &myoff))
2b63aca3 4498 return false;
2b63aca3
MK
4499
4500 // Found it.
4501 *offset = myoff;
4502 return true;
4503}
4504
40d85a7f
MK
4505template<>
4506bool
4507Target_s390<32>::ss_match_ear(unsigned char* view,
4508 section_size_type view_size,
4509 section_offset_type *offset) const
4510{
4511 static const unsigned char ss_code_ear[] = {
4512 0xb2, 0x4f, 0x00, 0x10, // ear %r1, %a0
4513 };
4514 if (!this->match_view_u(view, view_size, *offset, ss_code_ear,
4515 sizeof ss_code_ear))
4516 return false;
4517 *offset += sizeof ss_code_ear;
4518 return true;
4519}
4520
4521template<>
4522bool
4523Target_s390<64>::ss_match_ear(unsigned char* view,
4524 section_size_type view_size,
4525 section_offset_type *offset) const
4526{
4527 static const unsigned char ss_code_ear[] = {
4528 0xb2, 0x4f, 0x00, 0x10, // ear %r1, %a0
4529 0xeb, 0x11, 0x00, 0x20, 0x00, 0x0d, // sllg %r1,%r1,32
4530 0xb2, 0x4f, 0x00, 0x11, // ear %r1, %a1
4531 };
4532 if (!this->match_view_u(view, view_size, *offset, ss_code_ear,
4533 sizeof ss_code_ear))
4534 return false;
4535 *offset += sizeof ss_code_ear;
4536 return true;
4537}
4538
4539template<>
4540bool
4541Target_s390<32>::ss_match_c(unsigned char* view,
4542 section_size_type view_size,
4543 section_offset_type *offset) const
4544{
4545 static const unsigned char ss_code_c[] = {
4546 0x59, 0xf0, 0x10, 0x20, // c %r15, 0x20(%r1)
4547 };
4548 if (!this->match_view_u(view, view_size, *offset, ss_code_c,
4549 sizeof ss_code_c))
4550 return false;
4551 *offset += sizeof ss_code_c;
4552 return true;
4553}
4554
4555template<>
4556bool
4557Target_s390<64>::ss_match_c(unsigned char* view,
4558 section_size_type view_size,
4559 section_offset_type *offset) const
4560{
4561 static const unsigned char ss_code_c[] = {
4562 0xe3, 0xf0, 0x10, 0x38, 0x00, 0x20, // cg %r15, 0x38(%r1)
4563 };
4564 if (!this->match_view_u(view, view_size, *offset, ss_code_c,
4565 sizeof ss_code_c))
4566 return false;
4567 *offset += sizeof ss_code_c;
4568 return true;
4569}
4570
2b63aca3
MK
4571template<>
4572bool
4573Target_s390<32>::ss_match_l(unsigned char* view,
4574 section_size_type view_size,
4575 section_offset_type *offset,
4576 int *guard_reg) const
4577{
4578 // l %guard_reg, 0x20(%r1)
4579 if (convert_to_section_size_type(*offset + 4) > view_size
4580 || view[*offset] != 0x58
4581 || (view[*offset + 1] & 0xf) != 0x0
4582 || view[*offset + 2] != 0x10
4583 || view[*offset + 3] != 0x20)
4584 return false;
4585 *offset += 4;
4586 *guard_reg = view[*offset + 1] >> 4 & 0xf;
4587 return true;
4588}
4589
4590template<>
4591bool
4592Target_s390<64>::ss_match_l(unsigned char* view,
4593 section_size_type view_size,
4594 section_offset_type *offset,
4595 int *guard_reg) const
4596{
4597 // lg %guard_reg, 0x38(%r1)
4598 if (convert_to_section_size_type(*offset + 6) > view_size
4599 || view[*offset] != 0xe3
4600 || (view[*offset + 1] & 0xf) != 0x0
4601 || view[*offset + 2] != 0x10
4602 || view[*offset + 3] != 0x38
4603 || view[*offset + 4] != 0x00
4604 || view[*offset + 5] != 0x04)
4605 return false;
4606 *offset += 6;
4607 *guard_reg = view[*offset + 1] >> 4 & 0xf;
4608 return true;
4609}
4610
4611template<int size>
4612bool
4613Target_s390<size>::ss_match_ahi(unsigned char* view,
4614 section_size_type view_size,
4615 section_offset_type *offset,
4616 int guard_reg,
4617 uint32_t *arg) const
4618{
4619 int op = size == 32 ? 0xa : 0xb;
4620 // a[g]hi %guard_reg, <arg>
4621 if (convert_to_section_size_type(*offset + 4) > view_size
4622 || view[*offset] != 0xa7
4623 || view[*offset + 1] != (guard_reg << 4 | op)
4624 // Disallow negative size.
4625 || view[*offset + 2] & 0x80)
4626 return false;
4627 *arg = elfcpp::Swap<16, true>::readval(view + *offset + 2);
4628 *offset += 4;
4629 return true;
4630}
4631
4632template<int size>
4633bool
4634Target_s390<size>::ss_match_alfi(unsigned char* view,
4635 section_size_type view_size,
4636 section_offset_type *offset,
4637 int guard_reg,
4638 uint32_t *arg) const
4639{
4640 int op = size == 32 ? 0xb : 0xa;
4641 // al[g]fi %guard_reg, <arg>
4642 if (convert_to_section_size_type(*offset + 6) > view_size
4643 || view[*offset] != 0xc2
4644 || view[*offset + 1] != (guard_reg << 4 | op))
4645 return false;
4646 *arg = elfcpp::Swap<32, true>::readval(view + *offset + 2);
4647 *offset += 6;
4648 return true;
4649}
4650
4651template<>
4652bool
4653Target_s390<32>::ss_match_cr(unsigned char* view,
4654 section_size_type view_size,
4655 section_offset_type *offset,
4656 int guard_reg) const
4657{
4658 // cr %r15, %guard_reg
4659 if (convert_to_section_size_type(*offset + 2) > view_size
4660 || view[*offset] != 0x19
4661 || view[*offset + 1] != (0xf0 | guard_reg))
4662 return false;
4663 *offset += 2;
4664 return true;
4665}
4666
4667template<>
4668bool
4669Target_s390<64>::ss_match_cr(unsigned char* view,
4670 section_size_type view_size,
4671 section_offset_type *offset,
4672 int guard_reg) const
4673{
4674 // cgr %r15, %guard_reg
4675 if (convert_to_section_size_type(*offset + 4) > view_size
4676 || view[*offset] != 0xb9
4677 || view[*offset + 1] != 0x20
4678 || view[*offset + 2] != 0x00
4679 || view[*offset + 3] != (0xf0 | guard_reg))
4680 return false;
4681 *offset += 4;
4682 return true;
4683}
4684
4685
4686// FNOFFSET in section SHNDX in OBJECT is the start of a function
4687// compiled with -fsplit-stack. The function calls non-split-stack
4688// code. We have to change the function so that it always ensures
4689// that it has enough stack space to run some random function.
4690
4691template<int size>
4692void
4693Target_s390<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
4694 section_offset_type fnoffset,
4695 section_size_type,
4696 const unsigned char *prelocs,
4697 size_t reloc_count,
4698 unsigned char* view,
4699 section_size_type view_size,
4700 std::string*,
4701 std::string*) const
4702{
4703 // true if there's a conditional call to __morestack in the function,
4704 // false if there's an unconditional one.
4705 bool conditional = false;
4706 // Offset of the byte after the compare insn, if conditional.
4707 section_offset_type cmpend = 0;
4708 // Type and immediate offset of the add instruction that adds frame size
4709 // to guard.
4710 enum {
4711 SS_ADD_NONE,
4712 SS_ADD_AHI,
4713 SS_ADD_ALFI,
4714 } fsadd_type = SS_ADD_NONE;
4715 section_offset_type fsadd_offset = 0;
4716 uint32_t fsadd_frame_size = 0;
4717 // Register used for loading guard. Usually r1, but can also be r0 or r2-r5.
4718 int guard_reg;
4719 // Offset of the conditional jump.
4720 section_offset_type jump_offset = 0;
4721 // Section view and offset of param block.
4722 section_offset_type param_offset = 0;
4723 unsigned char *param_view = 0;
4724 section_size_type param_view_size = 0;
4725 // Current position in function.
4726 section_offset_type curoffset = fnoffset;
4727 // And the position of split-stack prologue.
4728 section_offset_type ssoffset;
4729 // Frame size.
4730 typename elfcpp::Elf_types<size>::Elf_Addr frame_size;
4731 // Relocation parsing.
4732 typedef typename Reloc_types<elfcpp::SHT_RELA, size, true>::Reloc Reltype;
4733 const int reloc_size = Reloc_types<elfcpp::SHT_RELA, size, true>::reloc_size;
4734 const unsigned char *pr = prelocs;
4735
4736 // If the function was compiled with -pg, the profiling code may come before
4737 // the split-stack prologue. Skip it.
4738
4739 this->ss_match_mcount(view, view_size, &curoffset);
4740 ssoffset = curoffset;
4741
4742 // First, figure out if there's a conditional call by looking for the
4743 // extract-tp, add, cmp sequence.
4744
40d85a7f 4745 if (this->ss_match_ear(view, view_size, &curoffset))
2b63aca3
MK
4746 {
4747 // Found extract-tp, now look for an add and compare.
2b63aca3 4748 conditional = true;
40d85a7f 4749 if (this->ss_match_c(view, view_size, &curoffset))
2b63aca3
MK
4750 {
4751 // Found a direct compare of stack pointer with the guard,
4752 // we're done here.
2b63aca3
MK
4753 }
4754 else if (this->ss_match_l(view, view_size, &curoffset, &guard_reg))
4755 {
4756 // Found a load of guard to register, look for an add and compare.
4757 if (this->ss_match_ahi(view, view_size, &curoffset, guard_reg,
4758 &fsadd_frame_size))
4759 {
4760 fsadd_type = SS_ADD_AHI;
4761 fsadd_offset = curoffset - 2;
4762 }
4763 else if (this->ss_match_alfi(view, view_size, &curoffset, guard_reg,
4764 &fsadd_frame_size))
4765 {
4766 fsadd_type = SS_ADD_ALFI;
4767 fsadd_offset = curoffset - 4;
4768 }
4769 else
4770 {
4771 goto bad;
4772 }
4773 // Now, there has to be a compare.
4774 if (!this->ss_match_cr(view, view_size, &curoffset, guard_reg))
4775 goto bad;
4776 }
4777 else
4778 {
4779 goto bad;
4780 }
4781 cmpend = curoffset;
4782 }
4783
4784 // Second, look for the call.
4785 if (!this->match_view_u(view, view_size, curoffset, ss_code_larl,
4786 sizeof ss_code_larl))
4787 goto bad;
4788 curoffset += sizeof ss_code_larl;
4789
4790 // Find out larl's operand. It should be a local symbol in .rodata
4791 // section.
4792 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
4793 {
4794 Reltype reloc(pr);
4795 if (static_cast<section_offset_type>(reloc.get_r_offset())
4796 == curoffset)
4797 {
4798 typename elfcpp::Elf_types<size>::Elf_WXword r_info
4799 = reloc.get_r_info();
4800 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4801 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4802 if (r_type != elfcpp::R_390_PC32DBL)
4803 goto bad;
4804 if (r_sym >= object->local_symbol_count())
4805 goto bad;
4806 Sized_relobj_file<size, true> *object_sized =
4807 static_cast<Sized_relobj_file<size, true> *>(object);
4808 const Symbol_value<size>* sym = object_sized->local_symbol(r_sym);
4809 bool param_shndx_ordinary;
4810 const unsigned int param_shndx =
4811 sym->input_shndx(&param_shndx_ordinary);
4812 if (!param_shndx_ordinary)
4813 goto bad;
4814 param_offset = sym->input_value() + reloc.get_r_addend() - 2
4815 - object->output_section(param_shndx)->address()
4816 - object->output_section_offset(param_shndx);
4817 param_view = object->get_output_view(param_shndx,
4818 &param_view_size);
4819 break;
4820 }
4821 }
4822
4823 if (!param_view)
4824 goto bad;
4825
4826 curoffset += 4;
4827
4828 // Now, there has to be a jump to __morestack.
4829 jump_offset = curoffset;
4830
4831 if (this->match_view_u(view, view_size, curoffset,
4832 conditional ? ss_code_jgl : ss_code_jg,
4833 sizeof ss_code_jg))
4834 curoffset += sizeof ss_code_jg;
4835 else
4836 goto bad;
4837
4838 curoffset += 4;
4839
4840 // Read the frame size.
4841 if (convert_to_section_size_type(param_offset + size / 8) > param_view_size)
4842 goto bad;
4843 frame_size = elfcpp::Swap<size, true>::readval(param_view + param_offset);
4844
4845 // Sanity check.
4846 if (fsadd_type != SS_ADD_NONE && fsadd_frame_size != frame_size)
4847 goto bad;
4848
4849 // Bump the frame size.
4850 frame_size += parameters->options().split_stack_adjust_size();
4851
4852 // Store it to the param block.
4853 elfcpp::Swap<size, true>::writeval(param_view + param_offset, frame_size);
4854
4855 if (!conditional)
4856 {
4857 // If the call was already unconditional, we're done.
4858 }
4859 else if (frame_size <= 0xffffffff && fsadd_type == SS_ADD_ALFI)
4860 {
4861 // Using alfi to add the frame size, and it still fits. Adjust it.
4862 elfcpp::Swap_unaligned<32, true>::writeval(view + fsadd_offset,
4863 frame_size);
4864 }
4865 else
4866 {
4867 // We were either relying on the backoff area, or used ahi to load
4868 // frame size. This won't fly, as our new frame size is too large.
4869 // Convert the sequence to unconditional by nopping out the comparison,
4870 // and rewiring the jump.
4871 this->set_view_to_nop(view, view_size, ssoffset, cmpend - ssoffset);
4872
4873 // The jump is jgl, we'll mutate it to jg.
4874 view[jump_offset+1] = 0xf4;
4875 }
4876
4877 return;
4878
4879bad:
4880 if (!object->has_no_split_stack())
4881 object->error(_("failed to match split-stack sequence at "
4882 "section %u offset %0zx"),
4883 shndx, static_cast<size_t>(fnoffset));
4884}
4885
e79a4bad
MK
4886// Relocate section data.
4887
4888template<int size>
4889void
4890Target_s390<size>::relocate_section(
4891 const Relocate_info<size, true>* relinfo,
4892 unsigned int sh_type,
4893 const unsigned char* prelocs,
4894 size_t reloc_count,
4895 Output_section* output_section,
4896 bool needs_special_offset_handling,
4897 unsigned char* view,
4898 typename elfcpp::Elf_types<size>::Elf_Addr address,
4899 section_size_type view_size,
4900 const Reloc_symbol_changes* reloc_symbol_changes)
4901{
4d625b70
CC
4902 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, true>
4903 Classify_reloc;
4904
e79a4bad
MK
4905 gold_assert(sh_type == elfcpp::SHT_RELA);
4906
4d625b70
CC
4907 gold::relocate_section<size, true, Target_s390<size>, Relocate,
4908 gold::Default_comdat_behavior, Classify_reloc>(
e79a4bad
MK
4909 relinfo,
4910 this,
4911 prelocs,
4912 reloc_count,
4913 output_section,
4914 needs_special_offset_handling,
4915 view,
4916 address,
4917 view_size,
4918 reloc_symbol_changes);
4919}
4920
4921// Apply an incremental relocation. Incremental relocations always refer
4922// to global symbols.
4923
4924template<int size>
4925void
4926Target_s390<size>::apply_relocation(
4927 const Relocate_info<size, true>* relinfo,
4928 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4929 unsigned int r_type,
4930 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4931 const Symbol* gsym,
4932 unsigned char* view,
4933 typename elfcpp::Elf_types<size>::Elf_Addr address,
4934 section_size_type view_size)
4935{
4936 gold::apply_relocation<size, true, Target_s390<size>,
4937 typename Target_s390<size>::Relocate>(
4938 relinfo,
4939 this,
4940 r_offset,
4941 r_type,
4942 r_addend,
4943 gsym,
4944 view,
4945 address,
4946 view_size);
4947}
4948
4949// The selector for s390 object files.
4950
4951template<int size>
4952class Target_selector_s390 : public Target_selector
4953{
4954public:
4955 Target_selector_s390()
4956 : Target_selector(elfcpp::EM_S390, size, true,
4957 (size == 64 ? "elf64-s390" : "elf32-s390"),
4958 (size == 64 ? "elf64_s390" : "elf32_s390"))
4959 { }
4960
4961 virtual Target*
4962 do_instantiate_target()
4963 { return new Target_s390<size>(); }
4964};
4965
4966Target_selector_s390<32> target_selector_s390;
4967Target_selector_s390<64> target_selector_s390x;
4968
4969} // End anonymous namespace.