Coverage Report

Created: 2026-09-14 08:07

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