]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-i386.c
Fix the BFD library's find_nearest_line feature to produce consistent results.
[thirdparty/binutils-gdb.git] / bfd / elf32-i386.c
CommitLineData
252b5132 1/* Intel 80386/80486-specific support for 32-bit ELF
d87bef3a 2 Copyright (C) 1993-2023 Free Software Foundation, Inc.
252b5132 3
571fe01f 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
571fe01f
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
571fe01f 9 (at your option) any later version.
252b5132 10
571fe01f
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
571fe01f
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
0afcef53 21#include "elfxx-x86.h"
eac338cf 22#include "elf-vxworks.h"
e41b3a13 23#include "dwarf2.h"
02a86693 24#include "opcode/i386.h"
252b5132 25
55fd94b0
AM
26/* 386 uses REL relocations instead of RELA. */
27#define USE_REL 1
252b5132 28
252b5132
RH
29static reloc_howto_type elf_howto_table[]=
30{
c94cb026 31 HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
1b452ec6 32 bfd_elf_generic_reloc, "R_386_NONE",
0a1b45a2 33 true, 0x00000000, 0x00000000, false),
c94cb026 34 HOWTO(R_386_32, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 35 bfd_elf_generic_reloc, "R_386_32",
0a1b45a2 36 true, 0xffffffff, 0xffffffff, false),
c94cb026 37 HOWTO(R_386_PC32, 0, 4, 32, true, 0, complain_overflow_dont,
1b452ec6 38 bfd_elf_generic_reloc, "R_386_PC32",
0a1b45a2 39 true, 0xffffffff, 0xffffffff, true),
c94cb026 40 HOWTO(R_386_GOT32, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 41 bfd_elf_generic_reloc, "R_386_GOT32",
0a1b45a2 42 true, 0xffffffff, 0xffffffff, false),
c94cb026 43 HOWTO(R_386_PLT32, 0, 4, 32, true, 0, complain_overflow_dont,
1b452ec6 44 bfd_elf_generic_reloc, "R_386_PLT32",
0a1b45a2 45 true, 0xffffffff, 0xffffffff, true),
c94cb026 46 HOWTO(R_386_COPY, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 47 bfd_elf_generic_reloc, "R_386_COPY",
0a1b45a2 48 true, 0xffffffff, 0xffffffff, false),
c94cb026 49 HOWTO(R_386_GLOB_DAT, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 50 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
0a1b45a2 51 true, 0xffffffff, 0xffffffff, false),
c94cb026 52 HOWTO(R_386_JUMP_SLOT, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 53 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
0a1b45a2 54 true, 0xffffffff, 0xffffffff, false),
c94cb026 55 HOWTO(R_386_RELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 56 bfd_elf_generic_reloc, "R_386_RELATIVE",
0a1b45a2 57 true, 0xffffffff, 0xffffffff, false),
c94cb026 58 HOWTO(R_386_GOTOFF, 0, 4, 32, false, 0, complain_overflow_dont,
1b452ec6 59 bfd_elf_generic_reloc, "R_386_GOTOFF",
0a1b45a2 60 true, 0xffffffff, 0xffffffff, false),
c94cb026 61 HOWTO(R_386_GOTPC, 0, 4, 32, true, 0, complain_overflow_dont,
1b452ec6 62 bfd_elf_generic_reloc, "R_386_GOTPC",
0a1b45a2 63 true, 0xffffffff, 0xffffffff, true),
1b452ec6 64
dc47f327
AM
65 /* We have a gap in the reloc numbers here.
66 R_386_standard counts the number up to this point, and
67 R_386_ext_offset is the value to subtract from a reloc type of
68 R_386_16 thru R_386_PC8 to form an index into this table. */
55fd94b0
AM
69#define R_386_standard (R_386_GOTPC + 1)
70#define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
1b452ec6 71
37e55690 72 /* These relocs are a GNU extension. */
c94cb026 73 HOWTO(R_386_TLS_TPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
37e55690 74 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
0a1b45a2 75 true, 0xffffffff, 0xffffffff, false),
c94cb026 76 HOWTO(R_386_TLS_IE, 0, 4, 32, false, 0, complain_overflow_dont,
37e55690 77 bfd_elf_generic_reloc, "R_386_TLS_IE",
0a1b45a2 78 true, 0xffffffff, 0xffffffff, false),
c94cb026 79 HOWTO(R_386_TLS_GOTIE, 0, 4, 32, false, 0, complain_overflow_dont,
37e55690 80 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
0a1b45a2 81 true, 0xffffffff, 0xffffffff, false),
c94cb026 82 HOWTO(R_386_TLS_LE, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 83 bfd_elf_generic_reloc, "R_386_TLS_LE",
0a1b45a2 84 true, 0xffffffff, 0xffffffff, false),
c94cb026 85 HOWTO(R_386_TLS_GD, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 86 bfd_elf_generic_reloc, "R_386_TLS_GD",
0a1b45a2 87 true, 0xffffffff, 0xffffffff, false),
c94cb026 88 HOWTO(R_386_TLS_LDM, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 89 bfd_elf_generic_reloc, "R_386_TLS_LDM",
0a1b45a2 90 true, 0xffffffff, 0xffffffff, false),
c94cb026 91 HOWTO(R_386_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
1b452ec6 92 bfd_elf_generic_reloc, "R_386_16",
0a1b45a2 93 true, 0xffff, 0xffff, false),
c94cb026 94 HOWTO(R_386_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
1b452ec6 95 bfd_elf_generic_reloc, "R_386_PC16",
0a1b45a2 96 true, 0xffff, 0xffff, true),
c94cb026 97 HOWTO(R_386_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
1b452ec6 98 bfd_elf_generic_reloc, "R_386_8",
0a1b45a2 99 true, 0xff, 0xff, false),
c94cb026 100 HOWTO(R_386_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
1b452ec6 101 bfd_elf_generic_reloc, "R_386_PC8",
0a1b45a2 102 true, 0xff, 0xff, true),
dc47f327 103
55fd94b0
AM
104#define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
105#define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
13ae64f3 106 /* These are common with Solaris TLS implementation. */
c94cb026 107 HOWTO(R_386_TLS_LDO_32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 108 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
0a1b45a2 109 true, 0xffffffff, 0xffffffff, false),
c94cb026 110 HOWTO(R_386_TLS_IE_32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 111 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
0a1b45a2 112 true, 0xffffffff, 0xffffffff, false),
c94cb026 113 HOWTO(R_386_TLS_LE_32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 114 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
0a1b45a2 115 true, 0xffffffff, 0xffffffff, false),
c94cb026 116 HOWTO(R_386_TLS_DTPMOD32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 117 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
0a1b45a2 118 true, 0xffffffff, 0xffffffff, false),
c94cb026 119 HOWTO(R_386_TLS_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 120 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
0a1b45a2 121 true, 0xffffffff, 0xffffffff, false),
c94cb026 122 HOWTO(R_386_TLS_TPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
13ae64f3 123 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
0a1b45a2 124 true, 0xffffffff, 0xffffffff, false),
c94cb026 125 HOWTO(R_386_SIZE32, 0, 4, 32, false, 0, complain_overflow_dont,
1788fc08 126 bfd_elf_generic_reloc, "R_386_SIZE32",
0a1b45a2 127 true, 0xffffffff, 0xffffffff, false),
c94cb026 128 HOWTO(R_386_TLS_GOTDESC, 0, 4, 32, false, 0, complain_overflow_dont,
67a4f2b7 129 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
0a1b45a2 130 true, 0xffffffff, 0xffffffff, false),
c94cb026 131 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, false, 0, complain_overflow_dont,
67a4f2b7 132 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
0a1b45a2 133 false, 0, 0, false),
c94cb026 134 HOWTO(R_386_TLS_DESC, 0, 4, 32, false, 0, complain_overflow_dont,
67a4f2b7 135 bfd_elf_generic_reloc, "R_386_TLS_DESC",
0a1b45a2 136 true, 0xffffffff, 0xffffffff, false),
c94cb026 137 HOWTO(R_386_IRELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
cbe950e9 138 bfd_elf_generic_reloc, "R_386_IRELATIVE",
0a1b45a2 139 true, 0xffffffff, 0xffffffff, false),
c94cb026 140 HOWTO(R_386_GOT32X, 0, 4, 32, false, 0, complain_overflow_dont,
02a86693 141 bfd_elf_generic_reloc, "R_386_GOT32X",
0a1b45a2 142 true, 0xffffffff, 0xffffffff, false),
13ae64f3
JJ
143
144 /* Another gap. */
02a86693 145#define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
c74be520 146#define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
252b5132
RH
147
148/* GNU extension to record C++ vtable hierarchy. */
252b5132
RH
149 HOWTO (R_386_GNU_VTINHERIT, /* type */
150 0, /* rightshift */
c94cb026 151 4, /* size */
252b5132 152 0, /* bitsize */
0a1b45a2 153 false, /* pc_relative */
252b5132
RH
154 0, /* bitpos */
155 complain_overflow_dont, /* complain_on_overflow */
156 NULL, /* special_function */
157 "R_386_GNU_VTINHERIT", /* name */
0a1b45a2 158 false, /* partial_inplace */
252b5132
RH
159 0, /* src_mask */
160 0, /* dst_mask */
0a1b45a2 161 false), /* pcrel_offset */
252b5132
RH
162
163/* GNU extension to record C++ vtable member usage. */
252b5132
RH
164 HOWTO (R_386_GNU_VTENTRY, /* type */
165 0, /* rightshift */
c94cb026 166 4, /* size */
252b5132 167 0, /* bitsize */
0a1b45a2 168 false, /* pc_relative */
252b5132
RH
169 0, /* bitpos */
170 complain_overflow_dont, /* complain_on_overflow */
171 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
172 "R_386_GNU_VTENTRY", /* name */
0a1b45a2 173 false, /* partial_inplace */
252b5132
RH
174 0, /* src_mask */
175 0, /* dst_mask */
1273b2f8 176 false) /* pcrel_offset */
dc47f327 177
55fd94b0 178#define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
dc47f327
AM
179
180};
181
252b5132 182#ifdef DEBUG_GEN_RELOC
55fd94b0
AM
183#define TRACE(str) \
184 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
252b5132
RH
185#else
186#define TRACE(str)
187#endif
188
189static reloc_howto_type *
f3185997 190elf_i386_reloc_type_lookup (bfd *abfd,
55fd94b0 191 bfd_reloc_code_real_type code)
252b5132
RH
192{
193 switch (code)
194 {
195 case BFD_RELOC_NONE:
196 TRACE ("BFD_RELOC_NONE");
55fd94b0 197 return &elf_howto_table[R_386_NONE];
252b5132
RH
198
199 case BFD_RELOC_32:
200 TRACE ("BFD_RELOC_32");
55fd94b0 201 return &elf_howto_table[R_386_32];
252b5132
RH
202
203 case BFD_RELOC_CTOR:
204 TRACE ("BFD_RELOC_CTOR");
55fd94b0 205 return &elf_howto_table[R_386_32];
252b5132
RH
206
207 case BFD_RELOC_32_PCREL:
208 TRACE ("BFD_RELOC_PC32");
55fd94b0 209 return &elf_howto_table[R_386_PC32];
252b5132
RH
210
211 case BFD_RELOC_386_GOT32:
212 TRACE ("BFD_RELOC_386_GOT32");
55fd94b0 213 return &elf_howto_table[R_386_GOT32];
252b5132
RH
214
215 case BFD_RELOC_386_PLT32:
216 TRACE ("BFD_RELOC_386_PLT32");
55fd94b0 217 return &elf_howto_table[R_386_PLT32];
252b5132
RH
218
219 case BFD_RELOC_386_COPY:
220 TRACE ("BFD_RELOC_386_COPY");
55fd94b0 221 return &elf_howto_table[R_386_COPY];
252b5132
RH
222
223 case BFD_RELOC_386_GLOB_DAT:
224 TRACE ("BFD_RELOC_386_GLOB_DAT");
55fd94b0 225 return &elf_howto_table[R_386_GLOB_DAT];
252b5132
RH
226
227 case BFD_RELOC_386_JUMP_SLOT:
228 TRACE ("BFD_RELOC_386_JUMP_SLOT");
55fd94b0 229 return &elf_howto_table[R_386_JUMP_SLOT];
252b5132
RH
230
231 case BFD_RELOC_386_RELATIVE:
232 TRACE ("BFD_RELOC_386_RELATIVE");
55fd94b0 233 return &elf_howto_table[R_386_RELATIVE];
252b5132
RH
234
235 case BFD_RELOC_386_GOTOFF:
236 TRACE ("BFD_RELOC_386_GOTOFF");
55fd94b0 237 return &elf_howto_table[R_386_GOTOFF];
252b5132
RH
238
239 case BFD_RELOC_386_GOTPC:
240 TRACE ("BFD_RELOC_386_GOTPC");
55fd94b0 241 return &elf_howto_table[R_386_GOTPC];
252b5132 242
37e55690
JJ
243 /* These relocs are a GNU extension. */
244 case BFD_RELOC_386_TLS_TPOFF:
245 TRACE ("BFD_RELOC_386_TLS_TPOFF");
55fd94b0 246 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
37e55690
JJ
247
248 case BFD_RELOC_386_TLS_IE:
249 TRACE ("BFD_RELOC_386_TLS_IE");
55fd94b0 250 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
37e55690
JJ
251
252 case BFD_RELOC_386_TLS_GOTIE:
253 TRACE ("BFD_RELOC_386_TLS_GOTIE");
55fd94b0 254 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
37e55690 255
13ae64f3
JJ
256 case BFD_RELOC_386_TLS_LE:
257 TRACE ("BFD_RELOC_386_TLS_LE");
55fd94b0 258 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
13ae64f3
JJ
259
260 case BFD_RELOC_386_TLS_GD:
261 TRACE ("BFD_RELOC_386_TLS_GD");
55fd94b0 262 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
13ae64f3
JJ
263
264 case BFD_RELOC_386_TLS_LDM:
265 TRACE ("BFD_RELOC_386_TLS_LDM");
55fd94b0 266 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
13ae64f3 267
252b5132
RH
268 case BFD_RELOC_16:
269 TRACE ("BFD_RELOC_16");
55fd94b0 270 return &elf_howto_table[R_386_16 - R_386_ext_offset];
252b5132
RH
271
272 case BFD_RELOC_16_PCREL:
273 TRACE ("BFD_RELOC_16_PCREL");
55fd94b0 274 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
252b5132
RH
275
276 case BFD_RELOC_8:
277 TRACE ("BFD_RELOC_8");
55fd94b0 278 return &elf_howto_table[R_386_8 - R_386_ext_offset];
252b5132
RH
279
280 case BFD_RELOC_8_PCREL:
281 TRACE ("BFD_RELOC_8_PCREL");
55fd94b0 282 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
252b5132 283
13ae64f3
JJ
284 /* Common with Sun TLS implementation. */
285 case BFD_RELOC_386_TLS_LDO_32:
286 TRACE ("BFD_RELOC_386_TLS_LDO_32");
55fd94b0 287 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
13ae64f3
JJ
288
289 case BFD_RELOC_386_TLS_IE_32:
290 TRACE ("BFD_RELOC_386_TLS_IE_32");
55fd94b0 291 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
13ae64f3
JJ
292
293 case BFD_RELOC_386_TLS_LE_32:
294 TRACE ("BFD_RELOC_386_TLS_LE_32");
55fd94b0 295 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
13ae64f3
JJ
296
297 case BFD_RELOC_386_TLS_DTPMOD32:
298 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
55fd94b0 299 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
13ae64f3
JJ
300
301 case BFD_RELOC_386_TLS_DTPOFF32:
302 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
55fd94b0 303 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
13ae64f3
JJ
304
305 case BFD_RELOC_386_TLS_TPOFF32:
306 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
55fd94b0 307 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
13ae64f3 308
1788fc08
L
309 case BFD_RELOC_SIZE32:
310 TRACE ("BFD_RELOC_SIZE32");
311 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
312
67a4f2b7
AO
313 case BFD_RELOC_386_TLS_GOTDESC:
314 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
315 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
316
317 case BFD_RELOC_386_TLS_DESC_CALL:
318 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
319 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
320
321 case BFD_RELOC_386_TLS_DESC:
322 TRACE ("BFD_RELOC_386_TLS_DESC");
323 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
324
cbe950e9
L
325 case BFD_RELOC_386_IRELATIVE:
326 TRACE ("BFD_RELOC_386_IRELATIVE");
2a750708 327 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
cbe950e9 328
02a86693
L
329 case BFD_RELOC_386_GOT32X:
330 TRACE ("BFD_RELOC_386_GOT32X");
331 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
332
252b5132
RH
333 case BFD_RELOC_VTABLE_INHERIT:
334 TRACE ("BFD_RELOC_VTABLE_INHERIT");
55fd94b0 335 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
252b5132
RH
336
337 case BFD_RELOC_VTABLE_ENTRY:
338 TRACE ("BFD_RELOC_VTABLE_ENTRY");
55fd94b0 339 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
252b5132
RH
340
341 default:
f3185997
NC
342 TRACE ("Unknown");
343 /* xgettext:c-format */
e8f5af78 344 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
f3185997
NC
345 abfd, (int) code);
346 bfd_set_error (bfd_error_bad_value);
347 return NULL;
252b5132 348 }
252b5132
RH
349}
350
157090f7
AM
351static reloc_howto_type *
352elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
353 const char *r_name)
354{
355 unsigned int i;
356
357 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
358 if (elf_howto_table[i].name != NULL
359 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
360 return &elf_howto_table[i];
361
362 return NULL;
363}
364
142411ca 365static reloc_howto_type *
1273b2f8 366elf_i386_rtype_to_howto (unsigned r_type)
252b5132 367{
dc47f327
AM
368 unsigned int indx;
369
370 if ((indx = r_type) >= R_386_standard
371 && ((indx = r_type - R_386_ext_offset) - R_386_standard
372 >= R_386_ext - R_386_standard)
13ae64f3 373 && ((indx = r_type - R_386_tls_offset) - R_386_ext
c74be520
L
374 >= R_386_ext2 - R_386_ext)
375 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
376 >= R_386_vt - R_386_ext2))
f3185997 377 return NULL;
06614111
NC
378 /* PR 17512: file: 0f67f69d. */
379 if (elf_howto_table [indx].type != r_type)
380 return NULL;
142411ca
L
381 return &elf_howto_table[indx];
382}
383
0a1b45a2 384static bool
f3185997 385elf_i386_info_to_howto_rel (bfd *abfd,
142411ca
L
386 arelent *cache_ptr,
387 Elf_Internal_Rela *dst)
388{
389 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
f3185997 390
1273b2f8 391 if ((cache_ptr->howto = elf_i386_rtype_to_howto (r_type)) == NULL)
f3185997
NC
392 {
393 /* xgettext:c-format */
394 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
395 abfd, r_type);
396 bfd_set_error (bfd_error_bad_value);
0a1b45a2 397 return false;
f3185997
NC
398 }
399
0a1b45a2 400 return true;
252b5132
RH
401}
402
403/* Return whether a symbol name implies a local label. The UnixWare
404 2.1 cc generates temporary symbols that start with .X, so we
405 recognize them here. FIXME: do other SVR4 compilers also use .X?.
406 If so, we should move the .X recognition into
407 _bfd_elf_is_local_label_name. */
408
0a1b45a2 409static bool
55fd94b0 410elf_i386_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
411{
412 if (name[0] == '.' && name[1] == 'X')
0a1b45a2 413 return true;
252b5132
RH
414
415 return _bfd_elf_is_local_label_name (abfd, name);
416}
417\f
38701953 418/* Support for core dump NOTE sections. */
61adc1a4 419
0a1b45a2 420static bool
55fd94b0 421elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
38701953
AM
422{
423 int offset;
eea6121a 424 size_t size;
38701953 425
61adc1a4 426 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 427 {
61adc1a4
NC
428 int pr_version = bfd_get_32 (abfd, note->descdata);
429
430 if (pr_version != 1)
0a1b45a2 431 return false;
61adc1a4
NC
432
433 /* pr_cursig */
228e534f 434 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
61adc1a4
NC
435
436 /* pr_pid */
228e534f 437 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
61adc1a4
NC
438
439 /* pr_reg */
440 offset = 28;
eea6121a 441 size = bfd_get_32 (abfd, note->descdata + 8);
61adc1a4
NC
442 }
443 else
444 {
445 switch (note->descsz)
446 {
447 default:
0a1b45a2 448 return false;
38701953 449
61adc1a4
NC
450 case 144: /* Linux/i386 */
451 /* pr_cursig */
228e534f 452 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
38701953 453
61adc1a4 454 /* pr_pid */
228e534f 455 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
38701953 456
61adc1a4
NC
457 /* pr_reg */
458 offset = 72;
eea6121a 459 size = 68;
38701953 460
61adc1a4
NC
461 break;
462 }
38701953
AM
463 }
464
465 /* Make a ".reg/999" section. */
466 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 467 size, note->descpos + offset);
38701953
AM
468}
469
0a1b45a2 470static bool
55fd94b0 471elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
38701953 472{
61adc1a4 473 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
38701953 474 {
61adc1a4
NC
475 int pr_version = bfd_get_32 (abfd, note->descdata);
476
477 if (pr_version != 1)
0a1b45a2 478 return false;
38701953 479
228e534f 480 elf_tdata (abfd)->core->program
61adc1a4 481 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
228e534f 482 elf_tdata (abfd)->core->command
61adc1a4
NC
483 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
484 }
485 else
486 {
487 switch (note->descsz)
488 {
489 default:
0a1b45a2 490 return false;
61adc1a4
NC
491
492 case 124: /* Linux/i386 elf_prpsinfo. */
228e534f 493 elf_tdata (abfd)->core->pid
261b8d08 494 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 495 elf_tdata (abfd)->core->program
61adc1a4 496 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 497 elf_tdata (abfd)->core->command
61adc1a4
NC
498 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
499 }
38701953
AM
500 }
501
502 /* Note that for some reason, a spurious space is tacked
503 onto the end of the args in some (at least one anyway)
504 implementations, so strip it off if it exists. */
38701953 505 {
228e534f 506 char *command = elf_tdata (abfd)->core->command;
38701953
AM
507 int n = strlen (command);
508
509 if (0 < n && command[n - 1] == ' ')
510 command[n - 1] = '\0';
511 }
512
0a1b45a2 513 return true;
38701953
AM
514}
515\f
516/* Functions for the i386 ELF linker.
517
518 In order to gain some understanding of code in this file without
519 knowing all the intricate details of the linker, note the
520 following:
521
522 Functions named elf_i386_* are called by external routines, other
523 functions are only called locally. elf_i386_* functions appear
524 in this file more or less in the order in which they are called
3747999c 525 from external routines. eg. elf_i386_scan_relocs is called
38701953
AM
526 early in the link process, elf_i386_finish_dynamic_sections is
527 one of the last functions. */
528
f604c2a2 529/* The size in bytes of an entry in the lazy procedure linkage table. */
252b5132 530
f604c2a2 531#define LAZY_PLT_ENTRY_SIZE 16
252b5132 532
f604c2a2
L
533/* The size in bytes of an entry in the non-lazy procedure linkage
534 table. */
252b5132 535
f604c2a2
L
536#define NON_LAZY_PLT_ENTRY_SIZE 8
537
538/* The first entry in an absolute lazy procedure linkage table looks
539 like this. See the SVR4 ABI i386 supplement to see how this works.
540 Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */
541
542static const bfd_byte elf_i386_lazy_plt0_entry[12] =
252b5132
RH
543{
544 0xff, 0x35, /* pushl contents of address */
545 0, 0, 0, 0, /* replaced with address of .got + 4. */
546 0xff, 0x25, /* jmp indirect */
eac338cf 547 0, 0, 0, 0 /* replaced with address of .got + 8. */
252b5132
RH
548};
549
f604c2a2
L
550/* Subsequent entries in an absolute lazy procedure linkage table look
551 like this. */
252b5132 552
f604c2a2 553static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
554{
555 0xff, 0x25, /* jmp indirect */
556 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
557 0x68, /* pushl immediate */
558 0, 0, 0, 0, /* replaced with offset into relocation table. */
559 0xe9, /* jmp relative */
560 0, 0, 0, 0 /* replaced with offset to start of .plt. */
561};
562
f604c2a2
L
563/* The first entry in a PIC lazy procedure linkage table look like
564 this. Will be padded to LAZY_PLT_ENTRY_SIZE with
565 lazy_plt->plt0_pad_byte. */
252b5132 566
f604c2a2 567static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
252b5132
RH
568{
569 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
eac338cf 570 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
252b5132
RH
571};
572
f604c2a2
L
573/* Subsequent entries in a PIC lazy procedure linkage table look like
574 this. */
252b5132 575
f604c2a2 576static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
252b5132
RH
577{
578 0xff, 0xa3, /* jmp *offset(%ebx) */
579 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
580 0x68, /* pushl immediate */
581 0, 0, 0, 0, /* replaced with offset into relocation table. */
582 0xe9, /* jmp relative */
583 0, 0, 0, 0 /* replaced with offset to start of .plt. */
584};
585
f604c2a2 586/* Entries in the non-lazy procedure linkage table look like this. */
dd7e64d4 587
f604c2a2 588static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
589{
590 0xff, 0x25, /* jmp indirect */
591 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
592 0x66, 0x90 /* xchg %ax,%ax */
593};
594
f604c2a2
L
595/* Entries in the PIC non-lazy procedure linkage table look like
596 this. */
dd7e64d4 597
f604c2a2 598static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
dd7e64d4
L
599{
600 0xff, 0xa3, /* jmp *offset(%ebx) */
601 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
602 0x66, 0x90 /* xchg %ax,%ax */
603};
604
ee2fdd6f
L
605/* The first entry in an absolute IBT-enabled lazy procedure linkage
606 table looks like this. */
607
608static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
609{
07d6d2b8
AM
610 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */
611 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */
612 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
ee2fdd6f
L
613};
614
615/* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
616 table look like this. Subsequent entries for a PIC IBT-enabled lazy
617 procedure linkage table are the same. */
618
619static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
620{
07d6d2b8
AM
621 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
622 0x68, 0, 0, 0, 0, /* pushl immediate */
623 0xe9, 0, 0, 0, 0, /* jmp relative */
624 0x66, 0x90 /* xchg %ax,%ax */
ee2fdd6f
L
625};
626
627/* The first entry in a PIC IBT-enabled lazy procedure linkage table
628 look like. */
629
630static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
631{
632 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
07d6d2b8
AM
633 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
634 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
ee2fdd6f
L
635};
636
637/* Entries for branches with IBT-enabled in the absolute non-lazey
638 procedure linkage table look like this. They have the same size
639 as the lazy PLT entry. */
640
641static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
642{
07d6d2b8 643 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
ee2fdd6f
L
644 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */
645 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
646};
647
648/* Entries for branches with IBT-enabled in the PIC non-lazey procedure
649 linkage table look like this. They have the same size as the lazy
650 PLT entry. */
651
652static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
653{
07d6d2b8 654 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
ee2fdd6f
L
655 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */
656 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
657};
658
f604c2a2 659/* .eh_frame covering the lazy .plt section. */
e41b3a13 660
f604c2a2 661static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
e41b3a13 662{
e41b3a13
JJ
663 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
664 0, 0, 0, 0, /* CIE ID */
665 1, /* CIE version */
666 'z', 'R', 0, /* Augmentation string */
667 1, /* Code alignment factor */
668 0x7c, /* Data alignment factor */
669 8, /* Return address column */
670 1, /* Augmentation size */
671 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
672 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
673 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
674 DW_CFA_nop, DW_CFA_nop,
675
676 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
677 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
678 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
679 0, 0, 0, 0, /* .plt size goes here */
680 0, /* Augmentation size */
681 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
682 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
683 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
684 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
685 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
686 11, /* Block length */
687 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
688 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
689 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
28ede8be 690 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
e41b3a13
JJ
691 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
692};
693
ee2fdd6f
L
694/* .eh_frame covering the lazy .plt section with IBT-enabled. */
695
696static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
697{
698 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
699 0, 0, 0, 0, /* CIE ID */
700 1, /* CIE version */
701 'z', 'R', 0, /* Augmentation string */
702 1, /* Code alignment factor */
703 0x7c, /* Data alignment factor */
704 8, /* Return address column */
705 1, /* Augmentation size */
706 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
707 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
708 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
709 DW_CFA_nop, DW_CFA_nop,
710
711 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
712 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
713 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
714 0, 0, 0, 0, /* .plt size goes here */
715 0, /* Augmentation size */
716 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
717 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
718 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
719 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
720 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
721 11, /* Block length */
722 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
723 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
724 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
725 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
726 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
727};
728
f604c2a2 729/* .eh_frame covering the non-lazy .plt section. */
fff53dae 730
f604c2a2 731static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
fff53dae
L
732{
733#define PLT_GOT_FDE_LENGTH 16
734 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
735 0, 0, 0, 0, /* CIE ID */
736 1, /* CIE version */
737 'z', 'R', 0, /* Augmentation string */
738 1, /* Code alignment factor */
739 0x7c, /* Data alignment factor */
740 8, /* Return address column */
741 1, /* Augmentation size */
742 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
743 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
744 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
745 DW_CFA_nop, DW_CFA_nop,
746
747 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
748 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
f604c2a2
L
749 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
750 0, 0, 0, 0, /* non-lazy .plt size goes here */
fff53dae
L
751 0, /* Augmentation size */
752 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
753};
754
25e762b9 755/* These are the standard parameters. */
765e526c 756static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
25e762b9 757 {
07d6d2b8
AM
758 elf_i386_lazy_plt0_entry, /* plt0_entry */
759 sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */
760 elf_i386_lazy_plt_entry, /* plt_entry */
761 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
762 NULL, /* plt_tlsdesc_entry */
763 0, /* plt_tlsdesc_entry_size*/
764 0, /* plt_tlsdesc_got1_offset */
765 0, /* plt_tlsdesc_got2_offset */
766 0, /* plt_tlsdesc_got1_insn_end */
767 0, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
768 2, /* plt0_got1_offset */
769 8, /* plt0_got2_offset */
770 0, /* plt0_got2_insn_end */
771 2, /* plt_got_offset */
772 7, /* plt_reloc_offset */
773 12, /* plt_plt_offset */
774 0, /* plt_got_insn_size */
775 0, /* plt_plt_insn_end */
776 6, /* plt_lazy_offset */
777 elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
778 elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */
779 elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */
f604c2a2
L
780 sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
781 };
782
765e526c 783static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
f604c2a2 784 {
07d6d2b8
AM
785 elf_i386_non_lazy_plt_entry, /* plt_entry */
786 elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */
787 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
788 2, /* plt_got_offset */
789 0, /* plt_got_insn_size */
790 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
f604c2a2 791 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
25e762b9 792 };
ee2fdd6f 793
765e526c 794static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
ee2fdd6f 795 {
07d6d2b8 796 elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
ee2fdd6f 797 sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
07d6d2b8
AM
798 elf_i386_lazy_ibt_plt_entry, /* plt_entry */
799 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
800 NULL, /* plt_tlsdesc_entry */
801 0, /* plt_tlsdesc_entry_size*/
802 0, /* plt_tlsdesc_got1_offset */
803 0, /* plt_tlsdesc_got2_offset */
804 0, /* plt_tlsdesc_got1_insn_end */
805 0, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
806 2, /* plt0_got1_offset */
807 8, /* plt0_got2_offset */
808 0, /* plt0_got2_insn_end */
809 4+2, /* plt_got_offset */
810 4+1, /* plt_reloc_offset */
811 4+6, /* plt_plt_offset */
812 0, /* plt_got_insn_size */
813 0, /* plt_plt_insn_end */
814 0, /* plt_lazy_offset */
815 elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
816 elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */
817 elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
ee2fdd6f
L
818 sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
819 };
820
765e526c 821static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
ee2fdd6f 822 {
07d6d2b8 823 elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */
ee2fdd6f 824 elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
07d6d2b8
AM
825 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
826 4+2, /* plt_got_offset */
827 0, /* plt_got_insn_size */
828 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
ee2fdd6f
L
829 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
830 };
25e762b9
RM
831\f
832
eac338cf
PB
833/* On VxWorks, the .rel.plt.unloaded section has absolute relocations
834 for the PLTResolve stub and then for each PLT entry. */
835#define PLTRESOLVE_RELOCS_SHLIB 0
836#define PLTRESOLVE_RELOCS 2
837#define PLT_NON_JUMP_SLOT_RELOCS 2
838
142411ca
L
839/* Return TRUE if the TLS access code sequence support transition
840 from R_TYPE. */
841
0a1b45a2 842static bool
bedfd056 843elf_i386_check_tls_transition (asection *sec,
142411ca
L
844 bfd_byte *contents,
845 Elf_Internal_Shdr *symtab_hdr,
846 struct elf_link_hash_entry **sym_hashes,
847 unsigned int r_type,
848 const Elf_Internal_Rela *rel,
849 const Elf_Internal_Rela *relend)
13ae64f3 850{
6eaa7fb5 851 unsigned int val, type, reg;
142411ca
L
852 unsigned long r_symndx;
853 struct elf_link_hash_entry *h;
854 bfd_vma offset;
6eaa7fb5 855 bfd_byte *call;
0a1b45a2 856 bool indirect_call;
142411ca 857
142411ca 858 offset = rel->r_offset;
13ae64f3 859 switch (r_type)
142411ca
L
860 {
861 case R_386_TLS_GD:
862 case R_386_TLS_LDM:
863 if (offset < 2 || (rel + 1) >= relend)
0a1b45a2 864 return false;
142411ca 865
0a1b45a2 866 indirect_call = false;
6eaa7fb5
L
867 call = contents + offset + 4;
868 val = *(call - 5);
869 type = *(call - 6);
142411ca
L
870 if (r_type == R_386_TLS_GD)
871 {
09e8c3bf 872 /* Check transition from GD access model. Only
6eaa7fb5
L
873 leal foo@tlsgd(,%ebx,1), %eax
874 call ___tls_get_addr@PLT
875 or
876 leal foo@tlsgd(%ebx) %eax
877 call ___tls_get_addr@PLT
878 nop
879 or
880 leal foo@tlsgd(%reg), %eax
881 call *___tls_get_addr@GOT(%reg)
882 which may be converted to
883 addr32 call ___tls_get_addr
142411ca 884 can transit to different access model. */
6eaa7fb5
L
885 if ((offset + 10) > sec->size
886 || (type != 0x8d && type != 0x04))
0a1b45a2 887 return false;
142411ca 888
142411ca
L
889 if (type == 0x04)
890 {
6eaa7fb5
L
891 /* leal foo@tlsgd(,%ebx,1), %eax
892 call ___tls_get_addr@PLT */
142411ca 893 if (offset < 3)
0a1b45a2 894 return false;
142411ca 895
6eaa7fb5
L
896 if (*(call - 7) != 0x8d
897 || val != 0x1d
898 || call[0] != 0xe8)
0a1b45a2 899 return false;
142411ca
L
900 }
901 else
902 {
6eaa7fb5
L
903 /* This must be
904 leal foo@tlsgd(%ebx), %eax
905 call ___tls_get_addr@PLT
906 nop
907 or
908 leal foo@tlsgd(%reg), %eax
909 call *___tls_get_addr@GOT(%reg)
910 which may be converted to
911 addr32 call ___tls_get_addr
912
913 %eax can't be used as the GOT base register since it
914 is used to pass parameter to ___tls_get_addr. */
915 reg = val & 7;
916 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
0a1b45a2 917 return false;
142411ca 918
6eaa7fb5
L
919 indirect_call = call[0] == 0xff;
920 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
921 && !(call[0] == 0x67 && call[1] == 0xe8)
922 && !(indirect_call
923 && (call[1] & 0xf8) == 0x90
924 && (call[1] & 0x7) == reg))
0a1b45a2 925 return false;
142411ca
L
926 }
927 }
928 else
929 {
930 /* Check transition from LD access model. Only
6eaa7fb5
L
931 leal foo@tlsldm(%ebx), %eax
932 call ___tls_get_addr@PLT
933 or
934 leal foo@tlsldm(%reg), %eax
935 call *___tls_get_addr@GOT(%reg)
936 which may be converted to
937 addr32 call ___tls_get_addr
142411ca
L
938 can transit to different access model. */
939 if (type != 0x8d || (offset + 9) > sec->size)
0a1b45a2 940 return false;
142411ca 941
6eaa7fb5
L
942 /* %eax can't be used as the GOT base register since it is
943 used to pass parameter to ___tls_get_addr. */
944 reg = val & 7;
945 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
0a1b45a2 946 return false;
142411ca 947
6eaa7fb5
L
948 indirect_call = call[0] == 0xff;
949 if (!(reg == 3 && call[0] == 0xe8)
950 && !(call[0] == 0x67 && call[1] == 0xe8)
951 && !(indirect_call
952 && (call[1] & 0xf8) == 0x90
953 && (call[1] & 0x7) == reg))
0a1b45a2 954 return false;
6eaa7fb5 955 }
142411ca
L
956
957 r_symndx = ELF32_R_SYM (rel[1].r_info);
958 if (r_symndx < symtab_hdr->sh_info)
0a1b45a2 959 return false;
142411ca
L
960
961 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1d4af308 962 if (h == NULL
0afcef53 963 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
0a1b45a2 964 return false;
6eaa7fb5
L
965 else if (indirect_call)
966 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
967 else
968 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
969 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
142411ca
L
970
971 case R_386_TLS_IE:
972 /* Check transition from IE access model:
973 movl foo@indntpoff(%rip), %eax
974 movl foo@indntpoff(%rip), %reg
975 addl foo@indntpoff(%rip), %reg
976 */
977
978 if (offset < 1 || (offset + 4) > sec->size)
0a1b45a2 979 return false;
142411ca
L
980
981 /* Check "movl foo@tpoff(%rip), %eax" first. */
982 val = bfd_get_8 (abfd, contents + offset - 1);
983 if (val == 0xa1)
0a1b45a2 984 return true;
142411ca
L
985
986 if (offset < 2)
0a1b45a2 987 return false;
142411ca
L
988
989 /* Check movl|addl foo@tpoff(%rip), %reg. */
990 type = bfd_get_8 (abfd, contents + offset - 2);
991 return ((type == 0x8b || type == 0x03)
992 && (val & 0xc7) == 0x05);
993
994 case R_386_TLS_GOTIE:
995 case R_386_TLS_IE_32:
996 /* Check transition from {IE_32,GOTIE} access model:
997 subl foo@{tpoff,gontoff}(%reg1), %reg2
998 movl foo@{tpoff,gontoff}(%reg1), %reg2
999 addl foo@{tpoff,gontoff}(%reg1), %reg2
1000 */
1001
1002 if (offset < 2 || (offset + 4) > sec->size)
0a1b45a2 1003 return false;
142411ca
L
1004
1005 val = bfd_get_8 (abfd, contents + offset - 1);
1006 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
0a1b45a2 1007 return false;
142411ca
L
1008
1009 type = bfd_get_8 (abfd, contents + offset - 2);
1010 return type == 0x8b || type == 0x2b || type == 0x03;
1011
1012 case R_386_TLS_GOTDESC:
1013 /* Check transition from GDesc access model:
1014 leal x@tlsdesc(%ebx), %eax
1015
1016 Make sure it's a leal adding ebx to a 32-bit offset
1017 into any register, although it's probably almost always
1018 going to be eax. */
1019
1020 if (offset < 2 || (offset + 4) > sec->size)
0a1b45a2 1021 return false;
142411ca
L
1022
1023 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
0a1b45a2 1024 return false;
142411ca
L
1025
1026 val = bfd_get_8 (abfd, contents + offset - 1);
1027 return (val & 0xc7) == 0x83;
1028
1029 case R_386_TLS_DESC_CALL:
1030 /* Check transition from GDesc access model:
6eaa7fb5 1031 call *x@tlsdesc(%eax)
142411ca
L
1032 */
1033 if (offset + 2 <= sec->size)
1034 {
6eaa7fb5
L
1035 /* Make sure that it's a call *x@tlsdesc(%eax). */
1036 call = contents + offset;
1037 return call[0] == 0xff && call[1] == 0x10;
142411ca
L
1038 }
1039
0a1b45a2 1040 return false;
142411ca
L
1041
1042 default:
1043 abort ();
1044 }
1045}
1046
1047/* Return TRUE if the TLS access transition is OK or no transition
1048 will be performed. Update R_TYPE if there is a transition. */
1049
0a1b45a2 1050static bool
142411ca
L
1051elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1052 asection *sec, bfd_byte *contents,
1053 Elf_Internal_Shdr *symtab_hdr,
1054 struct elf_link_hash_entry **sym_hashes,
1055 unsigned int *r_type, int tls_type,
1056 const Elf_Internal_Rela *rel,
1057 const Elf_Internal_Rela *relend,
4c544807 1058 struct elf_link_hash_entry *h,
bedfd056 1059 unsigned long r_symndx,
0a1b45a2 1060 bool from_relocate_section)
142411ca
L
1061{
1062 unsigned int from_type = *r_type;
1063 unsigned int to_type = from_type;
0a1b45a2 1064 bool check = true;
1bb82b89 1065 unsigned int to_le_type, to_ie_type;
142411ca 1066
bb1cb422
L
1067 /* Skip TLS transition for functions. */
1068 if (h != NULL
1069 && (h->type == STT_FUNC
1070 || h->type == STT_GNU_IFUNC))
0a1b45a2 1071 return true;
bb1cb422 1072
1bb82b89
RO
1073 if (get_elf_backend_data (abfd)->target_os == is_solaris)
1074 {
1075 /* NB: Solaris only supports R_386_TLS_LE and R_386_TLS_IE. */
1076 to_le_type = R_386_TLS_LE;
1077 to_ie_type = R_386_TLS_IE;
1078 }
1079 else
1080 {
1081 to_le_type = R_386_TLS_LE_32;
1082 to_ie_type = R_386_TLS_IE_32;
1083 }
1084
142411ca 1085 switch (from_type)
13ae64f3
JJ
1086 {
1087 case R_386_TLS_GD:
67a4f2b7
AO
1088 case R_386_TLS_GOTDESC:
1089 case R_386_TLS_DESC_CALL:
13ae64f3 1090 case R_386_TLS_IE_32:
37e55690
JJ
1091 case R_386_TLS_IE:
1092 case R_386_TLS_GOTIE:
0e1862bb 1093 if (bfd_link_executable (info))
142411ca
L
1094 {
1095 if (h == NULL)
1bb82b89 1096 to_type = to_le_type;
142411ca
L
1097 else if (from_type != R_386_TLS_IE
1098 && from_type != R_386_TLS_GOTIE)
1bb82b89 1099 to_type = to_ie_type;
142411ca
L
1100 }
1101
bedfd056
L
1102 /* When we are called from elf_i386_relocate_section, there may
1103 be additional transitions based on TLS_TYPE. */
1104 if (from_relocate_section)
142411ca
L
1105 {
1106 unsigned int new_to_type = to_type;
1107
51537393 1108 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1bb82b89 1109 new_to_type = to_le_type;
142411ca
L
1110
1111 if (to_type == R_386_TLS_GD
1112 || to_type == R_386_TLS_GOTDESC
1113 || to_type == R_386_TLS_DESC_CALL)
1114 {
1115 if (tls_type == GOT_TLS_IE_POS)
1116 new_to_type = R_386_TLS_GOTIE;
1117 else if (tls_type & GOT_TLS_IE)
1bb82b89 1118 new_to_type = to_ie_type;
142411ca
L
1119 }
1120
1121 /* We checked the transition before when we were called from
3747999c 1122 elf_i386_scan_relocs. We only want to check the new
142411ca
L
1123 transition which hasn't been checked before. */
1124 check = new_to_type != to_type && from_type == to_type;
1125 to_type = new_to_type;
1126 }
1127
1128 break;
1129
13ae64f3 1130 case R_386_TLS_LDM:
0e1862bb 1131 if (bfd_link_executable (info))
1bb82b89 1132 to_type = to_le_type;
142411ca
L
1133 break;
1134
1135 default:
0a1b45a2 1136 return true;
142411ca
L
1137 }
1138
1139 /* Return TRUE if there is no transition. */
1140 if (from_type == to_type)
0a1b45a2 1141 return true;
142411ca
L
1142
1143 /* Check if the transition can be performed. */
1144 if (check
bedfd056 1145 && ! elf_i386_check_tls_transition (sec, contents,
142411ca
L
1146 symtab_hdr, sym_hashes,
1147 from_type, rel, relend))
1148 {
2f629d23 1149 reloc_howto_type *from, *to;
4c544807 1150 const char *name;
142411ca 1151
1273b2f8
L
1152 from = elf_i386_rtype_to_howto (from_type);
1153 to = elf_i386_rtype_to_howto (to_type);
142411ca 1154
4c544807
L
1155 if (h)
1156 name = h->root.root.string;
1157 else
1158 {
0afcef53 1159 struct elf_x86_link_hash_table *htab;
4dfe6ac6 1160
0afcef53 1161 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6
NC
1162 if (htab == NULL)
1163 name = "*unknown*";
1164 else
1165 {
1166 Elf_Internal_Sym *isym;
1167
f1dfbfdb 1168 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4dfe6ac6
NC
1169 abfd, r_symndx);
1170 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1171 }
4c544807
L
1172 }
1173
4eca0228 1174 _bfd_error_handler
695344c0 1175 /* xgettext:c-format */
2dcf00ce
AM
1176 (_("%pB: TLS transition from %s to %s against `%s'"
1177 " at %#" PRIx64 " in section `%pA' failed"),
c08bb8dd 1178 abfd, from->name, to->name, name,
2dcf00ce 1179 (uint64_t) rel->r_offset, sec);
142411ca 1180 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1181 return false;
13ae64f3
JJ
1182 }
1183
142411ca 1184 *r_type = to_type;
0a1b45a2 1185 return true;
13ae64f3
JJ
1186}
1187
c175a657
L
1188/* With the local symbol, foo, we convert
1189 mov foo@GOT[(%reg1)], %reg2
1190 to
1191 lea foo[@GOTOFF(%reg1)], %reg2
1192 and convert
1193 call/jmp *foo@GOT[(%reg)]
1194 to
1195 nop call foo/jmp foo nop
1196 When PIC is false, convert
1197 test %reg1, foo@GOT[(%reg2)]
1198 to
1199 test $foo, %reg1
1200 and convert
1201 binop foo@GOT[(%reg1)], %reg2
1202 to
1203 binop $foo, %reg2
1204 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1205 instructions. */
1206
1207static
0a1b45a2 1208bool
c175a657
L
1209elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1210 bfd_byte *contents,
6999821f 1211 unsigned int *r_type_p,
c175a657
L
1212 Elf_Internal_Rela *irel,
1213 struct elf_link_hash_entry *h,
0a1b45a2 1214 bool *converted,
c175a657
L
1215 struct bfd_link_info *link_info)
1216{
0afcef53 1217 struct elf_x86_link_hash_table *htab;
c175a657
L
1218 unsigned int opcode;
1219 unsigned int modrm;
0a1b45a2 1220 bool baseless;
c175a657
L
1221 Elf_Internal_Sym *isym;
1222 unsigned int addend;
1223 unsigned int nop;
1224 bfd_vma nop_offset;
0a1b45a2
AM
1225 bool is_pic;
1226 bool to_reloc_32;
1227 bool abs_symbol;
c175a657
L
1228 unsigned int r_type;
1229 unsigned int r_symndx;
1230 bfd_vma roff = irel->r_offset;
0a1b45a2 1231 bool local_ref;
0a27fed7 1232 struct elf_x86_link_hash_entry *eh;
c175a657
L
1233
1234 if (roff < 2)
0a1b45a2 1235 return true;
c175a657 1236
7d4d9709 1237 /* Addend for R_386_GOT32X relocations must be 0. */
c175a657
L
1238 addend = bfd_get_32 (abfd, contents + roff);
1239 if (addend != 0)
0a1b45a2 1240 return true;
c175a657 1241
0afcef53 1242 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
c175a657
L
1243 is_pic = bfd_link_pic (link_info);
1244
6999821f 1245 r_type = *r_type_p;
c175a657
L
1246 r_symndx = ELF32_R_SYM (irel->r_info);
1247
1248 modrm = bfd_get_8 (abfd, contents + roff - 1);
1249 baseless = (modrm & 0xc7) == 0x5;
1250
382aae06
L
1251 if (h)
1252 {
1253 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1254 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1255 isym = NULL;
1256 abs_symbol = ABS_SYMBOL_P (h);
1257 }
1258 else
1259 {
0a1b45a2 1260 local_ref = true;
f1dfbfdb 1261 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
382aae06
L
1262 r_symndx);
1263 abs_symbol = isym->st_shndx == SHN_ABS;
1264 }
1265
7d4d9709 1266 if (baseless && is_pic)
c175a657
L
1267 {
1268 /* For PIC, disallow R_386_GOT32X without a base register
7d4d9709 1269 since we don't know what the GOT base is. */
c175a657
L
1270 const char *name;
1271
1272 if (h == NULL)
382aae06 1273 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
c175a657
L
1274 else
1275 name = h->root.root.string;
1276
4eca0228 1277 _bfd_error_handler
695344c0 1278 /* xgettext:c-format */
871b3ab2 1279 (_("%pB: direct GOT relocation R_386_GOT32X against `%s' without base"
63a5468a 1280 " register can not be used when making a shared object"),
c175a657 1281 abfd, name);
0a1b45a2 1282 return false;
c175a657
L
1283 }
1284
1285 opcode = bfd_get_8 (abfd, contents + roff - 2);
1286
c175a657
L
1287 /* Convert to R_386_32 if PIC is false or there is no base
1288 register. */
1289 to_reloc_32 = !is_pic || baseless;
1290
0a27fed7
L
1291 eh = elf_x86_hash_entry (h);
1292
7d4d9709
L
1293 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1294 reloc. */
c175a657
L
1295 if (h == NULL)
1296 {
1297 if (opcode == 0x0ff)
1298 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1299 goto convert_branch;
1300 else
1301 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1302 "test %reg1, foo@GOT(%reg2)" and
1303 "binop foo@GOT[(%reg1)], %reg2". */
1304 goto convert_load;
1305 }
1306
1307 /* Undefined weak symbol is only bound locally in executable
1308 and its reference is resolved as 0. */
0a27fed7
L
1309 if (h->root.type == bfd_link_hash_undefweak
1310 && !eh->linker_def
1311 && local_ref)
c175a657
L
1312 {
1313 if (opcode == 0xff)
1314 {
1315 /* No direct branch to 0 for PIC. */
1316 if (is_pic)
0a1b45a2 1317 return true;
c175a657
L
1318 else
1319 goto convert_branch;
1320 }
1321 else
1322 {
1323 /* We can convert load of address 0 to R_386_32. */
0a1b45a2 1324 to_reloc_32 = true;
c175a657
L
1325 goto convert_load;
1326 }
1327 }
1328
1329 if (opcode == 0xff)
1330 {
1331 /* We have "call/jmp *foo@GOT[(%reg)]". */
1332 if ((h->root.type == bfd_link_hash_defined
1333 || h->root.type == bfd_link_hash_defweak)
0a27fed7 1334 && local_ref)
c175a657
L
1335 {
1336 /* The function is locally defined. */
dc1e8a47 1337 convert_branch:
c175a657
L
1338 /* Convert R_386_GOT32X to R_386_PC32. */
1339 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1340 {
1341 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1342 is a nop prefix. */
1343 modrm = 0xe8;
6eaa7fb5
L
1344 /* To support TLS optimization, always use addr32 prefix
1345 for "call *___tls_get_addr@GOT(%reg)". */
1d4af308 1346 if (eh && eh->tls_get_addr)
c175a657 1347 {
6eaa7fb5
L
1348 nop = 0x67;
1349 nop_offset = irel->r_offset - 2;
c175a657
L
1350 }
1351 else
6eaa7fb5 1352 {
5b9c07b2
L
1353 nop = htab->params->call_nop_byte;
1354 if (htab->params->call_nop_as_suffix)
6eaa7fb5
L
1355 {
1356 nop_offset = roff + 3;
1357 irel->r_offset -= 1;
1358 }
1359 else
1360 nop_offset = roff - 2;
1361 }
c175a657
L
1362 }
1363 else
1364 {
1365 /* Convert to "jmp foo nop". */
1366 modrm = 0xe9;
1367 nop = NOP_OPCODE;
1368 nop_offset = roff + 3;
1369 irel->r_offset -= 1;
1370 }
1371
1372 bfd_put_8 (abfd, nop, contents + nop_offset);
1373 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1374 /* When converting to PC-relative relocation, we
1375 need to adjust addend by -4. */
1376 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1377 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
6999821f 1378 *r_type_p = R_386_PC32;
0a1b45a2 1379 *converted = true;
c175a657
L
1380 }
1381 }
1382 else
1383 {
1384 /* We have "mov foo@GOT[(%re1g)], %reg2",
1385 "test %reg1, foo@GOT(%reg2)" and
1386 "binop foo@GOT[(%reg1)], %reg2".
1387
1388 Avoid optimizing _DYNAMIC since ld.so may use its
1389 link-time address. */
1390 if (h == htab->elf.hdynamic)
0a1b45a2 1391 return true;
c175a657
L
1392
1393 /* def_regular is set by an assignment in a linker script in
cbd0eecf
L
1394 bfd_elf_record_link_assignment. start_stop is set on
1395 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1396 if (h->start_stop
0a27fed7 1397 || eh->linker_def
cbd0eecf
L
1398 || ((h->def_regular
1399 || h->root.type == bfd_link_hash_defined
1400 || h->root.type == bfd_link_hash_defweak)
0a27fed7 1401 && local_ref))
c175a657 1402 {
dc1e8a47 1403 convert_load:
c175a657
L
1404 if (opcode == 0x8b)
1405 {
382aae06 1406 if (abs_symbol && local_ref)
0a1b45a2 1407 to_reloc_32 = true;
382aae06 1408
c175a657
L
1409 if (to_reloc_32)
1410 {
1411 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1412 "mov $foo, %reg2" with R_386_32. */
1413 r_type = R_386_32;
1414 modrm = 0xc0 | (modrm & 0x38) >> 3;
1415 bfd_put_8 (abfd, modrm, contents + roff - 1);
1416 opcode = 0xc7;
1417 }
1418 else
1419 {
1420 /* Convert "mov foo@GOT(%reg1), %reg2" to
1421 "lea foo@GOTOFF(%reg1), %reg2". */
1422 r_type = R_386_GOTOFF;
1423 opcode = 0x8d;
1424 }
1425 }
1426 else
1427 {
1428 /* Only R_386_32 is supported. */
1429 if (!to_reloc_32)
0a1b45a2 1430 return true;
c175a657
L
1431
1432 if (opcode == 0x85)
1433 {
1434 /* Convert "test %reg1, foo@GOT(%reg2)" to
1435 "test $foo, %reg1". */
1436 modrm = 0xc0 | (modrm & 0x38) >> 3;
1437 opcode = 0xf7;
1438 }
1439 else
1440 {
1441 /* Convert "binop foo@GOT(%reg1), %reg2" to
1442 "binop $foo, %reg2". */
1443 modrm = (0xc0
1444 | (modrm & 0x38) >> 3
1445 | (opcode & 0x3c));
1446 opcode = 0x81;
1447 }
1448 bfd_put_8 (abfd, modrm, contents + roff - 1);
1449 r_type = R_386_32;
1450 }
1451
1452 bfd_put_8 (abfd, opcode, contents + roff - 2);
1453 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
6999821f 1454 *r_type_p = r_type;
0a1b45a2 1455 *converted = true;
c175a657
L
1456 }
1457 }
1458
0a1b45a2 1459 return true;
c175a657
L
1460}
1461
252b5132 1462/* Look through the relocs for a section during the first phase, and
3747999c
L
1463 calculate needed space in the global offset table, and procedure
1464 linkage table. */
252b5132 1465
0a1b45a2 1466static bool
3747999c
L
1467elf_i386_scan_relocs (bfd *abfd,
1468 struct bfd_link_info *info,
1469 asection *sec,
1470 const Elf_Internal_Rela *relocs)
252b5132 1471{
0afcef53 1472 struct elf_x86_link_hash_table *htab;
252b5132
RH
1473 Elf_Internal_Shdr *symtab_hdr;
1474 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
1475 const Elf_Internal_Rela *rel;
1476 const Elf_Internal_Rela *rel_end;
bedfd056 1477 bfd_byte *contents;
0a1b45a2 1478 bool converted;
252b5132 1479
0e1862bb 1480 if (bfd_link_relocatable (info))
0a1b45a2 1481 return true;
252b5132 1482
0afcef53 1483 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6 1484 if (htab == NULL)
c337a162
L
1485 {
1486 sec->check_relocs_failed = 1;
0a1b45a2 1487 return false;
c337a162 1488 }
4dfe6ac6 1489
fe53b4a4
L
1490 BFD_ASSERT (is_x86_elf (abfd, htab));
1491
bedfd056
L
1492 /* Get the section contents. */
1493 if (elf_section_data (sec)->this_hdr.contents != NULL)
1494 contents = elf_section_data (sec)->this_hdr.contents;
1495 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1496 {
1497 sec->check_relocs_failed = 1;
0a1b45a2 1498 return false;
bedfd056
L
1499 }
1500
0ffa91dd 1501 symtab_hdr = &elf_symtab_hdr (abfd);
252b5132 1502 sym_hashes = elf_sym_hashes (abfd);
252b5132 1503
0a1b45a2 1504 converted = false;
5e5e02ae 1505
252b5132
RH
1506 rel_end = relocs + sec->reloc_count;
1507 for (rel = relocs; rel < rel_end; rel++)
1508 {
13ae64f3 1509 unsigned int r_type;
d42c267e 1510 unsigned int r_symndx;
252b5132 1511 struct elf_link_hash_entry *h;
0afcef53 1512 struct elf_x86_link_hash_entry *eh;
4c544807
L
1513 Elf_Internal_Sym *isym;
1514 const char *name;
0a1b45a2
AM
1515 bool size_reloc;
1516 bool no_dynreloc;
252b5132
RH
1517
1518 r_symndx = ELF32_R_SYM (rel->r_info);
13ae64f3 1519 r_type = ELF32_R_TYPE (rel->r_info);
252b5132 1520
d9bc7a44 1521 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
f5f31454 1522 {
695344c0 1523 /* xgettext:c-format */
871b3ab2 1524 _bfd_error_handler (_("%pB: bad symbol index: %d"),
4eca0228 1525 abfd, r_symndx);
c337a162 1526 goto error_return;
f5f31454
L
1527 }
1528
252b5132 1529 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1530 {
1531 /* A local symbol. */
f1dfbfdb 1532 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
c2e61a4e
L
1533 abfd, r_symndx);
1534 if (isym == NULL)
c337a162 1535 goto error_return;
c25bc9fc
L
1536
1537 /* Check relocation against local STT_GNU_IFUNC symbol. */
c25bc9fc
L
1538 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1539 {
0a1b45a2 1540 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, true);
c25bc9fc 1541 if (h == NULL)
c337a162 1542 goto error_return;
6bbec505 1543
c25bc9fc 1544 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1545 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1546 isym, NULL);
c25bc9fc
L
1547 h->type = STT_GNU_IFUNC;
1548 h->def_regular = 1;
1549 h->ref_regular = 1;
1550 h->forced_local = 1;
1551 h->root.type = bfd_link_hash_defined;
1552 }
1553 else
1554 h = NULL;
1555 }
252b5132 1556 else
71cb9464 1557 {
4c544807 1558 isym = NULL;
71cb9464
L
1559 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1560 while (h->root.type == bfd_link_hash_indirect
1561 || h->root.type == bfd_link_hash_warning)
1562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1563 }
cbe950e9 1564
0afcef53 1565 eh = (struct elf_x86_link_hash_entry *) h;
c25bc9fc
L
1566 if (h != NULL)
1567 {
f604c2a2
L
1568 if (r_type == R_386_GOTOFF)
1569 eh->gotoff_ref = 1;
cbe950e9 1570
ad1e85de
L
1571 /* It is referenced by a non-shared object. */
1572 h->ref_regular = 1;
71cb9464 1573 }
252b5132 1574
6999821f
L
1575 if (r_type == R_386_GOT32X
1576 && (h == NULL || h->type != STT_GNU_IFUNC))
1577 {
1578 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1579 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
5e5e02ae
L
1580 &r_type, irel, h,
1581 &converted, info))
6999821f
L
1582 goto error_return;
1583 }
1584
382aae06
L
1585 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
1586 symtab_hdr, &no_dynreloc))
0a1b45a2 1587 return false;
382aae06 1588
bedfd056 1589 if (! elf_i386_tls_transition (info, abfd, sec, contents,
142411ca
L
1590 symtab_hdr, sym_hashes,
1591 &r_type, GOT_UNKNOWN,
0a1b45a2 1592 rel, rel_end, h, r_symndx, false))
c337a162 1593 goto error_return;
13ae64f3 1594
cd048363
L
1595 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
1596 if (h == htab->elf.hgot)
0a1b45a2 1597 htab->got_referenced = true;
cd048363 1598
13ae64f3 1599 switch (r_type)
252b5132 1600 {
37e55690 1601 case R_386_TLS_LDM:
0f09b4d8 1602 htab->tls_ld_or_ldm_got.refcount = 1;
37e55690
JJ
1603 goto create_got;
1604
1605 case R_386_PLT32:
1606 /* This symbol requires a procedure linkage table entry. We
1607 actually build the entry in adjust_dynamic_symbol,
1608 because this might be a case of linking PIC code which is
1609 never referenced by a dynamic object, in which case we
1610 don't need to generate a procedure linkage table entry
1611 after all. */
1612
1613 /* If this is a local symbol, we resolve it directly without
1614 creating a procedure linkage table entry. */
1615 if (h == NULL)
1616 continue;
1617
98b273dc 1618 eh->zero_undefweak &= 0x2;
f5385ebf 1619 h->needs_plt = 1;
79b0c981 1620 h->plt.refcount = 1;
37e55690
JJ
1621 break;
1622
6a3e1bae 1623 case R_386_SIZE32:
0a1b45a2 1624 size_reloc = true;
6a3e1bae
L
1625 goto do_size;
1626
13ae64f3 1627 case R_386_TLS_IE_32:
37e55690
JJ
1628 case R_386_TLS_IE:
1629 case R_386_TLS_GOTIE:
0e1862bb 1630 if (!bfd_link_executable (info))
13ae64f3 1631 info->flags |= DF_STATIC_TLS;
37e55690
JJ
1632 /* Fall through */
1633
252b5132 1634 case R_386_GOT32:
02a86693 1635 case R_386_GOT32X:
13ae64f3 1636 case R_386_TLS_GD:
67a4f2b7
AO
1637 case R_386_TLS_GOTDESC:
1638 case R_386_TLS_DESC_CALL:
252b5132 1639 /* This symbol requires a global offset table entry. */
13ae64f3
JJ
1640 {
1641 int tls_type, old_tls_type;
1642
1643 switch (r_type)
1644 {
1645 default:
02a86693
L
1646 case R_386_GOT32:
1647 case R_386_GOT32X:
1648 tls_type = GOT_NORMAL;
1649 break;
13ae64f3 1650 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
67a4f2b7
AO
1651 case R_386_TLS_GOTDESC:
1652 case R_386_TLS_DESC_CALL:
1653 tls_type = GOT_TLS_GDESC; break;
37e55690
JJ
1654 case R_386_TLS_IE_32:
1655 if (ELF32_R_TYPE (rel->r_info) == r_type)
1656 tls_type = GOT_TLS_IE_NEG;
1657 else
ebcfb3c0
JJ
1658 /* If this is a GD->IE transition, we may use either of
1659 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
37e55690
JJ
1660 tls_type = GOT_TLS_IE;
1661 break;
1662 case R_386_TLS_IE:
1663 case R_386_TLS_GOTIE:
1664 tls_type = GOT_TLS_IE_POS; break;
13ae64f3
JJ
1665 }
1666
1667 if (h != NULL)
1668 {
0f09b4d8 1669 h->got.refcount = 1;
0afcef53 1670 old_tls_type = elf_x86_hash_entry (h)->tls_type;
13ae64f3
JJ
1671 }
1672 else
1673 {
1674 bfd_signed_vma *local_got_refcounts;
1675
bbdeb108
L
1676 if (!elf_x86_allocate_local_got_info (abfd,
1677 symtab_hdr->sh_info))
1678 goto error_return;
1679
13ae64f3
JJ
1680 /* This is a global offset table entry for a local symbol. */
1681 local_got_refcounts = elf_local_got_refcounts (abfd);
0f09b4d8 1682 local_got_refcounts[r_symndx] = 1;
0afcef53 1683 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
13ae64f3
JJ
1684 }
1685
37e55690
JJ
1686 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1687 tls_type |= old_tls_type;
13ae64f3
JJ
1688 /* If a TLS symbol is accessed using IE at least once,
1689 there is no point to use dynamic model for it. */
ebcfb3c0 1690 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7 1691 && (! GOT_TLS_GD_ANY_P (old_tls_type)
37e55690 1692 || (tls_type & GOT_TLS_IE) == 0))
13ae64f3 1693 {
67a4f2b7 1694 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
37e55690 1695 tls_type = old_tls_type;
67a4f2b7
AO
1696 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1697 && GOT_TLS_GD_ANY_P (tls_type))
1698 tls_type |= old_tls_type;
13ae64f3
JJ
1699 else
1700 {
09a24cbf 1701 if (h)
4c544807
L
1702 name = h->root.root.string;
1703 else
1704 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1705 NULL);
4eca0228 1706 _bfd_error_handler
695344c0 1707 /* xgettext:c-format */
871b3ab2 1708 (_("%pB: `%s' accessed both as normal and "
55fd94b0 1709 "thread local symbol"),
4c544807 1710 abfd, name);
68c4a57e 1711 bfd_set_error (bfd_error_bad_value);
c337a162 1712 goto error_return;
13ae64f3
JJ
1713 }
1714 }
1715
1716 if (old_tls_type != tls_type)
1717 {
1718 if (h != NULL)
0afcef53 1719 elf_x86_hash_entry (h)->tls_type = tls_type;
13ae64f3 1720 else
0afcef53 1721 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
13ae64f3
JJ
1722 }
1723 }
0ac8d2ca
AM
1724 /* Fall through */
1725
1726 case R_386_GOTOFF:
1727 case R_386_GOTPC:
dc1e8a47 1728 create_got:
37e55690 1729 if (r_type != R_386_TLS_IE)
aec6b87e
L
1730 {
1731 if (eh != NULL)
cd048363
L
1732 {
1733 eh->zero_undefweak &= 0x2;
1734
1735 /* Need GOT to resolve undefined weak symbol to 0. */
1736 if (r_type == R_386_GOTOFF
1737 && h->root.type == bfd_link_hash_undefweak
1738 && bfd_link_executable (info))
0a1b45a2 1739 htab->got_referenced = true;
cd048363 1740 }
aec6b87e
L
1741 break;
1742 }
37e55690 1743 /* Fall through */
252b5132 1744
37e55690
JJ
1745 case R_386_TLS_LE_32:
1746 case R_386_TLS_LE:
aec6b87e 1747 if (eh != NULL)
98b273dc 1748 eh->zero_undefweak &= 0x2;
0e1862bb 1749 if (bfd_link_executable (info))
37e55690 1750 break;
bffbf940 1751 info->flags |= DF_STATIC_TLS;
aec6b87e 1752 goto do_relocation;
252b5132
RH
1753
1754 case R_386_32:
1755 case R_386_PC32:
aec6b87e 1756 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
98b273dc 1757 eh->zero_undefweak |= 0x2;
dc1e8a47 1758 do_relocation:
bf52d7c7
L
1759 /* We are called after all symbols have been resolved. Only
1760 relocation against STT_GNU_IFUNC symbol must go through
1761 PLT. */
d1ed1c7d
L
1762 if (h != NULL
1763 && (bfd_link_executable (info)
bf52d7c7 1764 || h->type == STT_GNU_IFUNC))
6725bdbf 1765 {
0a1b45a2 1766 bool func_pointer_ref = false;
233cc9c1 1767
4b627c18
L
1768 if (r_type == R_386_PC32)
1769 {
1770 /* Since something like ".long foo - ." may be used
1771 as pointer, make sure that PLT is used if foo is
1772 a function defined in a shared library. */
1773 if ((sec->flags & SEC_CODE) == 0)
1774 h->pointer_equality_needed = 1;
74437ea2
L
1775 else if (h->type == STT_GNU_IFUNC
1776 && bfd_link_pic (info))
1777 {
6322e5c5
L
1778 _bfd_error_handler
1779 /* xgettext:c-format */
871b3ab2 1780 (_("%pB: unsupported non-PIC call to IFUNC `%s'"),
6322e5c5 1781 abfd, h->root.root.string);
74437ea2
L
1782 bfd_set_error (bfd_error_bad_value);
1783 goto error_return;
1784 }
4b627c18
L
1785 }
1786 else
04ebc307 1787 {
68c4956b
L
1788 /* R_386_32 can be resolved at run-time. Function
1789 pointer reference doesn't need PLT for pointer
1790 equality. */
04ebc307
L
1791 if (r_type == R_386_32
1792 && (sec->flags & SEC_READONLY) == 0)
0a1b45a2 1793 func_pointer_ref = true;
98a1627f
L
1794
1795 /* IFUNC symbol needs pointer equality in PDE so that
1796 function pointer reference will be resolved to its
1797 PLT entry directly. */
1798 if (!func_pointer_ref
1799 || (bfd_link_pde (info)
1800 && h->type == STT_GNU_IFUNC))
68c4956b 1801 h->pointer_equality_needed = 1;
ebb191ad
L
1802 }
1803
79b0c981
L
1804 if (!func_pointer_ref)
1805 {
1806 /* If this reloc is in a read-only section, we might
1807 need a copy reloc. We can't check reliably at this
1808 stage whether the section is read-only, as input
1809 sections have not yet been mapped to output sections.
1810 Tentatively set the flag for now, and correct in
1811 adjust_dynamic_symbol. */
1812 h->non_got_ref = 1;
1813
6f365fda
L
1814 if (!elf_has_indirect_extern_access (sec->owner))
1815 eh->non_got_ref_without_indirect_extern_access = 1;
1816
79b0c981
L
1817 /* We may need a .plt entry if the symbol is a function
1818 defined in a shared lib or is a function referenced
1819 from the code or read-only section. */
1820 if (!h->def_regular
1821 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1822 h->plt.refcount = 1;
68c4956b 1823
2c43d202
L
1824 if (htab->elf.target_os != is_solaris
1825 && h->pointer_equality_needed
68c4956b
L
1826 && h->type == STT_FUNC
1827 && eh->def_protected
bd073612 1828 && !SYMBOL_DEFINED_NON_SHARED_P (h)
f6386577 1829 && h->def_dynamic)
68c4956b
L
1830 {
1831 /* Disallow non-canonical reference to canonical
1832 protected function. */
1833 _bfd_error_handler
1834 /* xgettext:c-format */
1835 (_("%pB: non-canonical reference to canonical "
1836 "protected function `%s' in %pB"),
1837 abfd, h->root.root.string,
1838 h->root.u.def.section->owner);
1839 bfd_set_error (bfd_error_bad_value);
1840 goto error_return;
1841 }
04ebc307 1842 }
6725bdbf 1843 }
7843f00e 1844
0a1b45a2 1845 size_reloc = false;
dc1e8a47 1846 do_size:
382aae06 1847 if (!no_dynreloc
831083d3
L
1848 && NEED_DYNAMIC_RELOCATION_P (false, info, false, h, sec,
1849 r_type, R_386_32))
252b5132 1850 {
e03a8ed8
L
1851 struct elf_dyn_relocs *p;
1852 struct elf_dyn_relocs **head;
ec338859 1853
0c715baa
AM
1854 /* If this is a global symbol, we count the number of
1855 relocations we need for this symbol. */
1856 if (h != NULL)
252b5132 1857 {
190eb1dd 1858 head = &h->dyn_relocs;
0c715baa
AM
1859 }
1860 else
1861 {
ec338859
AM
1862 /* Track dynamic relocs needed for local syms too.
1863 We really need local syms available to do this
1864 easily. Oh well. */
87d72d41 1865 void **vpp;
ec338859 1866 asection *s;
87d72d41 1867
f1dfbfdb 1868 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
1869 abfd, r_symndx);
1870 if (isym == NULL)
c337a162 1871 goto error_return;
87d72d41
AM
1872
1873 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
ec338859 1874 if (s == NULL)
87d72d41 1875 s = sec;
ec338859 1876
e81d3500 1877 vpp = &elf_section_data (s)->local_dynrel;
e03a8ed8 1878 head = (struct elf_dyn_relocs **)vpp;
ec338859
AM
1879 }
1880
1881 p = *head;
1882 if (p == NULL || p->sec != sec)
1883 {
986f0783 1884 size_t amt = sizeof *p;
a50b1753 1885 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
07d6d2b8 1886 amt);
ec338859 1887 if (p == NULL)
c337a162 1888 goto error_return;
ec338859
AM
1889 p->next = *head;
1890 *head = p;
1891 p->sec = sec;
1892 p->count = 0;
1893 p->pc_count = 0;
252b5132 1894 }
ec338859
AM
1895
1896 p->count += 1;
06a6a421
L
1897 /* Count size relocation as PC-relative relocation. */
1898 if (r_type == R_386_PC32 || size_reloc)
ec338859 1899 p->pc_count += 1;
252b5132 1900 }
252b5132
RH
1901 break;
1902
1903 /* This relocation describes the C++ object vtable hierarchy.
1904 Reconstruct it for later use during GC. */
1905 case R_386_GNU_VTINHERIT:
d17e0c6e 1906 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c337a162 1907 goto error_return;
252b5132
RH
1908 break;
1909
1910 /* This relocation describes which C++ vtable entries are actually
1911 used. Record for later use during GC. */
1912 case R_386_GNU_VTENTRY:
a0ea3a14 1913 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
c337a162 1914 goto error_return;
252b5132
RH
1915 break;
1916
1917 default:
1918 break;
1919 }
1920 }
1921
bedfd056
L
1922 if (elf_section_data (sec)->this_hdr.contents != contents)
1923 {
a8dde0a2 1924 if (!converted && !_bfd_link_keep_memory (info))
bedfd056
L
1925 free (contents);
1926 else
1927 {
5e5e02ae
L
1928 /* Cache the section contents for elf_link_input_bfd if any
1929 load is converted or --no-keep-memory isn't used. */
bedfd056 1930 elf_section_data (sec)->this_hdr.contents = contents;
a8dde0a2 1931 info->cache_size += sec->size;
bedfd056
L
1932 }
1933 }
1934
5e5e02ae
L
1935 /* Cache relocations if any load is converted. */
1936 if (elf_section_data (sec)->relocs != relocs && converted)
1937 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1938
0a1b45a2 1939 return true;
c337a162 1940
dc1e8a47 1941 error_return:
bedfd056
L
1942 if (elf_section_data (sec)->this_hdr.contents != contents)
1943 free (contents);
c337a162 1944 sec->check_relocs_failed = 1;
0a1b45a2 1945 return false;
252b5132
RH
1946}
1947
3747999c
L
1948static bool
1949elf_i386_always_size_sections (bfd *output_bfd,
1950 struct bfd_link_info *info)
1951{
1952 bfd *abfd;
1953
1954 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
1955 for (abfd = info->input_bfds;
1956 abfd != (bfd *) NULL;
1957 abfd = abfd->link.next)
1958 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1959 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
1960 elf_i386_scan_relocs))
1961 return false;
1962
1963 return _bfd_x86_elf_always_size_sections (output_bfd, info);
1964}
1965
38701953
AM
1966/* Set the correct type for an x86 ELF section. We do this by the
1967 section name, which is a hack, but ought to work. */
1968
0a1b45a2 1969static bool
55fd94b0
AM
1970elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1971 Elf_Internal_Shdr *hdr,
1972 asection *sec)
38701953 1973{
91d6fa6a 1974 const char *name;
38701953 1975
fd361982 1976 name = bfd_section_name (sec);
38701953
AM
1977
1978 /* This is an ugly, but unfortunately necessary hack that is
1979 needed when producing EFI binaries on x86. It tells
1980 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1981 containing ELF relocation info. We need this hack in order to
1982 be able to generate ELF binaries that can be translated into
1983 EFI applications (which are essentially COFF objects). Those
1984 files contain a COFF ".reloc" section inside an ELFNN object,
1985 which would normally cause BFD to segfault because it would
1986 attempt to interpret this section as containing relocation
1987 entries for section "oc". With this hack enabled, ".reloc"
1988 will be treated as a normal data section, which will avoid the
1989 segfault. However, you won't be able to create an ELFNN binary
1990 with a section named "oc" that needs relocations, but that's
1991 the kind of ugly side-effects you get when detecting section
1992 types based on their names... In practice, this limitation is
1993 unlikely to bite. */
1994 if (strcmp (name, ".reloc") == 0)
1995 hdr->sh_type = SHT_PROGBITS;
1996
0a1b45a2 1997 return true;
38701953
AM
1998}
1999
13ae64f3
JJ
2000/* Return the relocation value for @tpoff relocation
2001 if STT_TLS virtual address is ADDRESS. */
2002
2003static bfd_vma
eb4ff4d6 2004elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
13ae64f3 2005{
e1918d23 2006 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
2007 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2008 bfd_vma static_tls_size;
13ae64f3 2009
e1918d23
AM
2010 /* If tls_sec is NULL, we should have signalled an error already. */
2011 if (htab->tls_sec == NULL)
6a30718d 2012 return 0;
7dc98aea
RO
2013
2014 /* Consider special static TLS alignment requirements. */
2015 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2016 return static_tls_size + htab->tls_sec->vma - address;
13ae64f3
JJ
2017}
2018
252b5132
RH
2019/* Relocate an i386 ELF section. */
2020
0f684201 2021static int
55fd94b0
AM
2022elf_i386_relocate_section (bfd *output_bfd,
2023 struct bfd_link_info *info,
2024 bfd *input_bfd,
2025 asection *input_section,
2026 bfd_byte *contents,
2027 Elf_Internal_Rela *relocs,
2028 Elf_Internal_Sym *local_syms,
2029 asection **local_sections)
252b5132 2030{
0afcef53 2031 struct elf_x86_link_hash_table *htab;
252b5132
RH
2032 Elf_Internal_Shdr *symtab_hdr;
2033 struct elf_link_hash_entry **sym_hashes;
2034 bfd_vma *local_got_offsets;
67a4f2b7 2035 bfd_vma *local_tlsdesc_gotents;
252b5132 2036 Elf_Internal_Rela *rel;
60f2e42e 2037 Elf_Internal_Rela *wrel;
252b5132 2038 Elf_Internal_Rela *relend;
0a1b45a2 2039 bool is_vxworks_tls;
1bb82b89 2040 unsigned expected_tls_le;
25e762b9 2041 unsigned plt_entry_size;
252b5132 2042
3747999c 2043 /* Skip if check_relocs or scan_relocs failed. */
c337a162 2044 if (input_section->check_relocs_failed)
0a1b45a2 2045 return false;
c337a162 2046
0afcef53 2047 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6 2048 if (htab == NULL)
0a1b45a2 2049 return false;
fe53b4a4 2050
7af5d5c4
AM
2051 if (!is_x86_elf (input_bfd, htab))
2052 {
2053 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2054 return false;
7af5d5c4 2055 }
fe53b4a4 2056
0ffa91dd 2057 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
2058 sym_hashes = elf_sym_hashes (input_bfd);
2059 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 2060 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3348747a
NS
2061 /* We have to handle relocations in vxworks .tls_vars sections
2062 specially, because the dynamic loader is 'weird'. */
90c14f0c 2063 is_vxworks_tls = (htab->elf.target_os == is_vxworks
07d6d2b8 2064 && bfd_link_pic (info)
3348747a
NS
2065 && !strcmp (input_section->output_section->name,
2066 ".tls_vars"));
252b5132 2067
0afcef53 2068 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 2069
765e526c 2070 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 2071
60f2e42e 2072 rel = wrel = relocs;
252b5132 2073 relend = relocs + input_section->reloc_count;
60f2e42e 2074 for (; rel < relend; wrel++, rel++)
252b5132 2075 {
419414ea 2076 unsigned int r_type, r_type_tls;
252b5132
RH
2077 reloc_howto_type *howto;
2078 unsigned long r_symndx;
2079 struct elf_link_hash_entry *h;
0afcef53 2080 struct elf_x86_link_hash_entry *eh;
252b5132
RH
2081 Elf_Internal_Sym *sym;
2082 asection *sec;
dd7e64d4 2083 bfd_vma off, offplt, plt_offset;
252b5132 2084 bfd_vma relocation;
0a1b45a2 2085 bool unresolved_reloc;
252b5132 2086 bfd_reloc_status_type r;
1b452ec6 2087 unsigned int indx;
13ae64f3 2088 int tls_type;
1788fc08 2089 bfd_vma st_size;
dd7e64d4 2090 asection *resolved_plt;
0a1b45a2
AM
2091 bool resolved_to_zero;
2092 bool relative_reloc;
252b5132
RH
2093
2094 r_type = ELF32_R_TYPE (rel->r_info);
55fd94b0
AM
2095 if (r_type == R_386_GNU_VTINHERIT
2096 || r_type == R_386_GNU_VTENTRY)
18954b29
L
2097 {
2098 if (wrel != rel)
2099 *wrel = *rel;
2100 continue;
2101 }
dc47f327 2102
1273b2f8 2103 howto = elf_i386_rtype_to_howto (r_type);
76268e02 2104 if (howto == NULL)
47aeb64c
NC
2105 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2106
252b5132 2107 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
2108 h = NULL;
2109 sym = NULL;
2110 sec = NULL;
0a1b45a2 2111 unresolved_reloc = false;
252b5132
RH
2112 if (r_symndx < symtab_hdr->sh_info)
2113 {
2114 sym = local_syms + r_symndx;
2115 sec = local_sections[r_symndx];
2116 relocation = (sec->output_section->vma
2117 + sec->output_offset
2118 + sym->st_value);
1788fc08 2119 st_size = sym->st_size;
ab96bf03
AM
2120
2121 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2122 && ((sec->flags & SEC_MERGE) != 0
0e1862bb 2123 || (bfd_link_relocatable (info)
ab96bf03 2124 && sec->output_offset != 0)))
f8df10f4 2125 {
f8df10f4 2126 bfd_vma addend;
4a335f3d 2127 bfd_byte *where = contents + rel->r_offset;
f8df10f4 2128
57698478 2129 switch (bfd_get_reloc_size (howto))
f8df10f4 2130 {
57698478 2131 case 1:
4a335f3d
AM
2132 addend = bfd_get_8 (input_bfd, where);
2133 if (howto->pc_relative)
2134 {
2135 addend = (addend ^ 0x80) - 0x80;
2136 addend += 1;
2137 }
2138 break;
57698478 2139 case 2:
4a335f3d
AM
2140 addend = bfd_get_16 (input_bfd, where);
2141 if (howto->pc_relative)
2142 {
2143 addend = (addend ^ 0x8000) - 0x8000;
2144 addend += 2;
2145 }
2146 break;
57698478 2147 case 4:
4a335f3d
AM
2148 addend = bfd_get_32 (input_bfd, where);
2149 if (howto->pc_relative)
2150 {
2151 addend = (addend ^ 0x80000000) - 0x80000000;
2152 addend += 4;
2153 }
2154 break;
2155 default:
2156 abort ();
f8df10f4
JJ
2157 }
2158
0e1862bb 2159 if (bfd_link_relocatable (info))
ab96bf03
AM
2160 addend += sec->output_offset;
2161 else
2162 {
2163 asection *msec = sec;
2164 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2165 addend);
2166 addend -= relocation;
2167 addend += msec->output_section->vma + msec->output_offset;
2168 }
4a335f3d 2169
57698478 2170 switch (bfd_get_reloc_size (howto))
4a335f3d 2171 {
57698478 2172 case 1:
16a10388 2173 /* FIXME: overflow checks. */
4a335f3d
AM
2174 if (howto->pc_relative)
2175 addend -= 1;
2176 bfd_put_8 (input_bfd, addend, where);
4a335f3d 2177 break;
57698478 2178 case 2:
4a335f3d
AM
2179 if (howto->pc_relative)
2180 addend -= 2;
2181 bfd_put_16 (input_bfd, addend, where);
4a335f3d 2182 break;
57698478 2183 case 4:
4a335f3d
AM
2184 if (howto->pc_relative)
2185 addend -= 4;
2186 bfd_put_32 (input_bfd, addend, where);
2187 break;
2188 }
f8df10f4 2189 }
0e1862bb 2190 else if (!bfd_link_relocatable (info)
1f85278f 2191 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc
L
2192 {
2193 /* Relocate against local STT_GNU_IFUNC symbol. */
0afcef53 2194 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
0a1b45a2 2195 false);
c25bc9fc
L
2196 if (h == NULL)
2197 abort ();
2198
23209a78 2199 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
2200 h->root.u.def.value = sym->st_value;
2201 h->root.u.def.section = sec;
2202 }
252b5132
RH
2203 }
2204 else
2205 {
0a1b45a2
AM
2206 bool warned ATTRIBUTE_UNUSED;
2207 bool ignored ATTRIBUTE_UNUSED;
ffb2e45b 2208
b2a8e766
AM
2209 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2210 r_symndx, symtab_hdr, sym_hashes,
2211 h, sec, relocation,
62d887d4 2212 unresolved_reloc, warned, ignored);
1788fc08 2213 st_size = h->size;
252b5132
RH
2214 }
2215
dbaa2011 2216 if (sec != NULL && discarded_section (sec))
60f2e42e
L
2217 {
2218 _bfd_clear_contents (howto, input_bfd, input_section,
0930cb30 2219 contents, rel->r_offset);
60f2e42e
L
2220 wrel->r_offset = rel->r_offset;
2221 wrel->r_info = 0;
2222 wrel->r_addend = 0;
2223
2224 /* For ld -r, remove relocations in debug sections against
2225 sections defined in discarded sections. Not done for
2226 eh_frame editing code expects to be present. */
2227 if (bfd_link_relocatable (info)
2228 && (input_section->flags & SEC_DEBUGGING))
2229 wrel--;
2230
2231 continue;
2232 }
9635fe29 2233
0e1862bb 2234 if (bfd_link_relocatable (info))
2d5da473
L
2235 {
2236 if (wrel != rel)
2237 *wrel = *rel;
2238 continue;
2239 }
ab96bf03 2240
0afcef53 2241 eh = (struct elf_x86_link_hash_entry *) h;
ee2fdd6f 2242
cbe950e9
L
2243 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2244 it here if it is defined in a non-shared object. */
2245 if (h != NULL
2246 && h->type == STT_GNU_IFUNC
2247 && h->def_regular)
2248 {
ee2fdd6f 2249 asection *gotplt, *base_got;
cbe950e9 2250 bfd_vma plt_index;
4c544807 2251 const char *name;
cbe950e9 2252
97dc35c8
L
2253 if ((input_section->flags & SEC_ALLOC) == 0)
2254 {
347a8774
L
2255 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2256 STT_GNU_IFUNC symbol as STT_FUNC. */
2257 if (elf_section_type (input_section) == SHT_NOTE)
2258 goto skip_ifunc;
97dc35c8
L
2259 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2260 sections because such sections are not SEC_ALLOC and
2261 thus ld.so will not process them. */
2262 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 2263 continue;
97dc35c8
L
2264 abort ();
2265 }
cbe950e9
L
2266
2267 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 2268 if (htab->elf.splt != NULL)
cbe950e9 2269 {
ee2fdd6f
L
2270 if (htab->plt_second != NULL)
2271 {
2272 resolved_plt = htab->plt_second;
2273 plt_offset = eh->plt_second.offset;
2274 }
2275 else
2276 {
2277 resolved_plt = htab->elf.splt;
2278 plt_offset = h->plt.offset;
2279 }
6de2ae4a 2280 gotplt = htab->elf.sgotplt;
cbe950e9
L
2281 }
2282 else
2283 {
ee2fdd6f
L
2284 resolved_plt = htab->elf.iplt;
2285 plt_offset = h->plt.offset;
6de2ae4a 2286 gotplt = htab->elf.igotplt;
cbe950e9
L
2287 }
2288
cbe950e9
L
2289 switch (r_type)
2290 {
2291 default:
233cc9c1 2292 break;
cbe950e9
L
2293
2294 case R_386_GOT32:
02a86693 2295 case R_386_GOT32X:
6de2ae4a 2296 base_got = htab->elf.sgot;
cbe950e9
L
2297 off = h->got.offset;
2298
7afd84dc 2299 if (base_got == NULL)
cbe950e9
L
2300 abort ();
2301
7afd84dc 2302 if (off == (bfd_vma) -1)
cbe950e9 2303 {
7afd84dc
L
2304 /* We can't use h->got.offset here to save state, or
2305 even just remember the offset, as finish_dynamic_symbol
2306 would use that as offset into .got. */
6bbec505 2307
233cc9c1
L
2308 if (h->plt.offset == (bfd_vma) -1)
2309 abort ();
2310
6de2ae4a 2311 if (htab->elf.splt != NULL)
7afd84dc 2312 {
f604c2a2 2313 plt_index = (h->plt.offset / plt_entry_size
765e526c 2314 - htab->plt.has_plt0);
7afd84dc 2315 off = (plt_index + 3) * 4;
6de2ae4a 2316 base_got = htab->elf.sgotplt;
7afd84dc 2317 }
cbe950e9
L
2318 else
2319 {
25e762b9 2320 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 2321 off = plt_index * 4;
6de2ae4a 2322 base_got = htab->elf.igotplt;
7afd84dc
L
2323 }
2324
2325 if (h->dynindx == -1
2326 || h->forced_local
2327 || info->symbolic)
2328 {
2329 /* This references the local defitionion. We must
2330 initialize this entry in the global offset table.
cfe29d94 2331 Since the offset must always be a multiple of 4,
7afd84dc
L
2332 we use the least significant bit to record
2333 whether we have initialized it already.
2334
2335 When doing a dynamic link, we create a .rela.got
2336 relocation entry to initialize the value. This
2337 is done in the finish_dynamic_symbol routine. */
2338 if ((off & 1) != 0)
2339 off &= ~1;
2340 else
2341 {
2342 bfd_put_32 (output_bfd, relocation,
2343 base_got->contents + off);
2344 h->got.offset |= 1;
2345 }
cbe950e9 2346 }
cbe950e9 2347
7afd84dc 2348 relocation = off;
7afd84dc
L
2349 }
2350 else
ca8c86ef
L
2351 relocation = (base_got->output_section->vma
2352 + base_got->output_offset + off
2353 - gotplt->output_section->vma
2354 - gotplt->output_offset);
2355
2a568401
L
2356 if (rel->r_offset > 1
2357 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2358 && *(contents + rel->r_offset - 2) != 0x8d)
712ec279
L
2359 {
2360 if (bfd_link_pic (info))
2361 goto disallow_got32;
2362
2363 /* Add the GOT base if there is no base register. */
2364 relocation += (gotplt->output_section->vma
2365 + gotplt->output_offset);
2366 }
2367 else if (htab->elf.splt == NULL)
2368 {
2369 /* Adjust for static executables. */
2370 relocation += gotplt->output_offset;
2371 }
0018b0a3 2372
cbe950e9 2373 goto do_relocation;
233cc9c1
L
2374 }
2375
2376 if (h->plt.offset == (bfd_vma) -1)
2377 {
2378 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2379 if (r_type == R_386_32
2380 && (input_section->flags & SEC_CODE) == 0)
2381 goto do_ifunc_pointer;
2382 goto bad_ifunc_reloc;
2383 }
2384
ee2fdd6f
L
2385 relocation = (resolved_plt->output_section->vma
2386 + resolved_plt->output_offset + plt_offset);
233cc9c1
L
2387
2388 switch (r_type)
2389 {
2390 default:
dc1e8a47 2391 bad_ifunc_reloc:
233cc9c1
L
2392 if (h->root.root.string)
2393 name = h->root.root.string;
2394 else
2395 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2396 NULL);
4eca0228 2397 _bfd_error_handler
695344c0 2398 /* xgettext:c-format */
871b3ab2 2399 (_("%pB: relocation %s against STT_GNU_IFUNC "
233cc9c1
L
2400 "symbol `%s' isn't supported"), input_bfd,
2401 howto->name, name);
2402 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2403 return false;
233cc9c1
L
2404
2405 case R_386_32:
2406 /* Generate dynamic relcoation only when there is a
2407 non-GOT reference in a shared object. */
2408 if ((bfd_link_pic (info) && h->non_got_ref)
2409 || h->plt.offset == (bfd_vma) -1)
2410 {
2411 Elf_Internal_Rela outrel;
2412 asection *sreloc;
2413 bfd_vma offset;
2414
dc1e8a47 2415 do_ifunc_pointer:
233cc9c1
L
2416 /* Need a dynamic relocation to get the real function
2417 adddress. */
2418 offset = _bfd_elf_section_offset (output_bfd,
2419 info,
2420 input_section,
2421 rel->r_offset);
2422 if (offset == (bfd_vma) -1
2423 || offset == (bfd_vma) -2)
2424 abort ();
2425
2426 outrel.r_offset = (input_section->output_section->vma
2427 + input_section->output_offset
2428 + offset);
2429
cf1070f1 2430 if (POINTER_LOCAL_IFUNC_P (info, h))
233cc9c1 2431 {
871b3ab2 2432 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
2433 h->root.root.string,
2434 h->root.u.def.section->owner);
2435
233cc9c1
L
2436 /* This symbol is resolved locally. */
2437 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
68b00778
L
2438
2439 if (htab->params->report_relative_reloc)
2440 _bfd_x86_elf_link_report_relative_reloc
2441 (info, input_section, h, sym,
2442 "R_386_IRELATIVE", &outrel);
2443
233cc9c1
L
2444 bfd_put_32 (output_bfd,
2445 (h->root.u.def.value
2446 + h->root.u.def.section->output_section->vma
2447 + h->root.u.def.section->output_offset),
2448 contents + offset);
2449 }
2450 else
2451 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2452
2453 /* Dynamic relocations are stored in
2454 1. .rel.ifunc section in PIC object.
2455 2. .rel.got section in dynamic executable.
2456 3. .rel.iplt section in static executable. */
2457 if (bfd_link_pic (info))
2458 sreloc = htab->elf.irelifunc;
2459 else if (htab->elf.splt != NULL)
2460 sreloc = htab->elf.srelgot;
2461 else
2462 sreloc = htab->elf.irelplt;
2463 elf_append_rel (output_bfd, sreloc, &outrel);
2464
2465 /* If this reloc is against an external symbol, we
2466 do not want to fiddle with the addend. Otherwise,
2467 we need to include the symbol value so that it
2468 becomes an addend for the dynamic reloc. For an
2469 internal symbol, we have updated addend. */
2470 continue;
2471 }
2472 /* FALLTHROUGH */
2473 case R_386_PC32:
2474 case R_386_PLT32:
2475 goto do_relocation;
cbe950e9
L
2476
2477 case R_386_GOTOFF:
8f29211c
L
2478 /* NB: We can't use the PLT entry as the function address
2479 for PIC since the PIC register may not be set up
2480 properly for indirect call. */
2481 if (bfd_link_pic (info))
2482 goto bad_ifunc_reloc;
cbe950e9
L
2483 relocation -= (gotplt->output_section->vma
2484 + gotplt->output_offset);
2485 goto do_relocation;
2486 }
2487 }
2488
dc1e8a47 2489 skip_ifunc:
aec6b87e 2490 resolved_to_zero = (eh != NULL
c5bce5c6 2491 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
aec6b87e 2492
252b5132
RH
2493 switch (r_type)
2494 {
02a86693 2495 case R_386_GOT32X:
252b5132
RH
2496 case R_386_GOT32:
2497 /* Relocation is to the entry for this symbol in the global
2498 offset table. */
6de2ae4a 2499 if (htab->elf.sgot == NULL)
ffb2e45b 2500 abort ();
252b5132 2501
0a1b45a2 2502 relative_reloc = false;
252b5132
RH
2503 if (h != NULL)
2504 {
252b5132 2505 off = h->got.offset;
2eba97c2 2506 if (RESOLVED_LOCALLY_P (info, h, htab))
252b5132 2507 {
2eba97c2
L
2508 /* We must initialize this entry in the global offset
2509 table. Since the offset must always be a multiple
2510 of 4, we use the least significant bit to record
2511 whether we have initialized it already.
252b5132
RH
2512
2513 When doing a dynamic link, we create a .rel.got
2514 relocation entry to initialize the value. This
2515 is done in the finish_dynamic_symbol routine. */
2516 if ((off & 1) != 0)
2517 off &= ~1;
2518 else
2519 {
2520 bfd_put_32 (output_bfd, relocation,
6de2ae4a 2521 htab->elf.sgot->contents + off);
252b5132 2522 h->got.offset |= 1;
5af6f000
L
2523 /* NB: Don't generate relative relocation here if
2524 it has been generated by DT_RELR. */
2525 if (!info->enable_dt_relr
2526 && GENERATE_RELATIVE_RELOC_P (info, h))
e133d005
L
2527 {
2528 /* PR ld/21402: If this symbol isn't dynamic
0dc9a308 2529 in PIC, generate R_386_RELATIVE here. */
e133d005 2530 eh->no_finish_dynamic_symbol = 1;
0a1b45a2 2531 relative_reloc = true;
e133d005 2532 }
252b5132
RH
2533 }
2534 }
8c694914 2535 else
0a1b45a2 2536 unresolved_reloc = false;
252b5132
RH
2537 }
2538 else
2539 {
ffb2e45b
AM
2540 if (local_got_offsets == NULL)
2541 abort ();
252b5132
RH
2542
2543 off = local_got_offsets[r_symndx];
2544
2545 /* The offset must always be a multiple of 4. We use
83be169b
AM
2546 the least significant bit to record whether we have
2547 already generated the necessary reloc. */
252b5132
RH
2548 if ((off & 1) != 0)
2549 off &= ~1;
2550 else
2551 {
6725bdbf 2552 bfd_put_32 (output_bfd, relocation,
6de2ae4a 2553 htab->elf.sgot->contents + off);
e133d005 2554 local_got_offsets[r_symndx] |= 1;
252b5132 2555
5af6f000
L
2556 /* NB: Don't generate relative relocation here if it
2557 has been generated by DT_RELR. */
2558 if (!info->enable_dt_relr && bfd_link_pic (info))
0a1b45a2 2559 relative_reloc = true;
252b5132 2560 }
252b5132
RH
2561 }
2562
e133d005
L
2563 if (relative_reloc)
2564 {
2565 asection *s;
2566 Elf_Internal_Rela outrel;
2567
2568 s = htab->elf.srelgot;
2569 if (s == NULL)
2570 abort ();
2571
2572 outrel.r_offset = (htab->elf.sgot->output_section->vma
2573 + htab->elf.sgot->output_offset
2574 + off);
2575 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
68b00778
L
2576
2577 if (htab->params->report_relative_reloc)
2578 _bfd_x86_elf_link_report_relative_reloc
2579 (info, input_section, h, sym, "R_386_RELATIVE",
2580 &outrel);
2581
e133d005
L
2582 elf_append_rel (output_bfd, s, &outrel);
2583 }
2584
ffb2e45b
AM
2585 if (off >= (bfd_vma) -2)
2586 abort ();
2587
74d7f0aa
L
2588 relocation = (htab->elf.sgot->output_section->vma
2589 + htab->elf.sgot->output_offset + off);
2a568401
L
2590 if (rel->r_offset > 1
2591 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2592 && *(contents + rel->r_offset - 2) != 0x8d)
74d7f0aa
L
2593 {
2594 if (bfd_link_pic (info))
2595 {
2596 /* For PIC, disallow R_386_GOT32 without a base
2a568401
L
2597 register, except for "lea foo@GOT, %reg", since
2598 we don't know what the GOT base is. */
74d7f0aa
L
2599 const char *name;
2600
dc1e8a47 2601 disallow_got32:
a5def14f 2602 if (h == NULL || h->root.root.string == NULL)
74d7f0aa
L
2603 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2604 NULL);
2605 else
2606 name = h->root.root.string;
2607
4eca0228 2608 _bfd_error_handler
695344c0 2609 /* xgettext:c-format */
871b3ab2 2610 (_("%pB: direct GOT relocation %s against `%s'"
63a5468a
AM
2611 " without base register can not be used"
2612 " when making a shared object"),
712ec279 2613 input_bfd, howto->name, name);
74d7f0aa 2614 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2615 return false;
74d7f0aa
L
2616 }
2617 }
2618 else
2619 {
2620 /* Subtract the .got.plt section address only with a base
2621 register. */
2622 relocation -= (htab->elf.sgotplt->output_section->vma
2623 + htab->elf.sgotplt->output_offset);
2624 }
2625
252b5132
RH
2626 break;
2627
2628 case R_386_GOTOFF:
2629 /* Relocation is relative to the start of the global offset
2630 table. */
2631
3d949995
L
2632 /* Check to make sure it isn't a protected function or data
2633 symbol for shared library since it may not be local when
2634 used as function address or with copy relocation. We also
e3c0e327 2635 need to make sure that a symbol is referenced locally. */
0e1862bb 2636 if (!bfd_link_executable (info) && h)
90f487df 2637 {
41bed6dd
L
2638 if (!h->def_regular)
2639 {
2640 const char *v;
2641
2642 switch (ELF_ST_VISIBILITY (h->other))
2643 {
2644 case STV_HIDDEN:
2645 v = _("hidden symbol");
2646 break;
2647 case STV_INTERNAL:
2648 v = _("internal symbol");
2649 break;
2650 case STV_PROTECTED:
2651 v = _("protected symbol");
2652 break;
2653 default:
2654 v = _("symbol");
2655 break;
2656 }
2657
4eca0228 2658 _bfd_error_handler
695344c0 2659 /* xgettext:c-format */
871b3ab2 2660 (_("%pB: relocation R_386_GOTOFF against undefined %s"
63a5468a 2661 " `%s' can not be used when making a shared object"),
41bed6dd
L
2662 input_bfd, v, h->root.root.string);
2663 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2664 return false;
41bed6dd 2665 }
6999821f 2666 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
3d949995
L
2667 && (h->type == STT_FUNC
2668 || h->type == STT_OBJECT)
41bed6dd
L
2669 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2670 {
4eca0228 2671 _bfd_error_handler
695344c0 2672 /* xgettext:c-format */
871b3ab2 2673 (_("%pB: relocation R_386_GOTOFF against protected %s"
63a5468a 2674 " `%s' can not be used when making a shared object"),
3d949995
L
2675 input_bfd,
2676 h->type == STT_FUNC ? "function" : "data",
2677 h->root.root.string);
41bed6dd 2678 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2679 return false;
41bed6dd 2680 }
90f487df
L
2681 }
2682
8c37241b
JJ
2683 /* Note that sgot is not involved in this
2684 calculation. We always want the start of .got.plt. If we
2685 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
252b5132
RH
2686 permitted by the ABI, we might have to change this
2687 calculation. */
6de2ae4a
L
2688 relocation -= htab->elf.sgotplt->output_section->vma
2689 + htab->elf.sgotplt->output_offset;
252b5132
RH
2690 break;
2691
2692 case R_386_GOTPC:
2693 /* Use global offset table as symbol value. */
6de2ae4a
L
2694 relocation = htab->elf.sgotplt->output_section->vma
2695 + htab->elf.sgotplt->output_offset;
0a1b45a2 2696 unresolved_reloc = false;
252b5132
RH
2697 break;
2698
2699 case R_386_PLT32:
2700 /* Relocation is to the entry for this symbol in the
2701 procedure linkage table. */
2702
dd5724d5 2703 /* Resolve a PLT32 reloc against a local symbol directly,
83be169b 2704 without using the procedure linkage table. */
252b5132
RH
2705 if (h == NULL)
2706 break;
2707
dd7e64d4
L
2708 if ((h->plt.offset == (bfd_vma) -1
2709 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 2710 || htab->elf.splt == NULL)
252b5132
RH
2711 {
2712 /* We didn't make a PLT entry for this symbol. This
83be169b
AM
2713 happens when statically linking PIC code, or when
2714 using -Bsymbolic. */
252b5132
RH
2715 break;
2716 }
2717
dd7e64d4
L
2718 if (h->plt.offset != (bfd_vma) -1)
2719 {
ee2fdd6f
L
2720 if (htab->plt_second != NULL)
2721 {
2722 resolved_plt = htab->plt_second;
2723 plt_offset = eh->plt_second.offset;
2724 }
2725 else
2726 {
2727 resolved_plt = htab->elf.splt;
2728 plt_offset = h->plt.offset;
2729 }
dd7e64d4
L
2730 }
2731 else
2732 {
2733 resolved_plt = htab->plt_got;
2734 plt_offset = eh->plt_got.offset;
2735 }
2736
2737 relocation = (resolved_plt->output_section->vma
2738 + resolved_plt->output_offset
2739 + plt_offset);
0a1b45a2 2740 unresolved_reloc = false;
252b5132
RH
2741 break;
2742
1788fc08 2743 case R_386_SIZE32:
1788fc08
L
2744 /* Set to symbol size. */
2745 relocation = st_size;
2746 /* Fall through. */
2747
252b5132
RH
2748 case R_386_32:
2749 case R_386_PC32:
3348747a
NS
2750 if ((input_section->flags & SEC_ALLOC) == 0
2751 || is_vxworks_tls)
ec338859
AM
2752 break;
2753
831083d3
L
2754 if (GENERATE_DYNAMIC_RELOCATION_P (false, info, eh, r_type,
2755 sec, false,
2756 resolved_to_zero,
aebcc8ff 2757 (r_type == R_386_PC32)))
252b5132 2758 {
947216bf 2759 Elf_Internal_Rela outrel;
0a1b45a2 2760 bool skip, relocate;
5af6f000 2761 bool generate_dynamic_reloc = true;
0c715baa 2762 asection *sreloc;
252b5132
RH
2763
2764 /* When generating a shared object, these relocations
2765 are copied into the output file to be resolved at run
2766 time. */
2767
0a1b45a2
AM
2768 skip = false;
2769 relocate = false;
252b5132 2770
c629eae0
JJ
2771 outrel.r_offset =
2772 _bfd_elf_section_offset (output_bfd, info, input_section,
2773 rel->r_offset);
2774 if (outrel.r_offset == (bfd_vma) -1)
0a1b45a2 2775 skip = true;
0bb2d96a 2776 else if (outrel.r_offset == (bfd_vma) -2)
0a1b45a2 2777 skip = true, relocate = true;
252b5132
RH
2778 outrel.r_offset += (input_section->output_section->vma
2779 + input_section->output_offset);
2780
2781 if (skip)
0bb2d96a 2782 memset (&outrel, 0, sizeof outrel);
831083d3 2783 else if (COPY_INPUT_RELOC_P (false, info, h, r_type))
0bb2d96a 2784 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
2785 else
2786 {
5a15f56f 2787 /* This symbol is local, or marked to become local. */
0a1b45a2 2788 relocate = true;
5af6f000
L
2789 /* NB: Don't generate relative relocation here if it
2790 has been generated by DT_RELR. */
2791 if (info->enable_dt_relr)
2792 generate_dynamic_reloc = false;
2793 else
2794 {
2795 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
68b00778 2796
5af6f000
L
2797 if (htab->params->report_relative_reloc)
2798 _bfd_x86_elf_link_report_relative_reloc
2799 (info, input_section, h, sym, "R_386_RELATIVE",
2800 &outrel);
2801 }
252b5132
RH
2802 }
2803
5af6f000 2804 if (generate_dynamic_reloc)
62d78908 2805 {
5af6f000 2806 sreloc = elf_section_data (input_section)->sreloc;
0c715baa 2807
5af6f000
L
2808 if (sreloc == NULL || sreloc->contents == NULL)
2809 {
2810 r = bfd_reloc_notsupported;
2811 goto check_relocation_error;
2812 }
2813
2814 elf_append_rel (output_bfd, sreloc, &outrel);
2815 }
252b5132
RH
2816
2817 /* If this reloc is against an external symbol, we do
2818 not want to fiddle with the addend. Otherwise, we
2819 need to include the symbol value so that it becomes
2820 an addend for the dynamic reloc. */
0f88be7a 2821 if (! relocate)
252b5132
RH
2822 continue;
2823 }
252b5132
RH
2824 break;
2825
37e55690 2826 case R_386_TLS_IE:
0e1862bb 2827 if (!bfd_link_executable (info))
37e55690 2828 {
947216bf 2829 Elf_Internal_Rela outrel;
37e55690 2830 asection *sreloc;
37e55690
JJ
2831
2832 outrel.r_offset = rel->r_offset
2833 + input_section->output_section->vma
2834 + input_section->output_offset;
2835 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
68b00778
L
2836
2837 if (htab->params->report_relative_reloc)
2838 _bfd_x86_elf_link_report_relative_reloc
2839 (info, input_section, h, sym, "R_386_RELATIVE",
2840 &outrel);
2841
37e55690
JJ
2842 sreloc = elf_section_data (input_section)->sreloc;
2843 if (sreloc == NULL)
2844 abort ();
59d6ffb2 2845 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
2846 }
2847 /* Fall through */
2848
13ae64f3 2849 case R_386_TLS_GD:
67a4f2b7
AO
2850 case R_386_TLS_GOTDESC:
2851 case R_386_TLS_DESC_CALL:
13ae64f3 2852 case R_386_TLS_IE_32:
37e55690 2853 case R_386_TLS_GOTIE:
13ae64f3
JJ
2854 tls_type = GOT_UNKNOWN;
2855 if (h == NULL && local_got_offsets)
0afcef53 2856 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
13ae64f3 2857 else if (h != NULL)
0afcef53 2858 tls_type = elf_x86_hash_entry(h)->tls_type;
37e55690
JJ
2859 if (tls_type == GOT_TLS_IE)
2860 tls_type = GOT_TLS_IE_NEG;
142411ca 2861
419414ea 2862 r_type_tls = r_type;
142411ca
L
2863 if (! elf_i386_tls_transition (info, input_bfd,
2864 input_section, contents,
2865 symtab_hdr, sym_hashes,
419414ea 2866 &r_type_tls, tls_type, rel,
0a1b45a2
AM
2867 relend, h, r_symndx, true))
2868 return false;
13ae64f3 2869
1bb82b89
RO
2870 expected_tls_le = htab->elf.target_os == is_solaris
2871 ? R_386_TLS_LE : R_386_TLS_LE_32;
2872 if (r_type_tls == expected_tls_le)
13ae64f3 2873 {
1bb82b89
RO
2874 /* NB: Solaris only supports R_386_TLS_GD->R_386_TLS_LE. */
2875 BFD_ASSERT (! unresolved_reloc
2876 && (htab->elf.target_os != is_solaris
2877 || (htab->elf.target_os == is_solaris
2878 && (r_type == R_386_TLS_GD
2879 || r_type == R_386_TLS_IE
2880 || r_type == R_386_TLS_GOTIE))));
419414ea 2881 if (r_type == R_386_TLS_GD)
13ae64f3 2882 {
142411ca 2883 unsigned int type;
13ae64f3
JJ
2884 bfd_vma roff;
2885
2886 /* GD->LE transition. */
6eaa7fb5 2887 type = *(contents + rel->r_offset - 2);
13ae64f3
JJ
2888 if (type == 0x04)
2889 {
6eaa7fb5
L
2890 /* Change
2891 leal foo@tlsgd(,%ebx,1), %eax
2892 call ___tls_get_addr@PLT
2893 into:
2894 movl %gs:0, %eax
2895 subl $foo@tpoff, %eax
13ae64f3 2896 (6 byte form of subl). */
142411ca 2897 roff = rel->r_offset + 5;
13ae64f3
JJ
2898 }
2899 else
2900 {
6eaa7fb5
L
2901 /* Change
2902 leal foo@tlsgd(%ebx), %eax
2903 call ___tls_get_addr@PLT
2904 nop
2905 or
2906 leal foo@tlsgd(%reg), %eax
2907 call *___tls_get_addr@GOT(%reg)
2908 which may be converted to
2909 addr32 call ___tls_get_addr
2910 into:
2911 movl %gs:0, %eax; subl $foo@tpoff, %eax
142411ca 2912 (6 byte form of subl). */
142411ca 2913 roff = rel->r_offset + 6;
13ae64f3 2914 }
6eaa7fb5
L
2915 memcpy (contents + roff - 8,
2916 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
eb4ff4d6 2917 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
13ae64f3 2918 contents + roff);
6eaa7fb5 2919 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
13ae64f3 2920 rel++;
60f2e42e 2921 wrel++;
13ae64f3
JJ
2922 continue;
2923 }
419414ea 2924 else if (r_type == R_386_TLS_GOTDESC)
67a4f2b7
AO
2925 {
2926 /* GDesc -> LE transition.
2927 It's originally something like:
2928 leal x@tlsdesc(%ebx), %eax
2929
2930 leal x@ntpoff, %eax
2931
2932 Registers other than %eax may be set up here. */
2933
142411ca 2934 unsigned int val;
67a4f2b7
AO
2935 bfd_vma roff;
2936
67a4f2b7 2937 roff = rel->r_offset;
67a4f2b7 2938 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
2939
2940 /* Now modify the instruction as appropriate. */
2941 /* aoliva FIXME: remove the above and xor the byte
2942 below with 0x86. */
2943 bfd_put_8 (output_bfd, val ^ 0x86,
2944 contents + roff - 1);
eb4ff4d6 2945 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
67a4f2b7
AO
2946 contents + roff);
2947 continue;
2948 }
419414ea 2949 else if (r_type == R_386_TLS_DESC_CALL)
67a4f2b7
AO
2950 {
2951 /* GDesc -> LE transition.
2952 It's originally:
2953 call *(%eax)
2954 Turn it into:
142411ca 2955 xchg %ax,%ax */
67a4f2b7 2956
67a4f2b7 2957 bfd_vma roff;
6bbec505 2958
67a4f2b7 2959 roff = rel->r_offset;
10efb593 2960 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2961 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2962 continue;
2963 }
419414ea 2964 else if (r_type == R_386_TLS_IE)
13ae64f3 2965 {
142411ca 2966 unsigned int val;
13ae64f3
JJ
2967
2968 /* IE->LE transition:
37e55690
JJ
2969 Originally it can be one of:
2970 movl foo, %eax
2971 movl foo, %reg
2972 addl foo, %reg
2973 We change it into:
2974 movl $foo, %eax
2975 movl $foo, %reg
2976 addl $foo, %reg. */
37e55690 2977 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
37e55690
JJ
2978 if (val == 0xa1)
2979 {
2980 /* movl foo, %eax. */
55fd94b0
AM
2981 bfd_put_8 (output_bfd, 0xb8,
2982 contents + rel->r_offset - 1);
37e55690 2983 }
299bf759 2984 else
37e55690 2985 {
142411ca
L
2986 unsigned int type;
2987
55fd94b0
AM
2988 type = bfd_get_8 (input_bfd,
2989 contents + rel->r_offset - 2);
299bf759 2990 switch (type)
26e41594 2991 {
299bf759
L
2992 case 0x8b:
2993 /* movl */
299bf759
L
2994 bfd_put_8 (output_bfd, 0xc7,
2995 contents + rel->r_offset - 2);
2996 bfd_put_8 (output_bfd,
2997 0xc0 | ((val >> 3) & 7),
2998 contents + rel->r_offset - 1);
2999 break;
3000 case 0x03:
3001 /* addl */
299bf759
L
3002 bfd_put_8 (output_bfd, 0x81,
3003 contents + rel->r_offset - 2);
3004 bfd_put_8 (output_bfd,
3005 0xc0 | ((val >> 3) & 7),
3006 contents + rel->r_offset - 1);
3007 break;
3008 default:
3009 BFD_FAIL ();
3010 break;
26e41594 3011 }
37e55690 3012 }
eb4ff4d6 3013 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
3014 contents + rel->r_offset);
3015 continue;
3016 }
3017 else
3018 {
3019 unsigned int val, type;
3020
3021 /* {IE_32,GOTIE}->LE transition:
3022 Originally it can be one of:
13ae64f3 3023 subl foo(%reg1), %reg2
13ae64f3 3024 movl foo(%reg1), %reg2
37e55690 3025 addl foo(%reg1), %reg2
13ae64f3
JJ
3026 We change it into:
3027 subl $foo, %reg2
37e55690
JJ
3028 movl $foo, %reg2 (6 byte form)
3029 addl $foo, %reg2. */
13ae64f3
JJ
3030 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3031 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
13ae64f3
JJ
3032 if (type == 0x8b)
3033 {
3034 /* movl */
13ae64f3
JJ
3035 bfd_put_8 (output_bfd, 0xc7,
3036 contents + rel->r_offset - 2);
3037 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3038 contents + rel->r_offset - 1);
3039 }
3040 else if (type == 0x2b)
3041 {
3042 /* subl */
13ae64f3
JJ
3043 bfd_put_8 (output_bfd, 0x81,
3044 contents + rel->r_offset - 2);
3045 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3046 contents + rel->r_offset - 1);
3047 }
37e55690
JJ
3048 else if (type == 0x03)
3049 {
3050 /* addl */
3051 bfd_put_8 (output_bfd, 0x81,
3052 contents + rel->r_offset - 2);
3053 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3054 contents + rel->r_offset - 1);
3055 }
13ae64f3
JJ
3056 else
3057 BFD_FAIL ();
419414ea 3058 if (r_type == R_386_TLS_GOTIE)
eb4ff4d6 3059 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
37e55690
JJ
3060 contents + rel->r_offset);
3061 else
eb4ff4d6 3062 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
37e55690 3063 contents + rel->r_offset);
13ae64f3
JJ
3064 continue;
3065 }
3066 }
3067
6de2ae4a 3068 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3069 abort ();
3070
3071 if (h != NULL)
67a4f2b7
AO
3072 {
3073 off = h->got.offset;
0afcef53 3074 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 3075 }
13ae64f3
JJ
3076 else
3077 {
3078 if (local_got_offsets == NULL)
3079 abort ();
3080
3081 off = local_got_offsets[r_symndx];
67a4f2b7 3082 offplt = local_tlsdesc_gotents[r_symndx];
13ae64f3
JJ
3083 }
3084
3085 if ((off & 1) != 0)
3086 off &= ~1;
26e41594 3087 else
13ae64f3 3088 {
947216bf 3089 Elf_Internal_Rela outrel;
91d6fa6a 3090 int dr_type;
67a4f2b7 3091 asection *sreloc;
13ae64f3 3092
6de2ae4a 3093 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3094 abort ();
3095
67a4f2b7
AO
3096 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3097
3098 if (GOT_TLS_GDESC_P (tls_type))
3099 {
59d6ffb2 3100 bfd_byte *loc;
67a4f2b7
AO
3101 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3102 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
6de2ae4a
L
3103 <= htab->elf.sgotplt->size);
3104 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3105 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3106 + offplt
3107 + htab->sgotplt_jump_table_size);
6de2ae4a 3108 sreloc = htab->elf.srelplt;
67a4f2b7 3109 loc = sreloc->contents;
765e526c 3110 loc += (htab->next_tls_desc_index++
5ae0bfb6 3111 * sizeof (Elf32_External_Rel));
67a4f2b7 3112 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
c3b02097 3113 <= sreloc->contents + sreloc->size);
67a4f2b7
AO
3114 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3115 if (indx == 0)
3116 {
3117 BFD_ASSERT (! unresolved_reloc);
3118 bfd_put_32 (output_bfd,
0afcef53 3119 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3120 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3121 + htab->sgotplt_jump_table_size + 4);
3122 }
3123 else
3124 {
3125 bfd_put_32 (output_bfd, 0,
6de2ae4a 3126 htab->elf.sgotplt->contents + offplt
67a4f2b7
AO
3127 + htab->sgotplt_jump_table_size + 4);
3128 }
3129 }
3130
6de2ae4a 3131 sreloc = htab->elf.srelgot;
67a4f2b7 3132
6de2ae4a
L
3133 outrel.r_offset = (htab->elf.sgot->output_section->vma
3134 + htab->elf.sgot->output_offset + off);
13ae64f3 3135
67a4f2b7 3136 if (GOT_TLS_GD_P (tls_type))
13ae64f3 3137 dr_type = R_386_TLS_DTPMOD32;
67a4f2b7
AO
3138 else if (GOT_TLS_GDESC_P (tls_type))
3139 goto dr_done;
37e55690
JJ
3140 else if (tls_type == GOT_TLS_IE_POS)
3141 dr_type = R_386_TLS_TPOFF;
13ae64f3
JJ
3142 else
3143 dr_type = R_386_TLS_TPOFF32;
67a4f2b7 3144
37e55690 3145 if (dr_type == R_386_TLS_TPOFF && indx == 0)
eb4ff4d6 3146 bfd_put_32 (output_bfd,
0afcef53 3147 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3148 htab->elf.sgot->contents + off);
37e55690 3149 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
23209a78 3150 bfd_put_32 (output_bfd,
0afcef53 3151 _bfd_x86_elf_dtpoff_base (info) - relocation,
6de2ae4a 3152 htab->elf.sgot->contents + off);
67a4f2b7 3153 else if (dr_type != R_386_TLS_DESC)
c366c25e 3154 bfd_put_32 (output_bfd, 0,
6de2ae4a 3155 htab->elf.sgot->contents + off);
13ae64f3 3156 outrel.r_info = ELF32_R_INFO (indx, dr_type);
67a4f2b7 3157
59d6ffb2 3158 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3 3159
67a4f2b7 3160 if (GOT_TLS_GD_P (tls_type))
13ae64f3
JJ
3161 {
3162 if (indx == 0)
3163 {
07d6d2b8 3164 BFD_ASSERT (! unresolved_reloc);
13ae64f3 3165 bfd_put_32 (output_bfd,
0afcef53 3166 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3167 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3168 }
3169 else
3170 {
3171 bfd_put_32 (output_bfd, 0,
6de2ae4a 3172 htab->elf.sgot->contents + off + 4);
13ae64f3
JJ
3173 outrel.r_info = ELF32_R_INFO (indx,
3174 R_386_TLS_DTPOFF32);
3175 outrel.r_offset += 4;
59d6ffb2 3176 elf_append_rel (output_bfd, sreloc, &outrel);
13ae64f3
JJ
3177 }
3178 }
37e55690
JJ
3179 else if (tls_type == GOT_TLS_IE_BOTH)
3180 {
3181 bfd_put_32 (output_bfd,
eb4ff4d6 3182 (indx == 0
0afcef53 3183 ? relocation - _bfd_x86_elf_dtpoff_base (info)
eb4ff4d6 3184 : 0),
6de2ae4a 3185 htab->elf.sgot->contents + off + 4);
37e55690
JJ
3186 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3187 outrel.r_offset += 4;
59d6ffb2 3188 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690 3189 }
13ae64f3 3190
67a4f2b7 3191 dr_done:
13ae64f3
JJ
3192 if (h != NULL)
3193 h->got.offset |= 1;
3194 else
3195 local_got_offsets[r_symndx] |= 1;
3196 }
3197
67a4f2b7
AO
3198 if (off >= (bfd_vma) -2
3199 && ! GOT_TLS_GDESC_P (tls_type))
13ae64f3 3200 abort ();
419414ea
L
3201 if (r_type_tls == R_386_TLS_GOTDESC
3202 || r_type_tls == R_386_TLS_DESC_CALL)
67a4f2b7
AO
3203 {
3204 relocation = htab->sgotplt_jump_table_size + offplt;
0a1b45a2 3205 unresolved_reloc = false;
67a4f2b7 3206 }
419414ea 3207 else if (r_type_tls == r_type)
13ae64f3 3208 {
6de2ae4a
L
3209 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3210 + htab->elf.sgotplt->output_offset;
3211 relocation = htab->elf.sgot->output_section->vma
3212 + htab->elf.sgot->output_offset + off - g_o_t;
37e55690
JJ
3213 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3214 && tls_type == GOT_TLS_IE_BOTH)
3215 relocation += 4;
3216 if (r_type == R_386_TLS_IE)
8c37241b 3217 relocation += g_o_t;
0a1b45a2 3218 unresolved_reloc = false;
13ae64f3 3219 }
419414ea 3220 else if (r_type == R_386_TLS_GD)
13ae64f3
JJ
3221 {
3222 unsigned int val, type;
3223 bfd_vma roff;
3224
3225 /* GD->IE transition. */
6eaa7fb5
L
3226 type = *(contents + rel->r_offset - 2);
3227 val = *(contents + rel->r_offset - 1);
13ae64f3
JJ
3228 if (type == 0x04)
3229 {
6eaa7fb5
L
3230 /* Change
3231 leal foo@tlsgd(,%ebx,1), %eax
3232 call ___tls_get_addr@PLT
3233 into:
3234 movl %gs:0, %eax
3235 subl $foo@gottpoff(%ebx), %eax. */
13ae64f3 3236 val >>= 3;
142411ca 3237 roff = rel->r_offset - 3;
13ae64f3
JJ
3238 }
3239 else
3240 {
6eaa7fb5
L
3241 /* Change
3242 leal foo@tlsgd(%ebx), %eax
3243 call ___tls_get_addr@PLT
3244 nop
3245 or
3246 leal foo@tlsgd(%reg), %eax
3247 call *___tls_get_addr@GOT(%reg)
3248 which may be converted to
3249 addr32 call ___tls_get_addr
3250 into:
3251 movl %gs:0, %eax;
3252 subl $foo@gottpoff(%reg), %eax. */
13ae64f3
JJ
3253 roff = rel->r_offset - 2;
3254 }
3255 memcpy (contents + roff,
3256 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3257 contents[roff + 7] = 0x80 | (val & 7);
37e55690
JJ
3258 /* If foo is used only with foo@gotntpoff(%reg) and
3259 foo@indntpoff, but not with foo@gottpoff(%reg), change
3260 subl $foo@gottpoff(%reg), %eax
3261 into:
3262 addl $foo@gotntpoff(%reg), %eax. */
ebcfb3c0
JJ
3263 if (tls_type == GOT_TLS_IE_POS)
3264 contents[roff + 6] = 0x03;
8c37241b 3265 bfd_put_32 (output_bfd,
6de2ae4a
L
3266 htab->elf.sgot->output_section->vma
3267 + htab->elf.sgot->output_offset + off
3268 - htab->elf.sgotplt->output_section->vma
3269 - htab->elf.sgotplt->output_offset,
13ae64f3 3270 contents + roff + 8);
6eaa7fb5 3271 /* Skip R_386_PLT32 and R_386_GOT32X. */
13ae64f3 3272 rel++;
60f2e42e 3273 wrel++;
13ae64f3
JJ
3274 continue;
3275 }
419414ea 3276 else if (r_type == R_386_TLS_GOTDESC)
67a4f2b7
AO
3277 {
3278 /* GDesc -> IE transition.
3279 It's originally something like:
3280 leal x@tlsdesc(%ebx), %eax
3281
3282 Change it to:
142411ca 3283 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
67a4f2b7
AO
3284 or:
3285 movl x@gottpoff(%ebx), %eax # before negl %eax
3286
3287 Registers other than %eax may be set up here. */
3288
67a4f2b7
AO
3289 bfd_vma roff;
3290
3291 /* First, make sure it's a leal adding ebx to a 32-bit
3292 offset into any register, although it's probably
3293 almost always going to be eax. */
3294 roff = rel->r_offset;
67a4f2b7
AO
3295
3296 /* Now modify the instruction as appropriate. */
3297 /* To turn a leal into a movl in the form we use it, it
3298 suffices to change the first byte from 0x8d to 0x8b.
3299 aoliva FIXME: should we decide to keep the leal, all
3300 we have to do is remove the statement below, and
3301 adjust the relaxation of R_386_TLS_DESC_CALL. */
3302 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3303
3304 if (tls_type == GOT_TLS_IE_BOTH)
3305 off += 4;
3306
3307 bfd_put_32 (output_bfd,
6de2ae4a
L
3308 htab->elf.sgot->output_section->vma
3309 + htab->elf.sgot->output_offset + off
3310 - htab->elf.sgotplt->output_section->vma
3311 - htab->elf.sgotplt->output_offset,
67a4f2b7
AO
3312 contents + roff);
3313 continue;
3314 }
419414ea 3315 else if (r_type == R_386_TLS_DESC_CALL)
67a4f2b7
AO
3316 {
3317 /* GDesc -> IE transition.
3318 It's originally:
3319 call *(%eax)
3320
3321 Change it to:
142411ca 3322 xchg %ax,%ax
67a4f2b7
AO
3323 or
3324 negl %eax
3325 depending on how we transformed the TLS_GOTDESC above.
3326 */
3327
67a4f2b7
AO
3328 bfd_vma roff;
3329
67a4f2b7 3330 roff = rel->r_offset;
67a4f2b7
AO
3331
3332 /* Now modify the instruction as appropriate. */
3333 if (tls_type != GOT_TLS_IE_NEG)
3334 {
10efb593
L
3335 /* xchg %ax,%ax */
3336 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3337 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3338 }
3339 else
3340 {
3341 /* negl %eax */
3342 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3343 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3344 }
3345
3346 continue;
3347 }
3348 else
0a1b45a2 3349 BFD_ASSERT (false);
13ae64f3
JJ
3350 break;
3351
3352 case R_386_TLS_LDM:
142411ca
L
3353 if (! elf_i386_tls_transition (info, input_bfd,
3354 input_section, contents,
3355 symtab_hdr, sym_hashes,
3356 &r_type, GOT_UNKNOWN, rel,
0a1b45a2
AM
3357 relend, h, r_symndx, true))
3358 return false;
13ae64f3 3359
142411ca
L
3360 if (r_type != R_386_TLS_LDM)
3361 {
6eaa7fb5
L
3362 /* LD->LE transition. Change
3363 leal foo@tlsldm(%ebx) %eax
3364 call ___tls_get_addr@PLT
3365 into:
3366 movl %gs:0, %eax
3367 nop
3368 leal 0(%esi,1), %esi
3369 or change
3370 leal foo@tlsldm(%reg) %eax
3371 call *___tls_get_addr@GOT(%reg)
3372 which may be converted to
3373 addr32 call ___tls_get_addr
3374 into:
3375 movl %gs:0, %eax
3376 leal 0(%esi), %esi */
1bb82b89
RO
3377 expected_tls_le = htab->elf.target_os == is_solaris
3378 ? R_386_TLS_LE : R_386_TLS_LE_32;
3379 BFD_ASSERT (r_type == expected_tls_le);
6eaa7fb5
L
3380 if (*(contents + rel->r_offset + 4) == 0xff
3381 || *(contents + rel->r_offset + 4) == 0x67)
3382 memcpy (contents + rel->r_offset - 2,
3383 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3384 else
3385 memcpy (contents + rel->r_offset - 2,
3386 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
a3fadc9a 3387 /* Skip R_386_PC32/R_386_PLT32. */
13ae64f3 3388 rel++;
60f2e42e 3389 wrel++;
13ae64f3
JJ
3390 continue;
3391 }
3392
6de2ae4a 3393 if (htab->elf.sgot == NULL)
13ae64f3
JJ
3394 abort ();
3395
0afcef53 3396 off = htab->tls_ld_or_ldm_got.offset;
13ae64f3
JJ
3397 if (off & 1)
3398 off &= ~1;
3399 else
3400 {
947216bf 3401 Elf_Internal_Rela outrel;
13ae64f3 3402
6de2ae4a 3403 if (htab->elf.srelgot == NULL)
13ae64f3
JJ
3404 abort ();
3405
6de2ae4a
L
3406 outrel.r_offset = (htab->elf.sgot->output_section->vma
3407 + htab->elf.sgot->output_offset + off);
13ae64f3
JJ
3408
3409 bfd_put_32 (output_bfd, 0,
6de2ae4a 3410 htab->elf.sgot->contents + off);
13ae64f3 3411 bfd_put_32 (output_bfd, 0,
6de2ae4a 3412 htab->elf.sgot->contents + off + 4);
13ae64f3 3413 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
59d6ffb2 3414 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
0afcef53 3415 htab->tls_ld_or_ldm_got.offset |= 1;
13ae64f3 3416 }
6de2ae4a
L
3417 relocation = htab->elf.sgot->output_section->vma
3418 + htab->elf.sgot->output_offset + off
3419 - htab->elf.sgotplt->output_section->vma
3420 - htab->elf.sgotplt->output_offset;
0a1b45a2 3421 unresolved_reloc = false;
13ae64f3
JJ
3422 break;
3423
3424 case R_386_TLS_LDO_32:
0e1862bb
L
3425 if (!bfd_link_executable (info)
3426 || (input_section->flags & SEC_CODE) == 0)
0afcef53 3427 relocation -= _bfd_x86_elf_dtpoff_base (info);
13ae64f3
JJ
3428 else
3429 /* When converting LDO to LE, we must negate. */
eb4ff4d6 3430 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
3431 break;
3432
3433 case R_386_TLS_LE_32:
13ae64f3 3434 case R_386_TLS_LE:
0e1862bb 3435 if (!bfd_link_executable (info))
37e55690 3436 {
947216bf 3437 Elf_Internal_Rela outrel;
37e55690 3438 asection *sreloc;
37e55690
JJ
3439
3440 outrel.r_offset = rel->r_offset
3441 + input_section->output_section->vma
3442 + input_section->output_offset;
3443 if (h != NULL && h->dynindx != -1)
3444 indx = h->dynindx;
3445 else
3446 indx = 0;
3447 if (r_type == R_386_TLS_LE_32)
3448 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3449 else
3450 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3451 sreloc = elf_section_data (input_section)->sreloc;
3452 if (sreloc == NULL)
3453 abort ();
59d6ffb2 3454 elf_append_rel (output_bfd, sreloc, &outrel);
37e55690
JJ
3455 if (indx)
3456 continue;
3457 else if (r_type == R_386_TLS_LE_32)
0afcef53 3458 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
37e55690 3459 else
0afcef53 3460 relocation -= _bfd_x86_elf_dtpoff_base (info);
37e55690
JJ
3461 }
3462 else if (r_type == R_386_TLS_LE_32)
eb4ff4d6 3463 relocation = elf_i386_tpoff (info, relocation);
37e55690 3464 else
eb4ff4d6 3465 relocation = -elf_i386_tpoff (info, relocation);
13ae64f3
JJ
3466 break;
3467
252b5132
RH
3468 default:
3469 break;
3470 }
3471
239e1f3a
AM
3472 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3473 because such sections are not SEC_ALLOC and thus ld.so will
3474 not process them. */
8c694914 3475 if (unresolved_reloc
239e1f3a 3476 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
3477 && h->def_dynamic)
3478 && _bfd_elf_section_offset (output_bfd, info, input_section,
3479 rel->r_offset) != (bfd_vma) -1)
6a30718d 3480 {
4eca0228 3481 _bfd_error_handler
695344c0 3482 /* xgettext:c-format */
2dcf00ce 3483 (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3484 input_bfd,
3485 input_section,
2dcf00ce 3486 (uint64_t) rel->r_offset,
843fe662 3487 howto->name,
6a30718d 3488 h->root.root.string);
0a1b45a2 3489 return false;
6a30718d 3490 }
83be169b 3491
dc1e8a47 3492 do_relocation:
252b5132
RH
3493 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3494 contents, rel->r_offset,
55fd94b0 3495 relocation, 0);
252b5132 3496
dc1e8a47 3497 check_relocation_error:
cf5c0c5b 3498 if (r != bfd_reloc_ok)
252b5132 3499 {
cf5c0c5b 3500 const char *name;
ffb2e45b 3501
cf5c0c5b
AM
3502 if (h != NULL)
3503 name = h->root.root.string;
3504 else
3505 {
3506 name = bfd_elf_string_from_elf_section (input_bfd,
3507 symtab_hdr->sh_link,
3508 sym->st_name);
3509 if (name == NULL)
0a1b45a2 3510 return false;
cf5c0c5b 3511 if (*name == '\0')
fd361982 3512 name = bfd_section_name (sec);
cf5c0c5b 3513 }
ffb2e45b 3514
cf5c0c5b 3515 if (r == bfd_reloc_overflow)
1a72702b
AM
3516 (*info->callbacks->reloc_overflow)
3517 (info, (h ? &h->root : NULL), name, howto->name,
3518 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
cf5c0c5b
AM
3519 else
3520 {
4eca0228 3521 _bfd_error_handler
695344c0 3522 /* xgettext:c-format */
2dcf00ce 3523 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
d003868e 3524 input_bfd, input_section,
2dcf00ce 3525 (uint64_t) rel->r_offset, name, (int) r);
0a1b45a2 3526 return false;
cf5c0c5b 3527 }
252b5132 3528 }
60f2e42e
L
3529
3530 if (wrel != rel)
3531 *wrel = *rel;
3532 }
3533
3534 if (wrel != rel)
3535 {
3536 Elf_Internal_Shdr *rel_hdr;
3537 size_t deleted = rel - wrel;
3538
3539 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3540 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3541 if (rel_hdr->sh_size == 0)
3542 {
3543 /* It is too late to remove an empty reloc section. Leave
3544 one NONE reloc.
3545 ??? What is wrong with an empty section??? */
3546 rel_hdr->sh_size = rel_hdr->sh_entsize;
3547 deleted -= 1;
3548 }
3549 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3550 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3551 input_section->reloc_count -= deleted;
252b5132
RH
3552 }
3553
0a1b45a2 3554 return true;
252b5132
RH
3555}
3556
3557/* Finish up dynamic symbol handling. We set the contents of various
3558 dynamic sections here. */
3559
0a1b45a2 3560static bool
55fd94b0
AM
3561elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3562 struct bfd_link_info *info,
3563 struct elf_link_hash_entry *h,
3564 Elf_Internal_Sym *sym)
252b5132 3565{
0afcef53 3566 struct elf_x86_link_hash_table *htab;
25e762b9 3567 unsigned plt_entry_size;
0afcef53 3568 struct elf_x86_link_hash_entry *eh;
0a1b45a2
AM
3569 bool local_undefweak;
3570 bool use_plt_second;
252b5132 3571
0afcef53 3572 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4dfe6ac6 3573 if (htab == NULL)
0a1b45a2 3574 return false;
252b5132 3575
765e526c 3576 plt_entry_size = htab->plt.plt_entry_size;
25e762b9 3577
ee2fdd6f
L
3578 /* Use the second PLT section only if there is .plt section. */
3579 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3580
0afcef53 3581 eh = (struct elf_x86_link_hash_entry *) h;
e133d005
L
3582 if (eh->no_finish_dynamic_symbol)
3583 abort ();
dd7e64d4 3584
aec6b87e
L
3585 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3586 resolved undefined weak symbols in executable so that their
3587 references have value 0 at run-time. */
c5bce5c6 3588 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
aec6b87e 3589
252b5132
RH
3590 if (h->plt.offset != (bfd_vma) -1)
3591 {
ee2fdd6f 3592 bfd_vma plt_index, plt_offset;
252b5132 3593 bfd_vma got_offset;
947216bf
AM
3594 Elf_Internal_Rela rel;
3595 bfd_byte *loc;
ee2fdd6f 3596 asection *plt, *resolved_plt, *gotplt, *relplt;
cbe950e9
L
3597
3598 /* When building a static executable, use .iplt, .igot.plt and
3599 .rel.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 3600 if (htab->elf.splt != NULL)
cbe950e9 3601 {
6de2ae4a
L
3602 plt = htab->elf.splt;
3603 gotplt = htab->elf.sgotplt;
3604 relplt = htab->elf.srelplt;
cbe950e9
L
3605 }
3606 else
3607 {
6de2ae4a
L
3608 plt = htab->elf.iplt;
3609 gotplt = htab->elf.igotplt;
3610 relplt = htab->elf.irelplt;
cbe950e9 3611 }
252b5132 3612
f3180fa9 3613 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
252b5132
RH
3614
3615 /* Get the index in the procedure linkage table which
3616 corresponds to this symbol. This is the index of this symbol
3617 in all the symbols for which we are making plt entries. The
cbe950e9 3618 first entry in the procedure linkage table is reserved.
252b5132 3619
cbe950e9 3620 Get the offset into the .got table of the entry that
252b5132 3621 corresponds to this function. Each .got entry is 4 bytes.
cbe950e9 3622 The first three are reserved.
6bbec505 3623
cbe950e9
L
3624 For static executables, we don't reserve anything. */
3625
6de2ae4a 3626 if (plt == htab->elf.splt)
cbe950e9 3627 {
f604c2a2 3628 got_offset = (h->plt.offset / plt_entry_size
765e526c 3629 - htab->plt.has_plt0);
e1f98742 3630 got_offset = (got_offset + 3) * 4;
cbe950e9
L
3631 }
3632 else
3633 {
e1f98742
L
3634 got_offset = h->plt.offset / plt_entry_size;
3635 got_offset = got_offset * 4;
cbe950e9 3636 }
252b5132 3637
f604c2a2
L
3638 /* Fill in the entry in the procedure linkage table and update
3639 the first slot. */
765e526c 3640 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
f604c2a2 3641 plt_entry_size);
ee2fdd6f
L
3642
3643 if (use_plt_second)
3644 {
3645 const bfd_byte *plt_entry;
3646 if (bfd_link_pic (info))
765e526c 3647 plt_entry = htab->non_lazy_plt->pic_plt_entry;
ee2fdd6f 3648 else
765e526c 3649 plt_entry = htab->non_lazy_plt->plt_entry;
ee2fdd6f 3650 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c 3651 plt_entry, htab->non_lazy_plt->plt_entry_size);
ee2fdd6f
L
3652
3653 resolved_plt = htab->plt_second;
3654 plt_offset = eh->plt_second.offset;
3655 }
3656 else
3657 {
3658 resolved_plt = plt;
3659 plt_offset = h->plt.offset;
3660 }
3661
0e1862bb 3662 if (! bfd_link_pic (info))
252b5132 3663 {
252b5132 3664 bfd_put_32 (output_bfd,
cbe950e9
L
3665 (gotplt->output_section->vma
3666 + gotplt->output_offset
252b5132 3667 + got_offset),
ee2fdd6f 3668 resolved_plt->contents + plt_offset
07d6d2b8 3669 + htab->plt.plt_got_offset);
eac338cf 3670
90c14f0c 3671 if (htab->elf.target_os == is_vxworks)
eac338cf
PB
3672 {
3673 int s, k, reloc_index;
3674
3675 /* Create the R_386_32 relocation referencing the GOT
3676 for this PLT entry. */
3677
3678 /* S: Current slot number (zero-based). */
765e526c 3679 s = ((h->plt.offset - htab->plt.plt_entry_size)
07d6d2b8 3680 / htab->plt.plt_entry_size);
eac338cf 3681 /* K: Number of relocations for PLTResolve. */
0e1862bb 3682 if (bfd_link_pic (info))
eac338cf
PB
3683 k = PLTRESOLVE_RELOCS_SHLIB;
3684 else
3685 k = PLTRESOLVE_RELOCS;
3686 /* Skip the PLTresolve relocations, and the relocations for
3687 the other PLT slots. */
3688 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
765e526c 3689 loc = (htab->srelplt2->contents + reloc_index
eac338cf
PB
3690 * sizeof (Elf32_External_Rel));
3691
ee2fdd6f
L
3692 rel.r_offset = (plt->output_section->vma
3693 + plt->output_offset
eac338cf 3694 + h->plt.offset + 2),
7325306f 3695 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
eac338cf
PB
3696 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3697
3698 /* Create the R_386_32 relocation referencing the beginning of
3699 the PLT for this GOT entry. */
6de2ae4a
L
3700 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3701 + htab->elf.sgotplt->output_offset
eac338cf 3702 + got_offset);
7325306f 3703 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
eac338cf 3704 bfd_elf32_swap_reloc_out (output_bfd, &rel,
13ca3149 3705 loc + sizeof (Elf32_External_Rel));
eac338cf 3706 }
252b5132
RH
3707 }
3708 else
3709 {
252b5132 3710 bfd_put_32 (output_bfd, got_offset,
ee2fdd6f 3711 resolved_plt->contents + plt_offset
07d6d2b8 3712 + htab->plt.plt_got_offset);
252b5132
RH
3713 }
3714
aec6b87e
L
3715 /* Fill in the entry in the global offset table. Leave the entry
3716 as zero for undefined weak symbol in PIE. No PLT relocation
3717 against undefined weak symbol in PIE. */
3718 if (!local_undefweak)
cbe950e9 3719 {
765e526c 3720 if (htab->plt.has_plt0)
f604c2a2
L
3721 bfd_put_32 (output_bfd,
3722 (plt->output_section->vma
3723 + plt->output_offset
3724 + h->plt.offset
765e526c 3725 + htab->lazy_plt->plt_lazy_offset),
f604c2a2 3726 gotplt->contents + got_offset);
252b5132 3727
aec6b87e
L
3728 /* Fill in the entry in the .rel.plt section. */
3729 rel.r_offset = (gotplt->output_section->vma
3730 + gotplt->output_offset
3731 + got_offset);
cf1070f1 3732 if (PLT_LOCAL_IFUNC_P (info, h))
aec6b87e 3733 {
871b3ab2 3734 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
3735 h->root.root.string,
3736 h->root.u.def.section->owner);
3737
aec6b87e
L
3738 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3739 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3740 in the .got.plt section. */
3741 bfd_put_32 (output_bfd,
3742 (h->root.u.def.value
3743 + h->root.u.def.section->output_section->vma
3744 + h->root.u.def.section->output_offset),
3745 gotplt->contents + got_offset);
3746 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
68b00778
L
3747
3748 if (htab->params->report_relative_reloc)
3749 _bfd_x86_elf_link_report_relative_reloc
3750 (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3751
aec6b87e
L
3752 /* R_386_IRELATIVE comes last. */
3753 plt_index = htab->next_irelative_index--;
3754 }
3755 else
3756 {
3757 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3758 plt_index = htab->next_jump_slot_index++;
3759 }
3760
3761 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3762 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3763
f604c2a2
L
3764 /* Don't fill the second and third slots in PLT entry for
3765 static executables nor without PLT0. */
765e526c 3766 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e
L
3767 {
3768 bfd_put_32 (output_bfd,
3769 plt_index * sizeof (Elf32_External_Rel),
3770 plt->contents + h->plt.offset
765e526c 3771 + htab->lazy_plt->plt_reloc_offset);
f604c2a2
L
3772 bfd_put_32 (output_bfd,
3773 - (h->plt.offset
765e526c 3774 + htab->lazy_plt->plt_plt_offset + 4),
f604c2a2 3775 (plt->contents + h->plt.offset
765e526c 3776 + htab->lazy_plt->plt_plt_offset));
aec6b87e 3777 }
e1f98742 3778 }
dd7e64d4
L
3779 }
3780 else if (eh->plt_got.offset != (bfd_vma) -1)
3781 {
3782 bfd_vma got_offset, plt_offset;
3783 asection *plt, *got, *gotplt;
3784 const bfd_byte *got_plt_entry;
3785
dd7e64d4
L
3786 /* Set the entry in the GOT procedure linkage table. */
3787 plt = htab->plt_got;
3788 got = htab->elf.sgot;
3789 gotplt = htab->elf.sgotplt;
3790 got_offset = h->got.offset;
3791
3792 if (got_offset == (bfd_vma) -1
3793 || plt == NULL
3794 || got == NULL
3795 || gotplt == NULL)
3796 abort ();
3797
3798 /* Fill in the entry in the GOT procedure linkage table. */
0e1862bb 3799 if (! bfd_link_pic (info))
252b5132 3800 {
765e526c 3801 got_plt_entry = htab->non_lazy_plt->plt_entry;
dd7e64d4 3802 got_offset += got->output_section->vma + got->output_offset;
252b5132 3803 }
dd7e64d4
L
3804 else
3805 {
765e526c 3806 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
dd7e64d4
L
3807 got_offset += (got->output_section->vma
3808 + got->output_offset
3809 - gotplt->output_section->vma
3810 - gotplt->output_offset);
3811 }
3812
3813 plt_offset = eh->plt_got.offset;
3814 memcpy (plt->contents + plt_offset, got_plt_entry,
765e526c 3815 htab->non_lazy_plt->plt_entry_size);
dd7e64d4 3816 bfd_put_32 (output_bfd, got_offset,
f604c2a2 3817 (plt->contents + plt_offset
765e526c 3818 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
3819 }
3820
aec6b87e
L
3821 if (!local_undefweak
3822 && !h->def_regular
dd7e64d4
L
3823 && (h->plt.offset != (bfd_vma) -1
3824 || eh->plt_got.offset != (bfd_vma) -1))
3825 {
3826 /* Mark the symbol as undefined, rather than as defined in
3827 the .plt section. Leave the value if there were any
3828 relocations where pointer equality matters (this is a clue
3829 for the dynamic linker, to make function pointer
3830 comparisons work between an application and shared
3831 library), otherwise set it to zero. If a function is only
3832 called from a binary, there is no need to slow down
3833 shared libraries because of that. */
3834 sym->st_shndx = SHN_UNDEF;
3835 if (!h->pointer_equality_needed)
3836 sym->st_value = 0;
252b5132
RH
3837 }
3838
4ec09950
L
3839 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3840
aec6b87e
L
3841 /* Don't generate dynamic GOT relocation against undefined weak
3842 symbol in executable. */
13ae64f3 3843 if (h->got.offset != (bfd_vma) -1
0afcef53
L
3844 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3845 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
aec6b87e 3846 && !local_undefweak)
252b5132 3847 {
947216bf 3848 Elf_Internal_Rela rel;
233cc9c1 3849 asection *relgot = htab->elf.srelgot;
68b00778 3850 const char *relative_reloc_name = NULL;
5af6f000 3851 bool generate_dynamic_reloc = true;
252b5132
RH
3852
3853 /* This symbol has an entry in the global offset table. Set it
3854 up. */
3855
6de2ae4a 3856 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
ffb2e45b 3857 abort ();
252b5132 3858
6de2ae4a
L
3859 rel.r_offset = (htab->elf.sgot->output_section->vma
3860 + htab->elf.sgot->output_offset
dc810e39 3861 + (h->got.offset & ~(bfd_vma) 1));
252b5132 3862
dd5724d5
AM
3863 /* If this is a static link, or it is a -Bsymbolic link and the
3864 symbol is defined locally or was forced to be local because
3865 of a version file, we just want to emit a RELATIVE reloc.
252b5132
RH
3866 The entry in the global offset table will already have been
3867 initialized in the relocate_section function. */
710ab287 3868 if (h->def_regular
0018b0a3
L
3869 && h->type == STT_GNU_IFUNC)
3870 {
233cc9c1
L
3871 if (h->plt.offset == (bfd_vma) -1)
3872 {
3873 /* STT_GNU_IFUNC is referenced without PLT. */
3874 if (htab->elf.splt == NULL)
3875 {
3876 /* use .rel[a].iplt section to store .got relocations
3877 in static executable. */
3878 relgot = htab->elf.irelplt;
3879 }
6999821f 3880 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
233cc9c1 3881 {
871b3ab2 3882 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
3883 h->root.root.string,
3884 h->root.u.def.section->owner);
3885
233cc9c1
L
3886 bfd_put_32 (output_bfd,
3887 (h->root.u.def.value
3888 + h->root.u.def.section->output_section->vma
3889 + h->root.u.def.section->output_offset),
3890 htab->elf.sgot->contents + h->got.offset);
3891 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
68b00778 3892 relative_reloc_name = "R_386_IRELATIVE";
233cc9c1
L
3893 }
3894 else
3895 goto do_glob_dat;
3896 }
3897 else if (bfd_link_pic (info))
710ab287
L
3898 {
3899 /* Generate R_386_GLOB_DAT. */
3900 goto do_glob_dat;
3901 }
3902 else
3903 {
cd2b2c10 3904 asection *plt;
ee2fdd6f 3905 bfd_vma plt_offset;
cd2b2c10 3906
710ab287
L
3907 if (!h->pointer_equality_needed)
3908 abort ();
3909
3910 /* For non-shared object, we can't use .got.plt, which
3911 contains the real function addres if we need pointer
3912 equality. We load the GOT entry with the PLT entry. */
ee2fdd6f
L
3913 if (htab->plt_second != NULL)
3914 {
3915 plt = htab->plt_second;
3916 plt_offset = eh->plt_second.offset;
3917 }
3918 else
3919 {
3920 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3921 plt_offset = h->plt.offset;
3922 }
710ab287
L
3923 bfd_put_32 (output_bfd,
3924 (plt->output_section->vma
ee2fdd6f 3925 + plt->output_offset + plt_offset),
6de2ae4a 3926 htab->elf.sgot->contents + h->got.offset);
0a1b45a2 3927 return true;
710ab287 3928 }
0018b0a3 3929 }
0e1862bb 3930 else if (bfd_link_pic (info)
6999821f 3931 && SYMBOL_REFERENCES_LOCAL_P (info, h))
dd5724d5 3932 {
6725bdbf 3933 BFD_ASSERT((h->got.offset & 1) != 0);
5af6f000
L
3934 if (info->enable_dt_relr)
3935 generate_dynamic_reloc = false;
3936 else
3937 {
3938 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3939 relative_reloc_name = "R_386_RELATIVE";
3940 }
dd5724d5 3941 }
252b5132
RH
3942 else
3943 {
dd5724d5 3944 BFD_ASSERT((h->got.offset & 1) == 0);
dc1e8a47 3945 do_glob_dat:
6725bdbf 3946 bfd_put_32 (output_bfd, (bfd_vma) 0,
6de2ae4a 3947 htab->elf.sgot->contents + h->got.offset);
252b5132
RH
3948 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3949 }
3950
5af6f000
L
3951 if (generate_dynamic_reloc)
3952 {
3953 if (relative_reloc_name != NULL
3954 && htab->params->report_relative_reloc)
3955 _bfd_x86_elf_link_report_relative_reloc
3956 (info, relgot, h, sym, relative_reloc_name, &rel);
68b00778 3957
5af6f000
L
3958 elf_append_rel (output_bfd, relgot, &rel);
3959 }
252b5132
RH
3960 }
3961
f5385ebf 3962 if (h->needs_copy)
252b5132 3963 {
947216bf 3964 Elf_Internal_Rela rel;
5474d94f 3965 asection *s;
252b5132
RH
3966
3967 /* This symbol needs a copy reloc. Set it up. */
ff38b4cc 3968 VERIFY_COPY_RELOC (h, htab)
252b5132
RH
3969
3970 rel.r_offset = (h->root.u.def.value
3971 + h->root.u.def.section->output_section->vma
3972 + h->root.u.def.section->output_offset);
3973 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
afbf7e8e 3974 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
3975 s = htab->elf.sreldynrelro;
3976 else
3977 s = htab->elf.srelbss;
3978 elf_append_rel (output_bfd, s, &rel);
252b5132
RH
3979 }
3980
0a1b45a2 3981 return true;
252b5132
RH
3982}
3983
c25bc9fc
L
3984/* Finish up local dynamic symbol handling. We set the contents of
3985 various dynamic sections here. */
3986
1201fda6 3987static int
c25bc9fc
L
3988elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3989{
3990 struct elf_link_hash_entry *h
3991 = (struct elf_link_hash_entry *) *slot;
3992 struct bfd_link_info *info
23209a78 3993 = (struct bfd_link_info *) inf;
c25bc9fc
L
3994
3995 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3996 h, NULL);
3997}
3998
aec6b87e
L
3999/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4000 here since undefined weak symbol may not be dynamic and may not be
4001 called for elf_i386_finish_dynamic_symbol. */
4002
0a1b45a2 4003static bool
aec6b87e
L
4004elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4005 void *inf)
4006{
4007 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4008 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4009
4010 if (h->root.type != bfd_link_hash_undefweak
4011 || h->dynindx != -1)
0a1b45a2 4012 return true;
aec6b87e
L
4013
4014 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4015 info, h, NULL);
aec6b87e
L
4016}
4017
38701953
AM
4018/* Used to decide how to sort relocs in an optimal manner for the
4019 dynamic linker, before writing them out. */
4020
4021static enum elf_reloc_type_class
cae1fbbb 4022elf_i386_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
4023 const asection *rel_sec ATTRIBUTE_UNUSED,
4024 const Elf_Internal_Rela *rela)
38701953 4025{
cae1fbbb
L
4026 bfd *abfd = info->output_bfd;
4027 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4028 struct elf_link_hash_table *htab = elf_hash_table (info);
cae1fbbb 4029
d9e3b590
L
4030 if (htab->dynsym != NULL
4031 && htab->dynsym->contents != NULL)
4032 {
4033 /* Check relocation against STT_GNU_IFUNC symbol if there are
07d6d2b8 4034 dynamic symbols. */
d9e3b590 4035 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
c428ce9d
L
4036 if (r_symndx != STN_UNDEF)
4037 {
4038 Elf_Internal_Sym sym;
4039 if (!bed->s->swap_symbol_in (abfd,
4040 (htab->dynsym->contents
4041 + r_symndx * sizeof (Elf32_External_Sym)),
4042 0, &sym))
4043 abort ();
d9e3b590 4044
c428ce9d
L
4045 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4046 return reloc_class_ifunc;
4047 }
d9e3b590 4048 }
cae1fbbb 4049
55fd94b0 4050 switch (ELF32_R_TYPE (rela->r_info))
38701953 4051 {
c428ce9d
L
4052 case R_386_IRELATIVE:
4053 return reloc_class_ifunc;
38701953
AM
4054 case R_386_RELATIVE:
4055 return reloc_class_relative;
4056 case R_386_JUMP_SLOT:
4057 return reloc_class_plt;
4058 case R_386_COPY:
4059 return reloc_class_copy;
4060 default:
4061 return reloc_class_normal;
4062 }
4063}
4064
252b5132
RH
4065/* Finish up the dynamic sections. */
4066
0a1b45a2 4067static bool
55fd94b0
AM
4068elf_i386_finish_dynamic_sections (bfd *output_bfd,
4069 struct bfd_link_info *info)
252b5132 4070{
0afcef53 4071 struct elf_x86_link_hash_table *htab;
252b5132 4072
9577f60b 4073 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4dfe6ac6 4074 if (htab == NULL)
0a1b45a2 4075 return false;
4dfe6ac6 4076
9577f60b 4077 if (!htab->elf.dynamic_sections_created)
0a1b45a2 4078 return true;
252b5132 4079
9577f60b 4080 if (htab->elf.splt && htab->elf.splt->size > 0)
252b5132 4081 {
7c80aab9
L
4082 if (bfd_is_abs_section (htab->elf.splt->output_section))
4083 {
4084 info->callbacks->einfo
4085 (_("%F%P: discarded output section: `%pA'\n"),
4086 htab->elf.splt);
4087 return false;
4088 }
4089
9577f60b
L
4090 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4091 really seem like the right value. */
4092 elf_section_data (htab->elf.splt->output_section)
4093 ->this_hdr.sh_entsize = 4;
252b5132 4094
9577f60b 4095 if (htab->plt.has_plt0)
252b5132 4096 {
9577f60b
L
4097 /* Fill in the special first entry in the procedure linkage
4098 table. */
9577f60b
L
4099 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4100 htab->lazy_plt->plt0_entry_size);
4101 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
851b6fa1 4102 htab->plt0_pad_byte,
9577f60b
L
4103 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4104 if (!bfd_link_pic (info))
252b5132 4105 {
9577f60b
L
4106 bfd_put_32 (output_bfd,
4107 (htab->elf.sgotplt->output_section->vma
4108 + htab->elf.sgotplt->output_offset
4109 + 4),
4110 htab->elf.splt->contents
4111 + htab->lazy_plt->plt0_got1_offset);
4112 bfd_put_32 (output_bfd,
4113 (htab->elf.sgotplt->output_section->vma
4114 + htab->elf.sgotplt->output_offset
4115 + 8),
4116 htab->elf.splt->contents
4117 + htab->lazy_plt->plt0_got2_offset);
1f78f649 4118
90c14f0c 4119 if (htab->elf.target_os == is_vxworks)
eac338cf 4120 {
9577f60b
L
4121 Elf_Internal_Rela rel;
4122 int num_plts = (htab->elf.splt->size
4123 / htab->plt.plt_entry_size) - 1;
4124 unsigned char *p;
4125 asection *srelplt2 = htab->srelplt2;
4126
4127 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4128 + 4. On IA32 we use REL relocations so the
4129 addend goes in the PLT directly. */
4130 rel.r_offset = (htab->elf.splt->output_section->vma
4131 + htab->elf.splt->output_offset
4132 + htab->lazy_plt->plt0_got1_offset);
4133 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4134 R_386_32);
4135 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4136 srelplt2->contents);
4137 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4138 + 8. */
4139 rel.r_offset = (htab->elf.splt->output_section->vma
4140 + htab->elf.splt->output_offset
4141 + htab->lazy_plt->plt0_got2_offset);
4142 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4143 R_386_32);
4144 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4145 srelplt2->contents +
4146 sizeof (Elf32_External_Rel));
4147 /* Correct the .rel.plt.unloaded relocations. */
4148 p = srelplt2->contents;
4149 if (bfd_link_pic (info))
4150 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4151 else
4152 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
eac338cf 4153
9577f60b 4154 for (; num_plts; num_plts--)
1f78f649 4155 {
9577f60b 4156 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
f604c2a2 4157 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
1f78f649 4158 R_386_32);
9577f60b
L
4159 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4160 p += sizeof (Elf32_External_Rel);
f604c2a2 4161
9577f60b
L
4162 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4163 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4164 R_386_32);
4165 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4166 p += sizeof (Elf32_External_Rel);
1f78f649 4167 }
eac338cf
PB
4168 }
4169 }
252b5132 4170 }
fff53dae
L
4171 }
4172
aec6b87e
L
4173 /* Fill PLT entries for undefined weak symbols in PIE. */
4174 if (bfd_link_pie (info))
4175 bfd_hash_traverse (&info->hash->table,
4176 elf_i386_pie_finish_undefweak_symbol,
4177 info);
4178
0a1b45a2 4179 return true;
252b5132
RH
4180}
4181
233cc9c1
L
4182/* Fill PLT/GOT entries and allocate dynamic relocations for local
4183 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4184 It has to be done before elf_link_sort_relocs is called so that
4185 dynamic relocations are properly sorted. */
4186
0a1b45a2 4187static bool
233cc9c1
L
4188elf_i386_output_arch_local_syms
4189 (bfd *output_bfd ATTRIBUTE_UNUSED,
4190 struct bfd_link_info *info,
4191 void *flaginfo ATTRIBUTE_UNUSED,
4192 int (*func) (void *, const char *,
4193 Elf_Internal_Sym *,
4194 asection *,
4195 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4196{
0afcef53
L
4197 struct elf_x86_link_hash_table *htab
4198 = elf_x86_hash_table (info, I386_ELF_DATA);
233cc9c1 4199 if (htab == NULL)
0a1b45a2 4200 return false;
233cc9c1
L
4201
4202 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4203 htab_traverse (htab->loc_hash_table,
4204 elf_i386_finish_local_dynamic_symbol,
4205 info);
4206
0a1b45a2 4207 return true;
233cc9c1
L
4208}
4209
f604c2a2
L
4210/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4211 dynamic relocations. */
4212
4213static long
4214elf_i386_get_synthetic_symtab (bfd *abfd,
4215 long symcount ATTRIBUTE_UNUSED,
4216 asymbol **syms ATTRIBUTE_UNUSED,
4217 long dynsymcount,
4218 asymbol **dynsyms,
4219 asymbol **ret)
4220{
f493882d 4221 long count, i, n;
f604c2a2 4222 int j;
f604c2a2 4223 bfd_byte *plt_contents;
f493882d 4224 long relsize;
765e526c
L
4225 const struct elf_x86_lazy_plt_layout *lazy_plt;
4226 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4227 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4228 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
f604c2a2
L
4229 asection *plt;
4230 bfd_vma got_addr;
f493882d
L
4231 enum elf_x86_plt_type plt_type;
4232 struct elf_x86_plt plts[] =
3972882e 4233 {
f493882d
L
4234 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4235 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4236 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4237 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
f604c2a2 4238 };
144bed8d 4239
f604c2a2 4240 *ret = NULL;
144bed8d 4241
f604c2a2
L
4242 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4243 return 0;
3972882e 4244
f604c2a2
L
4245 if (dynsymcount <= 0)
4246 return 0;
cca5b8b6 4247
f604c2a2
L
4248 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4249 if (relsize <= 0)
4250 return -1;
cca5b8b6 4251
f604c2a2
L
4252 non_lazy_plt = NULL;
4253 /* Silence GCC 6. */
4254 lazy_plt = NULL;
ee2fdd6f
L
4255 non_lazy_ibt_plt = NULL;
4256 lazy_ibt_plt = NULL;
90c14f0c 4257 switch (get_elf_backend_data (abfd)->target_os)
144bed8d 4258 {
f604c2a2 4259 case is_normal:
3b4c3844 4260 case is_solaris:
f604c2a2 4261 non_lazy_plt = &elf_i386_non_lazy_plt;
ee2fdd6f
L
4262 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4263 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
f604c2a2
L
4264 /* Fall through */
4265 case is_vxworks:
4266 lazy_plt = &elf_i386_lazy_plt;
4267 break;
90c14f0c
L
4268 default:
4269 abort ();
f604c2a2
L
4270 }
4271
4272 got_addr = 0;
3972882e 4273
f604c2a2
L
4274 count = 0;
4275 for (j = 0; plts[j].name != NULL; j++)
4276 {
4277 plt = bfd_get_section_by_name (abfd, plts[j].name);
90efb642 4278 if (plt == NULL || plt->size == 0)
6f25f223 4279 continue;
533d0af0 4280
f604c2a2
L
4281 /* Get the PLT section contents. */
4282 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4283 if (plt_contents == NULL)
4284 break;
4285 if (!bfd_get_section_contents (abfd, (asection *) plt,
4286 plt_contents, 0, plt->size))
4287 {
4288 free (plt_contents);
4289 break;
4290 }
4291
4292 /* Check what kind of PLT it is. */
4293 plt_type = plt_unknown;
90efb642
L
4294 if (plts[j].type == plt_unknown
4295 && (plt->size >= (lazy_plt->plt0_entry_size
4296 + lazy_plt->plt_entry_size)))
f604c2a2
L
4297 {
4298 /* Match lazy PLT first. */
4299 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4300 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
4301 {
4302 /* The fist entry in the lazy IBT PLT is the same as the
4303 normal lazy PLT. */
4304 if (lazy_ibt_plt != NULL
90efb642 4305 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
4306 lazy_ibt_plt->plt_entry,
4307 lazy_ibt_plt->plt_got_offset) == 0))
4308 plt_type = plt_lazy | plt_second;
4309 else
4310 plt_type = plt_lazy;
4311 }
f604c2a2
L
4312 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4313 lazy_plt->plt0_got1_offset) == 0)
ee2fdd6f
L
4314 {
4315 /* The fist entry in the PIC lazy IBT PLT is the same as
4316 the normal PIC lazy PLT. */
4317 if (lazy_ibt_plt != NULL
90efb642 4318 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
ee2fdd6f
L
4319 lazy_ibt_plt->pic_plt_entry,
4320 lazy_ibt_plt->plt_got_offset) == 0))
4321 plt_type = plt_lazy | plt_pic | plt_second;
4322 else
4323 plt_type = plt_lazy | plt_pic;
4324 }
f604c2a2
L
4325 }
4326
4327 if (non_lazy_plt != NULL
90efb642
L
4328 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4329 && plt->size >= non_lazy_plt->plt_entry_size)
f604c2a2
L
4330 {
4331 /* Match non-lazy PLT. */
4332 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4333 non_lazy_plt->plt_got_offset) == 0)
4334 plt_type = plt_non_lazy;
4335 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4336 non_lazy_plt->plt_got_offset) == 0)
4337 plt_type = plt_pic;
4338 }
4339
ee2fdd6f 4340 if ((non_lazy_ibt_plt != NULL)
90efb642
L
4341 && (plt_type == plt_unknown || plt_type == plt_second)
4342 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
ee2fdd6f
L
4343 {
4344 if (memcmp (plt_contents,
4345 non_lazy_ibt_plt->plt_entry,
4346 non_lazy_ibt_plt->plt_got_offset) == 0)
4347 {
4348 /* Match IBT PLT. */
4349 plt_type = plt_second;
4350 non_lazy_plt = non_lazy_ibt_plt;
4351 }
4352 else if (memcmp (plt_contents,
4353 non_lazy_ibt_plt->pic_plt_entry,
4354 non_lazy_ibt_plt->plt_got_offset) == 0)
4355 {
4356 /* Match PIC IBT PLT. */
4357 plt_type = plt_second | plt_pic;
4358 non_lazy_plt = non_lazy_ibt_plt;
4359 }
4360 }
4361
f604c2a2 4362 if (plt_type == plt_unknown)
37c0b6ee
L
4363 {
4364 free (plt_contents);
4365 continue;
4366 }
3972882e 4367
f604c2a2
L
4368 plts[j].sec = plt;
4369 plts[j].type = plt_type;
ec1f73bb 4370
f604c2a2
L
4371 if ((plt_type & plt_lazy))
4372 {
4373 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4374 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4375 /* Skip PLT0 in lazy PLT. */
4376 i = 1;
4377 }
4378 else
4379 {
4380 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4381 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4382 i = 0;
4383 }
fca6ae69 4384
ee2fdd6f
L
4385 /* Skip lazy PLT when the second PLT is used. */
4386 if ((plt_type & (plt_lazy | plt_second))
4387 == (plt_lazy | plt_second))
4388 plts[j].count = 0;
4389 else
4390 {
4391 n = plt->size / plts[j].plt_entry_size;
4392 plts[j].count = n;
4393 count += n - i;
4394 }
f604c2a2
L
4395
4396 plts[j].contents = plt_contents;
4397
4398 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4399 if ((plt_type & plt_pic))
4400 got_addr = (bfd_vma) -1;
144bed8d
L
4401 }
4402
f493882d
L
4403 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4404 got_addr, plts, dynsyms,
4405 ret);
4c45e5c9
JJ
4406}
4407
f604c2a2
L
4408/* Set up i386 GNU properties. Return the first relocatable ELF input
4409 with GNU properties if found. Otherwise, return NULL. */
4410
4411static bfd *
4412elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4413{
1de031c8 4414 struct elf_x86_init_table init_table;
f604c2a2 4415
90c14f0c 4416 switch (get_elf_backend_data (info->output_bfd)->target_os)
f604c2a2
L
4417 {
4418 case is_normal:
3b4c3844 4419 case is_solaris:
851b6fa1 4420 init_table.plt0_pad_byte = 0x0;
1de031c8
L
4421 init_table.lazy_plt = &elf_i386_lazy_plt;
4422 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4423 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4424 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
f604c2a2
L
4425 break;
4426 case is_vxworks:
851b6fa1 4427 init_table.plt0_pad_byte = 0x90;
1de031c8
L
4428 init_table.lazy_plt = &elf_i386_lazy_plt;
4429 init_table.non_lazy_plt = NULL;
4430 init_table.lazy_ibt_plt = NULL;
4431 init_table.non_lazy_ibt_plt = NULL;
f604c2a2 4432 break;
90c14f0c
L
4433 default:
4434 abort ();
f604c2a2
L
4435 }
4436
1de031c8
L
4437 init_table.r_info = elf32_r_info;
4438 init_table.r_sym = elf32_r_sym;
7a382c1c 4439
1de031c8 4440 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
f604c2a2
L
4441}
4442
6d00b590 4443#define TARGET_LITTLE_SYM i386_elf32_vec
252b5132
RH
4444#define TARGET_LITTLE_NAME "elf32-i386"
4445#define ELF_ARCH bfd_arch_i386
ae95ffa6 4446#define ELF_TARGET_ID I386_ELF_DATA
252b5132
RH
4447#define ELF_MACHINE_CODE EM_386
4448#define ELF_MAXPAGESIZE 0x1000
252b5132
RH
4449
4450#define elf_backend_can_gc_sections 1
51b64d56 4451#define elf_backend_can_refcount 1
252b5132
RH
4452#define elf_backend_want_got_plt 1
4453#define elf_backend_plt_readonly 1
4454#define elf_backend_want_plt_sym 0
4455#define elf_backend_got_header_size 12
e41b3a13 4456#define elf_backend_plt_alignment 4
64f52338 4457#define elf_backend_dtrel_excludes_plt 1
bedfd056 4458#define elf_backend_caches_rawsize 1
5474d94f 4459#define elf_backend_want_dynrelro 1
252b5132 4460
8c29f035
AM
4461/* Support RELA for objdump of prelink objects. */
4462#define elf_info_to_howto elf_i386_info_to_howto_rel
dd5724d5
AM
4463#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4464
dd5724d5 4465#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
dd5724d5 4466#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
13285a1b 4467#define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3972882e 4468#define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
dd5724d5 4469
13285a1b 4470#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
3747999c 4471#define elf_backend_always_size_sections elf_i386_always_size_sections
f604c2a2 4472#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
0ac8d2ca 4473#define elf_backend_fake_sections elf_i386_fake_sections
dd5724d5
AM
4474#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4475#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
233cc9c1 4476#define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
c5fccbec
DJ
4477#define elf_backend_grok_prstatus elf_i386_grok_prstatus
4478#define elf_backend_grok_psinfo elf_i386_grok_psinfo
db6751f2 4479#define elf_backend_reloc_type_class elf_i386_reloc_type_class
0ac8d2ca 4480#define elf_backend_relocate_section elf_i386_relocate_section
f604c2a2 4481#define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
9f857535 4482#define elf_backend_hide_symbol _bfd_x86_elf_hide_symbol
dd5724d5 4483
0a1b45a2 4484#define elf_backend_linux_prpsinfo32_ugid16 true
a2f63b2e 4485
8f56f7a2
L
4486#define elf32_bed elf32_i386_bed
4487
252b5132 4488#include "elf32-target.h"
2bc3c89a
AM
4489
4490/* FreeBSD support. */
4491
4492#undef TARGET_LITTLE_SYM
6d00b590 4493#define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
2bc3c89a
AM
4494#undef TARGET_LITTLE_NAME
4495#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
d1036acb
L
4496#undef ELF_OSABI
4497#define ELF_OSABI ELFOSABI_FREEBSD
2bc3c89a
AM
4498
4499/* The kernel recognizes executables as valid only if they carry a
4500 "FreeBSD" label in the ELF header. So we put this label on all
4501 executables and (for simplicity) also all other object files. */
4502
0a1b45a2 4503static bool
ed7e9d0b 4504elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
2bc3c89a 4505{
ed7e9d0b 4506 if (!_bfd_elf_init_file_header (abfd, info))
0a1b45a2 4507 return false;
2bc3c89a 4508
2bc3c89a 4509#ifdef OLD_FREEBSD_ABI_LABEL
cf7363b4
L
4510 {
4511 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4512 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4513 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4514 }
caf47ea6 4515#endif
0a1b45a2 4516 return true;
2bc3c89a
AM
4517}
4518
ed7e9d0b
AM
4519#undef elf_backend_init_file_header
4520#define elf_backend_init_file_header elf_i386_fbsd_init_file_header
571fe01f
NC
4521#undef elf32_bed
4522#define elf32_bed elf32_i386_fbsd_bed
2bc3c89a 4523
d8045f23
NC
4524#undef elf_backend_add_symbol_hook
4525
2bc3c89a 4526#include "elf32-target.h"
eac338cf 4527
ed7e9d0b
AM
4528#undef elf_backend_init_file_header
4529
a6cc6b3b
RO
4530/* Solaris 2. */
4531
4532#undef TARGET_LITTLE_SYM
6d00b590 4533#define TARGET_LITTLE_SYM i386_elf32_sol2_vec
a6cc6b3b
RO
4534#undef TARGET_LITTLE_NAME
4535#define TARGET_LITTLE_NAME "elf32-i386-sol2"
4536
90c14f0c
L
4537#undef ELF_TARGET_OS
4538#define ELF_TARGET_OS is_solaris
3b4c3844 4539
a6cc6b3b
RO
4540/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4541 objects won't be recognized. */
4542#undef ELF_OSABI
4543
4544#undef elf32_bed
4545#define elf32_bed elf32_i386_sol2_bed
4546
7dc98aea
RO
4547/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4548 boundary. */
84865015 4549#undef elf_backend_static_tls_alignment
7dc98aea
RO
4550#define elf_backend_static_tls_alignment 8
4551
a6cc6b3b
RO
4552/* The Solaris 2 ABI requires a plt symbol on all platforms.
4553
4554 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4555 File, p.63. */
84865015 4556#undef elf_backend_want_plt_sym
a6cc6b3b
RO
4557#define elf_backend_want_plt_sym 1
4558
84865015
NC
4559#undef elf_backend_strtab_flags
4560#define elf_backend_strtab_flags SHF_STRINGS
4561
5522f910 4562/* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
07d6d2b8 4563 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
5522f910
NC
4564 FALSE otherwise. ISECTION is the best guess matching section from the
4565 input bfd IBFD, but it might be NULL. */
4566
0a1b45a2 4567static bool
5522f910
NC
4568elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4569 bfd *obfd ATTRIBUTE_UNUSED,
4570 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4571 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
4572{
4573 /* PR 19938: FIXME: Need to add code for setting the sh_info
5522f910 4574 and sh_link fields of Solaris specific section types. */
0a1b45a2 4575 return false;
84865015 4576
5522f910 4577 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
84865015
NC
4578 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4579
4580http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4581
4582 The following values should be set:
07d6d2b8
AM
4583
4584Type Link Info
84865015
NC
4585-----------------------------------------------------------------------------
4586SHT_SUNW_ancillary The section header index of 0
07d6d2b8
AM
4587 [0x6fffffee] the associated string table.
4588
84865015 4589SHT_SUNW_capinfo The section header index of For a dynamic object, the
07d6d2b8
AM
4590 [0x6ffffff0] the associated symbol table. section header index of
4591 the associated
84865015
NC
4592 SHT_SUNW_capchain table,
4593 otherwise 0.
4594
4595SHT_SUNW_symsort The section header index of 0
07d6d2b8 4596 [0x6ffffff1] the associated symbol table.
84865015
NC
4597
4598SHT_SUNW_tlssort The section header index of 0
07d6d2b8
AM
4599 [0x6ffffff2] the associated symbol table.
4600
4601SHT_SUNW_LDYNSYM The section header index of One greater than the
4602 [0x6ffffff3] the associated string table. symbol table index of the
4603 This index is the same string last local symbol,
84865015 4604 table used by the SHT_DYNSYM STB_LOCAL. Since
07d6d2b8
AM
4605 section. SHT_SUNW_LDYNSYM only
4606 contains local symbols,
84865015
NC
4607 sh_info is equivalent to
4608 the number of symbols in
4609 the table.
4610
07d6d2b8
AM
4611SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4612 [0x6ffffff5] the section header index of to named strings, the
4613 the associated section header index of
4614 SHT_SUNW_capinfo table, the associated string
4615 otherwise 0. table, otherwise 0.
84865015 4616
07d6d2b8
AM
4617SHT_SUNW_move The section header index of 0
4618 [0x6ffffffa] the associated symbol table.
4619
4620SHT_SUNW_COMDAT 0 0
84865015
NC
4621 [0x6ffffffb]
4622
4623SHT_SUNW_syminfo The section header index of The section header index
07d6d2b8
AM
4624 [0x6ffffffc] the associated symbol table. of the associated
4625 .dynamic section.
84865015 4626
07d6d2b8
AM
4627SHT_SUNW_verdef The section header index of The number of version
4628 [0x6ffffffd] the associated string table. definitions within the
4629 section.
84865015
NC
4630
4631SHT_SUNW_verneed The section header index of The number of version
07d6d2b8
AM
4632 [0x6ffffffe] the associated string table. dependencies within the
4633 section.
84865015 4634
07d6d2b8
AM
4635SHT_SUNW_versym The section header index of 0
4636 [0x6fffffff] the associated symbol table. */
84865015
NC
4637}
4638
5522f910
NC
4639#undef elf_backend_copy_special_section_fields
4640#define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
84865015 4641
a6cc6b3b
RO
4642#include "elf32-target.h"
4643
bf64a951
L
4644/* Intel MCU support. */
4645
0a1b45a2 4646static bool
bf64a951
L
4647elf32_iamcu_elf_object_p (bfd *abfd)
4648{
4649 /* Set the right machine number for an IAMCU elf32 file. */
4650 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
0a1b45a2 4651 return true;
bf64a951
L
4652}
4653
4654#undef TARGET_LITTLE_SYM
4655#define TARGET_LITTLE_SYM iamcu_elf32_vec
4656#undef TARGET_LITTLE_NAME
4657#define TARGET_LITTLE_NAME "elf32-iamcu"
84865015 4658#undef ELF_ARCH
bf64a951
L
4659#define ELF_ARCH bfd_arch_iamcu
4660
4661#undef ELF_MACHINE_CODE
4662#define ELF_MACHINE_CODE EM_IAMCU
4663
90c14f0c 4664#undef ELF_TARGET_OS
bf64a951
L
4665#undef ELF_OSABI
4666
4667#undef elf32_bed
4668#define elf32_bed elf32_iamcu_bed
4669
4670#undef elf_backend_object_p
4671#define elf_backend_object_p elf32_iamcu_elf_object_p
4672
4673#undef elf_backend_static_tls_alignment
4674
4675#undef elf_backend_want_plt_sym
07d6d2b8 4676#define elf_backend_want_plt_sym 0
bf64a951 4677
84865015 4678#undef elf_backend_strtab_flags
5522f910 4679#undef elf_backend_copy_special_section_fields
84865015 4680
bf64a951
L
4681#include "elf32-target.h"
4682
4683/* Restore defaults. */
4684#undef ELF_ARCH
4685#define ELF_ARCH bfd_arch_i386
4686#undef ELF_MACHINE_CODE
4687#define ELF_MACHINE_CODE EM_386
cc574a35 4688#undef elf_backend_object_p
bf64a951 4689
eac338cf
PB
4690/* VxWorks support. */
4691
4692#undef TARGET_LITTLE_SYM
6d00b590 4693#define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
eac338cf
PB
4694#undef TARGET_LITTLE_NAME
4695#define TARGET_LITTLE_NAME "elf32-i386-vxworks"
d1036acb 4696#undef ELF_OSABI
1587442d
L
4697#undef ELF_MAXPAGESIZE
4698#define ELF_MAXPAGESIZE 0x1000
a27e4371
RM
4699#undef elf_backend_plt_alignment
4700#define elf_backend_plt_alignment 4
eac338cf 4701
90c14f0c
L
4702#undef ELF_TARGET_OS
4703#define ELF_TARGET_OS is_vxworks
eac338cf 4704
13285a1b 4705#undef elf_backend_relocs_compatible
eac338cf
PB
4706#undef elf_backend_add_symbol_hook
4707#define elf_backend_add_symbol_hook \
4708 elf_vxworks_add_symbol_hook
4709#undef elf_backend_link_output_symbol_hook
4710#define elf_backend_link_output_symbol_hook \
9c72ff84 4711 elf_vxworks_link_output_symbol_hook
eac338cf
PB
4712#undef elf_backend_emit_relocs
4713#define elf_backend_emit_relocs elf_vxworks_emit_relocs
4714#undef elf_backend_final_write_processing
4715#define elf_backend_final_write_processing \
4716 elf_vxworks_final_write_processing
7dc98aea 4717#undef elf_backend_static_tls_alignment
eac338cf
PB
4718
4719/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4720 define it. */
4721#undef elf_backend_want_plt_sym
4722#define elf_backend_want_plt_sym 1
4723
4724#undef elf32_bed
4725#define elf32_bed elf32_i386_vxworks_bed
4726
4727#include "elf32-target.h"