Coverage Report

Created: 2026-11-21 09:00

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