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