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