Coverage Report

Created: 2026-07-25 10:20

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