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