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