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