Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf64-s390.c
Line
Count
Source
1
/* IBM S/390-specific support for 64-bit ELF
2
   Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
   Contributed Martin Schwidefsky (schwidefsky@de.ibm.com).
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf/s390.h"
28
#include "elf-s390.h"
29
#include "dwarf2.h"
30
#include "sframe.h"
31
#include "sframe-api.h"
32
#include <stdarg.h>
33
34
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
35
   from smaller values.  Start with zero, widen, *then* decrement.  */
36
#define MINUS_ONE      (((bfd_vma)0) - 1)
37
38
static bfd_reloc_status_type
39
s390_tls_reloc (bfd *, arelent *, asymbol *, void *,
40
    asection *, bfd *, char **);
41
static bfd_reloc_status_type
42
s390_elf_ldisp_reloc (bfd *, arelent *, asymbol *, void *,
43
          asection *, bfd *, char **);
44
45
/* The relocation "howto" table.  */
46
static reloc_howto_type elf_howto_table[] =
47
{
48
  HOWTO (R_390_NONE,    /* type */
49
   0,     /* rightshift */
50
   0,     /* size */
51
   0,     /* bitsize */
52
   false,     /* pc_relative */
53
   0,     /* bitpos */
54
   complain_overflow_dont, /* complain_on_overflow */
55
   bfd_elf_generic_reloc, /* special_function */
56
   "R_390_NONE",    /* name */
57
   false,     /* partial_inplace */
58
   0,     /* src_mask */
59
   0,     /* dst_mask */
60
   false),    /* pcrel_offset */
61
62
  HOWTO(R_390_8,   0, 1,  8, false, 0, complain_overflow_bitfield,
63
  bfd_elf_generic_reloc, "R_390_8",  false, 0,0x000000ff, false),
64
  HOWTO(R_390_12,  0, 2, 12, false, 0, complain_overflow_dont,
65
  bfd_elf_generic_reloc, "R_390_12",   false, 0,0x00000fff, false),
66
  HOWTO(R_390_16,  0, 2, 16, false, 0, complain_overflow_bitfield,
67
  bfd_elf_generic_reloc, "R_390_16",   false, 0,0x0000ffff, false),
68
  HOWTO(R_390_32,  0, 4, 32, false, 0, complain_overflow_bitfield,
69
  bfd_elf_generic_reloc, "R_390_32",   false, 0,0xffffffff, false),
70
  HOWTO(R_390_PC32,  0, 4, 32,  true, 0, complain_overflow_bitfield,
71
  bfd_elf_generic_reloc, "R_390_PC32",   false, 0,0xffffffff, true),
72
  HOWTO(R_390_GOT12,   0, 2, 12, false, 0, complain_overflow_bitfield,
73
  bfd_elf_generic_reloc, "R_390_GOT12",  false, 0,0x00000fff, false),
74
  HOWTO(R_390_GOT32,   0, 4, 32, false, 0, complain_overflow_bitfield,
75
  bfd_elf_generic_reloc, "R_390_GOT32",  false, 0,0xffffffff, false),
76
  HOWTO(R_390_PLT32,   0, 4, 32,  true, 0, complain_overflow_bitfield,
77
  bfd_elf_generic_reloc, "R_390_PLT32",  false, 0,0xffffffff, true),
78
  HOWTO(R_390_COPY,  0, 8, 64, false, 0, complain_overflow_bitfield,
79
  bfd_elf_generic_reloc, "R_390_COPY",   false, 0,MINUS_ONE,  false),
80
  HOWTO(R_390_GLOB_DAT,  0, 8, 64, false, 0, complain_overflow_bitfield,
81
  bfd_elf_generic_reloc, "R_390_GLOB_DAT", false, 0,MINUS_ONE,  false),
82
  HOWTO(R_390_JMP_SLOT,  0, 8, 64, false, 0, complain_overflow_bitfield,
83
  bfd_elf_generic_reloc, "R_390_JMP_SLOT", false, 0,MINUS_ONE,  false),
84
  HOWTO(R_390_RELATIVE,  0, 8, 64,  true, 0, complain_overflow_bitfield,
85
  bfd_elf_generic_reloc, "R_390_RELATIVE", false, 0,MINUS_ONE,  false),
86
  HOWTO(R_390_GOTOFF32,  0, 4, 32, false, 0, complain_overflow_bitfield,
87
  bfd_elf_generic_reloc, "R_390_GOTOFF32", false, 0,MINUS_ONE,  false),
88
  HOWTO(R_390_GOTPC,   0, 8, 64,  true, 0, complain_overflow_bitfield,
89
  bfd_elf_generic_reloc, "R_390_GOTPC",  false, 0,MINUS_ONE,  true),
90
  HOWTO(R_390_GOT16,   0, 2, 16, false, 0, complain_overflow_bitfield,
91
  bfd_elf_generic_reloc, "R_390_GOT16",  false, 0,0x0000ffff, false),
92
  HOWTO(R_390_PC16,  0, 2, 16,  true, 0, complain_overflow_bitfield,
93
  bfd_elf_generic_reloc, "R_390_PC16",   false, 0,0x0000ffff, true),
94
  HOWTO(R_390_PC16DBL,   1, 2, 16,  true, 0, complain_overflow_bitfield,
95
  bfd_elf_generic_reloc, "R_390_PC16DBL",  false, 0,0x0000ffff, true),
96
  HOWTO(R_390_PLT16DBL,  1, 2, 16,  true, 0, complain_overflow_bitfield,
97
  bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true),
98
  HOWTO(R_390_PC32DBL,   1, 4, 32,  true, 0, complain_overflow_bitfield,
99
  bfd_elf_generic_reloc, "R_390_PC32DBL",  false, 0,0xffffffff, true),
100
  HOWTO(R_390_PLT32DBL,  1, 4, 32,  true, 0, complain_overflow_bitfield,
101
  bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true),
102
  HOWTO(R_390_GOTPCDBL,  1, 4, 32,  true, 0, complain_overflow_bitfield,
103
  bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,MINUS_ONE,  true),
104
  HOWTO(R_390_64,  0, 8, 64, false, 0, complain_overflow_bitfield,
105
  bfd_elf_generic_reloc, "R_390_64",   false, 0,MINUS_ONE,  false),
106
  HOWTO(R_390_PC64,  0, 8, 64,  true, 0, complain_overflow_bitfield,
107
  bfd_elf_generic_reloc, "R_390_PC64",   false, 0,MINUS_ONE,  true),
108
  HOWTO(R_390_GOT64,   0, 8, 64, false, 0, complain_overflow_bitfield,
109
  bfd_elf_generic_reloc, "R_390_GOT64",  false, 0,MINUS_ONE,  false),
110
  HOWTO(R_390_PLT64,   0, 8, 64,  true, 0, complain_overflow_bitfield,
111
  bfd_elf_generic_reloc, "R_390_PLT64",  false, 0,MINUS_ONE,  true),
112
  HOWTO(R_390_GOTENT,  1, 4, 32,  true, 0, complain_overflow_bitfield,
113
  bfd_elf_generic_reloc, "R_390_GOTENT",   false, 0,MINUS_ONE,  true),
114
  HOWTO(R_390_GOTOFF16,  0, 2, 16, false, 0, complain_overflow_bitfield,
115
  bfd_elf_generic_reloc, "R_390_GOTOFF16", false, 0,0x0000ffff, false),
116
  HOWTO(R_390_GOTOFF64,  0, 8, 64, false, 0, complain_overflow_bitfield,
117
  bfd_elf_generic_reloc, "R_390_GOTOFF64", false, 0,MINUS_ONE,  false),
118
  HOWTO(R_390_GOTPLT12,  0, 2, 12, false, 0, complain_overflow_dont,
119
  bfd_elf_generic_reloc, "R_390_GOTPLT12", false, 0,0x00000fff, false),
120
  HOWTO(R_390_GOTPLT16,  0, 2, 16, false, 0, complain_overflow_bitfield,
121
  bfd_elf_generic_reloc, "R_390_GOTPLT16", false, 0,0x0000ffff, false),
122
  HOWTO(R_390_GOTPLT32,  0, 4, 32, false, 0, complain_overflow_bitfield,
123
  bfd_elf_generic_reloc, "R_390_GOTPLT32", false, 0,0xffffffff, false),
124
  HOWTO(R_390_GOTPLT64,  0, 8, 64, false, 0, complain_overflow_bitfield,
125
  bfd_elf_generic_reloc, "R_390_GOTPLT64", false, 0,MINUS_ONE,  false),
126
  HOWTO(R_390_GOTPLTENT, 1, 4, 32,  true, 0, complain_overflow_bitfield,
127
  bfd_elf_generic_reloc, "R_390_GOTPLTENT",false, 0,MINUS_ONE,  true),
128
  HOWTO(R_390_PLTOFF16,  0, 2, 16, false, 0, complain_overflow_bitfield,
129
  bfd_elf_generic_reloc, "R_390_PLTOFF16", false, 0,0x0000ffff, false),
130
  HOWTO(R_390_PLTOFF32,  0, 4, 32, false, 0, complain_overflow_bitfield,
131
  bfd_elf_generic_reloc, "R_390_PLTOFF32", false, 0,0xffffffff, false),
132
  HOWTO(R_390_PLTOFF64,  0, 8, 64, false, 0, complain_overflow_bitfield,
133
  bfd_elf_generic_reloc, "R_390_PLTOFF64", false, 0,MINUS_ONE,  false),
134
  HOWTO(R_390_TLS_LOAD, 0, 0, 0, false, 0, complain_overflow_dont,
135
  s390_tls_reloc, "R_390_TLS_LOAD", false, 0, 0, false),
136
  HOWTO(R_390_TLS_GDCALL, 0, 0, 0, false, 0, complain_overflow_dont,
137
  s390_tls_reloc, "R_390_TLS_GDCALL", false, 0, 0, false),
138
  HOWTO(R_390_TLS_LDCALL, 0, 0, 0, false, 0, complain_overflow_dont,
139
  s390_tls_reloc, "R_390_TLS_LDCALL", false, 0, 0, false),
140
  EMPTY_HOWTO (R_390_TLS_GD32), /* Empty entry for R_390_TLS_GD32.  */
141
  HOWTO(R_390_TLS_GD64,  0, 8, 64, false, 0, complain_overflow_bitfield,
142
  bfd_elf_generic_reloc, "R_390_TLS_GD64", false, 0, MINUS_ONE, false),
143
  HOWTO(R_390_TLS_GOTIE12, 0, 2, 12, false, 0, complain_overflow_dont,
144
  bfd_elf_generic_reloc, "R_390_TLS_GOTIE12", false, 0, 0x00000fff, false),
145
  EMPTY_HOWTO (R_390_TLS_GOTIE32),  /* Empty entry for R_390_TLS_GOTIE32.  */
146
  HOWTO(R_390_TLS_GOTIE64, 0, 8, 64, false, 0, complain_overflow_bitfield,
147
  bfd_elf_generic_reloc, "R_390_TLS_GOTIE64", false, 0, MINUS_ONE, false),
148
  EMPTY_HOWTO (R_390_TLS_LDM32),  /* Empty entry for R_390_TLS_LDM32.  */
149
  HOWTO(R_390_TLS_LDM64, 0, 8, 64, false, 0, complain_overflow_bitfield,
150
  bfd_elf_generic_reloc, "R_390_TLS_LDM64", false, 0, MINUS_ONE, false),
151
  EMPTY_HOWTO (R_390_TLS_IE32), /* Empty entry for R_390_TLS_IE32.  */
152
  HOWTO(R_390_TLS_IE64,  0, 8, 64, false, 0, complain_overflow_bitfield,
153
  bfd_elf_generic_reloc, "R_390_TLS_IE64", false, 0, MINUS_ONE, false),
154
  HOWTO(R_390_TLS_IEENT, 1, 4, 32, true, 0, complain_overflow_bitfield,
155
  bfd_elf_generic_reloc, "R_390_TLS_IEENT", false, 0, MINUS_ONE, true),
156
  EMPTY_HOWTO (R_390_TLS_LE32), /* Empty entry for R_390_TLS_LE32.  */
157
  HOWTO(R_390_TLS_LE64,  0, 8, 64, false, 0, complain_overflow_bitfield,
158
  bfd_elf_generic_reloc, "R_390_TLS_LE64", false, 0, MINUS_ONE, false),
159
  EMPTY_HOWTO (R_390_TLS_LDO32),  /* Empty entry for R_390_TLS_LDO32.  */
160
  HOWTO(R_390_TLS_LDO64, 0, 8, 64, false, 0, complain_overflow_bitfield,
161
  bfd_elf_generic_reloc, "R_390_TLS_LDO64", false, 0, MINUS_ONE, false),
162
  HOWTO(R_390_TLS_DTPMOD, 0, 8, 64, false, 0, complain_overflow_bitfield,
163
  bfd_elf_generic_reloc, "R_390_TLS_DTPMOD", false, 0, MINUS_ONE, false),
164
  HOWTO(R_390_TLS_DTPOFF, 0, 8, 64, false, 0, complain_overflow_bitfield,
165
  bfd_elf_generic_reloc, "R_390_TLS_DTPOFF", false, 0, MINUS_ONE, false),
166
  HOWTO(R_390_TLS_TPOFF, 0, 8, 64, false, 0, complain_overflow_bitfield,
167
  bfd_elf_generic_reloc, "R_390_TLS_TPOFF", false, 0, MINUS_ONE, false),
168
  HOWTO(R_390_20,  0, 4, 20, false, 8, complain_overflow_dont,
169
  s390_elf_ldisp_reloc, "R_390_20",      false, 0,0x0fffff00, false),
170
  HOWTO(R_390_GOT20,   0, 4, 20, false, 8, complain_overflow_dont,
171
  s390_elf_ldisp_reloc, "R_390_GOT20",   false, 0,0x0fffff00, false),
172
  HOWTO(R_390_GOTPLT20,  0, 4, 20, false, 8, complain_overflow_dont,
173
  s390_elf_ldisp_reloc, "R_390_GOTPLT20", false, 0,0x0fffff00, false),
174
  HOWTO(R_390_TLS_GOTIE20, 0, 4, 20, false, 8, complain_overflow_dont,
175
  s390_elf_ldisp_reloc, "R_390_TLS_GOTIE20", false, 0,0x0fffff00, false),
176
  HOWTO(R_390_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_bitfield,
177
  bfd_elf_generic_reloc, "R_390_IRELATIVE", false, 0, MINUS_ONE, false),
178
  HOWTO(R_390_PC12DBL,   1, 2, 12,  true, 0, complain_overflow_bitfield,
179
  bfd_elf_generic_reloc, "R_390_PC12DBL",  false, 0,0x00000fff, true),
180
  HOWTO(R_390_PLT12DBL,  1, 2, 12,  true, 0, complain_overflow_bitfield,
181
  bfd_elf_generic_reloc, "R_390_PLT12DBL", false, 0,0x00000fff, true),
182
  HOWTO(R_390_PC24DBL,   1, 4, 24,  true, 0, complain_overflow_bitfield,
183
  bfd_elf_generic_reloc, "R_390_PC24DBL",  false, 0,0x00ffffff, true),
184
  HOWTO(R_390_PLT24DBL,  1, 4, 24,  true, 0, complain_overflow_bitfield,
185
  bfd_elf_generic_reloc, "R_390_PLT24DBL", false, 0,0x00ffffff, true),
186
};
187
188
/* GNU extension to record C++ vtable hierarchy.  */
189
static reloc_howto_type elf64_s390_vtinherit_howto =
190
  HOWTO (R_390_GNU_VTINHERIT, 0,8,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false);
191
static reloc_howto_type elf64_s390_vtentry_howto =
192
  HOWTO (R_390_GNU_VTENTRY, 0,8,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", false,0,0, false);
193
194
static reloc_howto_type *
195
elf_s390_reloc_type_lookup (bfd *abfd,
196
          bfd_reloc_code_real_type code)
197
0
{
198
0
  switch (code)
199
0
    {
200
0
    case BFD_RELOC_NONE:
201
0
      return &elf_howto_table[(int) R_390_NONE];
202
0
    case BFD_RELOC_8:
203
0
      return &elf_howto_table[(int) R_390_8];
204
0
    case BFD_RELOC_390_12:
205
0
      return &elf_howto_table[(int) R_390_12];
206
0
    case BFD_RELOC_16:
207
0
      return &elf_howto_table[(int) R_390_16];
208
0
    case BFD_RELOC_32:
209
0
      return &elf_howto_table[(int) R_390_32];
210
0
    case BFD_RELOC_CTOR:
211
0
      return &elf_howto_table[(int) R_390_32];
212
0
    case BFD_RELOC_32_PCREL:
213
0
      return &elf_howto_table[(int) R_390_PC32];
214
0
    case BFD_RELOC_390_GOT12:
215
0
      return &elf_howto_table[(int) R_390_GOT12];
216
0
    case BFD_RELOC_32_GOT_PCREL:
217
0
      return &elf_howto_table[(int) R_390_GOT32];
218
0
    case BFD_RELOC_32_PLT_PCREL:
219
0
      return &elf_howto_table[(int) R_390_PLT32];
220
0
    case BFD_RELOC_COPY:
221
0
      return &elf_howto_table[(int) R_390_COPY];
222
0
    case BFD_RELOC_GLOB_DAT:
223
0
      return &elf_howto_table[(int) R_390_GLOB_DAT];
224
0
    case BFD_RELOC_JMP_SLOT:
225
0
      return &elf_howto_table[(int) R_390_JMP_SLOT];
226
0
    case BFD_RELOC_RELATIVE:
227
0
      return &elf_howto_table[(int) R_390_RELATIVE];
228
0
    case BFD_RELOC_32_GOTOFF:
229
0
      return &elf_howto_table[(int) R_390_GOTOFF32];
230
0
    case BFD_RELOC_390_GOTPC:
231
0
      return &elf_howto_table[(int) R_390_GOTPC];
232
0
    case BFD_RELOC_390_GOT16:
233
0
      return &elf_howto_table[(int) R_390_GOT16];
234
0
    case BFD_RELOC_16_PCREL:
235
0
      return &elf_howto_table[(int) R_390_PC16];
236
0
    case BFD_RELOC_390_PC12DBL:
237
0
      return &elf_howto_table[(int) R_390_PC12DBL];
238
0
    case BFD_RELOC_390_PLT12DBL:
239
0
      return &elf_howto_table[(int) R_390_PLT12DBL];
240
0
    case BFD_RELOC_390_PC16DBL:
241
0
      return &elf_howto_table[(int) R_390_PC16DBL];
242
0
    case BFD_RELOC_390_PLT16DBL:
243
0
      return &elf_howto_table[(int) R_390_PLT16DBL];
244
0
    case BFD_RELOC_390_PC24DBL:
245
0
      return &elf_howto_table[(int) R_390_PC24DBL];
246
0
    case BFD_RELOC_390_PLT24DBL:
247
0
      return &elf_howto_table[(int) R_390_PLT24DBL];
248
0
    case BFD_RELOC_390_PC32DBL:
249
0
      return &elf_howto_table[(int) R_390_PC32DBL];
250
0
    case BFD_RELOC_390_PLT32DBL:
251
0
      return &elf_howto_table[(int) R_390_PLT32DBL];
252
0
    case BFD_RELOC_390_GOTPCDBL:
253
0
      return &elf_howto_table[(int) R_390_GOTPCDBL];
254
0
    case BFD_RELOC_64:
255
0
      return &elf_howto_table[(int) R_390_64];
256
0
    case BFD_RELOC_64_PCREL:
257
0
      return &elf_howto_table[(int) R_390_PC64];
258
0
    case BFD_RELOC_390_GOT64:
259
0
      return &elf_howto_table[(int) R_390_GOT64];
260
0
    case BFD_RELOC_64_PLT_PCREL:
261
0
      return &elf_howto_table[(int) R_390_PLT64];
262
0
    case BFD_RELOC_390_GOTENT:
263
0
      return &elf_howto_table[(int) R_390_GOTENT];
264
0
    case BFD_RELOC_16_GOTOFF:
265
0
      return &elf_howto_table[(int) R_390_GOTOFF16];
266
0
    case BFD_RELOC_390_GOTOFF64:
267
0
      return &elf_howto_table[(int) R_390_GOTOFF64];
268
0
    case BFD_RELOC_390_GOTPLT12:
269
0
      return &elf_howto_table[(int) R_390_GOTPLT12];
270
0
    case BFD_RELOC_390_GOTPLT16:
271
0
      return &elf_howto_table[(int) R_390_GOTPLT16];
272
0
    case BFD_RELOC_390_GOTPLT32:
273
0
      return &elf_howto_table[(int) R_390_GOTPLT32];
274
0
    case BFD_RELOC_390_GOTPLT64:
275
0
      return &elf_howto_table[(int) R_390_GOTPLT64];
276
0
    case BFD_RELOC_390_GOTPLTENT:
277
0
      return &elf_howto_table[(int) R_390_GOTPLTENT];
278
0
    case BFD_RELOC_390_PLTOFF16:
279
0
      return &elf_howto_table[(int) R_390_PLTOFF16];
280
0
    case BFD_RELOC_390_PLTOFF32:
281
0
      return &elf_howto_table[(int) R_390_PLTOFF32];
282
0
    case BFD_RELOC_390_PLTOFF64:
283
0
      return &elf_howto_table[(int) R_390_PLTOFF64];
284
0
    case BFD_RELOC_390_TLS_LOAD:
285
0
      return &elf_howto_table[(int) R_390_TLS_LOAD];
286
0
    case BFD_RELOC_390_TLS_GDCALL:
287
0
      return &elf_howto_table[(int) R_390_TLS_GDCALL];
288
0
    case BFD_RELOC_390_TLS_LDCALL:
289
0
      return &elf_howto_table[(int) R_390_TLS_LDCALL];
290
0
    case BFD_RELOC_390_TLS_GD64:
291
0
      return &elf_howto_table[(int) R_390_TLS_GD64];
292
0
    case BFD_RELOC_390_TLS_GOTIE12:
293
0
      return &elf_howto_table[(int) R_390_TLS_GOTIE12];
294
0
    case BFD_RELOC_390_TLS_GOTIE64:
295
0
      return &elf_howto_table[(int) R_390_TLS_GOTIE64];
296
0
    case BFD_RELOC_390_TLS_LDM64:
297
0
      return &elf_howto_table[(int) R_390_TLS_LDM64];
298
0
    case BFD_RELOC_390_TLS_IE64:
299
0
      return &elf_howto_table[(int) R_390_TLS_IE64];
300
0
    case BFD_RELOC_390_TLS_IEENT:
301
0
      return &elf_howto_table[(int) R_390_TLS_IEENT];
302
0
    case BFD_RELOC_390_TLS_LE64:
303
0
      return &elf_howto_table[(int) R_390_TLS_LE64];
304
0
    case BFD_RELOC_390_TLS_LDO64:
305
0
      return &elf_howto_table[(int) R_390_TLS_LDO64];
306
0
    case BFD_RELOC_390_TLS_DTPMOD:
307
0
      return &elf_howto_table[(int) R_390_TLS_DTPMOD];
308
0
    case BFD_RELOC_390_TLS_DTPOFF:
309
0
      return &elf_howto_table[(int) R_390_TLS_DTPOFF];
310
0
    case BFD_RELOC_390_TLS_TPOFF:
311
0
      return &elf_howto_table[(int) R_390_TLS_TPOFF];
312
0
    case BFD_RELOC_390_20:
313
0
      return &elf_howto_table[(int) R_390_20];
314
0
    case BFD_RELOC_390_GOT20:
315
0
      return &elf_howto_table[(int) R_390_GOT20];
316
0
    case BFD_RELOC_390_GOTPLT20:
317
0
      return &elf_howto_table[(int) R_390_GOTPLT20];
318
0
    case BFD_RELOC_390_TLS_GOTIE20:
319
0
      return &elf_howto_table[(int) R_390_TLS_GOTIE20];
320
0
    case BFD_RELOC_IRELATIVE:
321
0
      return &elf_howto_table[(int) R_390_IRELATIVE];
322
0
    case BFD_RELOC_VTABLE_INHERIT:
323
0
      return &elf64_s390_vtinherit_howto;
324
0
    case BFD_RELOC_VTABLE_ENTRY:
325
0
      return &elf64_s390_vtentry_howto;
326
0
    default:
327
0
      break;
328
0
    }
329
330
  /* xgettext:c-format */
331
0
  _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
332
0
  bfd_set_error (bfd_error_bad_value);
333
0
  return NULL;
334
0
}
335
336
static reloc_howto_type *
337
elf_s390_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
338
          const char *r_name)
339
0
{
340
0
  unsigned int i;
341
342
0
  for (i = 0;
343
0
       i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]);
344
0
       i++)
345
0
    if (elf_howto_table[i].name != NULL
346
0
  && strcasecmp (elf_howto_table[i].name, r_name) == 0)
347
0
      return &elf_howto_table[i];
348
349
0
  if (strcasecmp (elf64_s390_vtinherit_howto.name, r_name) == 0)
350
0
    return &elf64_s390_vtinherit_howto;
351
0
  if (strcasecmp (elf64_s390_vtentry_howto.name, r_name) == 0)
352
0
    return &elf64_s390_vtentry_howto;
353
354
0
  return NULL;
355
0
}
356
357
/* We need to use ELF64_R_TYPE so we have our own copy of this function,
358
   and elf64-s390.c has its own copy.  */
359
360
static bool
361
elf_s390_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
362
      arelent *cache_ptr,
363
      Elf_Internal_Rela *dst)
364
0
{
365
0
  unsigned int r_type = ELF64_R_TYPE(dst->r_info);
366
367
0
  switch (r_type)
368
0
    {
369
0
    case R_390_GNU_VTINHERIT:
370
0
      cache_ptr->howto = &elf64_s390_vtinherit_howto;
371
0
      break;
372
373
0
    case R_390_GNU_VTENTRY:
374
0
      cache_ptr->howto = &elf64_s390_vtentry_howto;
375
0
      break;
376
377
0
    default:
378
0
      if (r_type >= sizeof (elf_howto_table) / sizeof (elf_howto_table[0]))
379
0
  {
380
    /* xgettext:c-format */
381
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
382
0
            abfd, r_type);
383
0
    bfd_set_error (bfd_error_bad_value);
384
0
    return false;
385
0
  }
386
0
      cache_ptr->howto = &elf_howto_table[r_type];
387
0
    }
388
0
  return true;
389
0
}
390
391
/* A relocation function which doesn't do anything.  */
392
static bfd_reloc_status_type
393
s390_tls_reloc (bfd *abfd ATTRIBUTE_UNUSED,
394
    arelent *reloc_entry,
395
    asymbol *symbol ATTRIBUTE_UNUSED,
396
    void * data ATTRIBUTE_UNUSED,
397
    asection *input_section,
398
    bfd *output_bfd,
399
    char **error_message ATTRIBUTE_UNUSED)
400
0
{
401
0
  if (output_bfd)
402
0
    reloc_entry->address += input_section->output_offset;
403
0
  return bfd_reloc_ok;
404
0
}
405
406
/* Handle the large displacement relocs.  */
407
static bfd_reloc_status_type
408
s390_elf_ldisp_reloc (bfd *abfd,
409
          arelent *reloc_entry,
410
          asymbol *symbol,
411
          void * data,
412
          asection *input_section,
413
          bfd *output_bfd,
414
          char **error_message ATTRIBUTE_UNUSED)
415
0
{
416
0
  reloc_howto_type *howto = reloc_entry->howto;
417
0
  bfd_vma relocation;
418
0
  bfd_vma insn;
419
420
0
  if (output_bfd != (bfd *) NULL
421
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
422
0
      && (! howto->partial_inplace
423
0
    || reloc_entry->addend == 0))
424
0
    {
425
0
      reloc_entry->address += input_section->output_offset;
426
0
      return bfd_reloc_ok;
427
0
    }
428
0
  if (output_bfd != NULL)
429
0
    return bfd_reloc_continue;
430
431
0
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
432
0
    return bfd_reloc_outofrange;
433
434
0
  relocation = (symbol->value
435
0
    + symbol->section->output_section->vma
436
0
    + symbol->section->output_offset);
437
0
  relocation += reloc_entry->addend;
438
0
  if (howto->pc_relative)
439
0
    {
440
0
      relocation -= (input_section->output_section->vma
441
0
         + input_section->output_offset);
442
0
      relocation -= reloc_entry->address;
443
0
    }
444
445
0
  insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
446
0
  insn |= (relocation & 0xfff) << 16 | (relocation & 0xff000) >> 4;
447
0
  bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
448
449
0
  if ((bfd_signed_vma) relocation < - 0x80000
450
0
      || (bfd_signed_vma) relocation > 0x7ffff)
451
0
    return bfd_reloc_overflow;
452
0
  else
453
0
    return bfd_reloc_ok;
454
0
}
455
456
static bool
457
elf_s390_is_local_label_name (bfd *abfd, const char *name)
458
0
{
459
0
  if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
460
0
    return true;
461
462
0
  return _bfd_elf_is_local_label_name (abfd, name);
463
0
}
464
465
/* Functions for the 390 ELF linker.  */
466
467
/* The name of the dynamic interpreter.  This is put in the .interp
468
   section.  */
469
470
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
471
472
/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
473
   copying dynamic variables from a shared lib into an app's dynbss
474
   section, and instead use a dynamic relocation to point into the
475
   shared lib.  */
476
0
#define ELIMINATE_COPY_RELOCS 1
477
478
/* The size in bytes of the first entry in the procedure linkage table.  */
479
0
#define PLT_FIRST_ENTRY_SIZE 32
480
/* The size in bytes of an entry in the procedure linkage table.  */
481
0
#define PLT_ENTRY_SIZE 32
482
483
0
#define GOT_ENTRY_SIZE 8
484
485
0
#define RELA_ENTRY_SIZE sizeof (Elf64_External_Rela)
486
487
/* The first three entries in a global offset table are reserved,
488
   and the initial contents are unimportant (we zero them out).
489
   Subsequent entries look like this.  See the SVR4 ABI 386
490
   supplement to see how this works.  */
491
492
/* For the s390, simple addr offset can only be 0 - 4096.
493
   To use the full 16777216 TB address space, several instructions
494
   are needed to load an address in a register and execute
495
   a branch( or just saving the address)
496
497
   Furthermore, only r 0 and 1 are free to use!!!  */
498
499
/* The first 3 words in the GOT are then reserved.
500
   Word 0 is the address of the dynamic table.
501
   Word 1 is a pointer to a structure describing the object
502
   Word 2 is used to point to the loader entry address.
503
504
   The code for PLT entries looks like this:
505
506
   The GOT holds the address in the PLT to be executed.
507
   The loader then gets:
508
   48(15) =  Pointer to the structure describing the object.
509
   56(15) =  Offset in symbol table
510
   The loader  must  then find the module where the function is
511
   and insert the address in the GOT.
512
513
   PLT1: LARL 1,<fn>@GOTENT # 6 bytes  Load address of GOT entry in r1
514
   LG   1,0(1)    # 6 bytes  Load address from GOT in r1
515
   BCR  15,1    # 2 bytes  Jump to address
516
   RET1: BASR 1,0   # 2 bytes  Return from GOT 1st time
517
   LGF  1,12(1)   # 6 bytes  Load rela.plt offset into r1
518
   BRCL 15,-x   # 6 bytes  Jump to first PLT entry
519
   .long ?    # 4 bytes  offset into .rela.plt
520
521
   Total = 32 bytes per PLT entry
522
   Fixup at offset 2: relative address to GOT entry
523
   Fixup at offset 22: relative branch to PLT0
524
   Fixup at offset 28: 32 bit offset into .rela.plt
525
526
   A 32 bit offset into the symbol table is enough. It allows for
527
   .rela.plt sections up to a size of 2 gigabyte.  A single dynamic
528
   object (the main program, any shared library) is limited to 4GB in
529
   size.  Having a .rela.plt of 2GB would already make the .plt
530
   section bigger than 8GB.  */
531
532
static const bfd_byte elf_s390x_plt_entry[PLT_ENTRY_SIZE] =
533
  {
534
    0xc0, 0x10, 0x00, 0x00, 0x00, 0x00,     /* larl    %r1,.     */
535
    0xe3, 0x10, 0x10, 0x00, 0x00, 0x04,     /* lg      %r1,0(%r1)  */
536
    0x07, 0xf1,           /* br      %r1     */
537
    0x0d, 0x10,           /* basr    %r1,%r0     */
538
    0xe3, 0x10, 0x10, 0x0c, 0x00, 0x14,     /* lgf     %r1,12(%r1) */
539
    0xc0, 0xf4, 0x00, 0x00, 0x00, 0x00,     /* jg      first plt   */
540
    0x00, 0x00, 0x00, 0x00        /* .long   0x00000000  */
541
  };
542
543
/* The first PLT entry pushes the offset into the symbol table
544
   from R1 onto the stack at 56(15) and the loader object info
545
   at 48(15), loads the loader address in R1 and jumps to it.  */
546
547
/* The first entry in the PLT:
548
549
  PLT0:
550
     STG  1,56(15)  # r1 contains the offset into the symbol table
551
     LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table
552
     MVC  48(8,15),8(1) # move loader ino (object struct address) to stack
553
     LG   1,16(1)   # get entry address of loader
554
     BCR  15,1      # jump to loader
555
556
     Fixup at offset 8: relative address to start of GOT.  */
557
558
static const bfd_byte elf_s390x_first_plt_entry[PLT_FIRST_ENTRY_SIZE] =
559
  {
560
    0xe3, 0x10, 0xf0, 0x38, 0x00, 0x24,     /* stg     %r1,56(%r15)  */
561
    0xc0, 0x10, 0x00, 0x00, 0x00, 0x00,     /* larl    %r1,.     */
562
    0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08,     /* mvc     48(8,%r15),8(%r1) */
563
    0xe3, 0x10, 0x10, 0x10, 0x00, 0x04,     /* lg      %r1,16(%r1)   */
564
    0x07, 0xf1,           /* br      %r1     */
565
    0x07, 0x00,           /* nopr    %r0     */
566
    0x07, 0x00,           /* nopr    %r0     */
567
    0x07, 0x00            /* nopr    %r0     */
568
  };
569
570
/* .eh_frame covering the .plt section.  */
571
572
0
#define PLT_CIE_SIZE    24
573
#define PLT_FDE_SIZE    20
574
0
#define PLT_FDE_START_OFFSET  (PLT_CIE_SIZE + 8)
575
#define PLT_FDE_LEN_OFFSET  (PLT_CIE_SIZE + 12)
576
577
static const bfd_byte elf_s390x_eh_frame_plt[] =
578
{
579
  0, 0, 0, PLT_CIE_SIZE - 4,  /* CIE length */
580
  0, 0, 0, 0,     /* CIE ID */
581
  1,        /* CIE version */
582
  'z', 'R', 0,      /* Augmentation string */
583
  1,        /* Code alignment factor */
584
  0x78,       /* Data alignment factor */
585
  14,       /* Return address column */
586
  1,        /* Augmentation size */
587
  DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
588
  DW_CFA_def_cfa, 15, 0xa0, 0x01, /* DW_CFA_def_cfa: r15 ofs 160 */
589
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
590
591
  0, 0, 0, PLT_FDE_SIZE - 4,  /* FDE length */
592
  0, 0, 0, PLT_CIE_SIZE + 4,  /* CIE pointer */
593
  0, 0, 0, 0,     /* R_S390_PC32 .plt goes here */
594
  0, 0, 0, 0,     /* .plt size goes here */
595
  0,        /* Augmentation size */
596
  DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
597
};
598
599
/* .sframe covering the .plt section.  */
600
601
/* This must be the same as sframe_get_hdr_size (sfh).  For s390x, this value
602
   is the same as sizeof (sframe_header) because there is no SFrame auxilliary
603
   header.  */
604
0
#define PLT_SFRAME_FDE_START_OFFSET sizeof (sframe_header)
605
606
#define SFRAME_PLT0_MAX_NUM_FRES 1
607
#define SFRAME_PLTN_MAX_NUM_FRES 1
608
609
struct elf_s390x_sframe_plt
610
{
611
  unsigned int plt0_entry_size;
612
  unsigned int plt0_num_fres;
613
  const sframe_frame_row_entry *plt0_fres[SFRAME_PLT0_MAX_NUM_FRES];
614
615
  unsigned int pltn_entry_size;
616
  unsigned int pltn_num_fres;
617
  const sframe_frame_row_entry *pltn_fres[SFRAME_PLTN_MAX_NUM_FRES];
618
};
619
620
/* .sframe FRE covering the PLT0/PLTn .plt section entry.  */
621
static const sframe_frame_row_entry elf_s390x_sframe_plt_fre =
622
{
623
  0, /* SFrame FRE start address.  */
624
  { SFRAME_V2_S390X_CFA_OFFSET_ENCODE (160), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* Offset bytes.  */
625
  SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info.  */
626
};
627
628
/* SFrame helper object for PLT.  */
629
static const struct elf_s390x_sframe_plt elf_s390x_sframe_plt =
630
{
631
  PLT_FIRST_ENTRY_SIZE,
632
  1, /* Number of FREs for PLT0.  */
633
  /* Array of SFrame FREs for PLT0.  */
634
  { &elf_s390x_sframe_plt_fre },
635
636
  PLT_ENTRY_SIZE,
637
  1, /* Number of FREs for PLTn.  */
638
  /* Array of SFrame FREs for PLTn.  */
639
  { &elf_s390x_sframe_plt_fre },
640
};
641
642
643
/* s390 ELF linker hash entry.  */
644
645
struct elf_s390_link_hash_entry
646
{
647
  struct elf_link_hash_entry elf;
648
649
  /* Number of GOTPLT references for a function.  */
650
  bfd_signed_vma gotplt_refcount;
651
652
0
#define GOT_UNKNOWN 0
653
0
#define GOT_NORMAL  1
654
0
#define GOT_TLS_GD  2
655
0
#define GOT_TLS_IE  3
656
0
#define GOT_TLS_IE_NLT  4  /* Initial Exec, no literal pool entry.  */
657
  unsigned char tls_type;
658
659
  /* For pointer equality reasons we might need to change the symbol
660
     type from STT_GNU_IFUNC to STT_FUNC together with its value and
661
     section entry.  So after alloc_dynrelocs only these values should
662
     be used.  In order to check whether a symbol is IFUNC use
663
     s390_is_ifunc_symbol_p.  */
664
  bfd_vma ifunc_resolver_address;
665
  asection *ifunc_resolver_section;
666
};
667
668
#define elf_s390_hash_entry(ent) \
669
0
  ((struct elf_s390_link_hash_entry *)(ent))
670
671
/* This structure represents an entry in the local PLT list needed for
672
   local IFUNC symbols.  */
673
struct plt_entry
674
{
675
  /* The section of the local symbol.
676
     Set in relocate_section and used in finish_dynamic_sections.  */
677
  asection *sec;
678
679
  union
680
    {
681
      bfd_signed_vma refcount;
682
      bfd_vma offset;
683
    } plt;
684
};
685
686
/* NOTE: Keep this structure in sync with
687
   the one declared in elf32-s390.c.  */
688
struct elf_s390_obj_tdata
689
{
690
  struct elf_obj_tdata root;
691
692
  /* A local PLT is needed for ifunc symbols.  */
693
  struct plt_entry *local_plt;
694
695
  /* TLS type for each local got entry.  */
696
  char *local_got_tls_type;
697
};
698
699
#define elf_s390_tdata(abfd) \
700
0
  ((struct elf_s390_obj_tdata *) (abfd)->tdata.any)
701
702
#define elf_s390_local_plt(abfd) \
703
0
  (elf_s390_tdata (abfd)->local_plt)
704
705
#define elf_s390_local_got_tls_type(abfd) \
706
0
  (elf_s390_tdata (abfd)->local_got_tls_type)
707
708
#define is_s390_elf(bfd)        \
709
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
710
0
   && elf_tdata (bfd) != NULL        \
711
0
   && elf_object_id (bfd) == S390_ELF_DATA)
712
713
static bool
714
elf_s390_mkobject (bfd *abfd)
715
1.93k
{
716
1.93k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_s390_obj_tdata));
717
1.93k
}
718
719
static bool
720
elf_s390_object_p (bfd *abfd)
721
6
{
722
  /* Set the right machine number for an s390 elf32 file.  */
723
6
  return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_64);
724
6
}
725
726
/* s390 ELF linker hash table.  */
727
728
struct elf_s390_link_hash_table
729
{
730
  struct elf_link_hash_table elf;
731
732
  /* Short-cuts to get to dynamic linker sections.  */
733
  asection *irelifunc;
734
  asection *plt_eh_frame;
735
736
  sframe_encoder_ctx *plt_cfe_ctx;
737
  asection *plt_sframe;
738
  /* The .sframe helper object for .plt section.  */
739
  const struct elf_s390x_sframe_plt *sframe_plt;
740
741
  union {
742
    bfd_signed_vma refcount;
743
    bfd_vma offset;
744
  } tls_ldm_got;
745
746
  /* Options passed from the linker.  */
747
  struct s390_elf_params *params;
748
};
749
750
/* Get the s390 ELF linker hash table from a link_info structure.  */
751
752
#define elf_s390_hash_table(p)            \
753
0
  ((is_elf_hash_table ((p)->hash)          \
754
0
    && elf_hash_table_id (elf_hash_table (p)) == S390_ELF_DATA)   \
755
0
   ? (struct elf_s390_link_hash_table *) (p)->hash : NULL)
756
757
#define ELF64 1
758
#include "elf-s390-common.c"
759
760
/* Create an entry in an s390 ELF linker hash table.  */
761
762
static struct bfd_hash_entry *
763
link_hash_newfunc (struct bfd_hash_entry *entry,
764
       struct bfd_hash_table *table,
765
       const char *string)
766
0
{
767
  /* Allocate the structure if it has not already been allocated by a
768
     subclass.  */
769
0
  if (entry == NULL)
770
0
    {
771
0
      entry = bfd_hash_allocate (table,
772
0
         sizeof (struct elf_s390_link_hash_entry));
773
0
      if (entry == NULL)
774
0
  return entry;
775
0
    }
776
777
  /* Call the allocation method of the superclass.  */
778
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
779
0
  if (entry != NULL)
780
0
    {
781
0
      struct elf_s390_link_hash_entry *eh;
782
783
0
      eh = (struct elf_s390_link_hash_entry *) entry;
784
0
      eh->gotplt_refcount = 0;
785
0
      eh->tls_type = GOT_UNKNOWN;
786
0
      eh->ifunc_resolver_address = 0;
787
0
      eh->ifunc_resolver_section = NULL;
788
0
    }
789
790
0
  return entry;
791
0
}
792
793
static void
794
elf_s390_link_hash_table_free (bfd *obfd)
795
0
{
796
0
  struct elf_s390_link_hash_table *htab
797
0
    = (struct elf_s390_link_hash_table *) obfd->link.hash;
798
0
  sframe_encoder_free (&htab->plt_cfe_ctx);
799
0
  _bfd_elf_link_hash_table_free (obfd);
800
0
}
801
802
/* Create an s390 ELF linker hash table.  */
803
804
static struct bfd_link_hash_table *
805
elf_s390_link_hash_table_create (bfd *abfd)
806
0
{
807
0
  struct elf_s390_link_hash_table *ret;
808
809
0
  ret = bfd_zmalloc (sizeof (*ret));
810
0
  if (ret == NULL)
811
0
    return NULL;
812
813
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
814
0
              sizeof (struct elf_s390_link_hash_entry)))
815
0
    {
816
0
      free (ret);
817
0
      return NULL;
818
0
    }
819
0
  ret->elf.root.hash_table_free = elf_s390_link_hash_table_free;
820
821
0
  return &ret->elf.root;
822
0
}
823
824
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
825
826
static void
827
elf_s390_copy_indirect_symbol (struct bfd_link_info *info,
828
             struct elf_link_hash_entry *dir,
829
             struct elf_link_hash_entry *ind)
830
0
{
831
0
  struct elf_s390_link_hash_entry *edir, *eind;
832
833
0
  edir = (struct elf_s390_link_hash_entry *) dir;
834
0
  eind = (struct elf_s390_link_hash_entry *) ind;
835
836
0
  if (ind->root.type == bfd_link_hash_indirect
837
0
      && dir->got.refcount <= 0)
838
0
    {
839
0
      edir->tls_type = eind->tls_type;
840
0
      eind->tls_type = GOT_UNKNOWN;
841
0
    }
842
843
0
  if (ELIMINATE_COPY_RELOCS
844
0
      && ind->root.type != bfd_link_hash_indirect
845
0
      && dir->dynamic_adjusted)
846
0
    {
847
      /* If called to transfer flags for a weakdef during processing
848
   of elf_adjust_dynamic_symbol, don't copy non_got_ref.
849
   We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
850
0
      if (dir->versioned != versioned_hidden)
851
0
  dir->ref_dynamic |= ind->ref_dynamic;
852
0
      dir->ref_regular |= ind->ref_regular;
853
0
      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
854
0
      dir->needs_plt |= ind->needs_plt;
855
0
      dir->pointer_equality_needed |= ind->pointer_equality_needed;
856
0
    }
857
0
  else
858
0
    _bfd_elf_link_hash_copy_indirect (info, dir, ind);
859
0
}
860
861
static int
862
elf_s390_tls_transition (struct bfd_link_info *info,
863
       int r_type,
864
       int is_local)
865
0
{
866
0
  if (bfd_link_dll (info))
867
0
    return r_type;
868
869
0
  switch (r_type)
870
0
    {
871
0
    case R_390_TLS_GD64:
872
0
    case R_390_TLS_IE64:
873
0
      if (is_local)
874
0
  return R_390_TLS_LE64;
875
0
      return R_390_TLS_IE64;
876
0
    case R_390_TLS_GOTIE64:
877
0
      if (is_local)
878
0
  return R_390_TLS_LE64;
879
0
      return R_390_TLS_GOTIE64;
880
0
    case R_390_TLS_LDM64:
881
0
      return R_390_TLS_LE64;
882
0
    }
883
884
0
  return r_type;
885
0
}
886
887
/* Look through the relocs for a section during the first phase, and
888
   allocate space in the global offset table or procedure linkage
889
   table.  */
890
891
static bool
892
elf_s390_check_relocs (bfd *abfd,
893
           struct bfd_link_info *info,
894
           asection *sec,
895
           const Elf_Internal_Rela *relocs)
896
0
{
897
0
  struct elf_s390_link_hash_table *htab;
898
0
  Elf_Internal_Shdr *symtab_hdr;
899
0
  struct elf_link_hash_entry **sym_hashes;
900
0
  const Elf_Internal_Rela *rel;
901
0
  const Elf_Internal_Rela *rel_end;
902
0
  asection *sreloc;
903
0
  bfd_signed_vma *local_got_refcounts;
904
0
  int tls_type, old_tls_type;
905
906
0
  if (bfd_link_relocatable (info))
907
0
    return true;
908
909
0
  BFD_ASSERT (is_s390_elf (abfd));
910
911
0
  htab = elf_s390_hash_table (info);
912
0
  if (htab == NULL)
913
0
    return false;
914
915
0
  symtab_hdr = &elf_symtab_hdr (abfd);
916
0
  sym_hashes = elf_sym_hashes (abfd);
917
0
  local_got_refcounts = elf_local_got_refcounts (abfd);
918
919
0
  sreloc = NULL;
920
921
0
  rel_end = relocs + sec->reloc_count;
922
0
  for (rel = relocs; rel < rel_end; rel++)
923
0
    {
924
0
      unsigned int r_type;
925
0
      unsigned int r_symndx;
926
0
      struct elf_link_hash_entry *h;
927
0
      Elf_Internal_Sym *isym;
928
929
0
      r_symndx = ELF64_R_SYM (rel->r_info);
930
931
0
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
932
0
  {
933
    /* xgettext:c-format */
934
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"),
935
0
            abfd, r_symndx);
936
0
    return false;
937
0
  }
938
939
0
      if (r_symndx < symtab_hdr->sh_info)
940
0
  {
941
    /* A local symbol.  */
942
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
943
0
          abfd, r_symndx);
944
0
    if (isym == NULL)
945
0
      return false;
946
947
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
948
0
      {
949
0
        struct plt_entry *plt;
950
951
0
        if (htab->elf.dynobj == NULL)
952
0
    htab->elf.dynobj = abfd;
953
954
0
        if (!s390_elf_create_ifunc_sections (htab->elf.dynobj, info))
955
0
    return false;
956
957
0
        if (local_got_refcounts == NULL)
958
0
    {
959
0
      if (!elf_s390_allocate_local_syminfo (abfd, symtab_hdr))
960
0
        return false;
961
0
      local_got_refcounts = elf_local_got_refcounts (abfd);
962
0
    }
963
0
        plt = elf_s390_local_plt (abfd);
964
0
        plt[r_symndx].plt.refcount++;
965
0
      }
966
0
    h = NULL;
967
0
  }
968
0
      else
969
0
  {
970
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
971
0
    while (h->root.type == bfd_link_hash_indirect
972
0
     || h->root.type == bfd_link_hash_warning)
973
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
974
0
  }
975
976
      /* Create got section and local_got_refcounts array if they
977
   are needed.  */
978
0
      r_type = elf_s390_tls_transition (info,
979
0
          ELF64_R_TYPE (rel->r_info),
980
0
          h == NULL);
981
0
      switch (r_type)
982
0
  {
983
0
  case R_390_GOT12:
984
0
  case R_390_GOT16:
985
0
  case R_390_GOT20:
986
0
  case R_390_GOT32:
987
0
  case R_390_GOT64:
988
0
  case R_390_GOTENT:
989
0
  case R_390_GOTPLT12:
990
0
  case R_390_GOTPLT16:
991
0
  case R_390_GOTPLT20:
992
0
  case R_390_GOTPLT32:
993
0
  case R_390_GOTPLT64:
994
0
  case R_390_GOTPLTENT:
995
0
  case R_390_TLS_GD64:
996
0
  case R_390_TLS_GOTIE12:
997
0
  case R_390_TLS_GOTIE20:
998
0
  case R_390_TLS_GOTIE64:
999
0
  case R_390_TLS_IEENT:
1000
0
  case R_390_TLS_IE64:
1001
0
  case R_390_TLS_LDM64:
1002
0
    if (h == NULL
1003
0
        && local_got_refcounts == NULL)
1004
0
      {
1005
0
        if (!elf_s390_allocate_local_syminfo (abfd, symtab_hdr))
1006
0
    return false;
1007
0
        local_got_refcounts = elf_local_got_refcounts (abfd);
1008
0
      }
1009
1010
    /* Fall through.  */
1011
0
  case R_390_GOTOFF16:
1012
0
  case R_390_GOTOFF32:
1013
0
  case R_390_GOTOFF64:
1014
0
  case R_390_GOTPC:
1015
0
  case R_390_GOTPCDBL:
1016
0
    if (htab->elf.sgot == NULL)
1017
0
      {
1018
0
        if (htab->elf.dynobj == NULL)
1019
0
    htab->elf.dynobj = abfd;
1020
0
        if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
1021
0
    return false;
1022
0
      }
1023
0
  }
1024
1025
0
      if (h != NULL)
1026
0
  {
1027
0
    if (htab->elf.dynobj == NULL)
1028
0
      htab->elf.dynobj = abfd;
1029
0
    if (!s390_elf_create_ifunc_sections (htab->elf.dynobj, info))
1030
0
      return false;
1031
1032
    /* Make sure an IFUNC symbol defined in a non-shared object
1033
       always gets a PLT slot.  */
1034
0
    if (s390_is_ifunc_symbol_p (h) && h->def_regular)
1035
0
      {
1036
        /* The symbol is called by the dynamic loader in order
1037
     to resolve the relocation.  So it is in fact also
1038
     referenced.  */
1039
0
        h->ref_regular = 1;
1040
0
        h->needs_plt = 1;
1041
0
        h->pointer_equality_needed = 1;
1042
0
      }
1043
0
  }
1044
1045
0
      switch (r_type)
1046
0
  {
1047
0
  case R_390_GOTPC:
1048
0
  case R_390_GOTPCDBL:
1049
    /* These relocs do not need a GOT slot.  They just load the
1050
       GOT pointer itself or address something else relative to
1051
       the GOT.  Since the GOT pointer has been set up above we
1052
       are done.  */
1053
0
    break;
1054
0
  case R_390_GOTOFF16:
1055
0
  case R_390_GOTOFF32:
1056
0
  case R_390_GOTOFF64:
1057
0
    if (h == NULL || !s390_is_ifunc_symbol_p (h) || !h->def_regular)
1058
0
      break;
1059
    /* Fall through.  */
1060
1061
0
  case R_390_PLT12DBL:
1062
0
  case R_390_PLT16DBL:
1063
0
  case R_390_PLT24DBL:
1064
0
  case R_390_PLT32:
1065
0
  case R_390_PLT32DBL:
1066
0
  case R_390_PLT64:
1067
0
  case R_390_PLTOFF16:
1068
0
  case R_390_PLTOFF32:
1069
0
  case R_390_PLTOFF64:
1070
    /* This symbol requires a procedure linkage table entry.  We
1071
       actually build the entry in adjust_dynamic_symbol,
1072
       because this might be a case of linking PIC code which is
1073
       never referenced by a dynamic object, in which case we
1074
       don't need to generate a procedure linkage table entry
1075
       after all.  */
1076
1077
    /* If this is a local symbol, we resolve it directly without
1078
       creating a procedure linkage table entry.  */
1079
0
    if (h != NULL)
1080
0
      {
1081
0
        h->needs_plt = 1;
1082
0
        h->plt.refcount += 1;
1083
1084
        /* GCC 12-14 unconditionally suffix non-local symbols
1085
     with @PLT, regardless of whether they are used in
1086
     function call instructions (i.e. brasl) or address
1087
     taking instructions (i.e. larl).  Treat PLT32DBL
1088
     relocation for "larl rX,<sym>@PLT" instruction as
1089
     address taking and require pointer equality.  */
1090
0
        if (bfd_link_executable (info)
1091
0
      && r_type == R_390_PLT32DBL
1092
0
      && rel->r_offset >= 2)
1093
0
    {
1094
0
      bfd_byte *contents;
1095
0
      void *insn_start;
1096
0
      uint16_t op;
1097
1098
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
1099
0
        contents = elf_section_data (sec)->this_hdr.contents;
1100
0
      else if (!_bfd_elf_mmap_section_contents (abfd, sec, &contents))
1101
0
        return false;
1102
1103
0
      insn_start = contents + rel->r_offset - 2;
1104
0
      op = bfd_get_16 (abfd, insn_start) & 0xff0f;
1105
1106
0
      if (op == 0xc000)
1107
0
        {
1108
          /* larl rX,<sym>@PLT  */
1109
0
          h->pointer_equality_needed = 1;
1110
0
        }
1111
1112
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
1113
0
        _bfd_elf_munmap_section_contents (sec, contents);
1114
0
    }
1115
0
      }
1116
0
    break;
1117
1118
0
  case R_390_GOTPLT12:
1119
0
  case R_390_GOTPLT16:
1120
0
  case R_390_GOTPLT20:
1121
0
  case R_390_GOTPLT32:
1122
0
  case R_390_GOTPLT64:
1123
0
  case R_390_GOTPLTENT:
1124
    /* This symbol requires either a procedure linkage table entry
1125
       or an entry in the local got. We actually build the entry
1126
       in adjust_dynamic_symbol because whether this is really a
1127
       global reference can change and with it the fact if we have
1128
       to create a plt entry or a local got entry. To be able to
1129
       make a once global symbol a local one we have to keep track
1130
       of the number of gotplt references that exist for this
1131
       symbol.  */
1132
0
    if (h != NULL)
1133
0
      {
1134
0
        ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount++;
1135
0
        h->needs_plt = 1;
1136
0
        h->plt.refcount += 1;
1137
0
      }
1138
0
    else
1139
0
      local_got_refcounts[r_symndx] += 1;
1140
0
    break;
1141
1142
0
  case R_390_TLS_LDM64:
1143
0
    htab->tls_ldm_got.refcount += 1;
1144
0
    break;
1145
1146
0
  case R_390_TLS_IE64:
1147
0
  case R_390_TLS_GOTIE12:
1148
0
  case R_390_TLS_GOTIE20:
1149
0
  case R_390_TLS_GOTIE64:
1150
0
  case R_390_TLS_IEENT:
1151
0
    if (bfd_link_dll (info))
1152
0
      info->flags |= DF_STATIC_TLS;
1153
    /* Fall through */
1154
1155
0
  case R_390_GOT12:
1156
0
  case R_390_GOT16:
1157
0
  case R_390_GOT20:
1158
0
  case R_390_GOT32:
1159
0
  case R_390_GOT64:
1160
0
  case R_390_GOTENT:
1161
0
  case R_390_TLS_GD64:
1162
    /* This symbol requires a global offset table entry.  */
1163
0
    switch (r_type)
1164
0
      {
1165
0
      default:
1166
0
      case R_390_GOT12:
1167
0
      case R_390_GOT16:
1168
0
      case R_390_GOT20:
1169
0
      case R_390_GOT32:
1170
0
      case R_390_GOTENT:
1171
0
        tls_type = GOT_NORMAL;
1172
0
        break;
1173
0
      case R_390_TLS_GD64:
1174
0
        tls_type = GOT_TLS_GD;
1175
0
        break;
1176
0
      case R_390_TLS_IE64:
1177
0
      case R_390_TLS_GOTIE64:
1178
0
        tls_type = GOT_TLS_IE;
1179
0
        break;
1180
0
      case R_390_TLS_GOTIE12:
1181
0
      case R_390_TLS_GOTIE20:
1182
0
      case R_390_TLS_IEENT:
1183
0
        tls_type = GOT_TLS_IE_NLT;
1184
0
        break;
1185
0
      }
1186
1187
0
    if (h != NULL)
1188
0
      {
1189
0
        h->got.refcount += 1;
1190
0
        old_tls_type = elf_s390_hash_entry(h)->tls_type;
1191
0
      }
1192
0
    else
1193
0
      {
1194
0
        local_got_refcounts[r_symndx] += 1;
1195
0
        old_tls_type = elf_s390_local_got_tls_type (abfd) [r_symndx];
1196
0
      }
1197
    /* If a TLS symbol is accessed using IE at least once,
1198
       there is no point to use dynamic model for it.  */
1199
0
    if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN)
1200
0
      {
1201
0
        if (old_tls_type == GOT_NORMAL || tls_type == GOT_NORMAL)
1202
0
    {
1203
0
      _bfd_error_handler
1204
        /* xgettext:c-format */
1205
0
        (_("%pB: `%s' accessed both as normal and thread local symbol"),
1206
0
         abfd, h->root.root.string);
1207
0
      return false;
1208
0
    }
1209
0
        if (old_tls_type > tls_type)
1210
0
    tls_type = old_tls_type;
1211
0
      }
1212
1213
0
    if (old_tls_type != tls_type)
1214
0
      {
1215
0
        if (h != NULL)
1216
0
    elf_s390_hash_entry (h)->tls_type = tls_type;
1217
0
        else
1218
0
    elf_s390_local_got_tls_type (abfd) [r_symndx] = tls_type;
1219
0
      }
1220
1221
0
    if (r_type != R_390_TLS_IE64)
1222
0
      break;
1223
    /* Fall through */
1224
1225
0
  case R_390_TLS_LE64:
1226
    /* For static linking and executables this reloc will be
1227
       calculated at linktime otherwise a TLS_TPOFF runtime
1228
       reloc will be generated.  */
1229
0
    if (r_type == R_390_TLS_LE64 && bfd_link_pie (info))
1230
0
      break;
1231
1232
0
    if (!bfd_link_dll (info))
1233
0
      break;
1234
0
    info->flags |= DF_STATIC_TLS;
1235
    /* Fall through */
1236
1237
0
  case R_390_8:
1238
0
  case R_390_16:
1239
0
  case R_390_32:
1240
0
  case R_390_64:
1241
0
  case R_390_PC12DBL:
1242
0
  case R_390_PC16:
1243
0
  case R_390_PC16DBL:
1244
0
  case R_390_PC24DBL:
1245
0
  case R_390_PC32:
1246
0
  case R_390_PC32DBL:
1247
0
  case R_390_PC64:
1248
0
    if (h != NULL && bfd_link_executable (info))
1249
0
      {
1250
        /* If this reloc is in a read-only section, we might
1251
     need a copy reloc.  We can't check reliably at this
1252
     stage whether the section is read-only, as input
1253
     sections have not yet been mapped to output sections.
1254
     Tentatively set the flag for now, and correct in
1255
     adjust_dynamic_symbol.  */
1256
0
        h->non_got_ref = 1;
1257
1258
0
        if (!bfd_link_pic (info))
1259
0
    {
1260
      /* We may need a .plt entry if the function this reloc
1261
         refers to is in a shared lib.  */
1262
0
      h->plt.refcount += 1;
1263
0
    }
1264
1265
        /* Require pointer equality in PDE for above PC-relative
1266
     relocations, that are likely in address taken context,
1267
     and direct relocations, that are likely in function
1268
     reference context.  */
1269
0
        h->pointer_equality_needed = 1;
1270
0
      }
1271
1272
    /* If we are creating a shared library, and this is a reloc
1273
       against a global symbol, or a non PC relative reloc
1274
       against a local symbol, then we need to copy the reloc
1275
       into the shared library.  However, if we are linking with
1276
       -Bsymbolic, we do not need to copy a reloc against a
1277
       global symbol which is defined in an object we are
1278
       including in the link (i.e., DEF_REGULAR is set).  At
1279
       this point we have not seen all the input files, so it is
1280
       possible that DEF_REGULAR is not set now but will be set
1281
       later (it is never cleared).  In case of a weak definition,
1282
       DEF_REGULAR may be cleared later by a strong definition in
1283
       a shared library. We account for that possibility below by
1284
       storing information in the relocs_copied field of the hash
1285
       table entry.  A similar situation occurs when creating
1286
       shared libraries and symbol visibility changes render the
1287
       symbol local.
1288
1289
       If on the other hand, we are creating an executable, we
1290
       may need to keep relocations for symbols satisfied by a
1291
       dynamic library if we manage to avoid copy relocs for the
1292
       symbol.  */
1293
0
    if ((bfd_link_pic (info)
1294
0
         && (sec->flags & SEC_ALLOC) != 0
1295
0
         && ((ELF64_R_TYPE (rel->r_info) != R_390_PC16
1296
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC12DBL
1297
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC16DBL
1298
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC24DBL
1299
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC32
1300
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC32DBL
1301
0
        && ELF64_R_TYPE (rel->r_info) != R_390_PC64)
1302
0
       || (h != NULL
1303
0
           && (! SYMBOLIC_BIND (info, h)
1304
0
         || h->root.type == bfd_link_hash_defweak
1305
0
         || !h->def_regular))))
1306
0
        || (ELIMINATE_COPY_RELOCS
1307
0
      && !bfd_link_pic (info)
1308
0
      && (sec->flags & SEC_ALLOC) != 0
1309
0
      && h != NULL
1310
0
      && (h->root.type == bfd_link_hash_defweak
1311
0
          || !h->def_regular)))
1312
0
      {
1313
0
        struct elf_dyn_relocs *p;
1314
0
        struct elf_dyn_relocs **head;
1315
1316
        /* We must copy these reloc types into the output file.
1317
     Create a reloc section in dynobj and make room for
1318
     this reloc.  */
1319
0
        if (sreloc == NULL)
1320
0
    {
1321
0
      if (htab->elf.dynobj == NULL)
1322
0
        htab->elf.dynobj = abfd;
1323
1324
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
1325
0
        (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
1326
1327
0
      if (sreloc == NULL)
1328
0
        return false;
1329
0
    }
1330
1331
        /* If this is a global symbol, we count the number of
1332
     relocations we need for this symbol.  */
1333
0
        if (h != NULL)
1334
0
    {
1335
0
      head = &h->dyn_relocs;
1336
0
    }
1337
0
        else
1338
0
    {
1339
      /* Track dynamic relocs needed for local syms too.
1340
         We really need local syms available to do this
1341
         easily.  Oh well.  */
1342
0
      asection *s;
1343
0
      void *vpp;
1344
1345
0
      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1346
0
            abfd, r_symndx);
1347
0
      if (isym == NULL)
1348
0
        return false;
1349
1350
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1351
0
      if (s == NULL)
1352
0
        s = sec;
1353
1354
0
      vpp = &elf_section_data (s)->local_dynrel;
1355
0
      head = (struct elf_dyn_relocs **) vpp;
1356
0
    }
1357
1358
0
        p = *head;
1359
0
        if (p == NULL || p->sec != sec)
1360
0
    {
1361
0
      size_t amt = sizeof *p;
1362
0
      p = ((struct elf_dyn_relocs *)
1363
0
           bfd_alloc (htab->elf.dynobj, amt));
1364
0
      if (p == NULL)
1365
0
        return false;
1366
0
      p->next = *head;
1367
0
      *head = p;
1368
0
      p->sec = sec;
1369
0
      p->count = 0;
1370
0
      p->pc_count = 0;
1371
0
    }
1372
1373
0
        p->count += 1;
1374
0
        if (ELF64_R_TYPE (rel->r_info) == R_390_PC16
1375
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC12DBL
1376
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL
1377
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC24DBL
1378
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC32
1379
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL
1380
0
      || ELF64_R_TYPE (rel->r_info) == R_390_PC64)
1381
0
    p->pc_count += 1;
1382
0
      }
1383
0
    break;
1384
1385
    /* This relocation describes the C++ object vtable hierarchy.
1386
       Reconstruct it for later use during GC.  */
1387
0
  case R_390_GNU_VTINHERIT:
1388
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1389
0
      return false;
1390
0
    break;
1391
1392
    /* This relocation describes which C++ vtable entries are actually
1393
       used.  Record for later use during GC.  */
1394
0
  case R_390_GNU_VTENTRY:
1395
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1396
0
      return false;
1397
0
    break;
1398
1399
0
  default:
1400
0
    break;
1401
0
  }
1402
0
    }
1403
1404
0
  return true;
1405
0
}
1406
1407
/* Return the section that should be marked against GC for a given
1408
   relocation.  */
1409
1410
static asection *
1411
elf_s390_gc_mark_hook (asection *sec,
1412
           struct bfd_link_info *info,
1413
           struct elf_reloc_cookie *cookie,
1414
           struct elf_link_hash_entry *h,
1415
           unsigned int symndx)
1416
0
{
1417
0
  if (h != NULL)
1418
0
    switch (ELF64_R_TYPE (cookie->rel->r_info))
1419
0
      {
1420
0
      case R_390_GNU_VTINHERIT:
1421
0
      case R_390_GNU_VTENTRY:
1422
0
  return NULL;
1423
0
      }
1424
1425
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
1426
0
}
1427
1428
/* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1429
   entry but we found we will not create any.  Called when we find we will
1430
   not have any PLT for this symbol, by for example
1431
   elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link,
1432
   or elf_s390_late_size_sections if no dynamic sections will be
1433
   created (we're only linking static objects).  */
1434
1435
static void
1436
elf_s390_adjust_gotplt (struct elf_s390_link_hash_entry *h)
1437
0
{
1438
0
  if (h->elf.root.type == bfd_link_hash_warning)
1439
0
    h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1440
1441
0
  if (h->gotplt_refcount <= 0)
1442
0
    return;
1443
1444
  /* We simply add the number of gotplt references to the number
1445
   * of got references for this symbol.  */
1446
0
  h->elf.got.refcount += h->gotplt_refcount;
1447
0
  h->gotplt_refcount = -1;
1448
0
}
1449
1450
/* Adjust a symbol defined by a dynamic object and referenced by a
1451
   regular object.  The current definition is in some section of the
1452
   dynamic object, but we're not including those sections.  We have to
1453
   change the definition to something the rest of the link can
1454
   understand.  */
1455
1456
static bool
1457
elf_s390_adjust_dynamic_symbol (struct bfd_link_info *info,
1458
        struct elf_link_hash_entry *h)
1459
0
{
1460
0
  struct elf_s390_link_hash_table *htab;
1461
0
  asection *s, *srel;
1462
1463
  /* STT_GNU_IFUNC symbol must go through PLT. */
1464
0
  if (s390_is_ifunc_symbol_p (h))
1465
0
    {
1466
      /* All local STT_GNU_IFUNC references must be treated as local
1467
   calls via local PLT.  */
1468
0
      if (h->ref_regular && SYMBOL_CALLS_LOCAL (info, h))
1469
0
  {
1470
0
    bfd_size_type pc_count = 0, count = 0;
1471
0
    struct elf_dyn_relocs **pp;
1472
0
    struct elf_dyn_relocs *p;
1473
1474
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1475
0
      {
1476
0
        pc_count += p->pc_count;
1477
0
        p->count -= p->pc_count;
1478
0
        p->pc_count = 0;
1479
0
        count += p->count;
1480
0
        if (p->count == 0)
1481
0
    *pp = p->next;
1482
0
        else
1483
0
    pp = &p->next;
1484
0
      }
1485
1486
0
    if (pc_count || count)
1487
0
      {
1488
0
        h->needs_plt = 1;
1489
0
        h->non_got_ref = 1;
1490
0
        if (h->plt.refcount <= 0)
1491
0
    h->plt.refcount = 1;
1492
0
        else
1493
0
    h->plt.refcount += 1;
1494
0
      }
1495
0
  }
1496
1497
0
      if (h->plt.refcount <= 0)
1498
0
  {
1499
0
    h->plt.offset = (bfd_vma) -1;
1500
0
    h->needs_plt = 0;
1501
0
  }
1502
0
      return true;
1503
0
    }
1504
1505
  /* If this is a function, put it in the procedure linkage table.  We
1506
     will fill in the contents of the procedure linkage table later
1507
     (although we could actually do it here).  */
1508
0
  if (h->type == STT_FUNC
1509
0
      || h->needs_plt)
1510
0
    {
1511
0
      if (h->plt.refcount <= 0
1512
0
    || SYMBOL_CALLS_LOCAL (info, h)
1513
0
    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1514
0
  {
1515
    /* This case can occur if we saw a PLT32 reloc in an input
1516
       file, but the symbol was never referred to by a dynamic
1517
       object, or if all references were garbage collected.  In
1518
       such a case, we don't actually need to build a procedure
1519
       linkage table, and we can just do a PC32 reloc instead.  */
1520
0
    h->plt.offset = (bfd_vma) -1;
1521
0
    h->needs_plt = 0;
1522
0
    elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1523
0
  }
1524
1525
0
      return true;
1526
0
    }
1527
0
  else
1528
    /* It's possible that we incorrectly decided a .plt reloc was
1529
       needed for an R_390_PC32 reloc to a non-function sym in
1530
       check_relocs.  We can't decide accurately between function and
1531
       non-function syms in check-relocs;  Objects loaded later in
1532
       the link may change h->type.  So fix it now.  */
1533
0
    h->plt.offset = (bfd_vma) -1;
1534
1535
  /* If this is a weak symbol, and there is a real definition, the
1536
     processor independent code will have arranged for us to see the
1537
     real definition first, and we can just use the same value.  */
1538
0
  if (h->is_weakalias)
1539
0
    {
1540
0
      struct elf_link_hash_entry *def = weakdef (h);
1541
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1542
0
      h->root.u.def.section = def->root.u.def.section;
1543
0
      h->root.u.def.value = def->root.u.def.value;
1544
0
      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1545
0
  h->non_got_ref = def->non_got_ref;
1546
0
      return true;
1547
0
    }
1548
1549
  /* This is a reference to a symbol defined by a dynamic object which
1550
     is not a function.  */
1551
1552
  /* If we are creating a shared library, we must presume that the
1553
     only references to the symbol are via the global offset table.
1554
     For such cases we need not do anything here; the relocations will
1555
     be handled correctly by relocate_section.  */
1556
0
  if (bfd_link_pic (info))
1557
0
    return true;
1558
1559
  /* If there are no references to this symbol that do not use the
1560
     GOT, we don't need to generate a copy reloc.  */
1561
0
  if (!h->non_got_ref)
1562
0
    return true;
1563
1564
  /* If -z nocopyreloc was given, we won't generate them either.  */
1565
0
  if (info->nocopyreloc)
1566
0
    {
1567
0
      h->non_got_ref = 0;
1568
0
      return true;
1569
0
    }
1570
1571
  /* If we don't find any dynamic relocs in read-only sections, then
1572
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1573
0
  if (ELIMINATE_COPY_RELOCS && !_bfd_elf_readonly_dynrelocs (h))
1574
0
    {
1575
0
      h->non_got_ref = 0;
1576
0
      return true;
1577
0
    }
1578
1579
  /* We must allocate the symbol in our .dynbss section, which will
1580
     become part of the .bss section of the executable.  There will be
1581
     an entry for this symbol in the .dynsym section.  The dynamic
1582
     object will contain position independent code, so all references
1583
     from the dynamic object to this symbol will go through the global
1584
     offset table.  The dynamic linker will use the .dynsym entry to
1585
     determine the address it must put in the global offset table, so
1586
     both the dynamic object and the regular object will refer to the
1587
     same memory location for the variable.  */
1588
1589
0
  htab = elf_s390_hash_table (info);
1590
0
  if (htab == NULL)
1591
0
    return false;
1592
1593
  /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1594
     copy the initial value out of the dynamic object and into the
1595
     runtime process image.  */
1596
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1597
0
    {
1598
0
      s = htab->elf.sdynrelro;
1599
0
      srel = htab->elf.sreldynrelro;
1600
0
    }
1601
0
  else
1602
0
    {
1603
0
      s = htab->elf.sdynbss;
1604
0
      srel = htab->elf.srelbss;
1605
0
    }
1606
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1607
0
    {
1608
0
      srel->size += sizeof (Elf64_External_Rela);
1609
0
      h->needs_copy = 1;
1610
0
    }
1611
1612
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
1613
0
}
1614
1615
/* Create SFrame stack trace info for the PLT entries in the .plt section.  */
1616
1617
static bool
1618
_bfd_s390_elf_create_sframe_plt (struct bfd_link_info *info)
1619
0
{
1620
0
  struct elf_s390_link_hash_table *htab;
1621
1622
0
  unsigned int plt0_entry_size;
1623
0
  unsigned char func_info;
1624
0
  uint32_t fre_type;
1625
  /* The dynamic plt section for which .sframe stack trace information is being
1626
     created.  */
1627
0
  asection *dpltsec;
1628
1629
0
  int err = 0;
1630
1631
0
  sframe_encoder_ctx **ectx = NULL;
1632
0
  unsigned plt_entry_size = 0;
1633
0
  unsigned int num_pltn_fres = 0;
1634
0
  unsigned int num_pltn_entries = 0;
1635
0
  const sframe_frame_row_entry * const *pltn_fres;
1636
1637
0
  htab = elf_s390_hash_table (info);
1638
0
  ectx = &htab->plt_cfe_ctx;
1639
0
  dpltsec = htab->elf.splt;
1640
1641
0
  plt0_entry_size = htab->sframe_plt->plt0_entry_size;
1642
0
  plt_entry_size = htab->sframe_plt->pltn_entry_size;
1643
0
  pltn_fres = htab->sframe_plt->pltn_fres;
1644
0
  num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1645
0
  num_pltn_entries = (dpltsec->size - plt0_entry_size) / plt_entry_size;
1646
1647
0
  *ectx = sframe_encode (SFRAME_VERSION_3,
1648
0
       SFRAME_F_FDE_FUNC_START_PCREL,
1649
0
       SFRAME_ABI_S390X_ENDIAN_BIG,
1650
0
       SFRAME_CFA_FIXED_FP_INVALID,
1651
0
       SFRAME_CFA_FIXED_RA_INVALID,
1652
0
       &err);
1653
1654
  /* FRE type is dependent on the size of the function.  */
1655
0
  fre_type = sframe_calc_fre_type (dpltsec->size);
1656
0
  func_info = sframe_fde_create_func_info (fre_type, SFRAME_V3_FDE_PCTYPE_INC);
1657
1658
  /* Add SFrame FDE and the associated FREs for PLT0 if PLT0 has been
1659
     generated.  */
1660
0
  if (plt0_entry_size)
1661
0
    {
1662
      /* Add SFrame FDE for PLT0, the function start address is updated later
1663
   at _bfd_elf_merge_section_sframe time.  */
1664
0
      sframe_encoder_add_funcdesc_v3 (*ectx,
1665
0
              0, /* func start addr.  */
1666
0
              plt0_entry_size,
1667
0
              func_info,
1668
0
              0, /* func_info2.  */
1669
0
              0, /* Rep block size.  */
1670
0
              0 /* Num FREs.  */);
1671
0
      sframe_frame_row_entry plt0_fre;
1672
0
      unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1673
0
      for (unsigned int j = 0; j < num_plt0_fres; j++)
1674
0
  {
1675
0
    plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1676
0
    sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1677
0
  }
1678
0
    }
1679
1680
0
  if (num_pltn_entries)
1681
0
    {
1682
      /* PLTn entries use an SFrame FDE of type
1683
   SFRAME_V3_FDE_PCTYPE_MASK to exploit the repetitive
1684
   pattern of the instructions in these entries.  Using this SFrame FDE
1685
   type helps in keeping the SFrame stack trace info for PLTn entries
1686
   compact.  */
1687
0
      func_info = sframe_fde_create_func_info (fre_type,
1688
0
                 SFRAME_V3_FDE_PCTYPE_MASK);
1689
      /* Add the SFrame FDE for all PCs starting at the first PLTn entry (hence,
1690
   function start address = plt0_entry_size.  As usual, this will be
1691
   updated later at _bfd_elf_merge_section_sframe, by when the
1692
   sections are relocated.  */
1693
0
      sframe_encoder_add_funcdesc_v3 (*ectx,
1694
0
              plt0_entry_size, /* func start addr.  */
1695
0
              dpltsec->size - plt0_entry_size,
1696
0
              func_info,
1697
0
              0, /* func_info2.  */
1698
0
              plt_entry_size,
1699
0
              0 /* Num FREs.  */);
1700
1701
0
      sframe_frame_row_entry pltn_fre;
1702
      /* Now add the FREs for PLTn.  Simply adding the FREs suffices due
1703
   to the usage of SFRAME_V3_FDE_PCTYPE_MASK above.  */
1704
0
      for (unsigned int j = 0; j < num_pltn_fres; j++)
1705
0
  {
1706
0
    unsigned int func_idx = plt0_entry_size ? 1 : 0;
1707
0
    pltn_fre = *(pltn_fres[j]);
1708
0
    sframe_encoder_add_fre (*ectx, func_idx, &pltn_fre);
1709
0
  }
1710
0
    }
1711
1712
0
  return true;
1713
0
}
1714
1715
/* Write contents of the .sframe section.  */
1716
1717
static bool
1718
_bfd_s390_elf_write_sframe_plt (struct bfd_link_info *info)
1719
0
{
1720
0
  struct elf_s390_link_hash_table *htab;
1721
0
  sframe_encoder_ctx **ectx;
1722
0
  size_t sec_size;
1723
0
  asection *sec;
1724
0
  bfd *dynobj;
1725
1726
0
  int err = 0;
1727
1728
0
  htab = elf_s390_hash_table (info);
1729
0
  dynobj = htab->elf.dynobj;
1730
1731
0
  ectx = &htab->plt_cfe_ctx;
1732
0
  sec = htab->plt_sframe;
1733
1734
0
  BFD_ASSERT (*ectx);
1735
1736
0
  void *contents = sframe_encoder_write (*ectx, &sec_size, false, &err);
1737
1738
0
  sec->size = (bfd_size_type) sec_size;
1739
0
  sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
1740
0
  sec->alloced = 1;
1741
0
  memcpy (sec->contents, contents, sec_size);
1742
1743
0
  sframe_encoder_free (ectx);
1744
1745
0
  return true;
1746
0
}
1747
1748
/* Allocate space in .plt, .got and associated reloc sections for
1749
   dynamic relocs.  */
1750
1751
static bool
1752
allocate_dynrelocs (struct elf_link_hash_entry *h,
1753
        void * inf)
1754
0
{
1755
0
  struct bfd_link_info *info;
1756
0
  struct elf_s390_link_hash_table *htab;
1757
0
  struct elf_dyn_relocs *p;
1758
1759
0
  if (h->root.type == bfd_link_hash_indirect)
1760
0
    return true;
1761
1762
0
  info = (struct bfd_link_info *) inf;
1763
0
  htab = elf_s390_hash_table (info);
1764
0
  if (htab == NULL)
1765
0
    return false;
1766
1767
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1768
     here if it is defined and referenced in a non-shared object.  */
1769
0
  if (s390_is_ifunc_symbol_p (h) && h->def_regular)
1770
0
    return s390_elf_allocate_ifunc_dyn_relocs (info, h);
1771
0
  else if (htab->elf.dynamic_sections_created
1772
0
     && h->plt.refcount > 0)
1773
0
    {
1774
      /* Make sure this symbol is output as a dynamic symbol.
1775
   Undefined weak syms won't yet be marked as dynamic.  */
1776
0
      if (h->dynindx == -1
1777
0
    && !h->forced_local)
1778
0
  {
1779
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
1780
0
      return false;
1781
0
  }
1782
1783
0
      if (bfd_link_pic (info)
1784
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1785
0
  {
1786
0
    asection *s = htab->elf.splt;
1787
1788
    /* If this is the first .plt entry, make room for the special
1789
       first entry.  */
1790
0
    if (s->size == 0)
1791
0
      s->size += PLT_FIRST_ENTRY_SIZE;
1792
1793
0
    h->plt.offset = s->size;
1794
1795
    /* If this symbol is not defined in a regular file, and we are
1796
       not generating a shared library, then set the symbol to this
1797
       location in the .plt.  This is required to make function
1798
       pointers compare as equal between the normal executable and
1799
       the shared library.  */
1800
0
    if (! bfd_link_pic (info)
1801
0
        && !h->def_regular)
1802
0
      {
1803
0
        h->root.u.def.section = s;
1804
0
        h->root.u.def.value = h->plt.offset;
1805
0
      }
1806
1807
    /* Make room for this entry.  */
1808
0
    s->size += PLT_ENTRY_SIZE;
1809
1810
    /* We also need to make an entry in the .got.plt section.  */
1811
0
    htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1812
1813
    /* We also need to make an entry in the .rela.plt section.  */
1814
0
    htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1815
0
  }
1816
0
      else
1817
0
  {
1818
0
    h->plt.offset = (bfd_vma) -1;
1819
0
    h->needs_plt = 0;
1820
0
    elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1821
0
  }
1822
0
    }
1823
0
  else
1824
0
    {
1825
0
      h->plt.offset = (bfd_vma) -1;
1826
0
      h->needs_plt = 0;
1827
0
      elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1828
0
    }
1829
1830
  /* If R_390_TLS_{IE64,GOTIE64,GOTIE12,GOTIE20,IEENT} symbol is now local to
1831
     the binary, we can optimize a bit. IE64 and GOTIE64 get converted
1832
     to R_390_TLS_LE64 requiring no TLS entry. For GOTIE12, GOTIE20, and IEENT
1833
     we can save the dynamic TLS relocation.  */
1834
0
  if (h->got.refcount > 0
1835
0
      && !bfd_link_dll (info)
1836
0
      && h->dynindx == -1
1837
0
      && elf_s390_hash_entry(h)->tls_type >= GOT_TLS_IE)
1838
0
    {
1839
0
      if (elf_s390_hash_entry(h)->tls_type == GOT_TLS_IE_NLT)
1840
  /* For the GOTIE access without a literal pool entry the offset has
1841
     to be stored somewhere. The immediate value in the instruction
1842
     is not bit enough so the value is stored in the got.  */
1843
0
  {
1844
0
    h->got.offset = htab->elf.sgot->size;
1845
0
    htab->elf.sgot->size += GOT_ENTRY_SIZE;
1846
0
  }
1847
0
      else
1848
0
  h->got.offset = (bfd_vma) -1;
1849
0
    }
1850
0
  else if (h->got.refcount > 0)
1851
0
    {
1852
0
      asection *s;
1853
0
      bool dyn;
1854
0
      int tls_type = elf_s390_hash_entry(h)->tls_type;
1855
1856
      /* Make sure this symbol is output as a dynamic symbol.
1857
   Undefined weak syms won't yet be marked as dynamic.  */
1858
0
      if (h->dynindx == -1
1859
0
    && !h->forced_local)
1860
0
  {
1861
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
1862
0
      return false;
1863
0
  }
1864
1865
0
      s = htab->elf.sgot;
1866
0
      h->got.offset = s->size;
1867
0
      s->size += GOT_ENTRY_SIZE;
1868
      /* R_390_TLS_GD64 needs 2 consecutive GOT slots.  */
1869
0
      if (tls_type == GOT_TLS_GD)
1870
0
  s->size += GOT_ENTRY_SIZE;
1871
0
      dyn = htab->elf.dynamic_sections_created;
1872
      /* R_390_TLS_IE64 needs one dynamic relocation,
1873
   R_390_TLS_GD64 needs one if local symbol and two if global.  */
1874
0
      if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1875
0
    || tls_type >= GOT_TLS_IE)
1876
0
  htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1877
0
      else if (tls_type == GOT_TLS_GD)
1878
0
  htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1879
0
      else if (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
1880
0
         && (bfd_link_pic (info)
1881
0
       || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1882
0
  htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1883
0
    }
1884
0
  else
1885
0
    h->got.offset = (bfd_vma) -1;
1886
1887
0
  if (h->dyn_relocs == NULL)
1888
0
    return true;
1889
1890
  /* In the shared -Bsymbolic case, discard space allocated for
1891
     dynamic pc-relative relocs against symbols which turn out to be
1892
     defined in regular objects.  For the normal shared case, discard
1893
     space for pc-relative relocs that have become local due to symbol
1894
     visibility changes.  */
1895
1896
0
  if (bfd_link_pic (info))
1897
0
    {
1898
0
      if (SYMBOL_CALLS_LOCAL (info, h))
1899
0
  {
1900
0
    struct elf_dyn_relocs **pp;
1901
1902
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1903
0
      {
1904
0
        p->count -= p->pc_count;
1905
0
        p->pc_count = 0;
1906
0
        if (p->count == 0)
1907
0
    *pp = p->next;
1908
0
        else
1909
0
    pp = &p->next;
1910
0
      }
1911
0
  }
1912
1913
      /* Also discard relocs on undefined weak syms with non-default
1914
   visibility.  */
1915
0
      if (h->dyn_relocs != NULL
1916
0
    && h->root.type == bfd_link_hash_undefweak)
1917
0
  {
1918
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1919
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1920
0
      h->dyn_relocs = NULL;
1921
1922
    /* Make sure undefined weak symbols are output as a dynamic
1923
       symbol in PIEs.  */
1924
0
    else if (h->dynindx == -1
1925
0
       && !h->forced_local)
1926
0
      {
1927
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
1928
0
    return false;
1929
0
      }
1930
0
  }
1931
0
    }
1932
0
  else if (ELIMINATE_COPY_RELOCS)
1933
0
    {
1934
      /* For the non-shared case, discard space for relocs against
1935
   symbols which turn out to need copy relocs or are not
1936
   dynamic.  */
1937
1938
0
      if (!h->non_got_ref
1939
0
    && ((h->def_dynamic
1940
0
         && !h->def_regular)
1941
0
        || (htab->elf.dynamic_sections_created
1942
0
      && (h->root.type == bfd_link_hash_undefweak
1943
0
          || h->root.type == bfd_link_hash_undefined))))
1944
0
  {
1945
    /* Make sure this symbol is output as a dynamic symbol.
1946
       Undefined weak syms won't yet be marked as dynamic.  */
1947
0
    if (h->dynindx == -1
1948
0
        && !h->forced_local)
1949
0
      {
1950
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
1951
0
    return false;
1952
0
      }
1953
1954
    /* If that succeeded, we know we'll be keeping all the
1955
       relocs.  */
1956
0
    if (h->dynindx != -1)
1957
0
      goto keep;
1958
0
  }
1959
1960
0
      h->dyn_relocs = NULL;
1961
1962
0
    keep: ;
1963
0
    }
1964
1965
  /* Finally, allocate space.  */
1966
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
1967
0
    {
1968
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
1969
0
      sreloc->size += p->count * sizeof (Elf64_External_Rela);
1970
0
    }
1971
1972
0
  return true;
1973
0
}
1974
1975
/* Set the sizes of the dynamic sections.  */
1976
1977
static bool
1978
elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1979
           struct bfd_link_info *info)
1980
0
{
1981
0
  struct elf_s390_link_hash_table *htab;
1982
0
  bfd *dynobj;
1983
0
  asection *s;
1984
0
  bool relocs;
1985
0
  bfd *ibfd;
1986
1987
0
  htab = elf_s390_hash_table (info);
1988
0
  if (htab == NULL)
1989
0
    return false;
1990
1991
0
  dynobj = htab->elf.dynobj;
1992
0
  if (dynobj == NULL)
1993
0
    return true;
1994
1995
0
  if (htab->elf.dynamic_sections_created)
1996
0
    {
1997
      /* Set the contents of the .interp section to the interpreter.  */
1998
0
      if (bfd_link_executable (info) && !info->nointerp)
1999
0
  {
2000
0
    s = htab->elf.interp;
2001
0
    if (s == NULL)
2002
0
      abort ();
2003
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2004
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2005
0
    s->alloced = 1;
2006
0
  }
2007
0
    }
2008
2009
0
  if (htab->elf.sgot && s390_gotplt_after_got_p (info))
2010
0
    {
2011
      /* _bfd_elf_create_got_section adds the got header size always
2012
   to .got.plt but we need it in .got if this section comes
2013
   first.  */
2014
0
      htab->elf.sgot->size += 3 * GOT_ENTRY_SIZE;
2015
0
      htab->elf.sgotplt->size -= 3 * GOT_ENTRY_SIZE;
2016
2017
      /* Make the _GLOBAL_OFFSET_TABLE_ symbol point to the .got
2018
   instead of .got.plt.  */
2019
0
      htab->elf.hgot->root.u.def.section = htab->elf.sgot;
2020
0
      htab->elf.hgot->root.u.def.value = 0;
2021
0
    }
2022
2023
  /* Set up .got offsets for local syms, and space for local dynamic
2024
     relocs.  */
2025
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2026
0
    {
2027
0
      bfd_signed_vma *local_got;
2028
0
      bfd_signed_vma *end_local_got;
2029
0
      char *local_tls_type;
2030
0
      bfd_size_type locsymcount;
2031
0
      Elf_Internal_Shdr *symtab_hdr;
2032
0
      asection *srela;
2033
0
      struct plt_entry *local_plt;
2034
0
      unsigned int i;
2035
2036
0
      if (! is_s390_elf (ibfd))
2037
0
  continue;
2038
2039
0
      for (s = ibfd->sections; s != NULL; s = s->next)
2040
0
  {
2041
0
    struct elf_dyn_relocs *p;
2042
2043
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
2044
0
      {
2045
0
        if (!bfd_is_abs_section (p->sec)
2046
0
      && bfd_is_abs_section (p->sec->output_section))
2047
0
    {
2048
      /* Input section has been discarded, either because
2049
         it is a copy of a linkonce section or due to
2050
         linker script /DISCARD/, so we'll be discarding
2051
         the relocs too.  */
2052
0
    }
2053
0
        else if (p->count != 0)
2054
0
    {
2055
0
      srela = elf_section_data (p->sec)->sreloc;
2056
0
      srela->size += p->count * sizeof (Elf64_External_Rela);
2057
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2058
0
        info->flags |= DF_TEXTREL;
2059
0
    }
2060
0
      }
2061
0
  }
2062
2063
0
      local_got = elf_local_got_refcounts (ibfd);
2064
0
      if (!local_got)
2065
0
  continue;
2066
2067
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
2068
0
      locsymcount = symtab_hdr->sh_info;
2069
0
      end_local_got = local_got + locsymcount;
2070
0
      local_tls_type = elf_s390_local_got_tls_type (ibfd);
2071
0
      s = htab->elf.sgot;
2072
0
      srela = htab->elf.srelgot;
2073
0
      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2074
0
  {
2075
0
    if (*local_got > 0)
2076
0
      {
2077
0
        *local_got = s->size;
2078
0
        s->size += GOT_ENTRY_SIZE;
2079
0
        if (*local_tls_type == GOT_TLS_GD)
2080
0
    s->size += GOT_ENTRY_SIZE;
2081
0
        if (bfd_link_pic (info))
2082
0
    srela->size += sizeof (Elf64_External_Rela);
2083
0
      }
2084
0
    else
2085
0
      *local_got = (bfd_vma) -1;
2086
0
  }
2087
2088
0
      local_plt = elf_s390_local_plt (ibfd);
2089
0
      for (i = 0; i < symtab_hdr->sh_info; i++)
2090
0
  {
2091
0
    if (local_plt[i].plt.refcount > 0)
2092
0
      {
2093
0
        local_plt[i].plt.offset = htab->elf.iplt->size;
2094
0
        htab->elf.iplt->size += PLT_ENTRY_SIZE;
2095
0
        htab->elf.igotplt->size += GOT_ENTRY_SIZE;
2096
0
        htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
2097
0
      }
2098
0
    else
2099
0
      local_plt[i].plt.offset = (bfd_vma) -1;
2100
0
  }
2101
0
    }
2102
2103
0
  if (htab->tls_ldm_got.refcount > 0)
2104
0
    {
2105
      /* Allocate 2 got entries and 1 dynamic reloc for R_390_TLS_LDM64
2106
   relocs.  */
2107
0
      htab->tls_ldm_got.offset = htab->elf.sgot->size;
2108
0
      htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2109
0
      htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2110
0
    }
2111
0
  else
2112
0
    htab->tls_ldm_got.offset = -1;
2113
2114
  /* Allocate global sym .plt and .got entries, and space for global
2115
     sym dynamic relocs.  */
2116
0
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2117
2118
0
  if (_bfd_elf_eh_frame_present (info))
2119
0
    {
2120
0
      if (htab->plt_eh_frame != NULL
2121
0
    && htab->elf.splt != NULL
2122
0
    && htab->elf.splt->size != 0
2123
0
    && !bfd_is_abs_section (htab->elf.splt->output_section))
2124
0
  htab->plt_eh_frame->size = sizeof (elf_s390x_eh_frame_plt);
2125
0
    }
2126
2127
  /* No need to size the .sframe section explicitly because the write-out
2128
     mechanism is different.  Simply prep up the FDE/FRE for the
2129
     .plt section.  */
2130
0
  if (_bfd_elf_sframe_present (info))
2131
0
    {
2132
0
      if (htab->plt_sframe != NULL
2133
0
    && htab->elf.splt != NULL
2134
0
    && htab->elf.splt->size != 0
2135
0
    && !bfd_is_abs_section (htab->elf.splt->output_section))
2136
0
  {
2137
0
    _bfd_s390_elf_create_sframe_plt (info);
2138
    /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2139
       so that the section does not get stripped out below.  The precise
2140
       size of this section is known only when the contents are
2141
       serialized in _bfd_s390x_elf_write_sframe_plt.  */
2142
0
    htab->plt_sframe->size = sizeof (sframe_header) + 1;
2143
0
  }
2144
0
    }
2145
2146
  /* We now have determined the sizes of the various dynamic sections.
2147
     Allocate memory for them.  */
2148
0
  relocs = false;
2149
0
  for (s = dynobj->sections; s != NULL; s = s->next)
2150
0
    {
2151
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
2152
0
  continue;
2153
2154
0
      if (s == htab->elf.splt
2155
0
    || s == htab->elf.sgot
2156
0
    || s == htab->elf.sgotplt
2157
0
    || s == htab->plt_eh_frame
2158
0
    || s == htab->plt_sframe
2159
0
    || s == htab->elf.sdynbss
2160
0
    || s == htab->elf.sdynrelro
2161
0
    || s == htab->elf.iplt
2162
0
    || s == htab->elf.igotplt
2163
0
    || s == htab->irelifunc)
2164
0
  {
2165
    /* Strip this section if we don't need it; see the
2166
       comment below.  */
2167
0
  }
2168
0
      else if (startswith (bfd_section_name (s), ".rela"))
2169
0
  {
2170
0
    if (s->size != 0 && s != htab->elf.srelplt)
2171
0
      {
2172
0
        relocs = true;
2173
0
        if (s == htab->elf.irelplt)
2174
0
    {
2175
      /* In static-pie case, there are IRELATIVE-relocs in
2176
         .rela.iplt (htab->irelplt), which will later be grouped
2177
         to .rela.plt.  On s390, the IRELATIVE relocations are
2178
         always located in .rela.iplt - even for non-static case.
2179
         Ensure that DT_JMPREL, DT_PLTRELA, DT_PLTRELASZ is added
2180
         to the dynamic section even if htab->srelplt->size == 0.
2181
         See _bfd_elf_add_dynamic_tags in bfd/elflink.c.  */
2182
0
      htab->elf.dt_jmprel_required = true;
2183
0
    }
2184
0
      }
2185
2186
    /* We use the reloc_count field as a counter if we need
2187
       to copy relocs into the output file.  */
2188
0
    s->reloc_count = 0;
2189
0
  }
2190
0
      else
2191
0
  {
2192
    /* It's not one of our sections, so don't allocate space.  */
2193
0
    continue;
2194
0
  }
2195
2196
0
      if (s->size == 0)
2197
0
  {
2198
    /* If we don't need this section, strip it from the
2199
       output file.  This is to handle .rela.bss and
2200
       .rela.plt.  We must create it in
2201
       create_dynamic_sections, because it must be created
2202
       before the linker maps input sections to output
2203
       sections.  The linker does that before
2204
       adjust_dynamic_symbol is called, and it is that
2205
       function which decides whether anything needs to go
2206
       into these sections.  */
2207
2208
0
    s->flags |= SEC_EXCLUDE;
2209
0
    continue;
2210
0
  }
2211
2212
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
2213
0
  continue;
2214
2215
      /* Skip allocating contents for .sframe section as it is written
2216
   out differently.  See below.  */
2217
0
      if (s == htab->plt_sframe)
2218
0
  continue;
2219
2220
      /* Allocate memory for the section contents.  We use bfd_zalloc
2221
   here in case unused entries are not reclaimed before the
2222
   section's contents are written out.  This should not happen,
2223
   but this way if it does, we get a R_390_NONE reloc instead
2224
   of garbage.  */
2225
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2226
0
      if (s->contents == NULL)
2227
0
  return false;
2228
0
      s->alloced = 1;
2229
0
    }
2230
2231
0
  if (htab->plt_eh_frame != NULL
2232
0
      && htab->plt_eh_frame->contents != NULL)
2233
0
    {
2234
0
      memcpy (htab->plt_eh_frame->contents,
2235
0
        elf_s390x_eh_frame_plt,
2236
0
        htab->plt_eh_frame->size);
2237
0
      bfd_put_32 (dynobj, htab->elf.splt->size,
2238
0
      htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2239
0
    }
2240
2241
0
  if (_bfd_elf_sframe_present (info))
2242
0
    {
2243
0
      if (htab->plt_sframe != NULL
2244
0
    && htab->elf.splt != NULL
2245
0
    && htab->elf.splt->size != 0
2246
0
    && htab->plt_sframe->contents == NULL)
2247
0
  _bfd_s390_elf_write_sframe_plt (info);
2248
0
    }
2249
2250
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
2251
0
}
2252
2253
/* Return the base VMA address which should be subtracted from real addresses
2254
   when resolving @dtpoff relocation.
2255
   This is PT_TLS segment p_vaddr.  */
2256
2257
static bfd_vma
2258
dtpoff_base (struct bfd_link_info *info)
2259
0
{
2260
  /* If tls_sec is NULL, we should have signalled an error already.  */
2261
0
  if (elf_hash_table (info)->tls_sec == NULL)
2262
0
    return 0;
2263
0
  return elf_hash_table (info)->tls_sec->vma;
2264
0
}
2265
2266
/* Return the relocation value for @tpoff relocation
2267
   if STT_TLS virtual address is ADDRESS.  */
2268
2269
static bfd_vma
2270
tpoff (struct bfd_link_info *info, bfd_vma address)
2271
0
{
2272
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2273
2274
  /* If tls_sec is NULL, we should have signalled an error already.  */
2275
0
  if (htab->tls_sec == NULL)
2276
0
    return 0;
2277
0
  return htab->tls_size + htab->tls_sec->vma - address;
2278
0
}
2279
2280
/* Complain if TLS instruction relocation is against an invalid
2281
   instruction.  */
2282
2283
static void
2284
invalid_tls_insn (bfd *input_bfd,
2285
      asection *input_section,
2286
      Elf_Internal_Rela *rel)
2287
0
{
2288
0
  reloc_howto_type *howto;
2289
2290
0
  howto = elf_howto_table + ELF64_R_TYPE (rel->r_info);
2291
0
  _bfd_error_handler
2292
    /* xgettext:c-format */
2293
0
    (_("%pB(%pA+%#" PRIx64 "): invalid instruction for TLS relocation %s"),
2294
0
     input_bfd,
2295
0
     input_section,
2296
0
     (uint64_t) rel->r_offset,
2297
0
     howto->name);
2298
0
  bfd_set_error (bfd_error_bad_value);
2299
0
}
2300
2301
/* Relocate a 390 ELF section.  */
2302
2303
static int
2304
elf_s390_relocate_section (bfd *output_bfd,
2305
         struct bfd_link_info *info,
2306
         bfd *input_bfd,
2307
         asection *input_section,
2308
         bfd_byte *contents,
2309
         Elf_Internal_Rela *relocs,
2310
         Elf_Internal_Sym *local_syms,
2311
         asection **local_sections)
2312
0
{
2313
0
  struct elf_s390_link_hash_table *htab;
2314
0
  Elf_Internal_Shdr *symtab_hdr;
2315
0
  struct elf_link_hash_entry **sym_hashes;
2316
0
  bfd_vma *local_got_offsets;
2317
0
  Elf_Internal_Rela *rel;
2318
0
  Elf_Internal_Rela *relend;
2319
2320
0
  if (!is_s390_elf (input_bfd))
2321
0
    {
2322
0
      bfd_set_error (bfd_error_wrong_format);
2323
0
      return false;
2324
0
    }
2325
2326
0
  htab = elf_s390_hash_table (info);
2327
0
  if (htab == NULL)
2328
0
    return false;
2329
2330
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2331
0
  sym_hashes = elf_sym_hashes (input_bfd);
2332
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
2333
2334
0
  rel = relocs;
2335
0
  relend = relocs + input_section->reloc_count;
2336
0
  for (; rel < relend; rel++)
2337
0
    {
2338
0
      unsigned int r_type;
2339
0
      reloc_howto_type *howto;
2340
0
      unsigned long r_symndx;
2341
0
      struct elf_link_hash_entry *h;
2342
0
      Elf_Internal_Sym *sym;
2343
0
      asection *sec;
2344
0
      bfd_vma off;
2345
0
      bfd_vma relocation;
2346
0
      bool unresolved_reloc;
2347
0
      bfd_reloc_status_type r;
2348
0
      int tls_type;
2349
0
      bool resolved_to_zero;
2350
0
      bool relax;
2351
2352
0
      r_type = ELF64_R_TYPE (rel->r_info);
2353
0
      if (r_type == (int) R_390_GNU_VTINHERIT
2354
0
    || r_type == (int) R_390_GNU_VTENTRY)
2355
0
  continue;
2356
0
      if (r_type >= (int) R_390_max)
2357
0
  {
2358
0
    bfd_set_error (bfd_error_bad_value);
2359
0
    return false;
2360
0
  }
2361
2362
0
      howto = elf_howto_table + r_type;
2363
0
      r_symndx = ELF64_R_SYM (rel->r_info);
2364
2365
0
      h = NULL;
2366
0
      sym = NULL;
2367
0
      sec = NULL;
2368
0
      unresolved_reloc = false;
2369
0
      if (r_symndx < symtab_hdr->sh_info)
2370
0
  {
2371
0
    sym = local_syms + r_symndx;
2372
0
    sec = local_sections[r_symndx];
2373
2374
0
    if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2375
0
      {
2376
0
        struct plt_entry *local_plt = elf_s390_local_plt (input_bfd);
2377
0
        if (local_plt == NULL)
2378
0
    return false;
2379
2380
        /* Address of the PLT slot.  */
2381
0
        relocation = (htab->elf.iplt->output_section->vma
2382
0
          + htab->elf.iplt->output_offset
2383
0
          + local_plt[r_symndx].plt.offset);
2384
2385
0
        switch (r_type)
2386
0
    {
2387
0
    case R_390_PLTOFF16:
2388
0
    case R_390_PLTOFF32:
2389
0
    case R_390_PLTOFF64:
2390
0
      relocation -= s390_got_pointer (info);
2391
0
      break;
2392
0
    case R_390_GOTPLT12:
2393
0
    case R_390_GOTPLT16:
2394
0
    case R_390_GOTPLT20:
2395
0
    case R_390_GOTPLT32:
2396
0
    case R_390_GOTPLT64:
2397
0
    case R_390_GOTPLTENT:
2398
0
    case R_390_GOT12:
2399
0
    case R_390_GOT16:
2400
0
    case R_390_GOT20:
2401
0
    case R_390_GOT32:
2402
0
    case R_390_GOT64:
2403
0
    case R_390_GOTENT:
2404
0
      {
2405
        /* Write the PLT slot address into the GOT slot.  */
2406
0
        bfd_put_64 (output_bfd, relocation,
2407
0
        htab->elf.sgot->contents +
2408
0
        local_got_offsets[r_symndx]);
2409
0
        relocation = (local_got_offsets[r_symndx] +
2410
0
          s390_got_offset (info));
2411
2412
0
        if (r_type == R_390_GOTENT || r_type == R_390_GOTPLTENT)
2413
0
          relocation += s390_got_pointer (info);
2414
0
        break;
2415
0
      }
2416
0
    default:
2417
0
      break;
2418
0
    }
2419
        /* The output section is needed later in
2420
     finish_dynamic_section when creating the dynamic
2421
     relocation.  */
2422
0
        local_plt[r_symndx].sec = sec;
2423
0
        goto do_relocation;
2424
0
      }
2425
0
    else
2426
0
      relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2427
0
  }
2428
0
      else
2429
0
  {
2430
0
    bool warned ATTRIBUTE_UNUSED;
2431
0
    bool ignored ATTRIBUTE_UNUSED;
2432
2433
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2434
0
           r_symndx, symtab_hdr, sym_hashes,
2435
0
           h, sec, relocation,
2436
0
           unresolved_reloc, warned, ignored);
2437
0
  }
2438
2439
0
      if (sec != NULL && discarded_section (sec))
2440
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2441
0
           rel, 1, relend, R_390_NONE,
2442
0
           howto, 0, contents);
2443
2444
0
      if (bfd_link_relocatable (info))
2445
0
  continue;
2446
2447
0
      resolved_to_zero = (h != NULL
2448
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2449
2450
      /* Rewrite instructions and related relocations if (1) relaxation
2451
   disabled by default, (2) enabled by target, or (3) enabled by
2452
   user.  Suppress rewriting if linker option --no-relax is used.  */
2453
0
      relax = info->disable_target_specific_optimizations <= 1;
2454
2455
0
      switch (r_type)
2456
0
  {
2457
0
  case R_390_GOTPLT12:
2458
0
  case R_390_GOTPLT16:
2459
0
  case R_390_GOTPLT20:
2460
0
  case R_390_GOTPLT32:
2461
0
  case R_390_GOTPLT64:
2462
0
  case R_390_GOTPLTENT:
2463
    /* There are three cases for a GOTPLT relocation. 1) The
2464
       relocation is against the jump slot entry of a plt that
2465
       will get emitted to the output file. 2) The relocation
2466
       is against the jump slot of a plt entry that has been
2467
       removed. elf_s390_adjust_gotplt has created a GOT entry
2468
       as replacement. 3) The relocation is against a local symbol.
2469
       Cases 2) and 3) are the same as the GOT relocation code
2470
       so we just have to test for case 1 and fall through for
2471
       the other two.  */
2472
0
    if (h != NULL && h->plt.offset != (bfd_vma) -1)
2473
0
      {
2474
0
        bfd_vma plt_index;
2475
2476
0
        if (s390_is_ifunc_symbol_p (h))
2477
0
    {
2478
      /* Entry indices of .iplt and .igot.plt match
2479
         1:1. No magic PLT first entry here.  */
2480
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2481
0
      relocation = (plt_index * GOT_ENTRY_SIZE
2482
0
        + s390_gotplt_offset (info)
2483
0
        + htab->elf.igotplt->output_offset);
2484
0
    }
2485
0
        else
2486
0
    {
2487
0
      plt_index = ((h->plt.offset - PLT_FIRST_ENTRY_SIZE)
2488
0
             / PLT_ENTRY_SIZE);
2489
2490
0
      relocation = (plt_index * GOT_ENTRY_SIZE
2491
0
        + s390_gotplt_offset (info));
2492
0
    }
2493
0
        if (r_type == R_390_GOTPLTENT)
2494
0
    relocation += s390_got_pointer (info);
2495
0
        unresolved_reloc = false;
2496
0
        break;
2497
0
      }
2498
    /* Fall through.  */
2499
2500
0
  case R_390_GOT12:
2501
0
  case R_390_GOT16:
2502
0
  case R_390_GOT20:
2503
0
  case R_390_GOT32:
2504
0
  case R_390_GOT64:
2505
0
  case R_390_GOTENT:
2506
    /* Relocation is to the entry for this symbol in the global
2507
       offset table.  */
2508
0
    if (htab->elf.sgot == NULL)
2509
0
      abort ();
2510
2511
0
    if (h != NULL)
2512
0
      {
2513
0
        bool dyn;
2514
2515
0
        off = h->got.offset;
2516
0
        dyn = htab->elf.dynamic_sections_created;
2517
2518
0
        if (s390_is_ifunc_symbol_p (h))
2519
0
    {
2520
0
      BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2521
0
      if (off == (bfd_vma)-1)
2522
0
        {
2523
          /* No explicit GOT usage so redirect to the
2524
       got.iplt slot.  */
2525
0
          relocation = (s390_gotplt_offset (info)
2526
0
            + htab->elf.igotplt->output_offset
2527
0
            + (h->plt.offset / PLT_ENTRY_SIZE
2528
0
               * GOT_ENTRY_SIZE));
2529
2530
          /* For @GOTENT the relocation is against the offset between
2531
       the instruction and the symbols entry in the GOT and not
2532
       between the start of the GOT and the symbols entry. We
2533
       add the vma of the GOT to get the correct value.  */
2534
0
          if (r_type == R_390_GOTENT || r_type == R_390_GOTPLTENT)
2535
0
      relocation += s390_got_pointer (info);
2536
2537
0
          break;
2538
0
        }
2539
0
      else
2540
0
        {
2541
          /* Explicit GOT slots must contain the address
2542
       of the PLT slot. This will be handled in
2543
       finish_dynamic_symbol.  */
2544
0
        }
2545
0
    }
2546
0
        else if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2547
0
                bfd_link_pic (info),
2548
0
                h)
2549
0
           || SYMBOL_REFERENCES_LOCAL (info, h)
2550
0
           || resolved_to_zero)
2551
0
    {
2552
      /* This is actually a static link, or it is a
2553
         -Bsymbolic link and the symbol is defined
2554
         locally, or the symbol was forced to be local
2555
         because of a version file.  We must initialize
2556
         this entry in the global offset table.  Since the
2557
         offset must always be a multiple of 2, we use the
2558
         least significant bit to record whether we have
2559
         initialized it already.
2560
2561
         When doing a dynamic link, we create a .rel.got
2562
         relocation entry to initialize the value.  This
2563
         is done in the finish_dynamic_symbol routine.  */
2564
0
      if ((off & 1) != 0)
2565
0
        off &= ~1;
2566
0
      else
2567
0
        {
2568
0
          bfd_put_64 (output_bfd, relocation,
2569
0
          htab->elf.sgot->contents + off);
2570
0
          h->got.offset |= 1;
2571
0
        }
2572
2573
      /* When turning a GOT slot dereference into a direct
2574
         reference using larl we have to make sure that
2575
         the symbol is 1. properly aligned and 2. it is no
2576
         ABS symbol or will become one.  */
2577
0
      if (relax
2578
0
          && h->def_regular
2579
0
          && SYMBOL_REFERENCES_LOCAL (info, h)
2580
          /* lgrl rx,sym@GOTENT -> larl rx, sym */
2581
0
          && ((r_type == R_390_GOTENT
2582
0
         && (bfd_get_16 (input_bfd,
2583
0
             contents + rel->r_offset - 2)
2584
0
             & 0xff0f) == 0xc408)
2585
        /* lg rx, sym@GOT(r12) -> larl rx, sym */
2586
0
        || (r_type == R_390_GOT20
2587
0
            && (bfd_get_32 (input_bfd,
2588
0
                contents + rel->r_offset - 2)
2589
0
          & 0xff00f000) == 0xe300c000
2590
0
            && bfd_get_8 (input_bfd,
2591
0
              contents + rel->r_offset + 3) == 0x04))
2592
0
          && !bfd_is_abs_symbol (&h->root)
2593
0
          && h != htab->elf.hdynamic
2594
0
          && h != htab->elf.hgot
2595
0
          && h != htab->elf.hplt
2596
0
          && !((h->root.u.def.value
2597
0
          + sec->output_section->vma
2598
0
          + sec->output_offset) & 1))
2599
0
        {
2600
0
          unsigned short new_insn =
2601
0
      (0xc000 | (bfd_get_8 (input_bfd,
2602
0
                contents + rel->r_offset - 1) & 0xf0));
2603
0
          bfd_put_16 (output_bfd, new_insn,
2604
0
          contents + rel->r_offset - 2);
2605
0
          r_type = R_390_PC32DBL;
2606
0
          rel->r_info = ELF64_R_INFO (r_symndx, r_type);
2607
0
          rel->r_addend = 2;
2608
0
          howto = elf_howto_table + r_type;
2609
0
          relocation = h->root.u.def.value
2610
0
      + h->root.u.def.section->output_section->vma
2611
0
      + h->root.u.def.section->output_offset;
2612
0
          goto do_relocation;
2613
0
        }
2614
0
    }
2615
0
        else
2616
0
    unresolved_reloc = false;
2617
0
      }
2618
0
    else
2619
0
      {
2620
0
        if (local_got_offsets == NULL)
2621
0
    abort ();
2622
2623
0
        off = local_got_offsets[r_symndx];
2624
2625
        /* The offset must always be a multiple of 8.  We use
2626
     the least significant bit to record whether we have
2627
     already generated the necessary reloc.  */
2628
0
        if ((off & 1) != 0)
2629
0
    off &= ~1;
2630
0
        else
2631
0
    {
2632
0
      bfd_put_64 (output_bfd, relocation,
2633
0
            htab->elf.sgot->contents + off);
2634
2635
0
      if (bfd_link_pic (info))
2636
0
        {
2637
0
          asection *s;
2638
0
          Elf_Internal_Rela outrel;
2639
0
          bfd_byte *loc;
2640
2641
0
          s = htab->elf.srelgot;
2642
0
          if (s == NULL)
2643
0
      abort ();
2644
2645
0
          outrel.r_offset = (htab->elf.sgot->output_section->vma
2646
0
           + htab->elf.sgot->output_offset
2647
0
           + off);
2648
0
          outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2649
0
          outrel.r_addend = relocation;
2650
0
          loc = s->contents;
2651
0
          loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2652
0
          bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2653
0
        }
2654
2655
0
      local_got_offsets[r_symndx] |= 1;
2656
0
    }
2657
0
      }
2658
2659
0
    if (off >= (bfd_vma) -2)
2660
0
      abort ();
2661
2662
0
    relocation = s390_got_offset (info) + off;
2663
2664
    /* For @GOTENT the relocation is against the offset between
2665
       the instruction and the symbols entry in the GOT and not
2666
       between the start of the GOT and the symbols entry. We
2667
       add the vma of the GOT to get the correct value.  */
2668
0
    if (   r_type == R_390_GOTENT
2669
0
        || r_type == R_390_GOTPLTENT)
2670
0
      relocation += s390_got_pointer (info);
2671
2672
0
    break;
2673
2674
0
  case R_390_GOTOFF16:
2675
0
  case R_390_GOTOFF32:
2676
0
  case R_390_GOTOFF64:
2677
    /* Relocation is relative to the start of the global offset
2678
       table.  */
2679
2680
0
    if (h != NULL
2681
0
        && s390_is_ifunc_symbol_p (h)
2682
0
        && h->def_regular
2683
0
        && !bfd_link_executable (info))
2684
0
      {
2685
0
        relocation = (htab->elf.iplt->output_section->vma
2686
0
          + htab->elf.iplt->output_offset
2687
0
          + h->plt.offset
2688
0
          - s390_got_pointer (info));
2689
0
        goto do_relocation;
2690
0
      }
2691
2692
0
    relocation -= s390_got_pointer (info);
2693
0
    break;
2694
2695
0
  case R_390_GOTPC:
2696
0
  case R_390_GOTPCDBL:
2697
    /* Use global offset table as symbol value.  */
2698
0
    relocation = s390_got_pointer (info);
2699
0
    unresolved_reloc = false;
2700
0
    break;
2701
2702
0
  case R_390_PLT12DBL:
2703
0
  case R_390_PLT16DBL:
2704
0
  case R_390_PLT24DBL:
2705
0
  case R_390_PLT32:
2706
0
  case R_390_PLT32DBL:
2707
0
  case R_390_PLT64:
2708
    /* Relocation is to the entry for this symbol in the
2709
       procedure linkage table.  */
2710
2711
    /* Resolve a PLT32 reloc against a local symbol directly,
2712
       without using the procedure linkage table.  */
2713
0
    if (h == NULL)
2714
0
      break;
2715
2716
0
    if (h->plt.offset == (bfd_vma) -1
2717
0
        || (htab->elf.splt == NULL && !s390_is_ifunc_symbol_p (h)))
2718
0
      {
2719
        /* We didn't make a PLT entry for this symbol.  This
2720
     happens when statically linking PIC code, or when
2721
     using -Bsymbolic.  */
2722
2723
        /* Replace relative long addressing instructions of weak
2724
     symbols, which will definitely resolve to zero, with
2725
     either a load address of 0 or a trapping insn.
2726
     This prevents the PLT32DBL relocation from overflowing in
2727
     case the binary will be loaded at 4GB or more.  */
2728
0
        if (relax
2729
0
      && h->root.type == bfd_link_hash_undefweak
2730
0
      && !h->root.linker_def
2731
0
      && (bfd_link_executable (info)
2732
0
          || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2733
0
      && r_type == R_390_PLT32DBL
2734
0
      && rel->r_offset >= 2)
2735
0
    {
2736
0
      void *insn_start = contents + rel->r_offset - 2;
2737
0
      uint16_t op = bfd_get_16 (input_bfd, insn_start) & 0xff0f;
2738
0
      uint8_t reg = bfd_get_8 (input_bfd, insn_start + 1) & 0xf0;
2739
2740
      /* NOTE: The order of the if's is important!  */
2741
      /* Replace load address relative long (larl) with load
2742
         address (lay) */
2743
0
      if (op == 0xc000)
2744
0
        {
2745
          /* larl rX,<weak sym> -> lay rX,0(0)  */
2746
0
          bfd_put_16 (output_bfd, 0xe300 | reg, insn_start);
2747
0
          bfd_put_32 (output_bfd, 0x71, insn_start + 2);
2748
0
          rel->r_info = ELF64_R_INFO (0, R_390_NONE);
2749
0
          rel->r_addend = 0;
2750
0
          continue;
2751
0
        }
2752
      /* Replace branch relative and save long (brasl) with a trap.  */
2753
0
      else if (op == 0xc005)
2754
0
        {
2755
          /* brasl rX,<weak sym> -> jg .+2 (6-byte trap)  */
2756
0
          bfd_put_16 (output_bfd, 0xc0f4, insn_start);
2757
0
          bfd_put_32 (output_bfd, 0x1, insn_start + 2);
2758
0
          rel->r_info = ELF64_R_INFO (0, R_390_NONE);
2759
0
          rel->r_addend = 0;
2760
0
          continue;
2761
0
        }
2762
0
    }
2763
2764
0
        break;
2765
0
      }
2766
0
    if (s390_is_ifunc_symbol_p (h))
2767
0
      relocation = (htab->elf.iplt->output_section->vma
2768
0
        + htab->elf.iplt->output_offset
2769
0
        + h->plt.offset);
2770
0
    else
2771
0
      relocation = (htab->elf.splt->output_section->vma
2772
0
        + htab->elf.splt->output_offset
2773
0
        + h->plt.offset);
2774
0
    unresolved_reloc = false;
2775
0
    break;
2776
2777
0
  case R_390_PLTOFF16:
2778
0
  case R_390_PLTOFF32:
2779
0
  case R_390_PLTOFF64:
2780
    /* Relocation is to the entry for this symbol in the
2781
       procedure linkage table relative to the start of the GOT.  */
2782
2783
    /* For local symbols or if we didn't make a PLT entry for
2784
       this symbol resolve the symbol directly.  */
2785
0
    if (h == NULL
2786
0
        || h->plt.offset == (bfd_vma) -1
2787
0
        || (htab->elf.splt == NULL && !s390_is_ifunc_symbol_p (h)))
2788
0
      {
2789
0
        relocation -= s390_got_pointer (info);
2790
0
        break;
2791
0
      }
2792
2793
0
    if (s390_is_ifunc_symbol_p (h))
2794
0
      relocation = (htab->elf.iplt->output_section->vma
2795
0
        + htab->elf.iplt->output_offset
2796
0
        + h->plt.offset
2797
0
        - s390_got_pointer (info));
2798
0
    else
2799
0
      relocation = (htab->elf.splt->output_section->vma
2800
0
        + htab->elf.splt->output_offset
2801
0
        + h->plt.offset
2802
0
        - s390_got_pointer (info));
2803
0
    unresolved_reloc = false;
2804
0
    break;
2805
2806
0
  case R_390_PC16:
2807
0
  case R_390_PC12DBL:
2808
0
  case R_390_PC16DBL:
2809
0
  case R_390_PC24DBL:
2810
0
  case R_390_PC32:
2811
0
  case R_390_PC32DBL:
2812
0
  case R_390_PC64:
2813
0
    if (h != NULL
2814
0
        && bfd_link_pie (info)
2815
0
        && !h->def_regular)
2816
0
      {
2817
0
        _bfd_error_handler (_("%pB: `%s' non-PLT reloc for symbol defined "
2818
0
            "in shared library and accessed "
2819
0
            "from executable "
2820
0
            "(rebuild file with -fPIC ?)"),
2821
0
          input_bfd, h->root.root.string);
2822
0
        bfd_set_error (bfd_error_bad_value);
2823
0
        return false;
2824
0
      }
2825
    /* The target of these relocs are instruction operands
2826
       residing in read-only sections.  We cannot emit a runtime
2827
       reloc for it.  */
2828
0
    if (h != NULL
2829
0
        && s390_is_ifunc_symbol_p (h)
2830
0
        && h->def_regular
2831
0
        && bfd_link_pic (info))
2832
0
      {
2833
0
        relocation = (htab->elf.iplt->output_section->vma
2834
0
          + htab->elf.iplt->output_offset
2835
0
          + h->plt.offset);
2836
0
        goto do_relocation;
2837
0
      }
2838
2839
    /* Replace relative long addressing instructions of weak
2840
       symbols, which will definitely resolve to zero, with
2841
       either a load address of 0, a NOP, or a trapping insn.
2842
       This prevents the PC32DBL relocation from overflowing in
2843
       case the binary will be loaded at 4GB or more.  */
2844
0
    if (relax
2845
0
        && h != NULL
2846
0
        && h->root.type == bfd_link_hash_undefweak
2847
0
        && !h->root.linker_def
2848
0
        && (bfd_link_executable (info)
2849
0
      || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2850
0
        && r_type == R_390_PC32DBL
2851
0
        && rel->r_offset >= 2)
2852
0
      {
2853
0
        void *insn_start = contents + rel->r_offset - 2;
2854
0
        uint16_t op = bfd_get_16 (input_bfd, insn_start) & 0xff0f;
2855
0
        uint8_t reg = bfd_get_8 (input_bfd, insn_start + 1) & 0xf0;
2856
2857
        /* NOTE: The order of the if's is important!  */
2858
        /* Replace load address relative long (larl) with load
2859
     address (lay) */
2860
0
        if (op == 0xc000)
2861
0
    {
2862
      /* larl rX,<weak sym> -> lay rX,0(0)  */
2863
0
      bfd_put_16 (output_bfd, 0xe300 | reg, insn_start);
2864
0
      bfd_put_32 (output_bfd, 0x71, insn_start + 2);
2865
0
      rel->r_info = ELF64_R_INFO (0, R_390_NONE);
2866
0
      rel->r_addend = 0;
2867
0
      continue;
2868
0
    }
2869
        /* Replace prefetch data relative long (pfdrl) with a NOP  */
2870
0
        else if (op == 0xc602)
2871
0
    {
2872
      /* Emit a 6-byte NOP: jgnop .  */
2873
0
      bfd_put_16 (output_bfd, 0xc004, insn_start);
2874
0
      bfd_put_32 (output_bfd, 0x0, insn_start + 2);
2875
0
      rel->r_info = ELF64_R_INFO (0, R_390_NONE);
2876
0
      rel->r_addend = 0;
2877
0
      continue;
2878
0
    }
2879
        /* Replace the following instructions with a trap:
2880
     - branch relative and save long (brasl)
2881
     - load (logical) relative long (lrl, lgrl, lgfrl, llgfrl)
2882
     - load (logical) halfword relative long (lhrl, lghrl, llhrl, llghrl)
2883
     - store relative long (strl, stgrl)
2884
     - store halfword relative long (sthrl)
2885
     - execute relative long (exrl)
2886
     - compare (logical) relative long (crl, clrl, cgrl, clgrl, cgfrl, clgfrl)
2887
     - compare (logical) halfword relative long (chrl, cghrl, clhrl, clghrl)  */
2888
0
        else if (op == 0xc005 || (op & 0xff00) == 0xc400
2889
0
           || (op & 0xff00) == 0xc600)
2890
0
    {
2891
      /* Emit a 6-byte trap: jg .+2  */
2892
0
      bfd_put_16 (output_bfd, 0xc0f4, insn_start);
2893
0
      bfd_put_32 (output_bfd, 0x1, insn_start + 2);
2894
0
      rel->r_info = ELF64_R_INFO (0, R_390_NONE);
2895
0
      rel->r_addend = 0;
2896
0
      continue;
2897
0
    }
2898
0
      }
2899
    /* Fall through.  */
2900
2901
0
  case R_390_8:
2902
0
  case R_390_16:
2903
0
  case R_390_32:
2904
0
  case R_390_64:
2905
2906
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2907
0
      break;
2908
2909
0
    if (h != NULL
2910
0
        && s390_is_ifunc_symbol_p (h)
2911
0
        && h->def_regular)
2912
0
      {
2913
0
        if (!bfd_link_pic (info))
2914
0
    {
2915
      /* For a non-shared object the symbol will not
2916
         change.  Hence we can write the address of the
2917
         target IPLT slot now.  */
2918
0
      relocation = (htab->elf.iplt->output_section->vma
2919
0
        + htab->elf.iplt->output_offset
2920
0
        + h ->plt.offset);
2921
0
      goto do_relocation;
2922
0
    }
2923
0
        else
2924
0
    {
2925
      /* For shared objects a runtime relocation is needed.  */
2926
2927
0
      Elf_Internal_Rela outrel;
2928
0
      asection *sreloc;
2929
2930
      /* Need a dynamic relocation to get the real function
2931
         address.  */
2932
0
      outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2933
0
                   info,
2934
0
                   input_section,
2935
0
                   rel->r_offset);
2936
0
      if (outrel.r_offset == (bfd_vma) -1
2937
0
          || outrel.r_offset == (bfd_vma) -2)
2938
0
        abort ();
2939
2940
0
      outrel.r_offset += (input_section->output_section->vma
2941
0
              + input_section->output_offset);
2942
2943
0
      if (h->dynindx == -1
2944
0
          || h->forced_local
2945
0
          || bfd_link_executable (info))
2946
0
        {
2947
          /* This symbol is resolved locally.  */
2948
0
          outrel.r_info = ELF64_R_INFO (0, R_390_IRELATIVE);
2949
0
          outrel.r_addend = (h->root.u.def.value
2950
0
           + h->root.u.def.section->output_section->vma
2951
0
           + h->root.u.def.section->output_offset);
2952
0
        }
2953
0
      else
2954
0
        {
2955
0
          outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2956
0
          outrel.r_addend = 0;
2957
0
        }
2958
2959
0
      sreloc = htab->elf.irelifunc;
2960
0
      _bfd_elf_append_rela (output_bfd, sreloc, &outrel);
2961
2962
      /* If this reloc is against an external symbol, we
2963
         do not want to fiddle with the addend.  Otherwise,
2964
         we need to include the symbol value so that it
2965
         becomes an addend for the dynamic reloc.  For an
2966
         internal symbol, we have updated addend.  */
2967
0
      continue;
2968
0
    }
2969
0
      }
2970
2971
0
    if ((bfd_link_pic (info)
2972
0
         && (h == NULL
2973
0
       || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2974
0
           && !resolved_to_zero)
2975
0
       || h->root.type != bfd_link_hash_undefweak)
2976
0
         && ((r_type != R_390_PC16
2977
0
        && r_type != R_390_PC12DBL
2978
0
        && r_type != R_390_PC16DBL
2979
0
        && r_type != R_390_PC24DBL
2980
0
        && r_type != R_390_PC32
2981
0
        && r_type != R_390_PC32DBL
2982
0
        && r_type != R_390_PC64)
2983
0
       || !SYMBOL_CALLS_LOCAL (info, h)))
2984
0
        || (ELIMINATE_COPY_RELOCS
2985
0
      && !bfd_link_pic (info)
2986
0
      && h != NULL
2987
0
      && h->dynindx != -1
2988
0
      && !h->non_got_ref
2989
0
      && ((h->def_dynamic
2990
0
           && !h->def_regular)
2991
0
          || h->root.type == bfd_link_hash_undefweak
2992
0
          || h->root.type == bfd_link_hash_undefined)))
2993
0
      {
2994
0
        Elf_Internal_Rela outrel;
2995
0
        bool skip, relocate;
2996
0
        asection *sreloc;
2997
0
        bfd_byte *loc;
2998
2999
        /* When generating a shared object, these relocations
3000
     are copied into the output file to be resolved at run
3001
     time.  */
3002
0
        skip = false;
3003
0
        relocate = false;
3004
3005
0
        outrel.r_offset =
3006
0
    _bfd_elf_section_offset (output_bfd, info, input_section,
3007
0
           rel->r_offset);
3008
0
        if (outrel.r_offset == (bfd_vma) -1)
3009
0
    skip = true;
3010
0
        else if (outrel.r_offset == (bfd_vma) -2)
3011
0
    skip = true, relocate = true;
3012
3013
0
        outrel.r_offset += (input_section->output_section->vma
3014
0
          + input_section->output_offset);
3015
3016
0
        if (skip)
3017
0
    memset (&outrel, 0, sizeof outrel);
3018
0
        else if (h != NULL
3019
0
           && h->dynindx != -1
3020
0
           && (r_type == R_390_PC16
3021
0
         || r_type == R_390_PC12DBL
3022
0
         || r_type == R_390_PC16DBL
3023
0
         || r_type == R_390_PC24DBL
3024
0
         || r_type == R_390_PC32
3025
0
         || r_type == R_390_PC32DBL
3026
0
         || r_type == R_390_PC64
3027
0
         || !(bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3028
0
         || !h->def_regular))
3029
0
    {
3030
0
      outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3031
0
      outrel.r_addend = rel->r_addend;
3032
0
    }
3033
0
        else
3034
0
    {
3035
      /* This symbol is local, or marked to become local.  */
3036
0
      outrel.r_addend = relocation + rel->r_addend;
3037
0
      if (r_type == R_390_64)
3038
0
        {
3039
0
          relocate = true;
3040
0
          outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
3041
0
        }
3042
0
      else
3043
0
        {
3044
0
          long sindx;
3045
3046
0
          if (bfd_is_abs_section (sec))
3047
0
      sindx = 0;
3048
0
          else if (sec == NULL || sec->owner == NULL)
3049
0
      {
3050
0
        bfd_set_error(bfd_error_bad_value);
3051
0
        return false;
3052
0
      }
3053
0
          else
3054
0
      {
3055
0
        asection *osec;
3056
3057
0
        osec = sec->output_section;
3058
0
        sindx = elf_section_data (osec)->dynindx;
3059
3060
0
        if (sindx == 0)
3061
0
          {
3062
0
            osec = htab->elf.text_index_section;
3063
0
            sindx = elf_section_data (osec)->dynindx;
3064
0
          }
3065
0
        BFD_ASSERT (sindx != 0);
3066
3067
        /* We are turning this relocation into one
3068
           against a section symbol, so subtract out
3069
           the output section's address but not the
3070
           offset of the input section in the output
3071
           section.  */
3072
0
        outrel.r_addend -= osec->vma;
3073
0
      }
3074
0
          outrel.r_info = ELF64_R_INFO (sindx, r_type);
3075
0
        }
3076
0
    }
3077
3078
0
        sreloc = elf_section_data (input_section)->sreloc;
3079
0
        if (sreloc == NULL)
3080
0
    abort ();
3081
3082
0
        loc = sreloc->contents;
3083
0
        loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3084
0
        bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3085
3086
        /* If this reloc is against an external symbol, we do
3087
     not want to fiddle with the addend.  Otherwise, we
3088
     need to include the symbol value so that it becomes
3089
     an addend for the dynamic reloc.  */
3090
0
        if (! relocate)
3091
0
    continue;
3092
0
      }
3093
3094
0
    break;
3095
3096
    /* Relocations for tls literal pool entries.  */
3097
0
  case R_390_TLS_IE64:
3098
0
    if (bfd_link_dll (info))
3099
0
      {
3100
0
        Elf_Internal_Rela outrel;
3101
0
        asection *sreloc;
3102
0
        bfd_byte *loc;
3103
3104
0
        outrel.r_offset = rel->r_offset
3105
0
        + input_section->output_section->vma
3106
0
        + input_section->output_offset;
3107
0
        outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
3108
0
        sreloc = elf_section_data (input_section)->sreloc;
3109
0
        if (sreloc == NULL)
3110
0
    abort ();
3111
0
        loc = sreloc->contents;
3112
0
        loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3113
0
        bfd_elf64_swap_reloc_out (output_bfd, &outrel, loc);
3114
0
      }
3115
    /* Fall through.  */
3116
3117
0
  case R_390_TLS_GD64:
3118
0
  case R_390_TLS_GOTIE64:
3119
0
    r_type = elf_s390_tls_transition (info, r_type, h == NULL);
3120
0
    tls_type = GOT_UNKNOWN;
3121
0
    if (h == NULL && local_got_offsets)
3122
0
      tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
3123
0
    else if (h != NULL)
3124
0
      {
3125
0
        tls_type = elf_s390_hash_entry(h)->tls_type;
3126
0
        if (!bfd_link_dll (info) && h->dynindx == -1 && tls_type >= GOT_TLS_IE)
3127
0
    r_type = R_390_TLS_LE64;
3128
0
      }
3129
0
    if (r_type == R_390_TLS_GD64 && tls_type >= GOT_TLS_IE)
3130
0
      r_type = R_390_TLS_IE64;
3131
3132
0
    if (r_type == R_390_TLS_LE64)
3133
0
      {
3134
        /* This relocation gets optimized away by the local exec
3135
     access optimization.  */
3136
0
        BFD_ASSERT (! unresolved_reloc);
3137
0
        bfd_put_64 (output_bfd, -tpoff (info, relocation) + rel->r_addend,
3138
0
        contents + rel->r_offset);
3139
0
        continue;
3140
0
      }
3141
3142
0
    if (htab->elf.sgot == NULL)
3143
0
      abort ();
3144
3145
0
    if (h != NULL)
3146
0
      off = h->got.offset;
3147
0
    else
3148
0
      {
3149
0
        if (local_got_offsets == NULL)
3150
0
    abort ();
3151
3152
0
        off = local_got_offsets[r_symndx];
3153
0
      }
3154
3155
0
  emit_tls_relocs:
3156
3157
0
    if ((off & 1) != 0)
3158
0
      off &= ~1;
3159
0
    else
3160
0
      {
3161
0
        Elf_Internal_Rela outrel;
3162
0
        bfd_byte *loc;
3163
0
        int dr_type, indx;
3164
3165
0
        if (htab->elf.srelgot == NULL)
3166
0
    abort ();
3167
3168
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3169
0
         + htab->elf.sgot->output_offset + off);
3170
3171
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
3172
0
        if (r_type == R_390_TLS_GD64)
3173
0
    dr_type = R_390_TLS_DTPMOD;
3174
0
        else
3175
0
    dr_type = R_390_TLS_TPOFF;
3176
0
        if (dr_type == R_390_TLS_TPOFF && indx == 0)
3177
0
    outrel.r_addend = relocation - dtpoff_base (info);
3178
0
        else
3179
0
    outrel.r_addend = 0;
3180
0
        outrel.r_info = ELF64_R_INFO (indx, dr_type);
3181
0
        loc = htab->elf.srelgot->contents;
3182
0
        loc += htab->elf.srelgot->reloc_count++
3183
0
    * sizeof (Elf64_External_Rela);
3184
0
        bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3185
3186
0
        if (r_type == R_390_TLS_GD64)
3187
0
    {
3188
0
      if (indx == 0)
3189
0
        {
3190
0
          BFD_ASSERT (! unresolved_reloc);
3191
0
          bfd_put_64 (output_bfd,
3192
0
          relocation - dtpoff_base (info),
3193
0
          htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3194
0
        }
3195
0
      else
3196
0
        {
3197
0
          outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_DTPOFF);
3198
0
          outrel.r_offset += GOT_ENTRY_SIZE;
3199
0
          outrel.r_addend = 0;
3200
0
          htab->elf.srelgot->reloc_count++;
3201
0
          loc += sizeof (Elf64_External_Rela);
3202
0
          bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3203
0
        }
3204
0
    }
3205
3206
0
        if (h != NULL)
3207
0
    h->got.offset |= 1;
3208
0
        else
3209
0
    local_got_offsets[r_symndx] |= 1;
3210
0
      }
3211
3212
0
    if (off >= (bfd_vma) -2)
3213
0
      abort ();
3214
0
    if (r_type == ELF64_R_TYPE (rel->r_info))
3215
0
      {
3216
0
        relocation = htab->elf.sgot->output_offset + off;
3217
0
        if (r_type == R_390_TLS_IE64 || r_type == R_390_TLS_IEENT)
3218
0
    relocation += htab->elf.sgot->output_section->vma;
3219
0
        unresolved_reloc = false;
3220
0
      }
3221
0
    else
3222
0
      {
3223
0
        bfd_put_64 (output_bfd, htab->elf.sgot->output_offset + off,
3224
0
        contents + rel->r_offset);
3225
0
        continue;
3226
0
      }
3227
0
    break;
3228
3229
0
  case R_390_TLS_GOTIE12:
3230
0
  case R_390_TLS_GOTIE20:
3231
0
  case R_390_TLS_IEENT:
3232
0
    if (h == NULL)
3233
0
      {
3234
0
        if (local_got_offsets == NULL)
3235
0
    abort();
3236
0
        off = local_got_offsets[r_symndx];
3237
0
        if (bfd_link_dll (info))
3238
0
    goto emit_tls_relocs;
3239
0
      }
3240
0
    else
3241
0
      {
3242
0
        off = h->got.offset;
3243
0
        tls_type = elf_s390_hash_entry(h)->tls_type;
3244
0
        if (bfd_link_dll (info) || h->dynindx != -1 || tls_type < GOT_TLS_IE)
3245
0
    goto emit_tls_relocs;
3246
0
      }
3247
3248
0
    if (htab->elf.sgot == NULL)
3249
0
      abort ();
3250
3251
0
    BFD_ASSERT (! unresolved_reloc);
3252
0
    bfd_put_64 (output_bfd, -tpoff (info, relocation),
3253
0
          htab->elf.sgot->contents + off);
3254
0
    relocation = htab->elf.sgot->output_offset + off;
3255
0
    if (r_type == R_390_TLS_IEENT)
3256
0
      relocation += htab->elf.sgot->output_section->vma;
3257
0
    unresolved_reloc = false;
3258
0
    break;
3259
3260
0
  case R_390_TLS_LDM64:
3261
0
    if (! bfd_link_dll (info))
3262
      /* The literal pool entry this relocation refers to gets ignored
3263
         by the optimized code of the local exec model. Do nothing
3264
         and the value will turn out zero.  */
3265
0
      continue;
3266
3267
0
    if (htab->elf.sgot == NULL)
3268
0
      abort ();
3269
3270
0
    off = htab->tls_ldm_got.offset;
3271
0
    if (off & 1)
3272
0
      off &= ~1;
3273
0
    else
3274
0
      {
3275
0
        Elf_Internal_Rela outrel;
3276
0
        bfd_byte *loc;
3277
3278
0
        if (htab->elf.srelgot == NULL)
3279
0
    abort ();
3280
3281
0
        outrel.r_offset = (htab->elf.sgot->output_section->vma
3282
0
         + htab->elf.sgot->output_offset + off);
3283
3284
0
        bfd_put_64 (output_bfd, 0,
3285
0
        htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3286
0
        outrel.r_info = ELF64_R_INFO (0, R_390_TLS_DTPMOD);
3287
0
        outrel.r_addend = 0;
3288
0
        loc = htab->elf.srelgot->contents;
3289
0
        loc += htab->elf.srelgot->reloc_count++
3290
0
    * sizeof (Elf64_External_Rela);
3291
0
        bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3292
0
        htab->tls_ldm_got.offset |= 1;
3293
0
      }
3294
0
    relocation = htab->elf.sgot->output_offset + off;
3295
0
    unresolved_reloc = false;
3296
0
    break;
3297
3298
0
  case R_390_TLS_LE64:
3299
0
    if (bfd_link_dll (info))
3300
0
      {
3301
        /* Linking a shared library with non-fpic code requires
3302
     a R_390_TLS_TPOFF relocation.  */
3303
0
        Elf_Internal_Rela outrel;
3304
0
        asection *sreloc;
3305
0
        bfd_byte *loc;
3306
0
        int indx;
3307
3308
0
        outrel.r_offset = rel->r_offset
3309
0
        + input_section->output_section->vma
3310
0
        + input_section->output_offset;
3311
0
        if (h != NULL && h->dynindx != -1)
3312
0
    indx = h->dynindx;
3313
0
        else
3314
0
    indx = 0;
3315
0
        outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_TPOFF);
3316
0
        if (indx == 0)
3317
0
    outrel.r_addend = relocation - dtpoff_base (info);
3318
0
        else
3319
0
    outrel.r_addend = 0;
3320
0
        sreloc = elf_section_data (input_section)->sreloc;
3321
0
        if (sreloc == NULL)
3322
0
    abort ();
3323
0
        loc = sreloc->contents;
3324
0
        loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3325
0
        bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3326
0
      }
3327
0
    else
3328
0
      {
3329
0
        BFD_ASSERT (! unresolved_reloc);
3330
0
        bfd_put_64 (output_bfd, -tpoff (info, relocation) + rel->r_addend,
3331
0
        contents + rel->r_offset);
3332
0
      }
3333
0
    continue;
3334
3335
0
  case R_390_TLS_LDO64:
3336
0
    if (bfd_link_dll (info) || (input_section->flags & SEC_DEBUGGING))
3337
0
      relocation -= dtpoff_base (info);
3338
0
    else
3339
      /* When converting LDO to LE, we must negate.  */
3340
0
      relocation = -tpoff (info, relocation);
3341
0
    break;
3342
3343
    /* Relocations for tls instructions.  */
3344
0
  case R_390_TLS_LOAD:
3345
0
  case R_390_TLS_GDCALL:
3346
0
  case R_390_TLS_LDCALL:
3347
0
    tls_type = GOT_UNKNOWN;
3348
0
    if (h == NULL && local_got_offsets)
3349
0
      tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
3350
0
    else if (h != NULL)
3351
0
      tls_type = elf_s390_hash_entry(h)->tls_type;
3352
3353
0
    if (tls_type == GOT_TLS_GD)
3354
0
      continue;
3355
3356
0
    if (r_type == R_390_TLS_LOAD)
3357
0
      {
3358
0
        if (!bfd_link_dll (info) && (h == NULL || h->dynindx == -1))
3359
0
    {
3360
      /* IE->LE transition. Four valid cases:
3361
         lg %rx,(0,%ry)    -> sllg %rx,%ry,0
3362
         lg %rx,(%ry,0)    -> sllg %rx,%ry,0
3363
         lg %rx,(%ry,%r12) -> sllg %rx,%ry,0
3364
         lg %rx,(%r12,%ry) -> sllg %rx,%ry,0  */
3365
0
      unsigned int insn0, insn1, ry;
3366
3367
0
      insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3368
0
      insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3369
0
      if (insn1 != 0x0004)
3370
0
        {
3371
0
          invalid_tls_insn (input_bfd, input_section, rel);
3372
0
          return false;
3373
0
        }
3374
0
      if ((insn0 & 0xff00f000) == 0xe3000000)
3375
        /* lg %rx,0(%ry,0) -> sllg %rx,%ry,0  */
3376
0
        ry = (insn0 & 0x000f0000);
3377
0
      else if ((insn0 & 0xff0f0000) == 0xe3000000)
3378
        /* lg %rx,0(0,%ry) -> sllg %rx,%ry,0  */
3379
0
        ry = (insn0 & 0x0000f000) << 4;
3380
0
      else if ((insn0 & 0xff00f000) == 0xe300c000)
3381
        /* lg %rx,0(%ry,%r12) -> sllg %rx,%ry,0  */
3382
0
        ry = (insn0 & 0x000f0000);
3383
0
      else if ((insn0 & 0xff0f0000) == 0xe30c0000)
3384
        /* lg %rx,0(%r12,%ry) -> sllg %rx,%ry,0  */
3385
0
        ry = (insn0 & 0x0000f000) << 4;
3386
0
      else
3387
0
        {
3388
0
          invalid_tls_insn (input_bfd, input_section, rel);
3389
0
          return false;
3390
0
        }
3391
0
      insn0 = 0xeb000000 | (insn0 & 0x00f00000) | ry;
3392
0
      insn1 = 0x000d;
3393
0
      bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3394
0
      bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3395
0
    }
3396
0
      }
3397
0
    else if (r_type == R_390_TLS_GDCALL)
3398
0
      {
3399
0
        unsigned int insn0, insn1;
3400
3401
0
        insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3402
0
        insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3403
0
        if ((insn0 & 0xffff0000) != 0xc0e50000)
3404
0
    {
3405
0
      invalid_tls_insn (input_bfd, input_section, rel);
3406
0
      return false;
3407
0
    }
3408
0
        if (!bfd_link_dll (info) && (h == NULL || h->dynindx == -1))
3409
0
    {
3410
      /* GD->LE transition.
3411
         brasl %r14,__tls_get_addr@plt -> brcl 0,. */
3412
0
      insn0 = 0xc0040000;
3413
0
      insn1 = 0x0000;
3414
0
    }
3415
0
        else
3416
0
    {
3417
      /* GD->IE transition.
3418
         brasl %r14,__tls_get_addr@plt -> lg %r2,0(%r2,%r12)  */
3419
0
      insn0 = 0xe322c000;
3420
0
      insn1 = 0x0004;
3421
0
    }
3422
0
        bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3423
0
        bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3424
0
      }
3425
0
    else if (r_type == R_390_TLS_LDCALL)
3426
0
      {
3427
0
        if (!bfd_link_dll (info))
3428
0
    {
3429
0
      unsigned int insn0, insn1;
3430
3431
0
      insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3432
0
      insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3433
0
      if ((insn0 & 0xffff0000) != 0xc0e50000)
3434
0
        {
3435
0
          invalid_tls_insn (input_bfd, input_section, rel);
3436
0
          return false;
3437
0
        }
3438
      /* LD->LE transition.
3439
         brasl %r14,__tls_get_addr@plt -> brcl 0,. */
3440
0
      insn0 = 0xc0040000;
3441
0
      insn1 = 0x0000;
3442
0
      bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3443
0
      bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3444
0
    }
3445
0
      }
3446
0
    continue;
3447
3448
0
  default:
3449
0
    break;
3450
0
  }
3451
3452
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3453
   because such sections are not SEC_ALLOC and thus ld.so will
3454
   not process them.  */
3455
0
      if (unresolved_reloc
3456
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
3457
0
         && h->def_dynamic)
3458
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
3459
0
              rel->r_offset) != (bfd_vma) -1)
3460
0
  _bfd_error_handler
3461
    /* xgettext:c-format */
3462
0
    (_("%pB(%pA+%#" PRIx64 "): "
3463
0
       "unresolvable %s relocation against symbol `%s'"),
3464
0
     input_bfd,
3465
0
     input_section,
3466
0
     (uint64_t) rel->r_offset,
3467
0
     howto->name,
3468
0
     h->root.root.string);
3469
3470
0
    do_relocation:
3471
3472
      /* When applying a 24 bit reloc we need to start one byte
3473
   earlier.  Otherwise the 32 bit get/put bfd operations might
3474
   access a byte after the actual section.  */
3475
0
      if (r_type == R_390_PC24DBL
3476
0
    || r_type == R_390_PLT24DBL)
3477
0
  rel->r_offset--;
3478
3479
      /* Issue an error if the right shift implied by the relocation
3480
   would drop bits from the symbol value.  */
3481
0
      if (howto->rightshift
3482
0
    && (relocation & (((bfd_vma)1 << howto->rightshift) - 1)))
3483
0
  {
3484
0
    _bfd_error_handler
3485
      /* xgettext:c-format */
3486
0
      (_("%pB(%pA+%#" PRIx64 "): "
3487
0
         "relocation %s against misaligned symbol `%s' (%#" PRIx64 ") in %pB"),
3488
0
       input_bfd,
3489
0
       input_section,
3490
0
       (uint64_t) rel->r_offset,
3491
0
       howto->name,
3492
0
       h->root.root.string,
3493
0
       (uint64_t)relocation,
3494
0
       sec->owner);
3495
0
    return false;
3496
0
  }
3497
3498
0
      if (r_type == R_390_20
3499
0
    || r_type == R_390_GOT20
3500
0
    || r_type == R_390_GOTPLT20
3501
0
    || r_type == R_390_TLS_GOTIE20)
3502
0
  {
3503
0
    relocation += rel->r_addend;
3504
0
    relocation = (relocation&0xfff) << 8 | (relocation&0xff000) >> 12;
3505
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3506
0
          contents, rel->r_offset,
3507
0
          relocation, 0);
3508
0
  }
3509
0
      else
3510
0
  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3511
0
              contents, rel->r_offset,
3512
0
              relocation, rel->r_addend);
3513
3514
0
      if (r != bfd_reloc_ok)
3515
0
  {
3516
0
    const char *name;
3517
3518
0
    if (h != NULL)
3519
0
      name = h->root.root.string;
3520
0
    else
3521
0
      {
3522
0
        name = bfd_elf_string_from_elf_section (input_bfd,
3523
0
                  symtab_hdr->sh_link,
3524
0
                  sym->st_name);
3525
0
        if (name == NULL)
3526
0
    return false;
3527
0
        if (*name == '\0')
3528
0
    name = bfd_section_name (sec);
3529
0
      }
3530
3531
0
    if (r == bfd_reloc_overflow)
3532
0
      (*info->callbacks->reloc_overflow)
3533
0
        (info, (h ? &h->root : NULL), name, howto->name,
3534
0
         (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3535
0
    else
3536
0
      {
3537
0
        _bfd_error_handler
3538
    /* xgettext:c-format */
3539
0
    (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3540
0
     input_bfd, input_section,
3541
0
     (uint64_t) rel->r_offset, name, (int) r);
3542
0
        return false;
3543
0
      }
3544
0
  }
3545
0
    }
3546
3547
0
  return true;
3548
0
}
3549
3550
/* Generate the PLT slots together with the dynamic relocations needed
3551
   for IFUNC symbols.  */
3552
3553
static void
3554
elf_s390_finish_ifunc_symbol (bfd *output_bfd,
3555
            struct bfd_link_info *info,
3556
            struct elf_link_hash_entry *h,
3557
            struct elf_s390_link_hash_table *htab,
3558
            bfd_vma plt_offset,
3559
            bfd_vma resolver_address)
3560
0
{
3561
0
  bfd_vma plt_index;
3562
0
  bfd_vma got_offset;
3563
0
  Elf_Internal_Rela rela;
3564
0
  bfd_byte *loc;
3565
0
  asection *plt, *gotplt, *relplt;
3566
3567
0
  if (htab->elf.iplt == NULL
3568
0
      || htab->elf.igotplt == NULL
3569
0
      || htab->elf.irelplt == NULL)
3570
0
    abort ();
3571
3572
  /* Index of the PLT slot within iplt section.  */
3573
0
  plt_index = plt_offset / PLT_ENTRY_SIZE;
3574
0
  plt = htab->elf.iplt;
3575
  /* Offset into the igot.plt section.  */
3576
0
  got_offset = plt_index * GOT_ENTRY_SIZE;
3577
0
  gotplt = htab->elf.igotplt;
3578
0
  relplt = htab->elf.irelplt;
3579
3580
  /* Fill in the blueprint of a PLT.  */
3581
0
  memcpy (plt->contents + plt_offset, elf_s390x_plt_entry,
3582
0
    PLT_ENTRY_SIZE);
3583
3584
  /* Fixup the relative address to the GOT entry */
3585
0
  bfd_put_32 (output_bfd,
3586
0
        (gotplt->output_section->vma +
3587
0
         gotplt->output_offset + got_offset
3588
0
         - (plt->output_section->vma +
3589
0
      plt->output_offset +
3590
0
      plt_offset))/2,
3591
0
        plt->contents + plt_offset + 2);
3592
  /* Fixup the relative branch to PLT 0 */
3593
0
  bfd_put_32 (output_bfd, - (plt->output_offset +
3594
0
           (PLT_ENTRY_SIZE * plt_index) + 22)/2,
3595
0
        plt->contents + plt_offset + 24);
3596
  /* Fixup offset into .rela.plt section.  */
3597
0
  bfd_put_32 (output_bfd, relplt->output_offset +
3598
0
        plt_index * sizeof (Elf64_External_Rela),
3599
0
        plt->contents + plt_offset + 28);
3600
3601
  /* Fill in the entry in the global offset table.
3602
     Points to instruction after GOT offset.  */
3603
0
  bfd_put_64 (output_bfd,
3604
0
        (plt->output_section->vma
3605
0
         + plt->output_offset
3606
0
         + plt_offset
3607
0
         + 14),
3608
0
        gotplt->contents + got_offset);
3609
3610
  /* Fill in the entry in the .rela.plt section.  */
3611
0
  rela.r_offset = (gotplt->output_section->vma
3612
0
       + gotplt->output_offset
3613
0
       + got_offset);
3614
3615
0
  if (!h
3616
0
      || h->dynindx == -1
3617
0
      || ((bfd_link_executable (info)
3618
0
     || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3619
0
    && h->def_regular))
3620
0
    {
3621
      /* The symbol can be locally resolved.  */
3622
0
      rela.r_info = ELF64_R_INFO (0, R_390_IRELATIVE);
3623
0
      rela.r_addend = resolver_address;
3624
0
    }
3625
0
  else
3626
0
    {
3627
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
3628
0
      rela.r_addend = 0;
3629
0
    }
3630
3631
0
  loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3632
0
  bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3633
0
}
3634
3635
3636
/* Finish up dynamic symbol handling.  We set the contents of various
3637
   dynamic sections here.  */
3638
3639
static bool
3640
elf_s390_finish_dynamic_symbol (bfd *output_bfd,
3641
        struct bfd_link_info *info,
3642
        struct elf_link_hash_entry *h,
3643
        Elf_Internal_Sym *sym)
3644
0
{
3645
0
  struct elf_s390_link_hash_table *htab;
3646
0
  struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
3647
3648
0
  htab = elf_s390_hash_table (info);
3649
3650
0
  if (h->plt.offset != (bfd_vma) -1)
3651
0
    {
3652
0
      bfd_vma plt_index;
3653
0
      bfd_vma gotplt_offset;
3654
0
      Elf_Internal_Rela rela;
3655
0
      bfd_byte *loc;
3656
3657
      /* This symbol has an entry in the procedure linkage table.  Set
3658
   it up.  */
3659
0
      if (s390_is_ifunc_symbol_p (h) && h->def_regular)
3660
0
  {
3661
0
    elf_s390_finish_ifunc_symbol (output_bfd, info, h,
3662
0
      htab, h->plt.offset,
3663
0
      eh->ifunc_resolver_address +
3664
0
      eh->ifunc_resolver_section->output_offset +
3665
0
      eh->ifunc_resolver_section->output_section->vma);
3666
3667
    /* Do not return yet.  Handling of explicit GOT slots of
3668
       IFUNC symbols is below.  */
3669
0
  }
3670
0
      else
3671
0
  {
3672
0
    if (h->dynindx == -1
3673
0
        || htab->elf.splt == NULL
3674
0
        || htab->elf.sgotplt == NULL
3675
0
        || htab->elf.srelplt == NULL)
3676
0
      abort ();
3677
3678
    /* Calc. index no.
3679
       Current offset - size first entry / entry size.  */
3680
0
    plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3681
3682
    /* The slots in the .got.plt correspond to the PLT slots in
3683
       the same order.  */
3684
0
    gotplt_offset = plt_index * GOT_ENTRY_SIZE;
3685
3686
    /* If .got.plt comes first it needs to contain the 3 header
3687
       entries.  */
3688
0
    if (!s390_gotplt_after_got_p (info))
3689
0
      gotplt_offset += 3 * GOT_ENTRY_SIZE;
3690
3691
    /* Fill in the blueprint of a PLT.  */
3692
0
    memcpy (htab->elf.splt->contents + h->plt.offset, elf_s390x_plt_entry,
3693
0
      PLT_ENTRY_SIZE);
3694
3695
    /* The first instruction in the PLT entry is a LARL loading
3696
       the address of the GOT slot.  We write the 4 byte
3697
       immediate operand of the LARL instruction here.  */
3698
0
    bfd_put_32 (output_bfd,
3699
0
          (htab->elf.sgotplt->output_section->vma +
3700
0
           htab->elf.sgotplt->output_offset + gotplt_offset
3701
0
           - (htab->elf.splt->output_section->vma +
3702
0
        htab->elf.splt->output_offset +
3703
0
        h->plt.offset))/2,
3704
0
          htab->elf.splt->contents + h->plt.offset + 2);
3705
    /* Fixup the relative branch to PLT 0 */
3706
0
    bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
3707
0
             (PLT_ENTRY_SIZE * plt_index) + 22)/2,
3708
0
          htab->elf.splt->contents + h->plt.offset + 24);
3709
    /* Fixup offset into .rela.plt section.  */
3710
0
    bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
3711
0
          htab->elf.splt->contents + h->plt.offset + 28);
3712
3713
    /* Fill in the entry in the global offset table.
3714
       Points to instruction after GOT offset.  */
3715
0
    bfd_put_64 (output_bfd,
3716
0
          (htab->elf.splt->output_section->vma
3717
0
           + htab->elf.splt->output_offset
3718
0
           + h->plt.offset
3719
0
           + 14),
3720
0
          htab->elf.sgotplt->contents + gotplt_offset);
3721
3722
    /* Fill in the entry in the .rela.plt section.  */
3723
0
    rela.r_offset = (htab->elf.sgotplt->output_section->vma
3724
0
         + htab->elf.sgotplt->output_offset
3725
0
         + gotplt_offset);
3726
0
    rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
3727
0
    rela.r_addend = 0;
3728
0
    loc = htab->elf.srelplt->contents + plt_index *
3729
0
      sizeof (Elf64_External_Rela);
3730
0
    bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3731
3732
0
    if (!h->def_regular)
3733
0
      {
3734
        /* Mark the symbol as undefined, rather than as defined in
3735
     the .plt section.  Leave the value if there were any
3736
     relocations where pointer equality matters (this is a clue
3737
     for the dynamic linker, to make function pointer
3738
     comparisons work between an application and shared
3739
     library), otherwise set it to zero.  If a function is only
3740
     called from a binary, there is no need to slow down
3741
     shared libraries because of that.  */
3742
0
        sym->st_shndx = SHN_UNDEF;
3743
0
        if (!h->pointer_equality_needed)
3744
0
    sym->st_value = 0;
3745
0
      }
3746
0
  }
3747
0
    }
3748
3749
0
  if (h->got.offset != (bfd_vma) -1
3750
0
      && elf_s390_hash_entry(h)->tls_type != GOT_TLS_GD
3751
0
      && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE
3752
0
      && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE_NLT)
3753
0
    {
3754
0
      Elf_Internal_Rela rela;
3755
0
      bfd_byte *loc;
3756
3757
      /* This symbol has an entry in the global offset table.  Set it
3758
   up.  */
3759
0
      if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3760
0
  abort ();
3761
3762
0
      rela.r_offset = (htab->elf.sgot->output_section->vma
3763
0
           + htab->elf.sgot->output_offset
3764
0
           + (h->got.offset &~ (bfd_vma) 1));
3765
3766
0
      if (h->def_regular && s390_is_ifunc_symbol_p (h))
3767
0
  {
3768
0
    if (bfd_link_pic (info))
3769
0
      {
3770
        /* An explicit GOT slot usage needs GLOB_DAT.  If the
3771
     symbol references local the implicit got.iplt slot
3772
     will be used and the IRELATIVE reloc has been created
3773
     above.  */
3774
0
        goto do_glob_dat;
3775
0
      }
3776
0
    else
3777
0
      {
3778
0
        if (!h->pointer_equality_needed)
3779
0
    abort ();
3780
3781
        /* For non-shared objects explicit GOT slots must be
3782
     filled with the PLT slot address for pointer
3783
     equality reasons.  */
3784
0
        bfd_put_64 (output_bfd, (htab->elf.iplt->output_section->vma
3785
0
               + htab->elf.iplt->output_offset
3786
0
               + h->plt.offset),
3787
0
        htab->elf.sgot->contents + h->got.offset);
3788
0
        return true;
3789
0
      }
3790
0
  }
3791
0
      else if (SYMBOL_REFERENCES_LOCAL (info, h))
3792
0
  {
3793
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
3794
0
      return true;
3795
3796
    /* If this is a static link, or it is a -Bsymbolic link and
3797
       the symbol is defined locally or was forced to be local
3798
       because of a version file, we just want to emit a
3799
       RELATIVE reloc.  The entry in the global offset table
3800
       will already have been initialized in the
3801
       relocate_section function.  */
3802
0
    if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
3803
0
      return false;
3804
0
    BFD_ASSERT((h->got.offset & 1) != 0);
3805
0
    rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
3806
0
    rela.r_addend = (h->root.u.def.value
3807
0
         + h->root.u.def.section->output_section->vma
3808
0
         + h->root.u.def.section->output_offset);
3809
0
  }
3810
0
      else
3811
0
  {
3812
0
    BFD_ASSERT((h->got.offset & 1) == 0);
3813
0
  do_glob_dat:
3814
0
    bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgot->contents + h->got.offset);
3815
0
    rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
3816
0
    rela.r_addend = 0;
3817
0
  }
3818
3819
0
      loc = htab->elf.srelgot->contents;
3820
0
      loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3821
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3822
0
    }
3823
3824
0
  if (h->needs_copy)
3825
0
    {
3826
0
      Elf_Internal_Rela rela;
3827
0
      asection *s;
3828
0
      bfd_byte *loc;
3829
3830
      /* This symbols needs a copy reloc.  Set it up.  */
3831
3832
0
      if (h->dynindx == -1
3833
0
    || (h->root.type != bfd_link_hash_defined
3834
0
        && h->root.type != bfd_link_hash_defweak)
3835
0
    || htab->elf.srelbss == NULL
3836
0
    || htab->elf.sreldynrelro == NULL)
3837
0
  abort ();
3838
3839
0
      rela.r_offset = (h->root.u.def.value
3840
0
           + h->root.u.def.section->output_section->vma
3841
0
           + h->root.u.def.section->output_offset);
3842
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
3843
0
      rela.r_addend = 0;
3844
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
3845
0
  s = htab->elf.sreldynrelro;
3846
0
      else
3847
0
  s = htab->elf.srelbss;
3848
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf64_External_Rela);
3849
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3850
0
    }
3851
3852
  /* Mark some specially defined symbols as absolute.  */
3853
0
  if (h == htab->elf.hdynamic
3854
0
      || h == htab->elf.hgot
3855
0
      || h == htab->elf.hplt)
3856
0
    sym->st_shndx = SHN_ABS;
3857
3858
0
  return true;
3859
0
}
3860
3861
/* Used to decide how to sort relocs in an optimal manner for the
3862
   dynamic linker, before writing them out.  */
3863
3864
static enum elf_reloc_type_class
3865
elf_s390_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3866
         const asection *rel_sec ATTRIBUTE_UNUSED,
3867
         const Elf_Internal_Rela *rela)
3868
0
{
3869
0
  bfd *abfd = info->output_bfd;
3870
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
3871
0
  struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
3872
0
  unsigned long r_symndx = ELF64_R_SYM (rela->r_info);
3873
0
  Elf_Internal_Sym sym;
3874
3875
0
  if (htab->elf.dynsym == NULL
3876
0
      || !bed->s->swap_symbol_in (abfd,
3877
0
          (htab->elf.dynsym->contents
3878
0
           + r_symndx * bed->s->sizeof_sym),
3879
0
          0, &sym))
3880
0
    abort ();
3881
3882
  /* Check relocation against STT_GNU_IFUNC symbol.  */
3883
0
  if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3884
0
    return reloc_class_ifunc;
3885
3886
0
  switch ((int) ELF64_R_TYPE (rela->r_info))
3887
0
    {
3888
0
    case R_390_RELATIVE:
3889
0
      return reloc_class_relative;
3890
0
    case R_390_JMP_SLOT:
3891
0
      return reloc_class_plt;
3892
0
    case R_390_COPY:
3893
0
      return reloc_class_copy;
3894
0
    default:
3895
0
      return reloc_class_normal;
3896
0
    }
3897
0
}
3898
3899
/* Finish up the dynamic sections.  */
3900
3901
static bool
3902
elf_s390_finish_dynamic_sections (bfd *output_bfd,
3903
          struct bfd_link_info *info,
3904
          bfd_byte *buf)
3905
0
{
3906
0
  struct elf_s390_link_hash_table *htab;
3907
0
  bfd *dynobj;
3908
0
  asection *sdyn;
3909
0
  bfd *ibfd;
3910
0
  unsigned int i;
3911
3912
0
  htab = elf_s390_hash_table (info);
3913
0
  if (htab == NULL)
3914
0
    return false;
3915
3916
0
  dynobj = htab->elf.dynobj;
3917
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3918
3919
0
  if (htab->elf.dynamic_sections_created)
3920
0
    {
3921
0
      Elf64_External_Dyn *dyncon, *dynconend;
3922
3923
0
      if (sdyn == NULL || htab->elf.sgot == NULL)
3924
0
  abort ();
3925
3926
0
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
3927
0
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3928
0
      for (; dyncon < dynconend; dyncon++)
3929
0
  {
3930
0
    Elf_Internal_Dyn dyn;
3931
0
    asection *s;
3932
3933
0
    bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3934
3935
0
    switch (dyn.d_tag)
3936
0
      {
3937
0
      default:
3938
0
        continue;
3939
3940
0
      case DT_PLTGOT:
3941
        /* DT_PLTGOT matches _GLOBAL_OFFSET_TABLE_ */
3942
0
        dyn.d_un.d_ptr = s390_got_pointer (info);
3943
0
        break;
3944
3945
0
      case DT_JMPREL:
3946
0
        s = htab->elf.srelplt;
3947
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3948
0
        break;
3949
3950
0
      case DT_PLTRELSZ:
3951
0
        dyn.d_un.d_val = htab->elf.srelplt->size;
3952
0
        if (htab->elf.irelplt)
3953
0
    dyn.d_un.d_val += htab->elf.irelplt->size;
3954
0
        break;
3955
3956
0
      case DT_RELASZ:
3957
        /* The procedure linkage table relocs (DT_JMPREL) should
3958
     not be included in the overall relocs (DT_RELA).
3959
     Therefore, we override the DT_RELASZ entry here to
3960
     make it not include the JMPREL relocs.  Since the
3961
     linker script arranges for .rela.plt to follow all
3962
     other relocation sections, we don't have to worry
3963
     about changing the DT_RELA entry.  */
3964
0
        dyn.d_un.d_val -= htab->elf.srelplt->size;
3965
0
        if (htab->elf.irelplt)
3966
0
    dyn.d_un.d_val -= htab->elf.irelplt->size;
3967
0
        break;
3968
0
      }
3969
3970
0
    bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3971
0
  }
3972
3973
      /* Fill in the special first entry in the procedure linkage table.  */
3974
0
      if (htab->elf.splt && htab->elf.splt->size > 0)
3975
0
  {
3976
    /* fill in blueprint for plt 0 entry */
3977
0
    memcpy (htab->elf.splt->contents, elf_s390x_first_plt_entry,
3978
0
      PLT_FIRST_ENTRY_SIZE);
3979
    /* The second instruction in the first PLT entry is a LARL
3980
       loading the GOT pointer.  Fill in the LARL immediate
3981
       address.  */
3982
0
    bfd_put_32 (output_bfd,
3983
0
          (s390_got_pointer (info)
3984
0
           - htab->elf.splt->output_section->vma
3985
0
           - htab->elf.splt->output_offset - 6)/2,
3986
0
          htab->elf.splt->contents + 8);
3987
0
  }
3988
0
      if (elf_section_data (htab->elf.splt->output_section) != NULL)
3989
0
  elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
3990
0
    = PLT_ENTRY_SIZE;
3991
0
    }
3992
3993
0
  if (htab->elf.hgot && htab->elf.hgot->root.u.def.section)
3994
0
    {
3995
      /* Fill in the first three entries in the global offset table.  */
3996
0
      if (htab->elf.hgot->root.u.def.section->size > 0)
3997
0
  {
3998
0
    bfd_put_64 (output_bfd,
3999
0
          (sdyn == NULL ? (bfd_vma) 0
4000
0
           : sdyn->output_section->vma + sdyn->output_offset),
4001
0
          htab->elf.hgot->root.u.def.section->contents);
4002
    /* One entry for shared object struct ptr.  */
4003
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
4004
0
          htab->elf.hgot->root.u.def.section->contents + 8);
4005
    /* One entry for _dl_runtime_resolve.  */
4006
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
4007
0
          htab->elf.hgot->root.u.def.section->contents + 16);
4008
0
  }
4009
0
      if (htab->elf.sgot != NULL && htab->elf.sgot->size > 0)
4010
0
  elf_section_data (htab->elf.sgot->output_section)
4011
0
    ->this_hdr.sh_entsize = 8;
4012
0
    }
4013
4014
  /* Finish dynamic symbol for local IFUNC symbols.  */
4015
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4016
0
    {
4017
0
      struct plt_entry *local_plt;
4018
0
      Elf_Internal_Sym *isym;
4019
0
      Elf_Internal_Shdr *symtab_hdr;
4020
4021
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4022
4023
0
      if (!is_s390_elf (ibfd))
4024
0
  continue;
4025
4026
0
      local_plt = elf_s390_local_plt (ibfd);
4027
0
      if (local_plt != NULL)
4028
0
  for (i = 0; i < symtab_hdr->sh_info; i++)
4029
0
    {
4030
0
      if (local_plt[i].plt.offset != (bfd_vma) -1)
4031
0
        {
4032
0
    asection *sec = local_plt[i].sec;
4033
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd, i);
4034
0
    if (isym == NULL)
4035
0
      return false;
4036
4037
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4038
0
      elf_s390_finish_ifunc_symbol (output_bfd, info, NULL, htab,
4039
0
            local_plt[i].plt.offset,
4040
0
            isym->st_value
4041
0
            + sec->output_section->vma
4042
0
            + sec->output_offset);
4043
4044
0
        }
4045
0
    }
4046
0
    }
4047
4048
  /* Adjust .eh_frame for .plt section.  */
4049
0
  if (htab->plt_eh_frame != NULL
4050
0
      && htab->plt_eh_frame->contents != NULL)
4051
0
    {
4052
0
      if (htab->elf.splt != NULL
4053
0
    && htab->elf.splt->size != 0
4054
0
    && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4055
0
    && htab->elf.splt->output_section != NULL
4056
0
    && htab->plt_eh_frame->output_section != NULL)
4057
0
  {
4058
0
    bfd_vma plt_start = htab->elf.splt->output_section->vma;
4059
0
    bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4060
0
           + htab->plt_eh_frame->output_offset
4061
0
           + PLT_FDE_START_OFFSET;
4062
    /* Note: Linker may have discarded the FDE, so that store may
4063
       be beyond current htab->plt_eh_frame->size.  Can be ignored,
4064
       as htab->plt_eh_frame->contents got allocated with
4065
       sizeof (elf_s390x_eh_frame_plt).  See PR 12570.  */
4066
0
    bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4067
0
           htab->plt_eh_frame->contents
4068
0
           + PLT_FDE_START_OFFSET);
4069
0
  }
4070
4071
0
      if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
4072
0
    && !_bfd_elf_write_linker_section_eh_frame (output_bfd, info,
4073
0
                  htab->plt_eh_frame, buf))
4074
0
  return NULL;
4075
0
    }
4076
4077
  /* Make any adjustment if necessary and merge .sframe section to
4078
     create the final .sframe section for output_bfd.  */
4079
0
  if (htab->plt_sframe != NULL
4080
0
      && htab->plt_sframe->contents != NULL)
4081
0
    {
4082
0
      if (htab->elf.splt != NULL
4083
0
    && htab->elf.splt->size != 0
4084
0
    && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4085
0
    && htab->elf.splt->output_section != NULL
4086
0
    && htab->plt_sframe->output_section != NULL)
4087
0
  {
4088
0
    bfd_vma plt_start = htab->elf.splt->output_section->vma;
4089
0
    bfd_vma sframe_start = htab->plt_sframe->output_section->vma
4090
0
           + htab->plt_sframe->output_offset
4091
0
           + PLT_SFRAME_FDE_START_OFFSET;
4092
0
    bfd_put_signed_64 (dynobj, plt_start - sframe_start,
4093
0
           htab->plt_sframe->contents
4094
0
           + PLT_SFRAME_FDE_START_OFFSET);
4095
0
  }
4096
0
      if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
4097
0
  {
4098
0
    if (! _bfd_elf_merge_section_sframe (output_bfd, info,
4099
0
                 htab->plt_sframe,
4100
0
                 htab->plt_sframe->contents))
4101
0
      return false;
4102
0
  }
4103
0
    }
4104
4105
0
  return true;
4106
0
}
4107

4108
/* Support for core dump NOTE sections.  */
4109
4110
static bool
4111
elf_s390_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4112
0
{
4113
0
  int offset;
4114
0
  size_t size;
4115
4116
0
  switch (note->descsz)
4117
0
    {
4118
0
    default:
4119
0
      return false;
4120
4121
0
    case 336:     /* sizeof(struct elf_prstatus) on s390x */
4122
      /* pr_cursig */
4123
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4124
4125
      /* pr_pid */
4126
0
      elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
4127
4128
      /* pr_reg */
4129
0
      offset = 112;
4130
0
      size = 216;
4131
0
      break;
4132
0
    }
4133
4134
  /* Make a ".reg/999" section.  */
4135
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4136
0
            size, note->descpos + offset);
4137
0
}
4138
4139
static bool
4140
elf_s390_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4141
0
{
4142
0
  switch (note->descsz)
4143
0
    {
4144
0
    default:
4145
0
      return false;
4146
4147
0
    case 136:     /* sizeof(struct elf_prpsinfo) on s390x */
4148
0
      elf_tdata (abfd)->core->pid
4149
0
  = bfd_get_32 (abfd, note->descdata + 24);
4150
0
      elf_tdata (abfd)->core->program
4151
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
4152
0
      elf_tdata (abfd)->core->command
4153
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
4154
0
    }
4155
4156
  /* Note that for some reason, a spurious space is tacked
4157
     onto the end of the args in some (at least one anyway)
4158
     implementations, so strip it off if it exists.  */
4159
4160
0
  {
4161
0
    char *command = elf_tdata (abfd)->core->command;
4162
0
    int n = strlen (command);
4163
4164
0
    if (0 < n && command[n - 1] == ' ')
4165
0
      command[n - 1] = '\0';
4166
0
  }
4167
4168
0
  return true;
4169
0
}
4170
4171
static char *
4172
elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz,
4173
        int note_type, ...)
4174
0
{
4175
0
  va_list ap;
4176
4177
0
  switch (note_type)
4178
0
    {
4179
0
    default:
4180
0
      return NULL;
4181
4182
0
    case NT_PRPSINFO:
4183
0
      {
4184
0
  char data[136] ATTRIBUTE_NONSTRING = { 0 };
4185
0
  const char *fname, *psargs;
4186
4187
0
  va_start (ap, note_type);
4188
0
  fname = va_arg (ap, const char *);
4189
0
  psargs = va_arg (ap, const char *);
4190
0
  va_end (ap);
4191
4192
0
  strncpy (data + 40, fname, 16);
4193
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
4194
  DIAGNOSTIC_PUSH;
4195
  /* GCC 8.0 and 8.1 warn about 80 equals destination size with
4196
     -Wstringop-truncation:
4197
     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
4198
   */
4199
  DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
4200
#endif
4201
0
  strncpy (data + 56, psargs, 80);
4202
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
4203
  DIAGNOSTIC_POP;
4204
#endif
4205
0
  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
4206
0
           &data, sizeof (data));
4207
0
      }
4208
4209
0
    case NT_PRSTATUS:
4210
0
      {
4211
0
  char data[336] = { 0 };
4212
0
  long pid;
4213
0
  int cursig;
4214
0
  const void *gregs;
4215
4216
0
  va_start (ap, note_type);
4217
0
  pid = va_arg (ap, long);
4218
0
  cursig = va_arg (ap, int);
4219
0
  gregs = va_arg (ap, const void *);
4220
0
  va_end (ap);
4221
4222
0
  bfd_put_16 (abfd, cursig, data + 12);
4223
0
  bfd_put_32 (abfd, pid, data + 32);
4224
0
  memcpy (data + 112, gregs, 216);
4225
0
  return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
4226
0
           &data, sizeof (data));
4227
0
      }
4228
0
    }
4229
  /* NOTREACHED */
4230
0
}
4231

4232
/* Return address for Ith PLT stub in section PLT, for relocation REL
4233
   or (bfd_vma) -1 if it should not be included.  */
4234
4235
static bfd_vma
4236
elf_s390_plt_sym_val (bfd_vma i, const asection *plt,
4237
          const arelent *rel ATTRIBUTE_UNUSED)
4238
0
{
4239
0
  return plt->vma + PLT_FIRST_ENTRY_SIZE + i * PLT_ENTRY_SIZE;
4240
0
}
4241
4242
/* Merge backend specific data from an object file to the output
4243
   object file when linking.  */
4244
4245
static bool
4246
elf64_s390_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4247
0
{
4248
0
  if (!is_s390_elf (ibfd))
4249
0
    return true;
4250
4251
0
  return elf_s390_merge_obj_attributes (ibfd, info);
4252
0
}
4253
4254
/* We may add a PT_S390_PGSTE program header.  */
4255
4256
static int
4257
elf_s390_additional_program_headers (bfd *abfd ATTRIBUTE_UNUSED,
4258
             struct bfd_link_info *info)
4259
0
{
4260
0
  struct elf_s390_link_hash_table *htab;
4261
4262
0
  if (info)
4263
0
    {
4264
0
      htab = elf_s390_hash_table (info);
4265
0
      if (htab)
4266
0
  return htab->params->pgste;
4267
0
    }
4268
0
  return 0;
4269
0
}
4270
4271
4272
/* Add the PT_S390_PGSTE program header.  */
4273
4274
static bool
4275
elf_s390_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
4276
0
{
4277
0
  struct elf_s390_link_hash_table *htab;
4278
0
  struct elf_segment_map *m, *pm = NULL;
4279
4280
0
  if (!abfd || !info)
4281
0
    return true;
4282
4283
0
  htab = elf_s390_hash_table (info);
4284
0
  if (!htab || !htab->params->pgste)
4285
0
    return true;
4286
4287
  /* If there is already a PT_S390_PGSTE header, avoid adding
4288
     another.  */
4289
0
  m = elf_seg_map (abfd);
4290
0
  while (m && m->p_type != PT_S390_PGSTE)
4291
0
    {
4292
0
      pm = m;
4293
0
      m = m->next;
4294
0
    }
4295
4296
0
  if (m)
4297
0
    return true;
4298
4299
0
  m = (struct elf_segment_map *)
4300
0
    bfd_zalloc (abfd, sizeof (struct elf_segment_map));
4301
0
  if (m == NULL)
4302
0
    return false;
4303
0
  m->p_type = PT_S390_PGSTE;
4304
0
  m->count = 0;
4305
0
  m->next = NULL;
4306
0
  if (pm)
4307
0
    pm->next = m;
4308
4309
0
  return true;
4310
0
}
4311
4312
bool
4313
bfd_elf_s390_set_options (struct bfd_link_info *info,
4314
        struct s390_elf_params *params)
4315
0
{
4316
0
  struct elf_s390_link_hash_table *htab;
4317
4318
0
  if (info)
4319
0
    {
4320
0
      htab = elf_s390_hash_table (info);
4321
0
      if (htab)
4322
0
  htab->params = params;
4323
0
    }
4324
4325
0
  return true;
4326
0
}
4327
4328
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4329
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4330
   hash table.  */
4331
4332
static bool
4333
elf_s390_create_dynamic_sections (bfd *dynobj,
4334
                                  struct bfd_link_info *info)
4335
0
{
4336
0
  struct elf_s390_link_hash_table *htab;
4337
4338
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4339
0
    return false;
4340
4341
0
  htab = elf_s390_hash_table (info);
4342
0
  if (htab == NULL)
4343
0
    return false;
4344
4345
0
  htab->sframe_plt = &elf_s390x_sframe_plt;
4346
4347
0
  if (htab->elf.splt != NULL)
4348
0
    {
4349
      /* Create .eh_frame section for .plt section.  */
4350
0
      if (!info->no_ld_generated_unwind_info)
4351
0
        {
4352
0
          flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4353
0
                            | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4354
0
                            | SEC_LINKER_CREATED);
4355
4356
0
          if (htab->plt_eh_frame == NULL)
4357
0
            {
4358
0
              htab->plt_eh_frame
4359
0
                = bfd_make_section_anyway_with_flags (dynobj,
4360
0
                                                      ".eh_frame",
4361
0
                                                      flags);
4362
0
              if (htab->plt_eh_frame == NULL
4363
0
                  || !bfd_set_section_alignment (htab->plt_eh_frame, 3))
4364
0
                return false;
4365
0
            }
4366
0
        }
4367
4368
      /* Create .sframe section for .plt section.
4369
   Do not make SFrame sections for dynobj unconditionally.  If there
4370
   are no SFrame sections for any input files, skip creating the linker
4371
   created SFrame sections too.  Since SFrame sections are marked KEEP,
4372
   prohibiting these linker-created SFrame sections when unnecessary,
4373
   helps avoid creating of empty SFrame sections in the output.  */
4374
0
      bool gen_plt_sframe_p = (_bfd_elf_sframe_present_input_bfds (info)
4375
0
             && !info->discard_sframe);
4376
0
      if (gen_plt_sframe_p)
4377
0
  {
4378
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4379
0
          | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4380
0
          | SEC_LINKER_CREATED);
4381
4382
0
    htab->plt_sframe = bfd_make_section_anyway_with_flags (dynobj,
4383
0
                 ".sframe",
4384
0
                 flags);
4385
0
    if (htab->plt_sframe == NULL)
4386
0
      return false;
4387
4388
0
    elf_section_type (htab->plt_sframe) = SHT_GNU_SFRAME;
4389
0
  }
4390
0
    }
4391
4392
0
  return true;
4393
0
}
4394
4395
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4396
4397
static bool
4398
elf_s390_hash_symbol (struct elf_link_hash_entry *h)
4399
0
{
4400
0
  if (h->plt.offset != (bfd_vma) -1
4401
0
      && !h->def_regular
4402
0
      && !h->pointer_equality_needed)
4403
0
    return false;
4404
4405
0
  return _bfd_elf_hash_symbol (h);
4406
0
}
4407
4408
/* Why was the hash table entry size definition changed from
4409
   ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
4410
   this is the only reason for the s390_elf64_size_info structure.  */
4411
4412
static const struct elf_size_info s390_elf64_size_info =
4413
{
4414
  sizeof (Elf64_External_Ehdr),
4415
  sizeof (Elf64_External_Phdr),
4416
  sizeof (Elf64_External_Shdr),
4417
  sizeof (Elf64_External_Rel),
4418
  sizeof (Elf64_External_Rela),
4419
  sizeof (Elf64_External_Sym),
4420
  sizeof (Elf64_External_Dyn),
4421
  sizeof (Elf_External_Note),
4422
  8,    /* hash-table entry size.  */
4423
  1,    /* internal relocations per external relocations.  */
4424
  64,   /* arch_size.  */
4425
  3,    /* log_file_align.  */
4426
  ELFCLASS64, EV_CURRENT,
4427
  bfd_elf64_write_out_phdrs,
4428
  bfd_elf64_write_shdrs_and_ehdr,
4429
  bfd_elf64_checksum_contents,
4430
  bfd_elf64_write_relocs,
4431
  bfd_elf64_swap_symbol_in,
4432
  bfd_elf64_swap_symbol_out,
4433
  bfd_elf64_slurp_reloc_table,
4434
  bfd_elf64_slurp_symbol_table,
4435
  bfd_elf64_swap_dyn_in,
4436
  bfd_elf64_swap_dyn_out,
4437
  bfd_elf64_swap_reloc_in,
4438
  bfd_elf64_swap_reloc_out,
4439
  bfd_elf64_swap_reloca_in,
4440
  bfd_elf64_swap_reloca_out
4441
};
4442
4443
#define TARGET_BIG_SYM  s390_elf64_vec
4444
#define TARGET_BIG_NAME "elf64-s390"
4445
#define ELF_ARCH  bfd_arch_s390
4446
#define ELF_TARGET_ID S390_ELF_DATA
4447
#define ELF_MACHINE_CODE EM_S390
4448
#define ELF_MACHINE_ALT1 EM_S390_OLD
4449
#define ELF_MAXPAGESIZE 0x1000
4450
4451
#define elf_backend_size_info   s390_elf64_size_info
4452
4453
#define elf_backend_can_gc_sections 1
4454
#define elf_backend_can_refcount  1
4455
#define elf_backend_want_got_plt  1
4456
#define elf_backend_plt_readonly  1
4457
#define elf_backend_want_plt_sym  0
4458
#define elf_backend_got_header_size 24
4459
#define elf_backend_want_dynrelro 1
4460
#define elf_backend_rela_normal   1
4461
4462
#define elf_info_to_howto   elf_s390_info_to_howto
4463
4464
#define bfd_elf64_bfd_is_local_label_name     elf_s390_is_local_label_name
4465
#define bfd_elf64_bfd_link_hash_table_create  elf_s390_link_hash_table_create
4466
#define bfd_elf64_bfd_reloc_type_lookup       elf_s390_reloc_type_lookup
4467
#define bfd_elf64_bfd_reloc_name_lookup       elf_s390_reloc_name_lookup
4468
#define bfd_elf64_bfd_merge_private_bfd_data  elf64_s390_merge_private_bfd_data
4469
4470
#define elf_backend_adjust_dynamic_symbol     elf_s390_adjust_dynamic_symbol
4471
#define elf_backend_check_relocs        elf_s390_check_relocs
4472
#define elf_backend_copy_indirect_symbol      elf_s390_copy_indirect_symbol
4473
#define elf_backend_create_dynamic_sections   elf_s390_create_dynamic_sections
4474
#define elf_backend_finish_dynamic_sections   elf_s390_finish_dynamic_sections
4475
#define elf_backend_finish_dynamic_symbol     elf_s390_finish_dynamic_symbol
4476
#define elf_backend_gc_mark_hook        elf_s390_gc_mark_hook
4477
#define elf_backend_reloc_type_class        elf_s390_reloc_type_class
4478
#define elf_backend_relocate_section        elf_s390_relocate_section
4479
#define elf_backend_late_size_sections        elf_s390_late_size_sections
4480
#define elf_backend_init_index_section        _bfd_elf_init_1_index_section
4481
#define elf_backend_grok_prstatus       elf_s390_grok_prstatus
4482
#define elf_backend_grok_psinfo         elf_s390_grok_psinfo
4483
#define elf_backend_write_core_note       elf_s390_write_core_note
4484
#define elf_backend_plt_sym_val         elf_s390_plt_sym_val
4485
#define elf_backend_sort_relocs_p       elf_s390_elf_sort_relocs_p
4486
#define elf_backend_additional_program_headers elf_s390_additional_program_headers
4487
#define elf_backend_modify_segment_map        elf_s390_modify_segment_map
4488
#define elf_backend_hash_symbol         elf_s390_hash_symbol
4489
4490
#define bfd_elf64_mkobject    elf_s390_mkobject
4491
#define elf_backend_object_p    elf_s390_object_p
4492
4493
#include "elf64-target.h"