Coverage Report

Created: 2025-06-24 06:45

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