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