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