020fb15c973cdf47710dc8935d9e7730c7203232
[external/binutils.git] / gold / s390.cc
1 // s390.cc -- s390 target support for gold.
2
3 // Copyright (C) 2015 Free Software Foundation, Inc.
4 // Written by Marcin Koƛcielnicki <koriakin@0x04.net>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "s390.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "gc.h"
42 #include "icf.h"
43
44 namespace
45 {
46
47 using namespace gold;
48
49 // A class to handle the .got.plt section.
50
51 template<int size>
52 class Output_data_got_plt_s390 : public Output_section_data_build
53 {
54  public:
55   Output_data_got_plt_s390(Layout* layout)
56     : Output_section_data_build(size/8),
57       layout_(layout)
58   { }
59
60   Output_data_got_plt_s390(Layout* layout, off_t data_size)
61     : Output_section_data_build(data_size, size/8),
62       layout_(layout)
63   { }
64
65  protected:
66   // Write out the PLT data.
67   void
68   do_write(Output_file*);
69
70   // Write to a map file.
71   void
72   do_print_to_mapfile(Mapfile* mapfile) const
73   { mapfile->print_output_data(this, "** GOT PLT"); }
74
75  private:
76   // A pointer to the Layout class, so that we can find the .dynamic
77   // section when we write out the GOT PLT section.
78   Layout* layout_;
79 };
80
81 // A class to handle the PLT data.
82
83 template<int size>
84 class Output_data_plt_s390 : public Output_section_data
85 {
86  public:
87   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, true>
88     Reloc_section;
89
90   Output_data_plt_s390(Layout* layout,
91                          Output_data_got<size, true>* got,
92                          Output_data_got_plt_s390<size>* got_plt,
93                          Output_data_space* got_irelative)
94     : Output_section_data(4), layout_(layout),
95       irelative_rel_(NULL), got_(got), got_plt_(got_plt),
96       got_irelative_(got_irelative), count_(0),
97       irelative_count_(0), free_list_()
98   { this->init(layout); }
99
100   Output_data_plt_s390(Layout* layout,
101                          Output_data_got<size, true>* got,
102                          Output_data_got_plt_s390<size>* got_plt,
103                          Output_data_space* got_irelative,
104                          unsigned int plt_count)
105     : Output_section_data((plt_count + 1) * plt_entry_size,
106                           4, false),
107       layout_(layout), irelative_rel_(NULL), got_(got),
108       got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
109       irelative_count_(0), free_list_()
110   {
111     this->init(layout);
112
113     // Initialize the free list and reserve the first entry.
114     this->free_list_.init((plt_count + 1) * plt_entry_size, false);
115     this->free_list_.remove(0, plt_entry_size);
116   }
117
118   // Initialize the PLT section.
119   void
120   init(Layout* layout);
121
122   // Add an entry to the PLT.
123   void
124   add_entry(Symbol_table*, Layout*, Symbol* gsym);
125
126   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
127   unsigned int
128   add_local_ifunc_entry(Symbol_table*, Layout*,
129     Sized_relobj_file<size, true>*, unsigned int);
130
131   // Add the relocation for a PLT entry.
132   void
133   add_relocation(Symbol_table*, Layout*, Symbol*, unsigned int);
134
135   // Return the .rela.plt section data.
136   Reloc_section*
137   rela_plt()
138   { return this->rel_; }
139
140   // Return where the IRELATIVE relocations should go in the PLT
141   // relocations.
142   Reloc_section*
143   rela_irelative(Symbol_table*, Layout*);
144
145   // Return whether we created a section for IRELATIVE relocations.
146   bool
147   has_irelative_section() const
148   { return this->irelative_rel_ != NULL; }
149
150   // Return the number of PLT entries.
151   unsigned int
152   entry_count() const
153   { return this->count_ + this->irelative_count_; }
154
155   // Return the offset of the first non-reserved PLT entry.
156   unsigned int
157   first_plt_entry_offset()
158   { return plt_entry_size; }
159
160   // Return the size of a PLT entry.
161   unsigned int
162   get_plt_entry_size() const
163   { return plt_entry_size; }
164
165   // Reserve a slot in the PLT for an existing symbol in an incremental update.
166   void
167   reserve_slot(unsigned int plt_index)
168   {
169     this->free_list_.remove((plt_index + 1) * plt_entry_size,
170                             (plt_index + 2) * plt_entry_size);
171   }
172
173   // Return the PLT address to use for a global symbol.
174   uint64_t
175   address_for_global(const Symbol*);
176
177   // Return the PLT address to use for a local symbol.
178   uint64_t
179   address_for_local(const Relobj*, unsigned int symndx);
180
181   // Add .eh_frame information for the PLT.
182   void
183   add_eh_frame(Layout* layout)
184   {
185           (void)layout;
186     layout->add_eh_frame_for_plt(this,
187                                  plt_eh_frame_cie,
188                                  plt_eh_frame_cie_size,
189                                  plt_eh_frame_fde,
190                                  plt_eh_frame_fde_size);
191   }
192
193  protected:
194   // Fill in the first PLT entry.
195   void
196   fill_first_plt_entry(unsigned char* pov,
197                        typename elfcpp::Elf_types<size>::Elf_Addr got_address,
198                        typename elfcpp::Elf_types<size>::Elf_Addr plt_address);
199
200   // Fill in a normal PLT entry.  Returns the offset into the entry that
201   // should be the initial GOT slot value.
202   unsigned int
203   fill_plt_entry(unsigned char* pov,
204                  typename elfcpp::Elf_types<size>::Elf_Addr got_address,
205                  typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
206                  unsigned int got_offset,
207                  unsigned int plt_offset,
208                  unsigned int plt_rel_offset);
209
210   void
211   do_adjust_output_section(Output_section* os);
212
213   // Write to a map file.
214   void
215   do_print_to_mapfile(Mapfile* mapfile) const
216   { mapfile->print_output_data(this, _("** PLT")); }
217
218  private:
219   // Set the final size.
220   void
221   set_final_data_size();
222
223   // Write out the PLT data.
224   void
225   do_write(Output_file*);
226
227   // A pointer to the Layout class, so that we can find the .dynamic
228   // section when we write out the GOT PLT section.
229   Layout* layout_;
230   // The reloc section.
231   Reloc_section* rel_;
232   // The IRELATIVE relocs, if necessary.  These must follow the
233   // regular PLT relocations.
234   Reloc_section* irelative_rel_;
235   // The .got section.
236   Output_data_got<size, true>* got_;
237   // The .got.plt section.
238   Output_data_got_plt_s390<size>* got_plt_;
239   // The part of the .got.plt section used for IRELATIVE relocs.
240   Output_data_space* got_irelative_;
241   // The number of PLT entries.
242   unsigned int count_;
243   // Number of PLT entries with R_TILEGX_IRELATIVE relocs.  These
244   // follow the regular PLT entries.
245   unsigned int irelative_count_;
246   // List of available regions within the section, for incremental
247   // update links.
248   Free_list free_list_;
249
250   // The size of an entry in the PLT.
251   static const int plt_entry_size = 0x20;
252   // The first entry in the PLT.
253   static const unsigned char first_plt_entry_32_abs[plt_entry_size];
254   static const unsigned char first_plt_entry_32_pic[plt_entry_size];
255   static const unsigned char first_plt_entry_64[plt_entry_size];
256   // Other entries in the PLT for an executable.
257   static const unsigned char plt_entry_32_abs[plt_entry_size];
258   static const unsigned char plt_entry_32_pic12[plt_entry_size];
259   static const unsigned char plt_entry_32_pic16[plt_entry_size];
260   static const unsigned char plt_entry_32_pic[plt_entry_size];
261   static const unsigned char plt_entry_64[plt_entry_size];
262
263   // The .eh_frame unwind information for the PLT.
264   static const int plt_eh_frame_cie_size = 12;
265   static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
266   static const int plt_eh_frame_fde_size = 12;
267   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
268 };
269
270
271 template<int size>
272 class Target_s390 : public Sized_target<size, true>
273 {
274  public:
275   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, true> Reloc_section;
276
277   Target_s390()
278     : Sized_target<size, true>(&s390_info),
279       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
280       global_offset_table_(NULL), rela_dyn_(NULL),
281       rela_irelative_(NULL), copy_relocs_(elfcpp::R_390_COPY),
282       got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
283       layout_(NULL)
284   { }
285
286   // Scan the relocations to look for symbol adjustments.
287   void
288   gc_process_relocs(Symbol_table* symtab,
289                     Layout* layout,
290                     Sized_relobj_file<size, true>* object,
291                     unsigned int data_shndx,
292                     unsigned int sh_type,
293                     const unsigned char* prelocs,
294                     size_t reloc_count,
295                     Output_section* output_section,
296                     bool needs_special_offset_handling,
297                     size_t local_symbol_count,
298                     const unsigned char* plocal_symbols);
299
300   // Scan the relocations to look for symbol adjustments.
301   void
302   scan_relocs(Symbol_table* symtab,
303               Layout* layout,
304               Sized_relobj_file<size, true>* object,
305               unsigned int data_shndx,
306               unsigned int sh_type,
307               const unsigned char* prelocs,
308               size_t reloc_count,
309               Output_section* output_section,
310               bool needs_special_offset_handling,
311               size_t local_symbol_count,
312               const unsigned char* plocal_symbols);
313
314   // Finalize the sections.
315   void
316   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
317
318   // Return the value to use for a dynamic which requires special
319   // treatment.
320   uint64_t
321   do_dynsym_value(const Symbol*) const;
322
323   // Relocate a section.
324   void
325   relocate_section(const Relocate_info<size, true>*,
326                    unsigned int sh_type,
327                    const unsigned char* prelocs,
328                    size_t reloc_count,
329                    Output_section* output_section,
330                    bool needs_special_offset_handling,
331                    unsigned char* view,
332                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
333                    section_size_type view_size,
334                    const Reloc_symbol_changes*);
335
336   // Scan the relocs during a relocatable link.
337   void
338   scan_relocatable_relocs(Symbol_table* symtab,
339                           Layout* layout,
340                           Sized_relobj_file<size, true>* object,
341                           unsigned int data_shndx,
342                           unsigned int sh_type,
343                           const unsigned char* prelocs,
344                           size_t reloc_count,
345                           Output_section* output_section,
346                           bool needs_special_offset_handling,
347                           size_t local_symbol_count,
348                           const unsigned char* plocal_symbols,
349                           Relocatable_relocs*);
350
351   // Return a string used to fill a code section with nops.
352   std::string
353   do_code_fill(section_size_type length) const;
354
355   // Emit relocations for a section.
356   void
357   relocate_relocs(
358       const Relocate_info<size, true>*,
359       unsigned int sh_type,
360       const unsigned char* prelocs,
361       size_t reloc_count,
362       Output_section* output_section,
363       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
364       const Relocatable_relocs*,
365       unsigned char* view,
366       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
367       section_size_type view_size,
368       unsigned char* reloc_view,
369       section_size_type reloc_view_size);
370
371   // Return whether SYM is defined by the ABI.
372   bool
373   do_is_defined_by_abi(const Symbol* sym) const
374   { return strcmp(sym->name(), "__tls_get_offset") == 0; }
375
376   // Return the PLT address to use for a global symbol.
377   uint64_t
378   do_plt_address_for_global(const Symbol* gsym) const
379   { return this->plt_section()->address_for_global(gsym); }
380
381   uint64_t
382   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
383   { return this->plt_section()->address_for_local(relobj, symndx); }
384
385   // Return the offset to use for the GOT_INDX'th got entry which is
386   // for a local tls symbol specified by OBJECT, SYMNDX.
387   int64_t
388   do_tls_offset_for_local(const Relobj* object,
389                           unsigned int symndx,
390                           unsigned int got_indx) const;
391
392   // Return the offset to use for the GOT_INDX'th got entry which is
393   // for global tls symbol GSYM.
394   int64_t
395   do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
396
397   // This function should be defined in targets that can use relocation
398   // types to determine (implemented in local_reloc_may_be_function_pointer
399   // and global_reloc_may_be_function_pointer)
400   // if a function's pointer is taken.  ICF uses this in safe mode to only
401   // fold those functions whose pointer is defintely not taken.
402   bool
403   do_can_check_for_function_pointers() const
404   { return true; }
405
406   // Return the size of the GOT section.
407   section_size_type
408   got_size() const
409   {
410     gold_assert(this->got_ != NULL);
411     return this->got_->data_size();
412   }
413
414   // Return the number of entries in the GOT.
415   unsigned int
416   got_entry_count() const
417   {
418     if (this->got_ == NULL)
419       return 0;
420     return this->got_size() / (size / 8);
421   }
422
423   // Return the number of entries in the PLT.
424   unsigned int
425   plt_entry_count() const;
426
427   // Return the offset of the first non-reserved PLT entry.
428   unsigned int
429   first_plt_entry_offset() const;
430
431   // Return the size of each PLT entry.
432   unsigned int
433   plt_entry_size() const;
434
435   // Create the GOT section for an incremental update.
436   Output_data_got_base*
437   init_got_plt_for_update(Symbol_table* symtab,
438                           Layout* layout,
439                           unsigned int got_count,
440                           unsigned int plt_count);
441
442   // Reserve a GOT entry for a local symbol, and regenerate any
443   // necessary dynamic relocations.
444   void
445   reserve_local_got_entry(unsigned int got_index,
446                           Sized_relobj<size, true>* obj,
447                           unsigned int r_sym,
448                           unsigned int got_type);
449
450   // Reserve a GOT entry for a global symbol, and regenerate any
451   // necessary dynamic relocations.
452   void
453   reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
454                            unsigned int got_type);
455
456   // Register an existing PLT entry for a global symbol.
457   void
458   register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
459                             Symbol* gsym);
460
461   // Force a COPY relocation for a given symbol.
462   void
463   emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
464
465   // Apply an incremental relocation.
466   void
467   apply_relocation(const Relocate_info<size, true>* relinfo,
468                    typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
469                    unsigned int r_type,
470                    typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
471                    const Symbol* gsym,
472                    unsigned char* view,
473                    typename elfcpp::Elf_types<size>::Elf_Addr address,
474                    section_size_type view_size);
475
476  private:
477
478   // The class which scans relocations.
479   class Scan
480   {
481   public:
482     Scan()
483       : issued_non_pic_error_(false)
484     { }
485
486     static inline int
487     get_reference_flags(unsigned int r_type);
488
489     inline void
490     local(Symbol_table* symtab, Layout* layout, Target_s390* target,
491           Sized_relobj_file<size, true>* object,
492           unsigned int data_shndx,
493           Output_section* output_section,
494           const elfcpp::Rela<size, true>& reloc, unsigned int r_type,
495           const elfcpp::Sym<size, true>& lsym,
496           bool is_discarded);
497
498     inline void
499     global(Symbol_table* symtab, Layout* layout, Target_s390* target,
500            Sized_relobj_file<size, true>* object,
501            unsigned int data_shndx,
502            Output_section* output_section,
503            const elfcpp::Rela<size, true>& reloc, unsigned int r_type,
504            Symbol* gsym);
505
506     inline bool
507     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
508                                         Target_s390* target,
509                                         Sized_relobj_file<size, true>* object,
510                                         unsigned int data_shndx,
511                                         Output_section* output_section,
512                                         const elfcpp::Rela<size, true>& reloc,
513                                         unsigned int r_type,
514                                         const elfcpp::Sym<size, true>& lsym);
515
516     inline bool
517     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
518                                          Target_s390* target,
519                                          Sized_relobj_file<size, true>* object,
520                                          unsigned int data_shndx,
521                                          Output_section* output_section,
522                                          const elfcpp::Rela<size, true>& reloc,
523                                          unsigned int r_type,
524                                          Symbol* gsym);
525
526   private:
527     static void
528     unsupported_reloc_local(Sized_relobj_file<size, true>*,
529                             unsigned int r_type);
530
531     static void
532     unsupported_reloc_global(Sized_relobj_file<size, true>*,
533                              unsigned int r_type, Symbol*);
534
535     void
536     check_non_pic(Relobj*, unsigned int r_type);
537
538     inline bool
539     possible_function_pointer_reloc(unsigned int r_type);
540
541     bool
542     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, true>*,
543                               unsigned int r_type);
544
545     // Whether we have issued an error about a non-PIC compilation.
546     bool issued_non_pic_error_;
547   };
548
549   // The class which implements relocation.
550   class Relocate
551   {
552    public:
553     // Do a relocation.  Return false if the caller should not issue
554     // any warnings about this relocation.
555     inline bool
556     relocate(const Relocate_info<size, true>*, Target_s390*,
557              Output_section*,
558              size_t relnum, const elfcpp::Rela<size, true>&,
559              unsigned int r_type, const Sized_symbol<size>*,
560              const Symbol_value<size>*,
561              unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
562              section_size_type);
563
564    private:
565     // Do a TLS relocation.
566     inline typename elfcpp::Elf_types<size>::Elf_Addr
567     relocate_tls(const Relocate_info<size, true>*, Target_s390*,
568                  size_t relnum, const elfcpp::Rela<size, true>&,
569                  unsigned int r_type, const Sized_symbol<size>*,
570                  const Symbol_value<size>*,
571                  unsigned char*, section_size_type);
572
573     // Do a TLS General-Dynamic to Initial-Exec transition.
574     inline void
575     tls_gd_to_ie(const Relocate_info<size, true>*, size_t relnum,
576                  const elfcpp::Rela<size, true>&,
577                  unsigned char* view,
578                  section_size_type view_size);
579
580     // Do a TLS General-Dynamic to Local-Exec transition.
581     inline void
582     tls_gd_to_le(const Relocate_info<size, true>*, size_t relnum,
583                  const elfcpp::Rela<size, true>&,
584                  unsigned char* view,
585                  section_size_type view_size);
586
587     // Do a TLS Local-Dynamic to Local-Exec transition.
588     inline void
589     tls_ld_to_le(const Relocate_info<size, true>*, size_t relnum,
590                  const elfcpp::Rela<size, true>&,
591                  unsigned char* view,
592                  section_size_type view_size);
593
594     // Do a TLS Initial-Exec to Local-Exec transition.
595     static inline void
596     tls_ie_to_le(const Relocate_info<size, true>*, size_t relnum,
597                  const elfcpp::Rela<size, true>&,
598                  unsigned char* view,
599                  section_size_type view_size);
600   };
601
602   // A class which returns the size required for a relocation type,
603   // used while scanning relocs during a relocatable link.
604   class Relocatable_size_for_reloc
605   {
606    public:
607     unsigned int
608     get_size_for_reloc(unsigned int, Relobj*);
609   };
610
611   // Adjust TLS relocation type based on the options and whether this
612   // is a local symbol.
613   static tls::Tls_optimization
614   optimize_tls_reloc(bool is_final, int r_type);
615
616   // Get the GOT section.
617   const Output_data_got<size, true>*
618   got_section() const
619   {
620     gold_assert(this->got_ != NULL);
621     return this->got_;
622   }
623
624   // Get the GOT section, creating it if necessary.
625   Output_data_got<size, true>*
626   got_section(Symbol_table*, Layout*);
627
628   typename elfcpp::Elf_types<size>::Elf_Addr
629   got_address() const
630   {
631     gold_assert(this->got_ != NULL);
632     return this->got_plt_->address();
633   }
634
635   typename elfcpp::Elf_types<size>::Elf_Addr
636   got_main_offset() const
637   {
638     gold_assert(this->got_ != NULL);
639     return this->got_->address() - this->got_address();
640   }
641
642   // Create the PLT section.
643   void
644   make_plt_section(Symbol_table* symtab, Layout* layout);
645
646   // Create a PLT entry for a global symbol.
647   void
648   make_plt_entry(Symbol_table*, Layout*, Symbol*);
649
650   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
651   void
652   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
653                              Sized_relobj_file<size, true>* relobj,
654                              unsigned int local_sym_index);
655
656   // Create a GOT entry for the TLS module index.
657   unsigned int
658   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
659                       Sized_relobj_file<size, true>* object);
660
661   // Get the PLT section.
662   Output_data_plt_s390<size>*
663   plt_section() const
664   {
665     gold_assert(this->plt_ != NULL);
666     return this->plt_;
667   }
668
669   // Get the dynamic reloc section, creating it if necessary.
670   Reloc_section*
671   rela_dyn_section(Layout*);
672
673   // Get the section to use for IRELATIVE relocations.
674   Reloc_section*
675   rela_irelative_section(Layout*);
676
677   // Add a potential copy relocation.
678   void
679   copy_reloc(Symbol_table* symtab, Layout* layout,
680              Sized_relobj_file<size, true>* object,
681              unsigned int shndx, Output_section* output_section,
682              Symbol* sym, const elfcpp::Rela<size, true>& reloc)
683   {
684     this->copy_relocs_.copy_reloc(symtab, layout,
685                                   symtab->get_sized_symbol<size>(sym),
686                                   object, shndx, output_section,
687                                   reloc, 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     Target_s390<size>* target,
3116     Output_section*,
3117     size_t relnum,
3118     const elfcpp::Rela<size, true>& rela,
3119     unsigned int r_type,
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 Sized_relobj_file<size, true>* object = relinfo->object;
3130
3131   // Pick the value to use for symbols defined in the PLT.
3132   Symbol_value<size> symval;
3133   if (gsym != NULL
3134       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3135     {
3136       symval.set_output_value(target->plt_address_for_global(gsym));
3137       psymval = &symval;
3138     }
3139   else if (gsym == NULL && psymval->is_ifunc_symbol())
3140     {
3141       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3142       if (object->local_has_plt_offset(r_sym))
3143         {
3144           symval.set_output_value(target->plt_address_for_local(object, r_sym));
3145           psymval = &symval;
3146         }
3147     }
3148
3149   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3150
3151   typename elfcpp::Elf_types<size>::Elf_Addr value = 0;
3152
3153   switch (r_type)
3154     {
3155     case elfcpp::R_390_PLT64:
3156     case elfcpp::R_390_PLT32:
3157     case elfcpp::R_390_PLT32DBL:
3158     case elfcpp::R_390_PLT24DBL:
3159     case elfcpp::R_390_PLT16DBL:
3160     case elfcpp::R_390_PLT12DBL:
3161       gold_assert(gsym == NULL
3162                   || gsym->has_plt_offset()
3163                   || gsym->final_value_is_known()
3164                   || (gsym->is_defined()
3165                       && !gsym->is_from_dynobj()
3166                       && !gsym->is_preemptible()));
3167       // fallthru
3168     case elfcpp::R_390_8:
3169     case elfcpp::R_390_12:
3170     case elfcpp::R_390_16:
3171     case elfcpp::R_390_20:
3172     case elfcpp::R_390_32:
3173     case elfcpp::R_390_64:
3174     case elfcpp::R_390_PC16:
3175     case elfcpp::R_390_PC32:
3176     case elfcpp::R_390_PC64:
3177     case elfcpp::R_390_PC32DBL:
3178     case elfcpp::R_390_PC24DBL:
3179     case elfcpp::R_390_PC16DBL:
3180     case elfcpp::R_390_PC12DBL:
3181       value = psymval->value(object, addend);
3182       break;
3183
3184     case elfcpp::R_390_GOTPC:
3185     case elfcpp::R_390_GOTPCDBL:
3186       gold_assert(gsym != NULL);
3187       value = target->got_address() + addend;
3188       break;
3189
3190     case elfcpp::R_390_PLTOFF64:
3191     case elfcpp::R_390_PLTOFF32:
3192     case elfcpp::R_390_PLTOFF16:
3193       gold_assert(gsym == NULL
3194                   || gsym->has_plt_offset()
3195                   || gsym->final_value_is_known());
3196       // fallthru
3197     case elfcpp::R_390_GOTOFF64:
3198     case elfcpp::R_390_GOTOFF32:
3199     case elfcpp::R_390_GOTOFF16:
3200       value = (psymval->value(object, addend)
3201                - target->got_address());
3202       break;
3203
3204     case elfcpp::R_390_GOT12:
3205     case elfcpp::R_390_GOT16:
3206     case elfcpp::R_390_GOT20:
3207     case elfcpp::R_390_GOT32:
3208     case elfcpp::R_390_GOT64:
3209     case elfcpp::R_390_GOTENT:
3210     case elfcpp::R_390_GOTPLT12:
3211     case elfcpp::R_390_GOTPLT16:
3212     case elfcpp::R_390_GOTPLT20:
3213     case elfcpp::R_390_GOTPLT32:
3214     case elfcpp::R_390_GOTPLT64:
3215     case elfcpp::R_390_GOTPLTENT:
3216       {
3217         unsigned int got_offset = 0;
3218         if (gsym != NULL)
3219           {
3220             gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3221             got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
3222           }
3223         else
3224           {
3225             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3226             gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3227             got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3228           }
3229         value = got_offset + target->got_main_offset() + addend;
3230       }
3231       break;
3232
3233       // These are initial tls relocs, which are expected when linking
3234     case elfcpp::R_390_TLS_LOAD:
3235     case elfcpp::R_390_TLS_GDCALL:          // Global-dynamic
3236     case elfcpp::R_390_TLS_GD32:
3237     case elfcpp::R_390_TLS_GD64:
3238     case elfcpp::R_390_TLS_LDCALL:          // Local-dynamic
3239     case elfcpp::R_390_TLS_LDM32:
3240     case elfcpp::R_390_TLS_LDM64:
3241     case elfcpp::R_390_TLS_LDO32:
3242     case elfcpp::R_390_TLS_LDO64:
3243     case elfcpp::R_390_TLS_GOTIE12:         // Initial-exec
3244     case elfcpp::R_390_TLS_GOTIE20:
3245     case elfcpp::R_390_TLS_GOTIE32:
3246     case elfcpp::R_390_TLS_GOTIE64:
3247     case elfcpp::R_390_TLS_IE32:
3248     case elfcpp::R_390_TLS_IE64:
3249     case elfcpp::R_390_TLS_IEENT:
3250     case elfcpp::R_390_TLS_LE32:            // Local-exec
3251     case elfcpp::R_390_TLS_LE64:
3252       value = this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
3253                          view, view_size);
3254       break;
3255
3256     default:
3257       break;
3258     }
3259
3260   typename S390_relocate_functions<size>::Status status
3261       = S390_relocate_functions<size>::STATUS_OK;
3262
3263   switch (r_type)
3264     {
3265     case elfcpp::R_390_NONE:
3266     case elfcpp::R_390_GNU_VTINHERIT:
3267     case elfcpp::R_390_GNU_VTENTRY:
3268     case elfcpp::R_390_TLS_GDCALL:
3269     case elfcpp::R_390_TLS_LDCALL:
3270     case elfcpp::R_390_TLS_LOAD:
3271       break;
3272
3273     case elfcpp::R_390_64:
3274     case elfcpp::R_390_GOT64:
3275     case elfcpp::R_390_GOTPLT64:
3276     case elfcpp::R_390_PLTOFF64:
3277     case elfcpp::R_390_GOTOFF64:
3278     case elfcpp::R_390_TLS_GD64:
3279     case elfcpp::R_390_TLS_LDM64:
3280     case elfcpp::R_390_TLS_LDO64:
3281     case elfcpp::R_390_TLS_GOTIE64:
3282     case elfcpp::R_390_TLS_IE64:
3283     case elfcpp::R_390_TLS_LE64:
3284       Relocate_functions<size, true>::rela64(view, value, 0);
3285       break;
3286
3287     case elfcpp::R_390_32:
3288     case elfcpp::R_390_GOT32:
3289     case elfcpp::R_390_GOTPLT32:
3290     case elfcpp::R_390_PLTOFF32:
3291     case elfcpp::R_390_GOTOFF32:
3292     case elfcpp::R_390_TLS_GD32:
3293     case elfcpp::R_390_TLS_LDM32:
3294     case elfcpp::R_390_TLS_LDO32:
3295     case elfcpp::R_390_TLS_GOTIE32:
3296     case elfcpp::R_390_TLS_IE32:
3297     case elfcpp::R_390_TLS_LE32:
3298       Relocate_functions<size, true>::rela32(view, value, 0);
3299       break;
3300
3301     case elfcpp::R_390_20:
3302     case elfcpp::R_390_GOT20:
3303     case elfcpp::R_390_GOTPLT20:
3304     case elfcpp::R_390_TLS_GOTIE20:
3305       status = S390_relocate_functions<size>::rela20(view, value);
3306       break;
3307
3308     case elfcpp::R_390_16:
3309     case elfcpp::R_390_GOT16:
3310     case elfcpp::R_390_GOTPLT16:
3311     case elfcpp::R_390_PLTOFF16:
3312     case elfcpp::R_390_GOTOFF16:
3313       status = S390_relocate_functions<size>::rela16(view, value);
3314       break;
3315
3316     case elfcpp::R_390_12:
3317     case elfcpp::R_390_GOT12:
3318     case elfcpp::R_390_GOTPLT12:
3319     case elfcpp::R_390_TLS_GOTIE12:
3320       status = S390_relocate_functions<size>::rela12(view, value);
3321       break;
3322
3323     case elfcpp::R_390_8:
3324       Relocate_functions<size, true>::rela8(view, value, 0);
3325       break;
3326
3327     case elfcpp::R_390_PC16:
3328       Relocate_functions<size, true>::pcrela16(view, value, 0,
3329                                                address);
3330       break;
3331
3332     case elfcpp::R_390_PLT64:
3333     case elfcpp::R_390_PC64:
3334       Relocate_functions<size, true>::pcrela64(view, value, 0, address);
3335       break;
3336
3337     case elfcpp::R_390_PLT32:
3338     case elfcpp::R_390_PC32:
3339     case elfcpp::R_390_GOTPC:
3340       Relocate_functions<size, true>::pcrela32(view, value, 0, address);
3341       break;
3342
3343     case elfcpp::R_390_PLT32DBL:
3344     case elfcpp::R_390_PC32DBL:
3345     case elfcpp::R_390_GOTPCDBL:
3346       status = S390_relocate_functions<size>::pcrela32dbl(view, value, address);
3347       break;
3348
3349     case elfcpp::R_390_PLT24DBL:
3350     case elfcpp::R_390_PC24DBL:
3351       status = S390_relocate_functions<size>::pcrela24dbl(view, value, address);
3352       break;
3353
3354     case elfcpp::R_390_PLT16DBL:
3355     case elfcpp::R_390_PC16DBL:
3356       status = S390_relocate_functions<size>::pcrela16dbl(view, value, address);
3357       break;
3358
3359     case elfcpp::R_390_PLT12DBL:
3360     case elfcpp::R_390_PC12DBL:
3361       status = S390_relocate_functions<size>::pcrela12dbl(view, value, address);
3362       break;
3363
3364     case elfcpp::R_390_GOTENT:
3365     case elfcpp::R_390_GOTPLTENT:
3366     case elfcpp::R_390_TLS_IEENT:
3367       value += target->got_address();
3368       status = S390_relocate_functions<size>::pcrela32dbl(view, value, address);
3369       break;
3370
3371     case elfcpp::R_390_COPY:
3372     case elfcpp::R_390_GLOB_DAT:
3373     case elfcpp::R_390_JMP_SLOT:
3374     case elfcpp::R_390_RELATIVE:
3375     case elfcpp::R_390_IRELATIVE:
3376       // These are outstanding tls relocs, which are unexpected when linking
3377     case elfcpp::R_390_TLS_TPOFF:
3378     case elfcpp::R_390_TLS_DTPMOD:
3379     case elfcpp::R_390_TLS_DTPOFF:
3380       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3381                              _("unexpected reloc %u in object file"),
3382                              r_type);
3383       break;
3384
3385     default:
3386       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3387                              _("unsupported reloc %u"),
3388                              r_type);
3389       break;
3390     }
3391
3392   if (status != S390_relocate_functions<size>::STATUS_OK)
3393     {
3394       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3395                              _("relocation overflow"));
3396     }
3397
3398   return true;
3399 }
3400
3401 // Perform a TLS relocation.
3402
3403 template<int size>
3404 inline typename elfcpp::Elf_types<size>::Elf_Addr
3405 Target_s390<size>::Relocate::relocate_tls(
3406     const Relocate_info<size, true>* relinfo,
3407     Target_s390<size>* target,
3408     size_t relnum,
3409     const elfcpp::Rela<size, true>& rela,
3410     unsigned int r_type,
3411     const Sized_symbol<size>* gsym,
3412     const Symbol_value<size>* psymval,
3413     unsigned char* view,
3414     section_size_type view_size)
3415 {
3416   Output_segment* tls_segment = relinfo->layout->tls_segment();
3417
3418   const Sized_relobj_file<size, true>* object = relinfo->object;
3419   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3420   elfcpp::Shdr<size, true> data_shdr(relinfo->data_shdr);
3421   bool is_allocatable = (data_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0;
3422
3423   typename elfcpp::Elf_types<size>::Elf_Addr value
3424       = psymval->value(relinfo->object, addend);
3425
3426   const bool is_final = (gsym == NULL
3427                          ? !parameters->options().shared()
3428                          : gsym->final_value_is_known());
3429   tls::Tls_optimization optimized_type
3430       = Target_s390<size>::optimize_tls_reloc(is_final, r_type);
3431   switch (r_type)
3432     {
3433     case elfcpp::R_390_TLS_GDCALL:            // Global-dynamic marker
3434       if (optimized_type == tls::TLSOPT_TO_LE)
3435         {
3436           if (tls_segment == NULL)
3437             {
3438               gold_assert(parameters->errors()->error_count() > 0
3439                           || issue_undefined_symbol_error(gsym));
3440               return 0;
3441             }
3442           this->tls_gd_to_le(relinfo, relnum, rela, view, view_size);
3443           break;
3444         }
3445       else
3446         {
3447           if (optimized_type == tls::TLSOPT_TO_IE)
3448             {
3449               this->tls_gd_to_ie(relinfo, relnum, rela, view, view_size);
3450               break;
3451             }
3452           else if (optimized_type == tls::TLSOPT_NONE)
3453             {
3454               break;
3455             }
3456         }
3457       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3458                              _("unsupported reloc %u"), r_type);
3459       break;
3460
3461     case elfcpp::R_390_TLS_GD32:            // Global-dynamic
3462     case elfcpp::R_390_TLS_GD64:
3463       if (optimized_type == tls::TLSOPT_TO_LE)
3464         {
3465           if (tls_segment == NULL)
3466             {
3467               gold_assert(parameters->errors()->error_count() > 0
3468                           || issue_undefined_symbol_error(gsym));
3469               return 0;
3470             }
3471           return value - tls_segment->memsz();
3472         }
3473       else
3474         {
3475           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3476                                    ? GOT_TYPE_TLS_OFFSET
3477                                    : GOT_TYPE_TLS_PAIR);
3478           if (gsym != NULL)
3479             {
3480               gold_assert(gsym->has_got_offset(got_type));
3481               return (gsym->got_offset(got_type)
3482                       + target->got_main_offset()
3483                       + addend);
3484             }
3485           else
3486             {
3487               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3488               gold_assert(object->local_has_got_offset(r_sym, got_type));
3489               return (object->local_got_offset(r_sym, got_type)
3490                       + target->got_main_offset()
3491                       + addend);
3492             }
3493         }
3494       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3495                              _("unsupported reloc %u"), r_type);
3496       break;
3497
3498     case elfcpp::R_390_TLS_LDCALL:            // Local-dynamic marker
3499       // This is a marker relocation. If the sequence is being turned to LE,
3500       // we modify the instruction, otherwise the instruction is untouched.
3501       if (optimized_type == tls::TLSOPT_TO_LE)
3502         {
3503           if (tls_segment == NULL)
3504             {
3505               gold_assert(parameters->errors()->error_count() > 0
3506                           || issue_undefined_symbol_error(gsym));
3507               return 0;
3508             }
3509           this->tls_ld_to_le(relinfo, relnum, rela, view, view_size);
3510           break;
3511         }
3512       else if (optimized_type == tls::TLSOPT_NONE)
3513         {
3514           break;
3515         }
3516       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3517                              _("unsupported reloc %u"), r_type);
3518       break;
3519
3520     case elfcpp::R_390_TLS_LDM32:            // Local-dynamic module
3521     case elfcpp::R_390_TLS_LDM64:
3522       if (optimized_type == tls::TLSOPT_TO_LE)
3523         {
3524           if (tls_segment == NULL)
3525             {
3526               gold_assert(parameters->errors()->error_count() > 0
3527                           || issue_undefined_symbol_error(gsym));
3528               return 0;
3529             }
3530           // Doesn't matter what we fill it with - it's going to be unused.
3531           return 0;
3532         }
3533       else if (optimized_type == tls::TLSOPT_NONE)
3534         {
3535           // Relocate the field with the offset of the GOT entry for
3536           // the module index.
3537           return (target->got_mod_index_entry(NULL, NULL, NULL)
3538                   + addend
3539                   + target->got_main_offset());
3540         }
3541       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3542                              _("unsupported reloc %u"), r_type);
3543       break;
3544
3545     case elfcpp::R_390_TLS_LDO32:         // Local-dynamic offset
3546     case elfcpp::R_390_TLS_LDO64:
3547       // This relocation type is used in debugging information.
3548       // In that case we need to not optimize the value.  If the
3549       // section is not allocatable, then we assume we should not
3550       // optimize this reloc.
3551       if (optimized_type == tls::TLSOPT_TO_LE && is_allocatable)
3552         {
3553           if (tls_segment == NULL)
3554             {
3555               gold_assert(parameters->errors()->error_count() > 0
3556                           || issue_undefined_symbol_error(gsym));
3557               return 0;
3558             }
3559           value -= tls_segment->memsz();
3560         }
3561       return value;
3562
3563     case elfcpp::R_390_TLS_LOAD:         // Initial-exec marker
3564       // This is a marker relocation. If the sequence is being turned to LE,
3565       // we modify the instruction, otherwise the instruction is untouched.
3566       if (gsym != NULL
3567           && gsym->is_undefined()
3568           && parameters->options().output_is_executable())
3569         {
3570           Target_s390<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3571                                                       rela, view,
3572                                                       view_size);
3573           break;
3574         }
3575       else if (optimized_type == tls::TLSOPT_TO_LE)
3576         {
3577           if (tls_segment == NULL)
3578             {
3579               gold_assert(parameters->errors()->error_count() > 0
3580                           || issue_undefined_symbol_error(gsym));
3581               return 0;
3582             }
3583           Target_s390<size>::Relocate::tls_ie_to_le(relinfo, relnum,
3584                                                       rela, view,
3585                                                       view_size);
3586           break;
3587         }
3588       else if (optimized_type == tls::TLSOPT_NONE)
3589         {
3590           break;
3591         }
3592       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3593                              _("unsupported reloc type %u"),
3594                              r_type);
3595       break;
3596
3597     case elfcpp::R_390_TLS_GOTIE12:       // Initial-exec, not optimizable
3598     case elfcpp::R_390_TLS_GOTIE20:
3599     case elfcpp::R_390_TLS_IEENT:
3600     case elfcpp::R_390_TLS_GOTIE32:       // Initial-exec, optimizable
3601     case elfcpp::R_390_TLS_GOTIE64:
3602     case elfcpp::R_390_TLS_IE32:
3603     case elfcpp::R_390_TLS_IE64:
3604       if (gsym != NULL
3605           && gsym->is_undefined()
3606           && parameters->options().output_is_executable()
3607           // These three cannot be optimized to LE, no matter what
3608           && r_type != elfcpp::R_390_TLS_GOTIE12
3609           && r_type != elfcpp::R_390_TLS_GOTIE20
3610           && r_type != elfcpp::R_390_TLS_IEENT)
3611         {
3612           return value;
3613         }
3614       else if (optimized_type == tls::TLSOPT_TO_LE)
3615         {
3616           if (tls_segment == NULL)
3617             {
3618               gold_assert(parameters->errors()->error_count() > 0
3619                           || issue_undefined_symbol_error(gsym));
3620               return 0;
3621             }
3622           return value - tls_segment->memsz();
3623         }
3624       else if (optimized_type == tls::TLSOPT_NONE)
3625         {
3626           // Relocate the field with the offset of the GOT entry for
3627           // the tp-relative offset of the symbol.
3628           unsigned int got_offset;
3629           if (gsym != NULL)
3630             {
3631               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3632               got_offset = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
3633             }
3634           else
3635             {
3636               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3637               gold_assert(object->local_has_got_offset(r_sym,
3638                                                        GOT_TYPE_TLS_OFFSET));
3639               got_offset = object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
3640             }
3641           got_offset += target->got_main_offset();
3642           if (r_type == elfcpp::R_390_TLS_IE32
3643               || r_type == elfcpp::R_390_TLS_IE64)
3644             return target->got_address() + got_offset + addend;
3645           else
3646             return got_offset + addend;
3647         }
3648       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3649                              _("unsupported reloc type %u"),
3650                              r_type);
3651       break;
3652
3653     case elfcpp::R_390_TLS_LE32:          // Local-exec
3654     case elfcpp::R_390_TLS_LE64:
3655       if (tls_segment == NULL)
3656         {
3657           gold_assert(parameters->errors()->error_count() > 0
3658                       || issue_undefined_symbol_error(gsym));
3659           return 0;
3660         }
3661       return value - tls_segment->memsz();
3662     }
3663   return 0;
3664 }
3665
3666 // Do a relocation in which we convert a TLS General-Dynamic to an
3667 // Initial-Exec.
3668
3669 template<int size>
3670 inline void
3671 Target_s390<size>::Relocate::tls_gd_to_ie(
3672     const Relocate_info<size, true>* relinfo,
3673     size_t relnum,
3674     const elfcpp::Rela<size, true>& rela,
3675     unsigned char* view,
3676     section_size_type view_size)
3677 {
3678   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3679   if (view[0] == 0x4d)
3680     {
3681       // bas, don't care about details
3682       // Change to l %r2, 0(%r2, %r12)
3683       view[0] = 0x58;
3684       view[1] = 0x22;
3685       view[2] = 0xc0;
3686       view[3] = 0x00;
3687       return;
3688     }
3689   else if (view[0] == 0xc0)
3690     {
3691       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3692       // brasl %r14, __tls_get_offset@plt
3693       if (view[1] == 0xe5)
3694         {
3695           // Change to l/lg %r2, 0(%r2, %r12)
3696           // There was a PLT32DBL reloc at the last 4 bytes, overwrite its result.
3697           if (size == 32)
3698             {
3699               // l
3700               view[0] = 0x58;
3701               view[1] = 0x22;
3702               view[2] = 0xc0;
3703               view[3] = 0x00;
3704               // nop
3705               view[4] = 0x07;
3706               view[5] = 0x07;
3707             }
3708           else
3709             {
3710               // lg
3711               view[0] = 0xe3;
3712               view[1] = 0x22;
3713               view[2] = 0xc0;
3714               view[3] = 0;
3715               view[4] = 0;
3716               view[5] = 0x04;
3717             }
3718           return;
3719         }
3720     }
3721   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3722                          _("unsupported op for GD to IE"));
3723 }
3724
3725 // Do a relocation in which we convert a TLS General-Dynamic to a
3726 // Local-Exec.
3727
3728 template<int size>
3729 inline void
3730 Target_s390<size>::Relocate::tls_gd_to_le(
3731     const Relocate_info<size, true>* relinfo,
3732     size_t relnum,
3733     const elfcpp::Rela<size, true>& rela,
3734     unsigned char* view,
3735     section_size_type view_size)
3736 {
3737   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3738   if (view[0] == 0x0d)
3739     {
3740       // basr, change to nop
3741       view[0] = 0x07;
3742       view[1] = 0x07;
3743     }
3744   else if (view[0] == 0x4d)
3745     {
3746       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3747       // bas, don't care about details, change to nop
3748       view[0] = 0x47;
3749       view[1] = 0;
3750       view[2] = 0;
3751       view[3] = 0;
3752       return;
3753     }
3754   else if (view[0] == 0xc0)
3755     {
3756       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3757       // brasl %r14, __tls_get_offset@plt
3758       if (view[1] == 0xe5)
3759         {
3760           // Change to nop jump. There was a PLT32DBL reloc at the last
3761           // 4 bytes, overwrite its result.
3762           view[1] = 0x04;
3763           view[2] = 0;
3764           view[3] = 0;
3765           view[4] = 0;
3766           view[5] = 0;
3767           return;
3768         }
3769     }
3770   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3771                          _("unsupported op for GD to LE"));
3772 }
3773
3774 template<int size>
3775 inline void
3776 Target_s390<size>::Relocate::tls_ld_to_le(
3777     const Relocate_info<size, true>* relinfo,
3778     size_t relnum,
3779     const elfcpp::Rela<size, true>& rela,
3780     unsigned char* view,
3781     section_size_type view_size)
3782 {
3783   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3784
3785   if (view[0] == 0x0d)
3786     {
3787       // basr, change to nop
3788       view[0] = 0x07;
3789       view[1] = 0x07;
3790     }
3791   else if (view[0] == 0x4d)
3792     {
3793       // bas, don't care about details, change to nop
3794       view[0] = 0x47;
3795       view[1] = 0;
3796       view[2] = 0;
3797       view[3] = 0;
3798       return;
3799     }
3800   else if (view[0] == 0xc0)
3801     {
3802       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3803       // brasl %r14, __tls_get_offset@plt
3804       if (view[1] == 0xe5)
3805         {
3806           // Change to nop jump. There was a PLT32DBL reloc at the last
3807           // 4 bytes, overwrite its result.
3808           view[1] = 0x04;
3809           view[2] = 0;
3810           view[3] = 0;
3811           view[4] = 0;
3812           view[5] = 0;
3813           return;
3814         }
3815     }
3816   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3817                          _("unsupported op for LD to LE"));
3818 }
3819
3820 // Do a relocation in which we convert a TLS Initial-Exec to a
3821 // Local-Exec.
3822
3823 template<int size>
3824 inline void
3825 Target_s390<size>::Relocate::tls_ie_to_le(
3826     const Relocate_info<size, true>* relinfo,
3827     size_t relnum,
3828     const elfcpp::Rela<size, true>& rela,
3829     unsigned char* view,
3830     section_size_type view_size)
3831 {
3832   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3833
3834   if (view[0] == 0x58)
3835     {
3836       // l %rX, 0(%rY) or l %rX, 0(%rY, %r12)
3837       if ((view[2] & 0x0f) != 0 || view[3] != 0)
3838         goto err;
3839       int rx = view[1] >> 4 & 0xf;
3840       int ry = view[1] & 0xf;
3841       int rz = view[2] >> 4 & 0xf;
3842       if (rz == 0)
3843         {
3844         }
3845       else if (ry == 0)
3846         {
3847           ry = rz;
3848         }
3849       else if (rz == 12)
3850         {
3851         }
3852       else if (ry == 12)
3853         {
3854           ry = rz;
3855         }
3856       else
3857         goto err;
3858       // to lr %rX, $rY
3859       view[0] = 0x18;
3860       view[1] = rx << 4 | ry;
3861       // and insert a nop
3862       view[2] = 0x07;
3863       view[3] = 0x00;
3864     }
3865   else if (view[0] == 0xe3)
3866     {
3867       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 6);
3868       // lg %rX, 0(%rY) or lg %rX, 0(%rY, %r12)
3869       if ((view[2] & 0x0f) != 0 ||
3870           view[3] != 0 ||
3871           view[4] != 0 ||
3872           view[5] != 0x04)
3873         goto err;
3874       int rx = view[1] >> 4 & 0xf;
3875       int ry = view[1] & 0xf;
3876       int rz = view[2] >> 4 & 0xf;
3877       if (rz == 0)
3878         {
3879         }
3880       else if (ry == 0)
3881         {
3882           ry = rz;
3883         }
3884       else if (rz == 12)
3885         {
3886         }
3887       else if (ry == 12)
3888         {
3889           ry = rz;
3890         }
3891       else
3892         goto err;
3893       // to sllg %rX, $rY, 0
3894       view[0] = 0xeb;
3895       view[1] = rx << 4 | ry;
3896       view[2] = 0x00;
3897       view[3] = 0x00;
3898       view[4] = 0x00;
3899       view[5] = 0x0d;
3900     }
3901   else
3902     {
3903 err:
3904       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3905                              _("unsupported op for IE to LE"));
3906     }
3907 }
3908
3909 // Scan relocations for a section.
3910
3911 template<int size>
3912 void
3913 Target_s390<size>::scan_relocs(Symbol_table* symtab,
3914                                  Layout* layout,
3915                                  Sized_relobj_file<size, true>* object,
3916                                  unsigned int data_shndx,
3917                                  unsigned int sh_type,
3918                                  const unsigned char* prelocs,
3919                                  size_t reloc_count,
3920                                  Output_section* output_section,
3921                                  bool needs_special_offset_handling,
3922                                  size_t local_symbol_count,
3923                                  const unsigned char* plocal_symbols)
3924 {
3925   if (sh_type == elfcpp::SHT_REL)
3926     {
3927       gold_error(_("%s: unsupported REL reloc section"),
3928                  object->name().c_str());
3929       return;
3930     }
3931
3932   gold::scan_relocs<size, true, Target_s390<size>, elfcpp::SHT_RELA,
3933       typename Target_s390<size>::Scan>(
3934     symtab,
3935     layout,
3936     this,
3937     object,
3938     data_shndx,
3939     prelocs,
3940     reloc_count,
3941     output_section,
3942     needs_special_offset_handling,
3943     local_symbol_count,
3944     plocal_symbols);
3945 }
3946
3947 // Finalize the sections.
3948
3949 template<int size>
3950 void
3951 Target_s390<size>::do_finalize_sections(
3952     Layout* layout,
3953     const Input_objects*,
3954     Symbol_table* symtab)
3955 {
3956   const Reloc_section* rel_plt = (this->plt_ == NULL
3957                                   ? NULL
3958                                   : this->plt_->rela_plt());
3959   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3960                                   this->rela_dyn_, true, size == 32);
3961
3962   this->layout_ = layout;
3963
3964   // Emit any relocs we saved in an attempt to avoid generating COPY
3965   // relocs.
3966   if (this->copy_relocs_.any_saved_relocs())
3967     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3968
3969   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3970   // the .got section.
3971   Symbol* sym = this->global_offset_table_;
3972   if (sym != NULL)
3973     {
3974       uint64_t data_size = this->got_->current_data_size();
3975       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3976     }
3977
3978   if (parameters->doing_static_link()
3979       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3980     {
3981       // If linking statically, make sure that the __rela_iplt symbols
3982       // were defined if necessary, even if we didn't create a PLT.
3983       static const Define_symbol_in_segment syms[] =
3984         {
3985           {
3986             "__rela_iplt_start",        // name
3987             elfcpp::PT_LOAD,            // segment_type
3988             elfcpp::PF_W,               // segment_flags_set
3989             elfcpp::PF(0),              // segment_flags_clear
3990             0,                          // value
3991             0,                          // size
3992             elfcpp::STT_NOTYPE,         // type
3993             elfcpp::STB_GLOBAL,         // binding
3994             elfcpp::STV_HIDDEN,         // visibility
3995             0,                          // nonvis
3996             Symbol::SEGMENT_START,      // offset_from_base
3997             true                        // only_if_ref
3998           },
3999           {
4000             "__rela_iplt_end",          // name
4001             elfcpp::PT_LOAD,            // segment_type
4002             elfcpp::PF_W,               // segment_flags_set
4003             elfcpp::PF(0),              // segment_flags_clear
4004             0,                          // value
4005             0,                          // size
4006             elfcpp::STT_NOTYPE,         // type
4007             elfcpp::STB_GLOBAL,         // binding
4008             elfcpp::STV_HIDDEN,         // visibility
4009             0,                          // nonvis
4010             Symbol::SEGMENT_START,      // offset_from_base
4011             true                        // only_if_ref
4012           }
4013         };
4014
4015       symtab->define_symbols(layout, 2, syms,
4016                              layout->script_options()->saw_sections_clause());
4017     }
4018 }
4019
4020 // Return the size of a relocation while scanning during a relocatable
4021 // link.
4022
4023 template<int size>
4024 unsigned int
4025 Target_s390<size>::Relocatable_size_for_reloc::get_size_for_reloc(
4026     unsigned int r_type,
4027     Relobj* object)
4028 {
4029   switch (r_type)
4030     {
4031     case elfcpp::R_390_NONE:
4032     case elfcpp::R_390_GNU_VTINHERIT:
4033     case elfcpp::R_390_GNU_VTENTRY:
4034     case elfcpp::R_390_TLS_GD32:          // Global-dynamic
4035     case elfcpp::R_390_TLS_GD64:
4036     case elfcpp::R_390_TLS_GDCALL:
4037     case elfcpp::R_390_TLS_LDM32:         // Local-dynamic
4038     case elfcpp::R_390_TLS_LDM64:
4039     case elfcpp::R_390_TLS_LDO32:
4040     case elfcpp::R_390_TLS_LDO64:
4041     case elfcpp::R_390_TLS_LDCALL:
4042     case elfcpp::R_390_TLS_IE32:          // Initial-exec
4043     case elfcpp::R_390_TLS_IE64:
4044     case elfcpp::R_390_TLS_IEENT:
4045     case elfcpp::R_390_TLS_GOTIE12:
4046     case elfcpp::R_390_TLS_GOTIE20:
4047     case elfcpp::R_390_TLS_GOTIE32:
4048     case elfcpp::R_390_TLS_GOTIE64:
4049     case elfcpp::R_390_TLS_LOAD:
4050     case elfcpp::R_390_TLS_LE32:          // Local-exec
4051     case elfcpp::R_390_TLS_LE64:
4052       return 0;
4053
4054     case elfcpp::R_390_64:
4055     case elfcpp::R_390_PC64:
4056     case elfcpp::R_390_GOT64:
4057     case elfcpp::R_390_PLT64:
4058     case elfcpp::R_390_GOTOFF64:
4059     case elfcpp::R_390_GOTPLT64:
4060     case elfcpp::R_390_PLTOFF64:
4061       return 8;
4062
4063     case elfcpp::R_390_32:
4064     case elfcpp::R_390_PC32:
4065     case elfcpp::R_390_GOT32:
4066     case elfcpp::R_390_PLT32:
4067     case elfcpp::R_390_GOTOFF32:
4068     case elfcpp::R_390_GOTPC:
4069     case elfcpp::R_390_PC32DBL:
4070     case elfcpp::R_390_PLT32DBL:
4071     case elfcpp::R_390_GOTPCDBL:
4072     case elfcpp::R_390_GOTENT:
4073     case elfcpp::R_390_GOTPLT32:
4074     case elfcpp::R_390_GOTPLTENT:
4075     case elfcpp::R_390_PLTOFF32:
4076     case elfcpp::R_390_20:
4077     case elfcpp::R_390_GOT20:
4078     case elfcpp::R_390_GOTPLT20:
4079       return 4;
4080
4081     case elfcpp::R_390_PC24DBL:
4082     case elfcpp::R_390_PLT24DBL:
4083       return 3;
4084
4085     case elfcpp::R_390_12:
4086     case elfcpp::R_390_GOT12:
4087     case elfcpp::R_390_GOTPLT12:
4088     case elfcpp::R_390_PC12DBL:
4089     case elfcpp::R_390_PLT12DBL:
4090     case elfcpp::R_390_16:
4091     case elfcpp::R_390_GOT16:
4092     case elfcpp::R_390_PC16:
4093     case elfcpp::R_390_PC16DBL:
4094     case elfcpp::R_390_PLT16DBL:
4095     case elfcpp::R_390_GOTOFF16:
4096     case elfcpp::R_390_GOTPLT16:
4097     case elfcpp::R_390_PLTOFF16:
4098       return 2;
4099
4100     case elfcpp::R_390_8:
4101       return 1;
4102
4103       // These are relocations which should only be seen by the
4104       // dynamic linker, and should never be seen here.
4105     case elfcpp::R_390_COPY:
4106     case elfcpp::R_390_GLOB_DAT:
4107     case elfcpp::R_390_JMP_SLOT:
4108     case elfcpp::R_390_RELATIVE:
4109     case elfcpp::R_390_IRELATIVE:
4110     case elfcpp::R_390_TLS_DTPMOD:
4111     case elfcpp::R_390_TLS_DTPOFF:
4112     case elfcpp::R_390_TLS_TPOFF:
4113       object->error(_("unexpected reloc %u in object file"), r_type);
4114       return 0;
4115
4116     default:
4117       object->error(_("unsupported reloc %u in object file"), r_type);
4118       return 0;
4119     }
4120 }
4121
4122 // Scan the relocs during a relocatable link.
4123
4124 template<int size>
4125 void
4126 Target_s390<size>::scan_relocatable_relocs(
4127     Symbol_table* symtab,
4128     Layout* layout,
4129     Sized_relobj_file<size, true>* object,
4130     unsigned int data_shndx,
4131     unsigned int sh_type,
4132     const unsigned char* prelocs,
4133     size_t reloc_count,
4134     Output_section* output_section,
4135     bool needs_special_offset_handling,
4136     size_t local_symbol_count,
4137     const unsigned char* plocal_symbols,
4138     Relocatable_relocs* rr)
4139 {
4140   gold_assert(sh_type == elfcpp::SHT_RELA);
4141
4142   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4143     Relocatable_size_for_reloc> Scan_relocatable_relocs;
4144
4145   gold::scan_relocatable_relocs<size, true, elfcpp::SHT_RELA,
4146       Scan_relocatable_relocs>(
4147     symtab,
4148     layout,
4149     object,
4150     data_shndx,
4151     prelocs,
4152     reloc_count,
4153     output_section,
4154     needs_special_offset_handling,
4155     local_symbol_count,
4156     plocal_symbols,
4157     rr);
4158 }
4159
4160 // Relocate a section during a relocatable link.
4161
4162 template<int size>
4163 void
4164 Target_s390<size>::relocate_relocs(
4165     const Relocate_info<size, true>* relinfo,
4166     unsigned int sh_type,
4167     const unsigned char* prelocs,
4168     size_t reloc_count,
4169     Output_section* output_section,
4170     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4171     const Relocatable_relocs* rr,
4172     unsigned char* view,
4173     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4174     section_size_type view_size,
4175     unsigned char* reloc_view,
4176     section_size_type reloc_view_size)
4177 {
4178   gold_assert(sh_type == elfcpp::SHT_RELA);
4179
4180   gold::relocate_relocs<size, true, elfcpp::SHT_RELA>(
4181     relinfo,
4182     prelocs,
4183     reloc_count,
4184     output_section,
4185     offset_in_output_section,
4186     rr,
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.