elf64-x86-64.c revision 1.1.1.7 1 1.1 christos /* X86-64 specific support for ELF
2 1.1.1.7 christos Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 1.1 christos Contributed by Jan Hubicka <jh (at) suse.cz>.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos #include "sysdep.h"
23 1.1 christos #include "bfd.h"
24 1.1 christos #include "bfdlink.h"
25 1.1 christos #include "libbfd.h"
26 1.1 christos #include "elf-bfd.h"
27 1.1.1.2 christos #include "elf-nacl.h"
28 1.1 christos #include "bfd_stdint.h"
29 1.1 christos #include "objalloc.h"
30 1.1 christos #include "hashtab.h"
31 1.1.1.2 christos #include "dwarf2.h"
32 1.1.1.2 christos #include "libiberty.h"
33 1.1 christos
34 1.1.1.6 christos #include "opcode/i386.h"
35 1.1 christos #include "elf/x86-64.h"
36 1.1 christos
37 1.1.1.2 christos #ifdef CORE_HEADER
38 1.1.1.2 christos #include <stdarg.h>
39 1.1.1.2 christos #include CORE_HEADER
40 1.1.1.2 christos #endif
41 1.1.1.2 christos
42 1.1 christos /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
43 1.1 christos #define MINUS_ONE (~ (bfd_vma) 0)
44 1.1 christos
45 1.1 christos /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
46 1.1 christos identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
47 1.1 christos relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
48 1.1 christos since they are the same. */
49 1.1 christos
50 1.1 christos #define ABI_64_P(abfd) \
51 1.1 christos (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
52 1.1 christos
53 1.1 christos /* The relocation "howto" table. Order of fields:
54 1.1 christos type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
55 1.1 christos special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
56 1.1 christos static reloc_howto_type x86_64_elf_howto_table[] =
57 1.1 christos {
58 1.1.1.5 christos HOWTO(R_X86_64_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
59 1.1 christos bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
60 1.1 christos FALSE),
61 1.1 christos HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 1.1 christos bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
63 1.1 christos FALSE),
64 1.1 christos HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
65 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
66 1.1 christos TRUE),
67 1.1 christos HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
68 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
69 1.1 christos FALSE),
70 1.1 christos HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
71 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
72 1.1 christos TRUE),
73 1.1 christos HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
74 1.1 christos bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
75 1.1 christos FALSE),
76 1.1 christos HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
77 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
78 1.1 christos MINUS_ONE, FALSE),
79 1.1 christos HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
80 1.1 christos bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
81 1.1 christos MINUS_ONE, FALSE),
82 1.1 christos HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 1.1 christos bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
84 1.1 christos MINUS_ONE, FALSE),
85 1.1 christos HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
86 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
87 1.1 christos 0xffffffff, TRUE),
88 1.1 christos HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
89 1.1 christos bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
90 1.1 christos FALSE),
91 1.1 christos HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
92 1.1 christos bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
93 1.1 christos FALSE),
94 1.1 christos HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
95 1.1 christos bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
96 1.1 christos HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
97 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
98 1.1 christos HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
99 1.1 christos bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
100 1.1 christos HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
101 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
102 1.1 christos HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
103 1.1 christos bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
104 1.1 christos MINUS_ONE, FALSE),
105 1.1 christos HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
106 1.1 christos bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
107 1.1 christos MINUS_ONE, FALSE),
108 1.1 christos HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
109 1.1 christos bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
110 1.1 christos MINUS_ONE, FALSE),
111 1.1 christos HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
112 1.1 christos bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
113 1.1 christos 0xffffffff, TRUE),
114 1.1 christos HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 1.1 christos bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
116 1.1 christos 0xffffffff, TRUE),
117 1.1 christos HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
118 1.1 christos bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
119 1.1 christos 0xffffffff, FALSE),
120 1.1 christos HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
121 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
122 1.1 christos 0xffffffff, TRUE),
123 1.1 christos HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
124 1.1 christos bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
125 1.1 christos 0xffffffff, FALSE),
126 1.1 christos HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
127 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
128 1.1 christos TRUE),
129 1.1 christos HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
130 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
131 1.1 christos FALSE, MINUS_ONE, MINUS_ONE, FALSE),
132 1.1 christos HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
133 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
134 1.1 christos FALSE, 0xffffffff, 0xffffffff, TRUE),
135 1.1 christos HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
136 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
137 1.1 christos FALSE),
138 1.1 christos HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
139 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
140 1.1 christos MINUS_ONE, TRUE),
141 1.1 christos HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
142 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
143 1.1 christos FALSE, MINUS_ONE, MINUS_ONE, TRUE),
144 1.1 christos HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
145 1.1 christos bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
146 1.1 christos MINUS_ONE, FALSE),
147 1.1 christos HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
148 1.1 christos bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
149 1.1 christos MINUS_ONE, FALSE),
150 1.1.1.2 christos HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
151 1.1.1.2 christos bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
152 1.1.1.2 christos FALSE),
153 1.1.1.2 christos HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
154 1.1.1.2 christos bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
155 1.1.1.2 christos FALSE),
156 1.1 christos HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
157 1.1 christos complain_overflow_bitfield, bfd_elf_generic_reloc,
158 1.1 christos "R_X86_64_GOTPC32_TLSDESC",
159 1.1 christos FALSE, 0xffffffff, 0xffffffff, TRUE),
160 1.1 christos HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
161 1.1 christos complain_overflow_dont, bfd_elf_generic_reloc,
162 1.1 christos "R_X86_64_TLSDESC_CALL",
163 1.1 christos FALSE, 0, 0, FALSE),
164 1.1 christos HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
165 1.1 christos complain_overflow_bitfield, bfd_elf_generic_reloc,
166 1.1 christos "R_X86_64_TLSDESC",
167 1.1 christos FALSE, MINUS_ONE, MINUS_ONE, FALSE),
168 1.1 christos HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
169 1.1 christos bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
170 1.1 christos MINUS_ONE, FALSE),
171 1.1.1.2 christos HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
172 1.1.1.2 christos bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
173 1.1.1.2 christos MINUS_ONE, FALSE),
174 1.1.1.3 christos HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
175 1.1.1.3 christos bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
176 1.1.1.3 christos TRUE),
177 1.1.1.3 christos HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
178 1.1.1.3 christos bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
179 1.1.1.3 christos TRUE),
180 1.1.1.6 christos HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
181 1.1.1.6 christos bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", FALSE, 0xffffffff,
182 1.1.1.6 christos 0xffffffff, TRUE),
183 1.1.1.6 christos HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
184 1.1.1.6 christos bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", FALSE, 0xffffffff,
185 1.1.1.6 christos 0xffffffff, TRUE),
186 1.1 christos
187 1.1 christos /* We have a gap in the reloc numbers here.
188 1.1 christos R_X86_64_standard counts the number up to this point, and
189 1.1 christos R_X86_64_vt_offset is the value to subtract from a reloc type of
190 1.1 christos R_X86_64_GNU_VT* to form an index into this table. */
191 1.1.1.6 christos #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
192 1.1 christos #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
193 1.1 christos
194 1.1 christos /* GNU extension to record C++ vtable hierarchy. */
195 1.1 christos HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
196 1.1 christos NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
197 1.1 christos
198 1.1 christos /* GNU extension to record C++ vtable member usage. */
199 1.1 christos HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
200 1.1 christos _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
201 1.1.1.2 christos FALSE),
202 1.1.1.2 christos
203 1.1.1.2 christos /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
204 1.1.1.2 christos HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
205 1.1.1.2 christos bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
206 1.1.1.2 christos FALSE)
207 1.1 christos };
208 1.1 christos
209 1.1 christos #define IS_X86_64_PCREL_TYPE(TYPE) \
210 1.1 christos ( ((TYPE) == R_X86_64_PC8) \
211 1.1 christos || ((TYPE) == R_X86_64_PC16) \
212 1.1 christos || ((TYPE) == R_X86_64_PC32) \
213 1.1.1.3 christos || ((TYPE) == R_X86_64_PC32_BND) \
214 1.1 christos || ((TYPE) == R_X86_64_PC64))
215 1.1 christos
216 1.1 christos /* Map BFD relocs to the x86_64 elf relocs. */
217 1.1 christos struct elf_reloc_map
218 1.1 christos {
219 1.1 christos bfd_reloc_code_real_type bfd_reloc_val;
220 1.1 christos unsigned char elf_reloc_val;
221 1.1 christos };
222 1.1 christos
223 1.1 christos static const struct elf_reloc_map x86_64_reloc_map[] =
224 1.1 christos {
225 1.1 christos { BFD_RELOC_NONE, R_X86_64_NONE, },
226 1.1 christos { BFD_RELOC_64, R_X86_64_64, },
227 1.1 christos { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
228 1.1 christos { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
229 1.1 christos { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
230 1.1 christos { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
231 1.1 christos { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
232 1.1 christos { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
233 1.1 christos { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
234 1.1 christos { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
235 1.1 christos { BFD_RELOC_32, R_X86_64_32, },
236 1.1 christos { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
237 1.1 christos { BFD_RELOC_16, R_X86_64_16, },
238 1.1 christos { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
239 1.1 christos { BFD_RELOC_8, R_X86_64_8, },
240 1.1 christos { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
241 1.1 christos { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
242 1.1 christos { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
243 1.1 christos { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
244 1.1 christos { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
245 1.1 christos { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
246 1.1 christos { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
247 1.1 christos { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
248 1.1 christos { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
249 1.1 christos { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
250 1.1 christos { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
251 1.1 christos { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
252 1.1 christos { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
253 1.1 christos { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
254 1.1 christos { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
255 1.1 christos { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
256 1.1 christos { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
257 1.1.1.2 christos { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
258 1.1.1.2 christos { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
259 1.1 christos { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
260 1.1 christos { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
261 1.1 christos { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
262 1.1 christos { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
263 1.1.1.6 christos { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
264 1.1.1.6 christos { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
265 1.1.1.6 christos { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
266 1.1.1.6 christos { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
267 1.1 christos { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
268 1.1 christos { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
269 1.1 christos };
270 1.1 christos
271 1.1 christos static reloc_howto_type *
272 1.1 christos elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
273 1.1 christos {
274 1.1 christos unsigned i;
275 1.1 christos
276 1.1.1.2 christos if (r_type == (unsigned int) R_X86_64_32)
277 1.1.1.2 christos {
278 1.1.1.2 christos if (ABI_64_P (abfd))
279 1.1.1.2 christos i = r_type;
280 1.1.1.2 christos else
281 1.1.1.2 christos i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
282 1.1.1.2 christos }
283 1.1.1.2 christos else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
284 1.1.1.2 christos || r_type >= (unsigned int) R_X86_64_max)
285 1.1 christos {
286 1.1 christos if (r_type >= (unsigned int) R_X86_64_standard)
287 1.1 christos {
288 1.1.1.7 christos /* xgettext:c-format */
289 1.1.1.7 christos _bfd_error_handler (_("%B: invalid relocation type %d"),
290 1.1.1.7 christos abfd, (int) r_type);
291 1.1 christos r_type = R_X86_64_NONE;
292 1.1 christos }
293 1.1 christos i = r_type;
294 1.1 christos }
295 1.1 christos else
296 1.1 christos i = r_type - (unsigned int) R_X86_64_vt_offset;
297 1.1 christos BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
298 1.1 christos return &x86_64_elf_howto_table[i];
299 1.1 christos }
300 1.1 christos
301 1.1 christos /* Given a BFD reloc type, return a HOWTO structure. */
302 1.1 christos static reloc_howto_type *
303 1.1 christos elf_x86_64_reloc_type_lookup (bfd *abfd,
304 1.1 christos bfd_reloc_code_real_type code)
305 1.1 christos {
306 1.1 christos unsigned int i;
307 1.1 christos
308 1.1 christos for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
309 1.1 christos i++)
310 1.1 christos {
311 1.1 christos if (x86_64_reloc_map[i].bfd_reloc_val == code)
312 1.1 christos return elf_x86_64_rtype_to_howto (abfd,
313 1.1 christos x86_64_reloc_map[i].elf_reloc_val);
314 1.1 christos }
315 1.1.1.4 christos return NULL;
316 1.1 christos }
317 1.1 christos
318 1.1 christos static reloc_howto_type *
319 1.1.1.2 christos elf_x86_64_reloc_name_lookup (bfd *abfd,
320 1.1 christos const char *r_name)
321 1.1 christos {
322 1.1 christos unsigned int i;
323 1.1 christos
324 1.1.1.2 christos if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
325 1.1.1.2 christos {
326 1.1.1.2 christos /* Get x32 R_X86_64_32. */
327 1.1.1.2 christos reloc_howto_type *reloc
328 1.1.1.2 christos = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
329 1.1.1.2 christos BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
330 1.1.1.2 christos return reloc;
331 1.1.1.2 christos }
332 1.1.1.2 christos
333 1.1.1.2 christos for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
334 1.1 christos if (x86_64_elf_howto_table[i].name != NULL
335 1.1 christos && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
336 1.1 christos return &x86_64_elf_howto_table[i];
337 1.1 christos
338 1.1 christos return NULL;
339 1.1 christos }
340 1.1 christos
341 1.1 christos /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
342 1.1 christos
343 1.1 christos static void
344 1.1 christos elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
345 1.1 christos Elf_Internal_Rela *dst)
346 1.1 christos {
347 1.1 christos unsigned r_type;
348 1.1 christos
349 1.1 christos r_type = ELF32_R_TYPE (dst->r_info);
350 1.1 christos cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
351 1.1 christos BFD_ASSERT (r_type == cache_ptr->howto->type);
352 1.1 christos }
353 1.1 christos
354 1.1 christos /* Support for core dump NOTE sections. */
356 1.1 christos static bfd_boolean
357 1.1 christos elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
358 1.1 christos {
359 1.1 christos int offset;
360 1.1 christos size_t size;
361 1.1 christos
362 1.1 christos switch (note->descsz)
363 1.1 christos {
364 1.1 christos default:
365 1.1 christos return FALSE;
366 1.1.1.2 christos
367 1.1.1.2 christos case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
368 1.1.1.2 christos /* pr_cursig */
369 1.1.1.2 christos elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
370 1.1.1.2 christos
371 1.1.1.2 christos /* pr_pid */
372 1.1.1.2 christos elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
373 1.1.1.2 christos
374 1.1.1.2 christos /* pr_reg */
375 1.1.1.2 christos offset = 72;
376 1.1.1.2 christos size = 216;
377 1.1.1.2 christos
378 1.1.1.2 christos break;
379 1.1 christos
380 1.1 christos case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
381 1.1.1.2 christos /* pr_cursig */
382 1.1 christos elf_tdata (abfd)->core->signal
383 1.1 christos = bfd_get_16 (abfd, note->descdata + 12);
384 1.1 christos
385 1.1.1.2 christos /* pr_pid */
386 1.1 christos elf_tdata (abfd)->core->lwpid
387 1.1 christos = bfd_get_32 (abfd, note->descdata + 32);
388 1.1 christos
389 1.1 christos /* pr_reg */
390 1.1 christos offset = 112;
391 1.1 christos size = 216;
392 1.1 christos
393 1.1 christos break;
394 1.1 christos }
395 1.1 christos
396 1.1 christos /* Make a ".reg/999" section. */
397 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg",
398 1.1 christos size, note->descpos + offset);
399 1.1 christos }
400 1.1 christos
401 1.1 christos static bfd_boolean
402 1.1 christos elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
403 1.1 christos {
404 1.1 christos switch (note->descsz)
405 1.1 christos {
406 1.1 christos default:
407 1.1 christos return FALSE;
408 1.1.1.2 christos
409 1.1.1.2 christos case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
410 1.1.1.2 christos elf_tdata (abfd)->core->pid
411 1.1.1.2 christos = bfd_get_32 (abfd, note->descdata + 12);
412 1.1.1.2 christos elf_tdata (abfd)->core->program
413 1.1.1.2 christos = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
414 1.1.1.2 christos elf_tdata (abfd)->core->command
415 1.1.1.2 christos = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
416 1.1.1.2 christos break;
417 1.1 christos
418 1.1.1.2 christos case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
419 1.1 christos elf_tdata (abfd)->core->pid
420 1.1.1.2 christos = bfd_get_32 (abfd, note->descdata + 24);
421 1.1 christos elf_tdata (abfd)->core->program
422 1.1.1.2 christos = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
423 1.1 christos elf_tdata (abfd)->core->command
424 1.1 christos = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
425 1.1 christos }
426 1.1 christos
427 1.1 christos /* Note that for some reason, a spurious space is tacked
428 1.1 christos onto the end of the args in some (at least one anyway)
429 1.1 christos implementations, so strip it off if it exists. */
430 1.1 christos
431 1.1.1.2 christos {
432 1.1 christos char *command = elf_tdata (abfd)->core->command;
433 1.1 christos int n = strlen (command);
434 1.1 christos
435 1.1 christos if (0 < n && command[n - 1] == ' ')
436 1.1 christos command[n - 1] = '\0';
437 1.1 christos }
438 1.1 christos
439 1.1 christos return TRUE;
440 1.1.1.2 christos }
441 1.1.1.2 christos
442 1.1.1.2 christos #ifdef CORE_HEADER
443 1.1.1.2 christos static char *
444 1.1.1.2 christos elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
445 1.1.1.2 christos int note_type, ...)
446 1.1.1.2 christos {
447 1.1.1.2 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
448 1.1.1.2 christos va_list ap;
449 1.1.1.2 christos const char *fname, *psargs;
450 1.1.1.2 christos long pid;
451 1.1.1.2 christos int cursig;
452 1.1.1.2 christos const void *gregs;
453 1.1.1.2 christos
454 1.1.1.2 christos switch (note_type)
455 1.1.1.2 christos {
456 1.1.1.2 christos default:
457 1.1.1.2 christos return NULL;
458 1.1.1.2 christos
459 1.1.1.2 christos case NT_PRPSINFO:
460 1.1.1.2 christos va_start (ap, note_type);
461 1.1.1.2 christos fname = va_arg (ap, const char *);
462 1.1.1.2 christos psargs = va_arg (ap, const char *);
463 1.1.1.2 christos va_end (ap);
464 1.1.1.2 christos
465 1.1.1.2 christos if (bed->s->elfclass == ELFCLASS32)
466 1.1.1.2 christos {
467 1.1.1.2 christos prpsinfo32_t data;
468 1.1.1.2 christos memset (&data, 0, sizeof (data));
469 1.1.1.2 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
470 1.1.1.2 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
471 1.1.1.2 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
472 1.1.1.2 christos &data, sizeof (data));
473 1.1.1.2 christos }
474 1.1.1.2 christos else
475 1.1.1.2 christos {
476 1.1.1.2 christos prpsinfo64_t data;
477 1.1.1.2 christos memset (&data, 0, sizeof (data));
478 1.1.1.2 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
479 1.1.1.2 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
480 1.1.1.2 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
481 1.1.1.2 christos &data, sizeof (data));
482 1.1.1.2 christos }
483 1.1.1.2 christos /* NOTREACHED */
484 1.1.1.2 christos
485 1.1.1.2 christos case NT_PRSTATUS:
486 1.1.1.2 christos va_start (ap, note_type);
487 1.1.1.2 christos pid = va_arg (ap, long);
488 1.1.1.2 christos cursig = va_arg (ap, int);
489 1.1.1.2 christos gregs = va_arg (ap, const void *);
490 1.1.1.2 christos va_end (ap);
491 1.1.1.2 christos
492 1.1.1.2 christos if (bed->s->elfclass == ELFCLASS32)
493 1.1.1.2 christos {
494 1.1.1.2 christos if (bed->elf_machine_code == EM_X86_64)
495 1.1.1.2 christos {
496 1.1.1.2 christos prstatusx32_t prstat;
497 1.1.1.2 christos memset (&prstat, 0, sizeof (prstat));
498 1.1.1.2 christos prstat.pr_pid = pid;
499 1.1.1.2 christos prstat.pr_cursig = cursig;
500 1.1.1.2 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
501 1.1.1.2 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
502 1.1.1.2 christos &prstat, sizeof (prstat));
503 1.1.1.2 christos }
504 1.1.1.2 christos else
505 1.1.1.2 christos {
506 1.1.1.2 christos prstatus32_t prstat;
507 1.1.1.2 christos memset (&prstat, 0, sizeof (prstat));
508 1.1.1.2 christos prstat.pr_pid = pid;
509 1.1.1.2 christos prstat.pr_cursig = cursig;
510 1.1.1.2 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
511 1.1.1.2 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
512 1.1.1.2 christos &prstat, sizeof (prstat));
513 1.1.1.2 christos }
514 1.1.1.2 christos }
515 1.1.1.2 christos else
516 1.1.1.2 christos {
517 1.1.1.2 christos prstatus64_t prstat;
518 1.1.1.2 christos memset (&prstat, 0, sizeof (prstat));
519 1.1.1.2 christos prstat.pr_pid = pid;
520 1.1.1.2 christos prstat.pr_cursig = cursig;
521 1.1.1.2 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
522 1.1.1.2 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
523 1.1.1.2 christos &prstat, sizeof (prstat));
524 1.1.1.2 christos }
525 1.1.1.2 christos }
526 1.1.1.2 christos /* NOTREACHED */
527 1.1.1.2 christos }
528 1.1 christos #endif
529 1.1 christos
530 1.1 christos /* Functions for the x86-64 ELF linker. */
532 1.1 christos
533 1.1 christos /* The name of the dynamic interpreter. This is put in the .interp
534 1.1 christos section. */
535 1.1.1.2 christos
536 1.1 christos #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
537 1.1 christos #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
538 1.1 christos
539 1.1 christos /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
540 1.1 christos copying dynamic variables from a shared lib into an app's dynbss
541 1.1 christos section, and instead use a dynamic relocation to point into the
542 1.1 christos shared lib. */
543 1.1 christos #define ELIMINATE_COPY_RELOCS 1
544 1.1 christos
545 1.1 christos /* The size in bytes of an entry in the global offset table. */
546 1.1 christos
547 1.1 christos #define GOT_ENTRY_SIZE 8
548 1.1 christos
549 1.1 christos /* The size in bytes of an entry in the procedure linkage table. */
550 1.1 christos
551 1.1 christos #define PLT_ENTRY_SIZE 16
552 1.1 christos
553 1.1 christos /* The first entry in a procedure linkage table looks like this. See the
554 1.1 christos SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
555 1.1 christos
556 1.1 christos static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
557 1.1 christos {
558 1.1 christos 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
559 1.1 christos 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
560 1.1 christos 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
561 1.1 christos };
562 1.1 christos
563 1.1 christos /* Subsequent entries in a procedure linkage table look like this. */
564 1.1 christos
565 1.1 christos static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
566 1.1 christos {
567 1.1 christos 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
568 1.1 christos 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
569 1.1 christos 0x68, /* pushq immediate */
570 1.1 christos 0, 0, 0, 0, /* replaced with index into relocation table. */
571 1.1 christos 0xe9, /* jmp relative */
572 1.1 christos 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
573 1.1.1.4 christos };
574 1.1.1.4 christos
575 1.1.1.4 christos /* The first entry in a procedure linkage table with BND relocations
576 1.1.1.4 christos like this. */
577 1.1.1.4 christos
578 1.1.1.4 christos static const bfd_byte elf_x86_64_bnd_plt0_entry[PLT_ENTRY_SIZE] =
579 1.1.1.4 christos {
580 1.1.1.4 christos 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
581 1.1.1.4 christos 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
582 1.1.1.4 christos 0x0f, 0x1f, 0 /* nopl (%rax) */
583 1.1.1.4 christos };
584 1.1.1.4 christos
585 1.1.1.4 christos /* Subsequent entries for legacy branches in a procedure linkage table
586 1.1.1.4 christos with BND relocations look like this. */
587 1.1.1.4 christos
588 1.1.1.4 christos static const bfd_byte elf_x86_64_legacy_plt_entry[PLT_ENTRY_SIZE] =
589 1.1.1.4 christos {
590 1.1.1.4 christos 0x68, 0, 0, 0, 0, /* pushq immediate */
591 1.1.1.4 christos 0xe9, 0, 0, 0, 0, /* jmpq relative */
592 1.1.1.4 christos 0x66, 0x0f, 0x1f, 0x44, 0, 0 /* nopw (%rax,%rax,1) */
593 1.1.1.4 christos };
594 1.1.1.4 christos
595 1.1.1.4 christos /* Subsequent entries for branches with BND prefx in a procedure linkage
596 1.1.1.4 christos table with BND relocations look like this. */
597 1.1.1.4 christos
598 1.1.1.4 christos static const bfd_byte elf_x86_64_bnd_plt_entry[PLT_ENTRY_SIZE] =
599 1.1.1.4 christos {
600 1.1.1.4 christos 0x68, 0, 0, 0, 0, /* pushq immediate */
601 1.1.1.4 christos 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
602 1.1.1.4 christos 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
603 1.1.1.4 christos };
604 1.1.1.4 christos
605 1.1.1.4 christos /* Entries for legacy branches in the second procedure linkage table
606 1.1.1.4 christos look like this. */
607 1.1.1.4 christos
608 1.1.1.4 christos static const bfd_byte elf_x86_64_legacy_plt2_entry[8] =
609 1.1.1.4 christos {
610 1.1.1.4 christos 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
611 1.1.1.4 christos 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
612 1.1.1.4 christos 0x66, 0x90 /* xchg %ax,%ax */
613 1.1.1.4 christos };
614 1.1.1.4 christos
615 1.1.1.4 christos /* Entries for branches with BND prefix in the second procedure linkage
616 1.1.1.4 christos table look like this. */
617 1.1.1.4 christos
618 1.1.1.4 christos static const bfd_byte elf_x86_64_bnd_plt2_entry[8] =
619 1.1.1.4 christos {
620 1.1.1.4 christos 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
621 1.1.1.4 christos 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
622 1.1.1.4 christos 0x90 /* nop */
623 1.1.1.2 christos };
624 1.1.1.2 christos
625 1.1.1.2 christos /* .eh_frame covering the .plt section. */
626 1.1.1.2 christos
627 1.1.1.2 christos static const bfd_byte elf_x86_64_eh_frame_plt[] =
628 1.1.1.2 christos {
629 1.1.1.2 christos #define PLT_CIE_LENGTH 20
630 1.1.1.2 christos #define PLT_FDE_LENGTH 36
631 1.1.1.2 christos #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
632 1.1.1.2 christos #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
633 1.1.1.2 christos PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
634 1.1.1.2 christos 0, 0, 0, 0, /* CIE ID */
635 1.1.1.2 christos 1, /* CIE version */
636 1.1.1.2 christos 'z', 'R', 0, /* Augmentation string */
637 1.1.1.2 christos 1, /* Code alignment factor */
638 1.1.1.2 christos 0x78, /* Data alignment factor */
639 1.1.1.2 christos 16, /* Return address column */
640 1.1.1.2 christos 1, /* Augmentation size */
641 1.1.1.2 christos DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
642 1.1.1.2 christos DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
643 1.1.1.2 christos DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
644 1.1.1.2 christos DW_CFA_nop, DW_CFA_nop,
645 1.1.1.2 christos
646 1.1.1.2 christos PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
647 1.1.1.2 christos PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
648 1.1.1.2 christos 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
649 1.1.1.2 christos 0, 0, 0, 0, /* .plt size goes here */
650 1.1.1.2 christos 0, /* Augmentation size */
651 1.1.1.2 christos DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
652 1.1.1.2 christos DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
653 1.1.1.2 christos DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
654 1.1.1.2 christos DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
655 1.1.1.2 christos DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
656 1.1.1.2 christos 11, /* Block length */
657 1.1.1.2 christos DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
658 1.1.1.2 christos DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
659 1.1.1.2 christos DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
660 1.1.1.2 christos DW_OP_lit3, DW_OP_shl, DW_OP_plus,
661 1.1.1.2 christos DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
662 1.1.1.7 christos };
663 1.1.1.7 christos
664 1.1.1.7 christos /* .eh_frame covering the BND .plt section. */
665 1.1.1.7 christos
666 1.1.1.7 christos static const bfd_byte elf_x86_64_eh_frame_bnd_plt[] =
667 1.1.1.7 christos {
668 1.1.1.7 christos PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
669 1.1.1.7 christos 0, 0, 0, 0, /* CIE ID */
670 1.1.1.7 christos 1, /* CIE version */
671 1.1.1.7 christos 'z', 'R', 0, /* Augmentation string */
672 1.1.1.7 christos 1, /* Code alignment factor */
673 1.1.1.7 christos 0x78, /* Data alignment factor */
674 1.1.1.7 christos 16, /* Return address column */
675 1.1.1.7 christos 1, /* Augmentation size */
676 1.1.1.7 christos DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
677 1.1.1.7 christos DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
678 1.1.1.7 christos DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
679 1.1.1.7 christos DW_CFA_nop, DW_CFA_nop,
680 1.1.1.7 christos
681 1.1.1.7 christos PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
682 1.1.1.7 christos PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
683 1.1.1.7 christos 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
684 1.1.1.7 christos 0, 0, 0, 0, /* .plt size goes here */
685 1.1.1.7 christos 0, /* Augmentation size */
686 1.1.1.7 christos DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
687 1.1.1.7 christos DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
688 1.1.1.7 christos DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
689 1.1.1.7 christos DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
690 1.1.1.7 christos DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
691 1.1.1.7 christos 11, /* Block length */
692 1.1.1.7 christos DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
693 1.1.1.7 christos DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
694 1.1.1.7 christos DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
695 1.1.1.7 christos DW_OP_lit3, DW_OP_shl, DW_OP_plus,
696 1.1.1.7 christos DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
697 1.1.1.7 christos };
698 1.1.1.7 christos
699 1.1.1.7 christos /* .eh_frame covering the .plt.got section. */
700 1.1.1.7 christos
701 1.1.1.7 christos static const bfd_byte elf_x86_64_eh_frame_plt_got[] =
702 1.1.1.7 christos {
703 1.1.1.7 christos #define PLT_GOT_FDE_LENGTH 20
704 1.1.1.7 christos PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
705 1.1.1.7 christos 0, 0, 0, 0, /* CIE ID */
706 1.1.1.7 christos 1, /* CIE version */
707 1.1.1.7 christos 'z', 'R', 0, /* Augmentation string */
708 1.1.1.7 christos 1, /* Code alignment factor */
709 1.1.1.7 christos 0x78, /* Data alignment factor */
710 1.1.1.7 christos 16, /* Return address column */
711 1.1.1.7 christos 1, /* Augmentation size */
712 1.1.1.7 christos DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
713 1.1.1.7 christos DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
714 1.1.1.7 christos DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
715 1.1.1.7 christos DW_CFA_nop, DW_CFA_nop,
716 1.1.1.7 christos
717 1.1.1.7 christos PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
718 1.1.1.7 christos PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
719 1.1.1.7 christos 0, 0, 0, 0, /* the start of .plt.got goes here */
720 1.1.1.7 christos 0, 0, 0, 0, /* .plt.got size goes here */
721 1.1.1.7 christos 0, /* Augmentation size */
722 1.1.1.7 christos DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
723 1.1.1.7 christos DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
724 1.1.1.2 christos };
725 1.1.1.2 christos
726 1.1.1.2 christos /* Architecture-specific backend data for x86-64. */
727 1.1.1.2 christos
728 1.1.1.2 christos struct elf_x86_64_backend_data
729 1.1.1.2 christos {
730 1.1.1.2 christos /* Templates for the initial PLT entry and for subsequent entries. */
731 1.1.1.2 christos const bfd_byte *plt0_entry;
732 1.1.1.2 christos const bfd_byte *plt_entry;
733 1.1.1.2 christos unsigned int plt_entry_size; /* Size of each PLT entry. */
734 1.1.1.2 christos
735 1.1.1.2 christos /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
736 1.1.1.2 christos unsigned int plt0_got1_offset;
737 1.1.1.2 christos unsigned int plt0_got2_offset;
738 1.1.1.2 christos
739 1.1.1.2 christos /* Offset of the end of the PC-relative instruction containing
740 1.1.1.2 christos plt0_got2_offset. */
741 1.1.1.2 christos unsigned int plt0_got2_insn_end;
742 1.1.1.2 christos
743 1.1.1.2 christos /* Offsets into plt_entry that are to be replaced with... */
744 1.1.1.2 christos unsigned int plt_got_offset; /* ... address of this symbol in .got. */
745 1.1.1.2 christos unsigned int plt_reloc_offset; /* ... offset into relocation table. */
746 1.1.1.2 christos unsigned int plt_plt_offset; /* ... offset to start of .plt. */
747 1.1.1.2 christos
748 1.1.1.2 christos /* Length of the PC-relative instruction containing plt_got_offset. */
749 1.1.1.2 christos unsigned int plt_got_insn_size;
750 1.1.1.2 christos
751 1.1.1.2 christos /* Offset of the end of the PC-relative jump to plt0_entry. */
752 1.1.1.2 christos unsigned int plt_plt_insn_end;
753 1.1.1.2 christos
754 1.1.1.2 christos /* Offset into plt_entry where the initial value of the GOT entry points. */
755 1.1.1.2 christos unsigned int plt_lazy_offset;
756 1.1.1.2 christos
757 1.1.1.2 christos /* .eh_frame covering the .plt section. */
758 1.1.1.7 christos const bfd_byte *eh_frame_plt;
759 1.1.1.7 christos unsigned int eh_frame_plt_size;
760 1.1.1.7 christos
761 1.1.1.7 christos /* .eh_frame covering the .plt.got section. */
762 1.1.1.2 christos const bfd_byte *eh_frame_plt_got;
763 1.1.1.2 christos unsigned int eh_frame_plt_got_size;
764 1.1.1.3 christos };
765 1.1.1.3 christos
766 1.1.1.3 christos #define get_elf_x86_64_arch_data(bed) \
767 1.1.1.2 christos ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
768 1.1.1.3 christos
769 1.1.1.2 christos #define get_elf_x86_64_backend_data(abfd) \
770 1.1.1.2 christos get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
771 1.1.1.2 christos
772 1.1.1.2 christos #define GET_PLT_ENTRY_SIZE(abfd) \
773 1.1.1.2 christos get_elf_x86_64_backend_data (abfd)->plt_entry_size
774 1.1.1.2 christos
775 1.1.1.2 christos /* These are the standard parameters. */
776 1.1.1.2 christos static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
777 1.1.1.2 christos {
778 1.1.1.2 christos elf_x86_64_plt0_entry, /* plt0_entry */
779 1.1.1.2 christos elf_x86_64_plt_entry, /* plt_entry */
780 1.1.1.2 christos sizeof (elf_x86_64_plt_entry), /* plt_entry_size */
781 1.1.1.2 christos 2, /* plt0_got1_offset */
782 1.1.1.2 christos 8, /* plt0_got2_offset */
783 1.1.1.2 christos 12, /* plt0_got2_insn_end */
784 1.1.1.2 christos 2, /* plt_got_offset */
785 1.1.1.2 christos 7, /* plt_reloc_offset */
786 1.1.1.2 christos 12, /* plt_plt_offset */
787 1.1.1.2 christos 6, /* plt_got_insn_size */
788 1.1.1.2 christos PLT_ENTRY_SIZE, /* plt_plt_insn_end */
789 1.1.1.2 christos 6, /* plt_lazy_offset */
790 1.1.1.7 christos elf_x86_64_eh_frame_plt, /* eh_frame_plt */
791 1.1.1.7 christos sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
792 1.1.1.2 christos elf_x86_64_eh_frame_plt_got, /* eh_frame_plt_got */
793 1.1.1.2 christos sizeof (elf_x86_64_eh_frame_plt_got), /* eh_frame_plt_got_size */
794 1.1.1.4 christos };
795 1.1.1.4 christos
796 1.1.1.4 christos static const struct elf_x86_64_backend_data elf_x86_64_bnd_arch_bed =
797 1.1.1.4 christos {
798 1.1.1.4 christos elf_x86_64_bnd_plt0_entry, /* plt0_entry */
799 1.1.1.4 christos elf_x86_64_bnd_plt_entry, /* plt_entry */
800 1.1.1.4 christos sizeof (elf_x86_64_bnd_plt_entry), /* plt_entry_size */
801 1.1.1.4 christos 2, /* plt0_got1_offset */
802 1.1.1.4 christos 1+8, /* plt0_got2_offset */
803 1.1.1.4 christos 1+12, /* plt0_got2_insn_end */
804 1.1.1.4 christos 1+2, /* plt_got_offset */
805 1.1.1.4 christos 1, /* plt_reloc_offset */
806 1.1.1.4 christos 7, /* plt_plt_offset */
807 1.1.1.4 christos 1+6, /* plt_got_insn_size */
808 1.1.1.7 christos 11, /* plt_plt_insn_end */
809 1.1.1.7 christos 0, /* plt_lazy_offset */
810 1.1.1.7 christos elf_x86_64_eh_frame_bnd_plt, /* eh_frame_plt */
811 1.1.1.7 christos sizeof (elf_x86_64_eh_frame_bnd_plt), /* eh_frame_plt_size */
812 1.1.1.4 christos elf_x86_64_eh_frame_plt_got, /* eh_frame_plt_got */
813 1.1.1.4 christos sizeof (elf_x86_64_eh_frame_plt_got), /* eh_frame_plt_got_size */
814 1.1.1.2 christos };
815 1.1.1.2 christos
816 1.1.1.6 christos #define elf_backend_arch_data &elf_x86_64_arch_bed
817 1.1.1.6 christos
818 1.1.1.6 christos /* Is a undefined weak symbol which is resolved to 0. Reference to an
819 1.1.1.6 christos undefined weak symbol is resolved to 0 when building executable if
820 1.1.1.6 christos it isn't dynamic and
821 1.1.1.6 christos 1. Has non-GOT/non-PLT relocations in text section. Or
822 1.1.1.6 christos 2. Has no GOT/PLT relocation.
823 1.1.1.6 christos */
824 1.1.1.6 christos #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH) \
825 1.1.1.6 christos ((EH)->elf.root.type == bfd_link_hash_undefweak \
826 1.1.1.6 christos && bfd_link_executable (INFO) \
827 1.1.1.6 christos && (elf_x86_64_hash_table (INFO)->interp == NULL \
828 1.1.1.6 christos || !(GOT_RELOC) \
829 1.1.1.6 christos || (EH)->has_non_got_reloc \
830 1.1 christos || !(INFO)->dynamic_undefined_weak))
831 1.1 christos
832 1.1 christos /* x86-64 ELF linker hash entry. */
833 1.1 christos
834 1.1 christos struct elf_x86_64_link_hash_entry
835 1.1 christos {
836 1.1 christos struct elf_link_hash_entry elf;
837 1.1 christos
838 1.1 christos /* Track dynamic relocs copied for this symbol. */
839 1.1 christos struct elf_dyn_relocs *dyn_relocs;
840 1.1 christos
841 1.1 christos #define GOT_UNKNOWN 0
842 1.1 christos #define GOT_NORMAL 1
843 1.1 christos #define GOT_TLS_GD 2
844 1.1 christos #define GOT_TLS_IE 3
845 1.1 christos #define GOT_TLS_GDESC 4
846 1.1 christos #define GOT_TLS_GD_BOTH_P(type) \
847 1.1 christos ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
848 1.1 christos #define GOT_TLS_GD_P(type) \
849 1.1 christos ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
850 1.1 christos #define GOT_TLS_GDESC_P(type) \
851 1.1 christos ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
852 1.1 christos #define GOT_TLS_GD_ANY_P(type) \
853 1.1 christos (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
854 1.1.1.4 christos unsigned char tls_type;
855 1.1.1.4 christos
856 1.1.1.4 christos /* TRUE if a weak symbol with a real definition needs a copy reloc.
857 1.1.1.4 christos When there is a weak symbol with a real definition, the processor
858 1.1.1.4 christos independent code will have arranged for us to see the real
859 1.1.1.4 christos definition first. We need to copy the needs_copy bit from the
860 1.1.1.4 christos real definition and check it when allowing copy reloc in PIE. */
861 1.1.1.4 christos unsigned int needs_copy : 1;
862 1.1.1.4 christos
863 1.1.1.4 christos /* TRUE if symbol has at least one BND relocation. */
864 1.1.1.6 christos unsigned int has_bnd_reloc : 1;
865 1.1.1.6 christos
866 1.1.1.6 christos /* TRUE if symbol has GOT or PLT relocations. */
867 1.1.1.6 christos unsigned int has_got_reloc : 1;
868 1.1.1.6 christos
869 1.1.1.6 christos /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
870 1.1.1.6 christos unsigned int has_non_got_reloc : 1;
871 1.1.1.6 christos
872 1.1.1.6 christos /* 0: symbol isn't __tls_get_addr.
873 1.1.1.6 christos 1: symbol is __tls_get_addr.
874 1.1.1.6 christos 2: symbol is unknown. */
875 1.1.1.6 christos unsigned int tls_get_addr : 2;
876 1.1.1.6 christos
877 1.1.1.6 christos /* Reference count of C/C++ function pointer relocations in read-write
878 1.1.1.6 christos section which can be resolved at run-time. */
879 1.1.1.4 christos bfd_signed_vma func_pointer_refcount;
880 1.1.1.4 christos
881 1.1.1.4 christos /* Information about the GOT PLT entry. Filled when there are both
882 1.1.1.4 christos GOT and PLT relocations against the same function. */
883 1.1.1.4 christos union gotplt_union plt_got;
884 1.1.1.4 christos
885 1.1.1.4 christos /* Information about the second PLT entry. Filled when has_bnd_reloc is
886 1.1.1.4 christos set. */
887 1.1 christos union gotplt_union plt_bnd;
888 1.1 christos
889 1.1 christos /* Offset of the GOTPLT entry reserved for the TLS descriptor,
890 1.1 christos starting at the end of the jump table. */
891 1.1 christos bfd_vma tlsdesc_got;
892 1.1 christos };
893 1.1 christos
894 1.1 christos #define elf_x86_64_hash_entry(ent) \
895 1.1 christos ((struct elf_x86_64_link_hash_entry *)(ent))
896 1.1 christos
897 1.1 christos struct elf_x86_64_obj_tdata
898 1.1 christos {
899 1.1 christos struct elf_obj_tdata root;
900 1.1 christos
901 1.1 christos /* tls_type for each local got entry. */
902 1.1 christos char *local_got_tls_type;
903 1.1 christos
904 1.1 christos /* GOTPLT entries for TLS descriptors. */
905 1.1 christos bfd_vma *local_tlsdesc_gotent;
906 1.1 christos };
907 1.1 christos
908 1.1 christos #define elf_x86_64_tdata(abfd) \
909 1.1 christos ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
910 1.1 christos
911 1.1 christos #define elf_x86_64_local_got_tls_type(abfd) \
912 1.1 christos (elf_x86_64_tdata (abfd)->local_got_tls_type)
913 1.1 christos
914 1.1 christos #define elf_x86_64_local_tlsdesc_gotent(abfd) \
915 1.1 christos (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
916 1.1 christos
917 1.1 christos #define is_x86_64_elf(bfd) \
918 1.1 christos (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
919 1.1 christos && elf_tdata (bfd) != NULL \
920 1.1 christos && elf_object_id (bfd) == X86_64_ELF_DATA)
921 1.1 christos
922 1.1 christos static bfd_boolean
923 1.1 christos elf_x86_64_mkobject (bfd *abfd)
924 1.1 christos {
925 1.1 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
926 1.1 christos X86_64_ELF_DATA);
927 1.1 christos }
928 1.1 christos
929 1.1 christos /* x86-64 ELF linker hash table. */
930 1.1 christos
931 1.1 christos struct elf_x86_64_link_hash_table
932 1.1 christos {
933 1.1 christos struct elf_link_hash_table elf;
934 1.1.1.6 christos
935 1.1.1.2 christos /* Short-cuts to get to dynamic linker sections. */
936 1.1.1.4 christos asection *interp;
937 1.1.1.7 christos asection *plt_eh_frame;
938 1.1.1.4 christos asection *plt_bnd;
939 1.1.1.7 christos asection *plt_bnd_eh_frame;
940 1.1 christos asection *plt_got;
941 1.1 christos asection *plt_got_eh_frame;
942 1.1 christos
943 1.1 christos union
944 1.1 christos {
945 1.1 christos bfd_signed_vma refcount;
946 1.1 christos bfd_vma offset;
947 1.1 christos } tls_ld_got;
948 1.1 christos
949 1.1 christos /* The amount of space used by the jump slots in the GOT. */
950 1.1 christos bfd_vma sgotplt_jump_table_size;
951 1.1 christos
952 1.1 christos /* Small local sym cache. */
953 1.1 christos struct sym_cache sym_cache;
954 1.1 christos
955 1.1 christos bfd_vma (*r_info) (bfd_vma, bfd_vma);
956 1.1 christos bfd_vma (*r_sym) (bfd_vma);
957 1.1 christos unsigned int pointer_r_type;
958 1.1 christos const char *dynamic_interpreter;
959 1.1 christos int dynamic_interpreter_size;
960 1.1 christos
961 1.1 christos /* _TLS_MODULE_BASE_ symbol. */
962 1.1 christos struct bfd_link_hash_entry *tls_module_base;
963 1.1 christos
964 1.1 christos /* Used by local STT_GNU_IFUNC symbols. */
965 1.1 christos htab_t loc_hash_table;
966 1.1 christos void * loc_hash_memory;
967 1.1 christos
968 1.1 christos /* The offset into splt of the PLT entry for the TLS descriptor
969 1.1 christos resolver. Special values are 0, if not necessary (or not found
970 1.1 christos to be necessary yet), and -1 if needed but not determined
971 1.1 christos yet. */
972 1.1 christos bfd_vma tlsdesc_plt;
973 1.1 christos /* The offset into sgot of the GOT entry used by the PLT entry
974 1.1.1.2 christos above. */
975 1.1.1.2 christos bfd_vma tlsdesc_got;
976 1.1.1.2 christos
977 1.1.1.2 christos /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
978 1.1.1.2 christos bfd_vma next_jump_slot_index;
979 1.1.1.6 christos /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
980 1.1.1.6 christos bfd_vma next_irelative_index;
981 1.1.1.6 christos
982 1.1.1.6 christos /* TRUE if there are dynamic relocs against IFUNC symbols that apply
983 1.1 christos to read-only sections. */
984 1.1 christos bfd_boolean readonly_dynrelocs_against_ifunc;
985 1.1 christos };
986 1.1 christos
987 1.1 christos /* Get the x86-64 ELF linker hash table from a link_info structure. */
988 1.1 christos
989 1.1 christos #define elf_x86_64_hash_table(p) \
990 1.1 christos (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
991 1.1 christos == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
992 1.1 christos
993 1.1 christos #define elf_x86_64_compute_jump_table_size(htab) \
994 1.1 christos ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
995 1.1 christos
996 1.1 christos /* Create an entry in an x86-64 ELF linker hash table. */
997 1.1 christos
998 1.1 christos static struct bfd_hash_entry *
999 1.1 christos elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
1000 1.1 christos struct bfd_hash_table *table,
1001 1.1 christos const char *string)
1002 1.1 christos {
1003 1.1 christos /* Allocate the structure if it has not already been allocated by a
1004 1.1 christos subclass. */
1005 1.1 christos if (entry == NULL)
1006 1.1.1.2 christos {
1007 1.1.1.2 christos entry = (struct bfd_hash_entry *)
1008 1.1 christos bfd_hash_allocate (table,
1009 1.1 christos sizeof (struct elf_x86_64_link_hash_entry));
1010 1.1 christos if (entry == NULL)
1011 1.1 christos return entry;
1012 1.1 christos }
1013 1.1 christos
1014 1.1 christos /* Call the allocation method of the superclass. */
1015 1.1 christos entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1016 1.1 christos if (entry != NULL)
1017 1.1 christos {
1018 1.1 christos struct elf_x86_64_link_hash_entry *eh;
1019 1.1 christos
1020 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) entry;
1021 1.1.1.4 christos eh->dyn_relocs = NULL;
1022 1.1.1.4 christos eh->tls_type = GOT_UNKNOWN;
1023 1.1.1.6 christos eh->needs_copy = 0;
1024 1.1.1.6 christos eh->has_bnd_reloc = 0;
1025 1.1.1.6 christos eh->has_got_reloc = 0;
1026 1.1.1.6 christos eh->has_non_got_reloc = 0;
1027 1.1.1.4 christos eh->tls_get_addr = 2;
1028 1.1.1.4 christos eh->func_pointer_refcount = 0;
1029 1.1 christos eh->plt_bnd.offset = (bfd_vma) -1;
1030 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
1031 1.1 christos eh->tlsdesc_got = (bfd_vma) -1;
1032 1.1 christos }
1033 1.1 christos
1034 1.1 christos return entry;
1035 1.1 christos }
1036 1.1 christos
1037 1.1 christos /* Compute a hash of a local hash entry. We use elf_link_hash_entry
1038 1.1 christos for local symbol so that we can handle local STT_GNU_IFUNC symbols
1039 1.1 christos as global symbol. We reuse indx and dynstr_index for local symbol
1040 1.1 christos hash since they aren't used by global symbols in this backend. */
1041 1.1 christos
1042 1.1 christos static hashval_t
1043 1.1 christos elf_x86_64_local_htab_hash (const void *ptr)
1044 1.1 christos {
1045 1.1 christos struct elf_link_hash_entry *h
1046 1.1 christos = (struct elf_link_hash_entry *) ptr;
1047 1.1 christos return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
1048 1.1 christos }
1049 1.1 christos
1050 1.1 christos /* Compare local hash entries. */
1051 1.1 christos
1052 1.1 christos static int
1053 1.1 christos elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
1054 1.1 christos {
1055 1.1 christos struct elf_link_hash_entry *h1
1056 1.1 christos = (struct elf_link_hash_entry *) ptr1;
1057 1.1 christos struct elf_link_hash_entry *h2
1058 1.1 christos = (struct elf_link_hash_entry *) ptr2;
1059 1.1 christos
1060 1.1 christos return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
1061 1.1 christos }
1062 1.1 christos
1063 1.1 christos /* Find and/or create a hash entry for local symbol. */
1064 1.1 christos
1065 1.1 christos static struct elf_link_hash_entry *
1066 1.1 christos elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
1067 1.1 christos bfd *abfd, const Elf_Internal_Rela *rel,
1068 1.1 christos bfd_boolean create)
1069 1.1 christos {
1070 1.1 christos struct elf_x86_64_link_hash_entry e, *ret;
1071 1.1 christos asection *sec = abfd->sections;
1072 1.1 christos hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1073 1.1 christos htab->r_sym (rel->r_info));
1074 1.1 christos void **slot;
1075 1.1 christos
1076 1.1 christos e.elf.indx = sec->id;
1077 1.1 christos e.elf.dynstr_index = htab->r_sym (rel->r_info);
1078 1.1 christos slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1079 1.1 christos create ? INSERT : NO_INSERT);
1080 1.1 christos
1081 1.1 christos if (!slot)
1082 1.1 christos return NULL;
1083 1.1 christos
1084 1.1 christos if (*slot)
1085 1.1 christos {
1086 1.1 christos ret = (struct elf_x86_64_link_hash_entry *) *slot;
1087 1.1 christos return &ret->elf;
1088 1.1 christos }
1089 1.1 christos
1090 1.1 christos ret = (struct elf_x86_64_link_hash_entry *)
1091 1.1 christos objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
1092 1.1 christos sizeof (struct elf_x86_64_link_hash_entry));
1093 1.1 christos if (ret)
1094 1.1 christos {
1095 1.1 christos memset (ret, 0, sizeof (*ret));
1096 1.1 christos ret->elf.indx = sec->id;
1097 1.1.1.6 christos ret->elf.dynstr_index = htab->r_sym (rel->r_info);
1098 1.1.1.4 christos ret->elf.dynindx = -1;
1099 1.1 christos ret->func_pointer_refcount = 0;
1100 1.1 christos ret->plt_got.offset = (bfd_vma) -1;
1101 1.1 christos *slot = ret;
1102 1.1 christos }
1103 1.1 christos return &ret->elf;
1104 1.1.1.4 christos }
1105 1.1.1.4 christos
1106 1.1.1.4 christos /* Destroy an X86-64 ELF linker hash table. */
1107 1.1.1.4 christos
1108 1.1.1.4 christos static void
1109 1.1.1.4 christos elf_x86_64_link_hash_table_free (bfd *obfd)
1110 1.1.1.4 christos {
1111 1.1.1.4 christos struct elf_x86_64_link_hash_table *htab
1112 1.1.1.4 christos = (struct elf_x86_64_link_hash_table *) obfd->link.hash;
1113 1.1.1.4 christos
1114 1.1.1.4 christos if (htab->loc_hash_table)
1115 1.1.1.4 christos htab_delete (htab->loc_hash_table);
1116 1.1.1.4 christos if (htab->loc_hash_memory)
1117 1.1.1.4 christos objalloc_free ((struct objalloc *) htab->loc_hash_memory);
1118 1.1.1.4 christos _bfd_elf_link_hash_table_free (obfd);
1119 1.1 christos }
1120 1.1 christos
1121 1.1 christos /* Create an X86-64 ELF linker hash table. */
1122 1.1 christos
1123 1.1 christos static struct bfd_link_hash_table *
1124 1.1 christos elf_x86_64_link_hash_table_create (bfd *abfd)
1125 1.1 christos {
1126 1.1 christos struct elf_x86_64_link_hash_table *ret;
1127 1.1.1.2 christos bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
1128 1.1 christos
1129 1.1 christos ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
1130 1.1 christos if (ret == NULL)
1131 1.1 christos return NULL;
1132 1.1 christos
1133 1.1 christos if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1134 1.1 christos elf_x86_64_link_hash_newfunc,
1135 1.1 christos sizeof (struct elf_x86_64_link_hash_entry),
1136 1.1 christos X86_64_ELF_DATA))
1137 1.1 christos {
1138 1.1 christos free (ret);
1139 1.1 christos return NULL;
1140 1.1 christos }
1141 1.1 christos
1142 1.1 christos if (ABI_64_P (abfd))
1143 1.1 christos {
1144 1.1 christos ret->r_info = elf64_r_info;
1145 1.1 christos ret->r_sym = elf64_r_sym;
1146 1.1 christos ret->pointer_r_type = R_X86_64_64;
1147 1.1 christos ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
1148 1.1 christos ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1149 1.1 christos }
1150 1.1 christos else
1151 1.1 christos {
1152 1.1 christos ret->r_info = elf32_r_info;
1153 1.1 christos ret->r_sym = elf32_r_sym;
1154 1.1 christos ret->pointer_r_type = R_X86_64_32;
1155 1.1 christos ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1156 1.1 christos ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1157 1.1 christos }
1158 1.1 christos
1159 1.1 christos ret->loc_hash_table = htab_try_create (1024,
1160 1.1 christos elf_x86_64_local_htab_hash,
1161 1.1 christos elf_x86_64_local_htab_eq,
1162 1.1 christos NULL);
1163 1.1 christos ret->loc_hash_memory = objalloc_create ();
1164 1.1.1.4 christos if (!ret->loc_hash_table || !ret->loc_hash_memory)
1165 1.1 christos {
1166 1.1 christos elf_x86_64_link_hash_table_free (abfd);
1167 1.1.1.4 christos return NULL;
1168 1.1 christos }
1169 1.1 christos ret->elf.root.hash_table_free = elf_x86_64_link_hash_table_free;
1170 1.1 christos
1171 1.1 christos return &ret->elf.root;
1172 1.1 christos }
1173 1.1 christos
1174 1.1 christos /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
1175 1.1 christos .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
1176 1.1 christos hash table. */
1177 1.1 christos
1178 1.1 christos static bfd_boolean
1179 1.1 christos elf_x86_64_create_dynamic_sections (bfd *dynobj,
1180 1.1 christos struct bfd_link_info *info)
1181 1.1 christos {
1182 1.1 christos struct elf_x86_64_link_hash_table *htab;
1183 1.1 christos
1184 1.1 christos if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1185 1.1 christos return FALSE;
1186 1.1 christos
1187 1.1 christos htab = elf_x86_64_hash_table (info);
1188 1.1 christos if (htab == NULL)
1189 1.1.1.6 christos return FALSE;
1190 1.1.1.6 christos
1191 1.1.1.6 christos /* Set the contents of the .interp section to the interpreter. */
1192 1.1.1.6 christos if (bfd_link_executable (info) && !info->nointerp)
1193 1.1.1.6 christos {
1194 1.1.1.6 christos asection *s = bfd_get_linker_section (dynobj, ".interp");
1195 1.1.1.6 christos if (s == NULL)
1196 1.1.1.6 christos abort ();
1197 1.1.1.6 christos s->size = htab->dynamic_interpreter_size;
1198 1.1.1.6 christos s->contents = (unsigned char *) htab->dynamic_interpreter;
1199 1.1.1.6 christos htab->interp = s;
1200 1.1.1.2 christos }
1201 1.1.1.2 christos
1202 1.1.1.2 christos if (!info->no_ld_generated_unwind_info
1203 1.1.1.2 christos && htab->plt_eh_frame == NULL
1204 1.1.1.2 christos && htab->elf.splt != NULL)
1205 1.1.1.2 christos {
1206 1.1.1.2 christos flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1207 1.1.1.2 christos | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1208 1.1.1.2 christos | SEC_LINKER_CREATED);
1209 1.1.1.2 christos htab->plt_eh_frame
1210 1.1.1.7 christos = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
1211 1.1.1.7 christos if (htab->plt_eh_frame == NULL
1212 1.1.1.2 christos || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame,
1213 1.1.1.2 christos ABI_64_P (dynobj) ? 3 : 2))
1214 1.1.1.6 christos return FALSE;
1215 1.1.1.6 christos }
1216 1.1.1.6 christos
1217 1.1.1.6 christos /* Align .got section to its entry size. */
1218 1.1.1.6 christos if (htab->elf.sgot != NULL
1219 1.1.1.6 christos && !bfd_set_section_alignment (dynobj, htab->elf.sgot, 3))
1220 1.1.1.6 christos return FALSE;
1221 1.1.1.6 christos
1222 1.1.1.6 christos /* Align .got.plt section to its entry size. */
1223 1.1.1.6 christos if (htab->elf.sgotplt != NULL
1224 1.1.1.6 christos && !bfd_set_section_alignment (dynobj, htab->elf.sgotplt, 3))
1225 1.1 christos return FALSE;
1226 1.1 christos
1227 1.1 christos return TRUE;
1228 1.1 christos }
1229 1.1 christos
1230 1.1 christos /* Copy the extra info we tack onto an elf_link_hash_entry. */
1231 1.1 christos
1232 1.1 christos static void
1233 1.1 christos elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
1234 1.1 christos struct elf_link_hash_entry *dir,
1235 1.1 christos struct elf_link_hash_entry *ind)
1236 1.1 christos {
1237 1.1 christos struct elf_x86_64_link_hash_entry *edir, *eind;
1238 1.1 christos
1239 1.1 christos edir = (struct elf_x86_64_link_hash_entry *) dir;
1240 1.1.1.7 christos eind = (struct elf_x86_64_link_hash_entry *) ind;
1241 1.1.1.7 christos
1242 1.1.1.7 christos edir->has_bnd_reloc |= eind->has_bnd_reloc;
1243 1.1.1.6 christos edir->has_got_reloc |= eind->has_got_reloc;
1244 1.1 christos edir->has_non_got_reloc |= eind->has_non_got_reloc;
1245 1.1 christos
1246 1.1 christos if (eind->dyn_relocs != NULL)
1247 1.1 christos {
1248 1.1 christos if (edir->dyn_relocs != NULL)
1249 1.1 christos {
1250 1.1 christos struct elf_dyn_relocs **pp;
1251 1.1 christos struct elf_dyn_relocs *p;
1252 1.1 christos
1253 1.1 christos /* Add reloc counts against the indirect sym to the direct sym
1254 1.1 christos list. Merge any entries against the same section. */
1255 1.1 christos for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1256 1.1 christos {
1257 1.1 christos struct elf_dyn_relocs *q;
1258 1.1 christos
1259 1.1 christos for (q = edir->dyn_relocs; q != NULL; q = q->next)
1260 1.1 christos if (q->sec == p->sec)
1261 1.1 christos {
1262 1.1 christos q->pc_count += p->pc_count;
1263 1.1 christos q->count += p->count;
1264 1.1 christos *pp = p->next;
1265 1.1 christos break;
1266 1.1 christos }
1267 1.1 christos if (q == NULL)
1268 1.1 christos pp = &p->next;
1269 1.1 christos }
1270 1.1 christos *pp = edir->dyn_relocs;
1271 1.1 christos }
1272 1.1 christos
1273 1.1 christos edir->dyn_relocs = eind->dyn_relocs;
1274 1.1 christos eind->dyn_relocs = NULL;
1275 1.1 christos }
1276 1.1 christos
1277 1.1 christos if (ind->root.type == bfd_link_hash_indirect
1278 1.1 christos && dir->got.refcount <= 0)
1279 1.1 christos {
1280 1.1 christos edir->tls_type = eind->tls_type;
1281 1.1 christos eind->tls_type = GOT_UNKNOWN;
1282 1.1 christos }
1283 1.1 christos
1284 1.1 christos if (ELIMINATE_COPY_RELOCS
1285 1.1 christos && ind->root.type != bfd_link_hash_indirect
1286 1.1 christos && dir->dynamic_adjusted)
1287 1.1 christos {
1288 1.1 christos /* If called to transfer flags for a weakdef during processing
1289 1.1.1.7 christos of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1290 1.1.1.7 christos We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1291 1.1 christos if (dir->versioned != versioned_hidden)
1292 1.1 christos dir->ref_dynamic |= ind->ref_dynamic;
1293 1.1 christos dir->ref_regular |= ind->ref_regular;
1294 1.1 christos dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1295 1.1 christos dir->needs_plt |= ind->needs_plt;
1296 1.1 christos dir->pointer_equality_needed |= ind->pointer_equality_needed;
1297 1.1.1.6 christos }
1298 1.1.1.6 christos else
1299 1.1.1.6 christos {
1300 1.1.1.6 christos if (eind->func_pointer_refcount > 0)
1301 1.1.1.6 christos {
1302 1.1.1.6 christos edir->func_pointer_refcount += eind->func_pointer_refcount;
1303 1.1.1.6 christos eind->func_pointer_refcount = 0;
1304 1.1.1.6 christos }
1305 1.1.1.6 christos
1306 1.1 christos _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1307 1.1 christos }
1308 1.1 christos }
1309 1.1 christos
1310 1.1 christos static bfd_boolean
1311 1.1 christos elf64_x86_64_elf_object_p (bfd *abfd)
1312 1.1 christos {
1313 1.1 christos /* Set the right machine number for an x86-64 elf64 file. */
1314 1.1 christos bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1315 1.1 christos return TRUE;
1316 1.1.1.2 christos }
1317 1.1.1.2 christos
1318 1.1.1.2 christos static bfd_boolean
1319 1.1.1.2 christos elf32_x86_64_elf_object_p (bfd *abfd)
1320 1.1.1.2 christos {
1321 1.1.1.2 christos /* Set the right machine number for an x86-64 elf32 file. */
1322 1.1.1.2 christos bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1323 1.1 christos return TRUE;
1324 1.1 christos }
1325 1.1 christos
1326 1.1 christos /* Return TRUE if the TLS access code sequence support transition
1327 1.1 christos from R_TYPE. */
1328 1.1 christos
1329 1.1 christos static bfd_boolean
1330 1.1 christos elf_x86_64_check_tls_transition (bfd *abfd,
1331 1.1 christos struct bfd_link_info *info,
1332 1.1 christos asection *sec,
1333 1.1 christos bfd_byte *contents,
1334 1.1 christos Elf_Internal_Shdr *symtab_hdr,
1335 1.1 christos struct elf_link_hash_entry **sym_hashes,
1336 1.1 christos unsigned int r_type,
1337 1.1 christos const Elf_Internal_Rela *rel,
1338 1.1 christos const Elf_Internal_Rela *relend)
1339 1.1 christos {
1340 1.1.1.3 christos unsigned int val;
1341 1.1 christos unsigned long r_symndx;
1342 1.1 christos bfd_boolean largepic = FALSE;
1343 1.1 christos struct elf_link_hash_entry *h;
1344 1.1.1.6 christos bfd_vma offset;
1345 1.1.1.6 christos struct elf_x86_64_link_hash_table *htab;
1346 1.1 christos bfd_byte *call;
1347 1.1 christos bfd_boolean indirect_call, tls_get_addr;
1348 1.1 christos
1349 1.1 christos htab = elf_x86_64_hash_table (info);
1350 1.1 christos offset = rel->r_offset;
1351 1.1 christos switch (r_type)
1352 1.1 christos {
1353 1.1 christos case R_X86_64_TLSGD:
1354 1.1 christos case R_X86_64_TLSLD:
1355 1.1 christos if ((rel + 1) >= relend)
1356 1.1 christos return FALSE;
1357 1.1 christos
1358 1.1 christos if (r_type == R_X86_64_TLSGD)
1359 1.1 christos {
1360 1.1.1.6 christos /* Check transition from GD access model. For 64bit, only
1361 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1362 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
1363 1.1.1.6 christos or
1364 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1365 1.1.1.6 christos .byte 0x66; rex64
1366 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
1367 1.1 christos which may be converted to
1368 1.1 christos addr32 call __tls_get_addr
1369 1.1.1.6 christos can transit to different access model. For 32bit, only
1370 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
1371 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
1372 1.1.1.6 christos or
1373 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
1374 1.1.1.6 christos .byte 0x66; rex64
1375 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
1376 1.1.1.6 christos which may be converted to
1377 1.1.1.3 christos addr32 call __tls_get_addr
1378 1.1.1.3 christos can transit to different access model. For largepic,
1379 1.1.1.3 christos we also support:
1380 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
1381 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
1382 1.1.1.6 christos addq $r15, %rax
1383 1.1.1.6 christos call *%rax
1384 1.1.1.6 christos or
1385 1.1.1.3 christos leaq foo@tlsgd(%rip), %rdi
1386 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
1387 1.1 christos addq $rbx, %rax
1388 1.1.1.2 christos call *%rax */
1389 1.1.1.2 christos
1390 1.1.1.3 christos static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1391 1.1 christos
1392 1.1 christos if ((offset + 12) > sec->size)
1393 1.1.1.6 christos return FALSE;
1394 1.1.1.6 christos
1395 1.1.1.6 christos call = contents + offset + 4;
1396 1.1.1.6 christos if (call[0] != 0x66
1397 1.1.1.6 christos || !((call[1] == 0x48
1398 1.1.1.6 christos && call[2] == 0xff
1399 1.1.1.6 christos && call[3] == 0x15)
1400 1.1.1.6 christos || (call[1] == 0x48
1401 1.1.1.6 christos && call[2] == 0x67
1402 1.1.1.6 christos && call[3] == 0xe8)
1403 1.1.1.6 christos || (call[1] == 0x66
1404 1.1.1.3 christos && call[2] == 0x48
1405 1.1.1.3 christos && call[3] == 0xe8)))
1406 1.1.1.3 christos {
1407 1.1.1.3 christos if (!ABI_64_P (abfd)
1408 1.1.1.6 christos || (offset + 19) > sec->size
1409 1.1.1.6 christos || offset < 3
1410 1.1.1.6 christos || memcmp (call - 7, leaq + 1, 3) != 0
1411 1.1.1.6 christos || memcmp (call, "\x48\xb8", 2) != 0
1412 1.1.1.6 christos || call[11] != 0x01
1413 1.1.1.6 christos || call[13] != 0xff
1414 1.1.1.6 christos || call[14] != 0xd0
1415 1.1.1.3 christos || !((call[10] == 0x48 && call[12] == 0xd8)
1416 1.1.1.3 christos || (call[10] == 0x4c && call[12] == 0xf8)))
1417 1.1.1.3 christos return FALSE;
1418 1.1.1.3 christos largepic = TRUE;
1419 1.1 christos }
1420 1.1 christos else if (ABI_64_P (abfd))
1421 1.1.1.2 christos {
1422 1.1 christos if (offset < 4
1423 1.1 christos || memcmp (contents + offset - 4, leaq, 4) != 0)
1424 1.1 christos return FALSE;
1425 1.1 christos }
1426 1.1 christos else
1427 1.1.1.2 christos {
1428 1.1 christos if (offset < 3
1429 1.1 christos || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1430 1.1.1.6 christos return FALSE;
1431 1.1 christos }
1432 1.1 christos indirect_call = call[2] == 0xff;
1433 1.1 christos }
1434 1.1 christos else
1435 1.1 christos {
1436 1.1.1.6 christos /* Check transition from LD access model. Only
1437 1.1.1.6 christos leaq foo@tlsld(%rip), %rdi;
1438 1.1.1.6 christos call __tls_get_addr@PLT
1439 1.1.1.6 christos or
1440 1.1.1.6 christos leaq foo@tlsld(%rip), %rdi;
1441 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
1442 1.1.1.3 christos which may be converted to
1443 1.1.1.3 christos addr32 call __tls_get_addr
1444 1.1.1.3 christos can transit to different access model. For largepic
1445 1.1.1.3 christos we also support:
1446 1.1.1.6 christos leaq foo@tlsld(%rip), %rdi
1447 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
1448 1.1.1.6 christos addq $r15, %rax
1449 1.1.1.6 christos call *%rax
1450 1.1.1.6 christos or
1451 1.1.1.3 christos leaq foo@tlsld(%rip), %rdi
1452 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
1453 1.1 christos addq $rbx, %rax
1454 1.1.1.2 christos call *%rax */
1455 1.1 christos
1456 1.1 christos static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1457 1.1 christos
1458 1.1 christos if (offset < 3 || (offset + 9) > sec->size)
1459 1.1.1.3 christos return FALSE;
1460 1.1 christos
1461 1.1.1.3 christos if (memcmp (contents + offset - 3, lea, 3) != 0)
1462 1.1.1.6 christos return FALSE;
1463 1.1.1.6 christos
1464 1.1.1.6 christos call = contents + offset + 4;
1465 1.1.1.6 christos if (!(call[0] == 0xe8
1466 1.1.1.3 christos || (call[0] == 0xff && call[1] == 0x15)
1467 1.1.1.3 christos || (call[0] == 0x67 && call[1] == 0xe8)))
1468 1.1.1.3 christos {
1469 1.1.1.6 christos if (!ABI_64_P (abfd)
1470 1.1.1.6 christos || (offset + 19) > sec->size
1471 1.1.1.6 christos || memcmp (call, "\x48\xb8", 2) != 0
1472 1.1.1.6 christos || call[11] != 0x01
1473 1.1.1.6 christos || call[13] != 0xff
1474 1.1.1.6 christos || call[14] != 0xd0
1475 1.1.1.3 christos || !((call[10] == 0x48 && call[12] == 0xd8)
1476 1.1.1.3 christos || (call[10] == 0x4c && call[12] == 0xf8)))
1477 1.1.1.3 christos return FALSE;
1478 1.1.1.6 christos largepic = TRUE;
1479 1.1 christos }
1480 1.1 christos indirect_call = call[0] == 0xff;
1481 1.1 christos }
1482 1.1 christos
1483 1.1 christos r_symndx = htab->r_sym (rel[1].r_info);
1484 1.1 christos if (r_symndx < symtab_hdr->sh_info)
1485 1.1.1.6 christos return FALSE;
1486 1.1 christos
1487 1.1.1.6 christos tls_get_addr = FALSE;
1488 1.1.1.6 christos h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1489 1.1.1.6 christos if (h != NULL && h->root.root.string != NULL)
1490 1.1.1.6 christos {
1491 1.1.1.6 christos struct elf_x86_64_link_hash_entry *eh
1492 1.1.1.6 christos = (struct elf_x86_64_link_hash_entry *) h;
1493 1.1.1.6 christos tls_get_addr = eh->tls_get_addr == 1;
1494 1.1.1.6 christos if (eh->tls_get_addr > 1)
1495 1.1.1.6 christos {
1496 1.1.1.6 christos /* Use strncmp to check __tls_get_addr since
1497 1.1.1.6 christos __tls_get_addr may be versioned. */
1498 1.1.1.6 christos if (strncmp (h->root.root.string, "__tls_get_addr", 14)
1499 1.1.1.6 christos == 0)
1500 1.1.1.6 christos {
1501 1.1.1.6 christos eh->tls_get_addr = 1;
1502 1.1.1.6 christos tls_get_addr = TRUE;
1503 1.1.1.6 christos }
1504 1.1.1.6 christos else
1505 1.1.1.6 christos eh->tls_get_addr = 0;
1506 1.1.1.6 christos }
1507 1.1.1.6 christos }
1508 1.1.1.6 christos
1509 1.1.1.6 christos if (!tls_get_addr)
1510 1.1.1.6 christos return FALSE;
1511 1.1.1.6 christos else if (largepic)
1512 1.1.1.6 christos return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64;
1513 1.1.1.6 christos else if (indirect_call)
1514 1.1.1.6 christos return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_GOTPCRELX;
1515 1.1.1.6 christos else
1516 1.1 christos return (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1517 1.1 christos || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
1518 1.1 christos
1519 1.1 christos case R_X86_64_GOTTPOFF:
1520 1.1 christos /* Check transition from IE access model:
1521 1.1 christos mov foo@gottpoff(%rip), %reg
1522 1.1 christos add foo@gottpoff(%rip), %reg
1523 1.1 christos */
1524 1.1 christos
1525 1.1 christos /* Check REX prefix first. */
1526 1.1 christos if (offset >= 3 && (offset + 4) <= sec->size)
1527 1.1 christos {
1528 1.1 christos val = bfd_get_8 (abfd, contents + offset - 3);
1529 1.1 christos if (val != 0x48 && val != 0x4c)
1530 1.1 christos {
1531 1.1 christos /* X32 may have 0x44 REX prefix or no REX prefix. */
1532 1.1 christos if (ABI_64_P (abfd))
1533 1.1 christos return FALSE;
1534 1.1 christos }
1535 1.1 christos }
1536 1.1 christos else
1537 1.1 christos {
1538 1.1 christos /* X32 may not have any REX prefix. */
1539 1.1 christos if (ABI_64_P (abfd))
1540 1.1 christos return FALSE;
1541 1.1 christos if (offset < 2 || (offset + 3) > sec->size)
1542 1.1 christos return FALSE;
1543 1.1 christos }
1544 1.1 christos
1545 1.1 christos val = bfd_get_8 (abfd, contents + offset - 2);
1546 1.1 christos if (val != 0x8b && val != 0x03)
1547 1.1 christos return FALSE;
1548 1.1 christos
1549 1.1 christos val = bfd_get_8 (abfd, contents + offset - 1);
1550 1.1 christos return (val & 0xc7) == 5;
1551 1.1 christos
1552 1.1 christos case R_X86_64_GOTPC32_TLSDESC:
1553 1.1 christos /* Check transition from GDesc access model:
1554 1.1 christos leaq x@tlsdesc(%rip), %rax
1555 1.1 christos
1556 1.1 christos Make sure it's a leaq adding rip to a 32-bit offset
1557 1.1 christos into any register, although it's probably almost always
1558 1.1 christos going to be rax. */
1559 1.1 christos
1560 1.1 christos if (offset < 3 || (offset + 4) > sec->size)
1561 1.1 christos return FALSE;
1562 1.1 christos
1563 1.1 christos val = bfd_get_8 (abfd, contents + offset - 3);
1564 1.1 christos if ((val & 0xfb) != 0x48)
1565 1.1 christos return FALSE;
1566 1.1 christos
1567 1.1 christos if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1568 1.1 christos return FALSE;
1569 1.1 christos
1570 1.1 christos val = bfd_get_8 (abfd, contents + offset - 1);
1571 1.1 christos return (val & 0xc7) == 0x05;
1572 1.1 christos
1573 1.1 christos case R_X86_64_TLSDESC_CALL:
1574 1.1 christos /* Check transition from GDesc access model:
1575 1.1 christos call *x@tlsdesc(%rax)
1576 1.1 christos */
1577 1.1 christos if (offset + 2 <= sec->size)
1578 1.1.1.6 christos {
1579 1.1.1.6 christos /* Make sure that it's a call *x@tlsdesc(%rax). */
1580 1.1 christos call = contents + offset;
1581 1.1 christos return call[0] == 0xff && call[1] == 0x10;
1582 1.1 christos }
1583 1.1 christos
1584 1.1 christos return FALSE;
1585 1.1 christos
1586 1.1 christos default:
1587 1.1 christos abort ();
1588 1.1 christos }
1589 1.1 christos }
1590 1.1 christos
1591 1.1 christos /* Return TRUE if the TLS access transition is OK or no transition
1592 1.1 christos will be performed. Update R_TYPE if there is a transition. */
1593 1.1 christos
1594 1.1 christos static bfd_boolean
1595 1.1 christos elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1596 1.1 christos asection *sec, bfd_byte *contents,
1597 1.1 christos Elf_Internal_Shdr *symtab_hdr,
1598 1.1 christos struct elf_link_hash_entry **sym_hashes,
1599 1.1 christos unsigned int *r_type, int tls_type,
1600 1.1 christos const Elf_Internal_Rela *rel,
1601 1.1.1.6 christos const Elf_Internal_Rela *relend,
1602 1.1.1.6 christos struct elf_link_hash_entry *h,
1603 1.1 christos unsigned long r_symndx,
1604 1.1 christos bfd_boolean from_relocate_section)
1605 1.1 christos {
1606 1.1 christos unsigned int from_type = *r_type;
1607 1.1 christos unsigned int to_type = from_type;
1608 1.1 christos bfd_boolean check = TRUE;
1609 1.1 christos
1610 1.1 christos /* Skip TLS transition for functions. */
1611 1.1 christos if (h != NULL
1612 1.1 christos && (h->type == STT_FUNC
1613 1.1 christos || h->type == STT_GNU_IFUNC))
1614 1.1 christos return TRUE;
1615 1.1 christos
1616 1.1 christos switch (from_type)
1617 1.1 christos {
1618 1.1 christos case R_X86_64_TLSGD:
1619 1.1 christos case R_X86_64_GOTPC32_TLSDESC:
1620 1.1.1.6 christos case R_X86_64_TLSDESC_CALL:
1621 1.1 christos case R_X86_64_GOTTPOFF:
1622 1.1 christos if (bfd_link_executable (info))
1623 1.1 christos {
1624 1.1 christos if (h == NULL)
1625 1.1 christos to_type = R_X86_64_TPOFF32;
1626 1.1 christos else
1627 1.1 christos to_type = R_X86_64_GOTTPOFF;
1628 1.1.1.6 christos }
1629 1.1.1.6 christos
1630 1.1.1.6 christos /* When we are called from elf_x86_64_relocate_section, there may
1631 1.1 christos be additional transitions based on TLS_TYPE. */
1632 1.1 christos if (from_relocate_section)
1633 1.1 christos {
1634 1.1.1.6 christos unsigned int new_to_type = to_type;
1635 1.1 christos
1636 1.1 christos if (bfd_link_executable (info)
1637 1.1 christos && h != NULL
1638 1.1 christos && h->dynindx == -1
1639 1.1 christos && tls_type == GOT_TLS_IE)
1640 1.1 christos new_to_type = R_X86_64_TPOFF32;
1641 1.1 christos
1642 1.1 christos if (to_type == R_X86_64_TLSGD
1643 1.1 christos || to_type == R_X86_64_GOTPC32_TLSDESC
1644 1.1 christos || to_type == R_X86_64_TLSDESC_CALL)
1645 1.1 christos {
1646 1.1 christos if (tls_type == GOT_TLS_IE)
1647 1.1 christos new_to_type = R_X86_64_GOTTPOFF;
1648 1.1 christos }
1649 1.1 christos
1650 1.1 christos /* We checked the transition before when we were called from
1651 1.1 christos elf_x86_64_check_relocs. We only want to check the new
1652 1.1 christos transition which hasn't been checked before. */
1653 1.1 christos check = new_to_type != to_type && from_type == to_type;
1654 1.1 christos to_type = new_to_type;
1655 1.1 christos }
1656 1.1 christos
1657 1.1 christos break;
1658 1.1.1.6 christos
1659 1.1 christos case R_X86_64_TLSLD:
1660 1.1 christos if (bfd_link_executable (info))
1661 1.1 christos to_type = R_X86_64_TPOFF32;
1662 1.1 christos break;
1663 1.1 christos
1664 1.1 christos default:
1665 1.1 christos return TRUE;
1666 1.1 christos }
1667 1.1 christos
1668 1.1 christos /* Return TRUE if there is no transition. */
1669 1.1 christos if (from_type == to_type)
1670 1.1 christos return TRUE;
1671 1.1 christos
1672 1.1 christos /* Check if the transition can be performed. */
1673 1.1 christos if (check
1674 1.1 christos && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1675 1.1 christos symtab_hdr, sym_hashes,
1676 1.1 christos from_type, rel, relend))
1677 1.1 christos {
1678 1.1 christos reloc_howto_type *from, *to;
1679 1.1 christos const char *name;
1680 1.1 christos
1681 1.1 christos from = elf_x86_64_rtype_to_howto (abfd, from_type);
1682 1.1 christos to = elf_x86_64_rtype_to_howto (abfd, to_type);
1683 1.1 christos
1684 1.1 christos if (h)
1685 1.1 christos name = h->root.root.string;
1686 1.1 christos else
1687 1.1 christos {
1688 1.1 christos struct elf_x86_64_link_hash_table *htab;
1689 1.1 christos
1690 1.1 christos htab = elf_x86_64_hash_table (info);
1691 1.1 christos if (htab == NULL)
1692 1.1 christos name = "*unknown*";
1693 1.1 christos else
1694 1.1 christos {
1695 1.1 christos Elf_Internal_Sym *isym;
1696 1.1 christos
1697 1.1 christos isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1698 1.1 christos abfd, r_symndx);
1699 1.1 christos name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1700 1.1 christos }
1701 1.1.1.7 christos }
1702 1.1.1.7 christos
1703 1.1 christos _bfd_error_handler
1704 1.1 christos /* xgettext:c-format */
1705 1.1.1.7 christos (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1706 1.1.1.7 christos "in section `%A' failed"),
1707 1.1 christos abfd, from->name, to->name, name,
1708 1.1 christos (unsigned long) rel->r_offset, sec);
1709 1.1 christos bfd_set_error (bfd_error_bad_value);
1710 1.1 christos return FALSE;
1711 1.1 christos }
1712 1.1 christos
1713 1.1 christos *r_type = to_type;
1714 1.1 christos return TRUE;
1715 1.1.1.5 christos }
1716 1.1.1.5 christos
1717 1.1.1.6 christos /* Rename some of the generic section flags to better document how they
1718 1.1.1.6 christos are used here. */
1719 1.1.1.6 christos #define need_convert_load sec_flg0
1720 1.1.1.6 christos #define check_relocs_failed sec_flg1
1721 1.1.1.6 christos
1722 1.1.1.6 christos static bfd_boolean
1723 1.1.1.6 christos elf_x86_64_need_pic (bfd *input_bfd, asection *sec,
1724 1.1.1.6 christos struct elf_link_hash_entry *h,
1725 1.1.1.6 christos Elf_Internal_Shdr *symtab_hdr,
1726 1.1.1.6 christos Elf_Internal_Sym *isym,
1727 1.1.1.6 christos reloc_howto_type *howto)
1728 1.1.1.6 christos {
1729 1.1.1.6 christos const char *v = "";
1730 1.1.1.6 christos const char *und = "";
1731 1.1.1.6 christos const char *pic = "";
1732 1.1.1.6 christos
1733 1.1.1.6 christos const char *name;
1734 1.1.1.6 christos if (h)
1735 1.1.1.6 christos {
1736 1.1.1.6 christos name = h->root.root.string;
1737 1.1.1.6 christos switch (ELF_ST_VISIBILITY (h->other))
1738 1.1.1.6 christos {
1739 1.1.1.6 christos case STV_HIDDEN:
1740 1.1.1.6 christos v = _("hidden symbol ");
1741 1.1.1.6 christos break;
1742 1.1.1.6 christos case STV_INTERNAL:
1743 1.1.1.6 christos v = _("internal symbol ");
1744 1.1.1.6 christos break;
1745 1.1.1.6 christos case STV_PROTECTED:
1746 1.1.1.6 christos v = _("protected symbol ");
1747 1.1.1.6 christos break;
1748 1.1.1.6 christos default:
1749 1.1.1.6 christos v = _("symbol ");
1750 1.1.1.6 christos pic = _("; recompile with -fPIC");
1751 1.1.1.6 christos break;
1752 1.1.1.6 christos }
1753 1.1.1.6 christos
1754 1.1.1.6 christos if (!h->def_regular && !h->def_dynamic)
1755 1.1.1.6 christos und = _("undefined ");
1756 1.1.1.6 christos }
1757 1.1.1.6 christos else
1758 1.1.1.6 christos {
1759 1.1.1.6 christos name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1760 1.1.1.6 christos pic = _("; recompile with -fPIC");
1761 1.1.1.7 christos }
1762 1.1.1.7 christos
1763 1.1.1.7 christos /* xgettext:c-format */
1764 1.1.1.7 christos _bfd_error_handler (_("%B: relocation %s against %s%s`%s' can "
1765 1.1.1.6 christos "not be used when making a shared object%s"),
1766 1.1.1.6 christos input_bfd, howto->name, und, v, name, pic);
1767 1.1.1.6 christos bfd_set_error (bfd_error_bad_value);
1768 1.1.1.6 christos sec->check_relocs_failed = 1;
1769 1.1.1.6 christos return FALSE;
1770 1.1.1.6 christos }
1771 1.1.1.6 christos
1772 1.1.1.6 christos /* With the local symbol, foo, we convert
1773 1.1.1.6 christos mov foo@GOTPCREL(%rip), %reg
1774 1.1.1.6 christos to
1775 1.1.1.6 christos lea foo(%rip), %reg
1776 1.1.1.6 christos and convert
1777 1.1.1.6 christos call/jmp *foo@GOTPCREL(%rip)
1778 1.1.1.6 christos to
1779 1.1.1.6 christos nop call foo/jmp foo nop
1780 1.1.1.6 christos When PIC is false, convert
1781 1.1.1.6 christos test %reg, foo@GOTPCREL(%rip)
1782 1.1.1.6 christos to
1783 1.1.1.6 christos test $foo, %reg
1784 1.1.1.6 christos and convert
1785 1.1.1.6 christos binop foo@GOTPCREL(%rip), %reg
1786 1.1.1.6 christos to
1787 1.1.1.6 christos binop $foo, %reg
1788 1.1.1.6 christos where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1789 1.1.1.6 christos instructions. */
1790 1.1.1.6 christos
1791 1.1.1.6 christos static bfd_boolean
1792 1.1.1.6 christos elf_x86_64_convert_load_reloc (bfd *abfd, asection *sec,
1793 1.1.1.6 christos bfd_byte *contents,
1794 1.1.1.6 christos Elf_Internal_Rela *irel,
1795 1.1.1.6 christos struct elf_link_hash_entry *h,
1796 1.1.1.6 christos bfd_boolean *converted,
1797 1.1.1.6 christos struct bfd_link_info *link_info)
1798 1.1.1.6 christos {
1799 1.1.1.6 christos struct elf_x86_64_link_hash_table *htab;
1800 1.1.1.6 christos bfd_boolean is_pic;
1801 1.1.1.6 christos bfd_boolean require_reloc_pc32;
1802 1.1.1.6 christos bfd_boolean relocx;
1803 1.1.1.6 christos bfd_boolean to_reloc_pc32;
1804 1.1.1.6 christos asection *tsec;
1805 1.1.1.6 christos char symtype;
1806 1.1.1.6 christos bfd_signed_vma raddend;
1807 1.1.1.6 christos unsigned int opcode;
1808 1.1.1.6 christos unsigned int modrm;
1809 1.1.1.6 christos unsigned int r_type = ELF32_R_TYPE (irel->r_info);
1810 1.1.1.6 christos unsigned int r_symndx;
1811 1.1.1.6 christos bfd_vma toff;
1812 1.1.1.6 christos bfd_vma roff = irel->r_offset;
1813 1.1.1.6 christos
1814 1.1.1.6 christos if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1815 1.1.1.6 christos return TRUE;
1816 1.1.1.6 christos
1817 1.1.1.6 christos raddend = irel->r_addend;
1818 1.1.1.6 christos /* Addend for 32-bit PC-relative relocation must be -4. */
1819 1.1.1.6 christos if (raddend != -4)
1820 1.1.1.6 christos return TRUE;
1821 1.1.1.6 christos
1822 1.1.1.6 christos htab = elf_x86_64_hash_table (link_info);
1823 1.1.1.6 christos is_pic = bfd_link_pic (link_info);
1824 1.1.1.6 christos
1825 1.1.1.6 christos relocx = (r_type == R_X86_64_GOTPCRELX
1826 1.1.1.6 christos || r_type == R_X86_64_REX_GOTPCRELX);
1827 1.1.1.6 christos
1828 1.1.1.6 christos /* TRUE if we can convert only to R_X86_64_PC32. Enable it for
1829 1.1.1.6 christos --no-relax. */
1830 1.1.1.6 christos require_reloc_pc32
1831 1.1.1.6 christos = link_info->disable_target_specific_optimizations > 1;
1832 1.1.1.6 christos
1833 1.1.1.6 christos r_symndx = htab->r_sym (irel->r_info);
1834 1.1.1.6 christos
1835 1.1.1.6 christos opcode = bfd_get_8 (abfd, contents + roff - 2);
1836 1.1.1.6 christos
1837 1.1.1.6 christos /* Convert mov to lea since it has been done for a while. */
1838 1.1.1.6 christos if (opcode != 0x8b)
1839 1.1.1.6 christos {
1840 1.1.1.6 christos /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1841 1.1.1.6 christos for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1842 1.1.1.6 christos test, xor instructions. */
1843 1.1.1.6 christos if (!relocx)
1844 1.1.1.6 christos return TRUE;
1845 1.1.1.6 christos }
1846 1.1.1.6 christos
1847 1.1.1.6 christos /* We convert only to R_X86_64_PC32:
1848 1.1.1.6 christos 1. Branch.
1849 1.1.1.6 christos 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1850 1.1.1.6 christos 3. require_reloc_pc32 is true.
1851 1.1.1.6 christos 4. PIC.
1852 1.1.1.6 christos */
1853 1.1.1.6 christos to_reloc_pc32 = (opcode == 0xff
1854 1.1.1.6 christos || !relocx
1855 1.1.1.6 christos || require_reloc_pc32
1856 1.1.1.6 christos || is_pic);
1857 1.1.1.6 christos
1858 1.1.1.6 christos /* Get the symbol referred to by the reloc. */
1859 1.1.1.6 christos if (h == NULL)
1860 1.1.1.6 christos {
1861 1.1.1.6 christos Elf_Internal_Sym *isym
1862 1.1.1.6 christos = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1863 1.1.1.6 christos
1864 1.1.1.6 christos /* Skip relocation against undefined symbols. */
1865 1.1.1.6 christos if (isym->st_shndx == SHN_UNDEF)
1866 1.1.1.6 christos return TRUE;
1867 1.1.1.6 christos
1868 1.1.1.6 christos symtype = ELF_ST_TYPE (isym->st_info);
1869 1.1.1.6 christos
1870 1.1.1.6 christos if (isym->st_shndx == SHN_ABS)
1871 1.1.1.6 christos tsec = bfd_abs_section_ptr;
1872 1.1.1.6 christos else if (isym->st_shndx == SHN_COMMON)
1873 1.1.1.6 christos tsec = bfd_com_section_ptr;
1874 1.1.1.6 christos else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1875 1.1.1.6 christos tsec = &_bfd_elf_large_com_section;
1876 1.1.1.6 christos else
1877 1.1.1.6 christos tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1878 1.1.1.6 christos
1879 1.1.1.6 christos toff = isym->st_value;
1880 1.1.1.6 christos }
1881 1.1.1.6 christos else
1882 1.1.1.6 christos {
1883 1.1.1.6 christos /* Undefined weak symbol is only bound locally in executable
1884 1.1.1.6 christos and its reference is resolved as 0 without relocation
1885 1.1.1.6 christos overflow. We can only perform this optimization for
1886 1.1.1.6 christos GOTPCRELX relocations since we need to modify REX byte.
1887 1.1.1.6 christos It is OK convert mov with R_X86_64_GOTPCREL to
1888 1.1.1.6 christos R_X86_64_PC32. */
1889 1.1.1.6 christos if ((relocx || opcode == 0x8b)
1890 1.1.1.6 christos && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
1891 1.1.1.6 christos TRUE,
1892 1.1.1.6 christos elf_x86_64_hash_entry (h)))
1893 1.1.1.6 christos {
1894 1.1.1.6 christos if (opcode == 0xff)
1895 1.1.1.6 christos {
1896 1.1.1.6 christos /* Skip for branch instructions since R_X86_64_PC32
1897 1.1.1.6 christos may overflow. */
1898 1.1.1.6 christos if (require_reloc_pc32)
1899 1.1.1.6 christos return TRUE;
1900 1.1.1.6 christos }
1901 1.1.1.6 christos else if (relocx)
1902 1.1.1.6 christos {
1903 1.1.1.6 christos /* For non-branch instructions, we can convert to
1904 1.1.1.6 christos R_X86_64_32/R_X86_64_32S since we know if there
1905 1.1.1.6 christos is a REX byte. */
1906 1.1.1.6 christos to_reloc_pc32 = FALSE;
1907 1.1.1.6 christos }
1908 1.1.1.6 christos
1909 1.1.1.6 christos /* Since we don't know the current PC when PIC is true,
1910 1.1.1.6 christos we can't convert to R_X86_64_PC32. */
1911 1.1.1.6 christos if (to_reloc_pc32 && is_pic)
1912 1.1.1.6 christos return TRUE;
1913 1.1.1.6 christos
1914 1.1.1.6 christos goto convert;
1915 1.1.1.6 christos }
1916 1.1.1.6 christos /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1917 1.1.1.6 christos ld.so may use its link-time address. */
1918 1.1.1.6 christos else if ((h->def_regular
1919 1.1.1.6 christos || h->root.type == bfd_link_hash_defined
1920 1.1.1.6 christos || h->root.type == bfd_link_hash_defweak)
1921 1.1.1.6 christos && h != htab->elf.hdynamic
1922 1.1.1.6 christos && SYMBOL_REFERENCES_LOCAL (link_info, h))
1923 1.1.1.6 christos {
1924 1.1.1.6 christos /* bfd_link_hash_new or bfd_link_hash_undefined is
1925 1.1.1.6 christos set by an assignment in a linker script in
1926 1.1.1.6 christos bfd_elf_record_link_assignment. */
1927 1.1.1.7 christos if (h->def_regular
1928 1.1.1.7 christos && (h->root.type == bfd_link_hash_new
1929 1.1.1.7 christos || h->root.type == bfd_link_hash_undefined
1930 1.1.1.7 christos || ((h->root.type == bfd_link_hash_defined
1931 1.1.1.6 christos || h->root.type == bfd_link_hash_defweak)
1932 1.1.1.6 christos && h->root.u.def.section == bfd_und_section_ptr)))
1933 1.1.1.6 christos {
1934 1.1.1.6 christos /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1935 1.1.1.6 christos if (require_reloc_pc32)
1936 1.1.1.6 christos return TRUE;
1937 1.1.1.6 christos goto convert;
1938 1.1.1.6 christos }
1939 1.1.1.6 christos tsec = h->root.u.def.section;
1940 1.1.1.6 christos toff = h->root.u.def.value;
1941 1.1.1.6 christos symtype = h->type;
1942 1.1.1.6 christos }
1943 1.1.1.6 christos else
1944 1.1.1.6 christos return TRUE;
1945 1.1.1.6 christos }
1946 1.1.1.6 christos
1947 1.1.1.6 christos /* Don't convert GOTPCREL relocation against large section. */
1948 1.1.1.6 christos if (elf_section_data (tsec) != NULL
1949 1.1.1.6 christos && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1950 1.1.1.6 christos return TRUE;
1951 1.1.1.6 christos
1952 1.1.1.6 christos /* We can only estimate relocation overflow for R_X86_64_PC32. */
1953 1.1.1.6 christos if (!to_reloc_pc32)
1954 1.1.1.6 christos goto convert;
1955 1.1.1.6 christos
1956 1.1.1.6 christos if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
1957 1.1.1.6 christos {
1958 1.1.1.6 christos /* At this stage in linking, no SEC_MERGE symbol has been
1959 1.1.1.6 christos adjusted, so all references to such symbols need to be
1960 1.1.1.6 christos passed through _bfd_merged_section_offset. (Later, in
1961 1.1.1.6 christos relocate_section, all SEC_MERGE symbols *except* for
1962 1.1.1.6 christos section symbols have been adjusted.)
1963 1.1.1.6 christos
1964 1.1.1.6 christos gas may reduce relocations against symbols in SEC_MERGE
1965 1.1.1.6 christos sections to a relocation against the section symbol when
1966 1.1.1.6 christos the original addend was zero. When the reloc is against
1967 1.1.1.6 christos a section symbol we should include the addend in the
1968 1.1.1.6 christos offset passed to _bfd_merged_section_offset, since the
1969 1.1.1.6 christos location of interest is the original symbol. On the
1970 1.1.1.6 christos other hand, an access to "sym+addend" where "sym" is not
1971 1.1.1.6 christos a section symbol should not include the addend; Such an
1972 1.1.1.6 christos access is presumed to be an offset from "sym"; The
1973 1.1.1.6 christos location of interest is just "sym". */
1974 1.1.1.6 christos if (symtype == STT_SECTION)
1975 1.1.1.6 christos toff += raddend;
1976 1.1.1.6 christos
1977 1.1.1.6 christos toff = _bfd_merged_section_offset (abfd, &tsec,
1978 1.1.1.6 christos elf_section_data (tsec)->sec_info,
1979 1.1.1.6 christos toff);
1980 1.1.1.6 christos
1981 1.1.1.6 christos if (symtype != STT_SECTION)
1982 1.1.1.6 christos toff += raddend;
1983 1.1.1.6 christos }
1984 1.1.1.6 christos else
1985 1.1.1.6 christos toff += raddend;
1986 1.1.1.6 christos
1987 1.1.1.6 christos /* Don't convert if R_X86_64_PC32 relocation overflows. */
1988 1.1.1.6 christos if (tsec->output_section == sec->output_section)
1989 1.1.1.6 christos {
1990 1.1.1.6 christos if ((toff - roff + 0x80000000) > 0xffffffff)
1991 1.1.1.6 christos return TRUE;
1992 1.1.1.6 christos }
1993 1.1.1.6 christos else
1994 1.1.1.6 christos {
1995 1.1.1.6 christos bfd_signed_vma distance;
1996 1.1.1.6 christos
1997 1.1.1.6 christos /* At this point, we don't know the load addresses of TSEC
1998 1.1.1.6 christos section nor SEC section. We estimate the distrance between
1999 1.1.1.6 christos SEC and TSEC. We store the estimated distances in the
2000 1.1.1.6 christos compressed_size field of the output section, which is only
2001 1.1.1.6 christos used to decompress the compressed input section. */
2002 1.1.1.6 christos if (sec->output_section->compressed_size == 0)
2003 1.1.1.6 christos {
2004 1.1.1.6 christos asection *asect;
2005 1.1.1.6 christos bfd_size_type size = 0;
2006 1.1.1.6 christos for (asect = link_info->output_bfd->sections;
2007 1.1.1.6 christos asect != NULL;
2008 1.1.1.6 christos asect = asect->next)
2009 1.1.1.6 christos /* Skip debug sections since compressed_size is used to
2010 1.1.1.6 christos compress debug sections. */
2011 1.1.1.6 christos if ((asect->flags & SEC_DEBUGGING) == 0)
2012 1.1.1.6 christos {
2013 1.1.1.6 christos asection *i;
2014 1.1.1.6 christos for (i = asect->map_head.s;
2015 1.1.1.6 christos i != NULL;
2016 1.1.1.6 christos i = i->map_head.s)
2017 1.1.1.6 christos {
2018 1.1.1.6 christos size = align_power (size, i->alignment_power);
2019 1.1.1.6 christos size += i->size;
2020 1.1.1.6 christos }
2021 1.1.1.6 christos asect->compressed_size = size;
2022 1.1.1.6 christos }
2023 1.1.1.6 christos }
2024 1.1.1.6 christos
2025 1.1.1.6 christos /* Don't convert GOTPCREL relocations if TSEC isn't placed
2026 1.1.1.6 christos after SEC. */
2027 1.1.1.6 christos distance = (tsec->output_section->compressed_size
2028 1.1.1.6 christos - sec->output_section->compressed_size);
2029 1.1.1.6 christos if (distance < 0)
2030 1.1.1.6 christos return TRUE;
2031 1.1.1.6 christos
2032 1.1.1.6 christos /* Take PT_GNU_RELRO segment into account by adding
2033 1.1.1.6 christos maxpagesize. */
2034 1.1.1.6 christos if ((toff + distance + get_elf_backend_data (abfd)->maxpagesize
2035 1.1.1.6 christos - roff + 0x80000000) > 0xffffffff)
2036 1.1.1.6 christos return TRUE;
2037 1.1.1.6 christos }
2038 1.1.1.6 christos
2039 1.1.1.6 christos convert:
2040 1.1.1.6 christos if (opcode == 0xff)
2041 1.1.1.6 christos {
2042 1.1.1.6 christos /* We have "call/jmp *foo@GOTPCREL(%rip)". */
2043 1.1.1.6 christos unsigned int nop;
2044 1.1.1.6 christos unsigned int disp;
2045 1.1.1.6 christos bfd_vma nop_offset;
2046 1.1.1.6 christos
2047 1.1.1.6 christos /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
2048 1.1.1.6 christos R_X86_64_PC32. */
2049 1.1.1.6 christos modrm = bfd_get_8 (abfd, contents + roff - 1);
2050 1.1.1.6 christos if (modrm == 0x25)
2051 1.1.1.6 christos {
2052 1.1.1.6 christos /* Convert to "jmp foo nop". */
2053 1.1.1.6 christos modrm = 0xe9;
2054 1.1.1.6 christos nop = NOP_OPCODE;
2055 1.1.1.6 christos nop_offset = irel->r_offset + 3;
2056 1.1.1.6 christos disp = bfd_get_32 (abfd, contents + irel->r_offset);
2057 1.1.1.6 christos irel->r_offset -= 1;
2058 1.1.1.6 christos bfd_put_32 (abfd, disp, contents + irel->r_offset);
2059 1.1.1.6 christos }
2060 1.1.1.6 christos else
2061 1.1.1.6 christos {
2062 1.1.1.6 christos struct elf_x86_64_link_hash_entry *eh
2063 1.1.1.6 christos = (struct elf_x86_64_link_hash_entry *) h;
2064 1.1.1.6 christos
2065 1.1.1.6 christos /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
2066 1.1.1.6 christos is a nop prefix. */
2067 1.1.1.6 christos modrm = 0xe8;
2068 1.1.1.6 christos /* To support TLS optimization, always use addr32 prefix for
2069 1.1.1.6 christos "call *__tls_get_addr@GOTPCREL(%rip)". */
2070 1.1.1.6 christos if (eh && eh->tls_get_addr == 1)
2071 1.1.1.6 christos {
2072 1.1.1.6 christos nop = 0x67;
2073 1.1.1.6 christos nop_offset = irel->r_offset - 2;
2074 1.1.1.6 christos }
2075 1.1.1.6 christos else
2076 1.1.1.6 christos {
2077 1.1.1.6 christos nop = link_info->call_nop_byte;
2078 1.1.1.6 christos if (link_info->call_nop_as_suffix)
2079 1.1.1.6 christos {
2080 1.1.1.6 christos nop_offset = irel->r_offset + 3;
2081 1.1.1.6 christos disp = bfd_get_32 (abfd, contents + irel->r_offset);
2082 1.1.1.6 christos irel->r_offset -= 1;
2083 1.1.1.6 christos bfd_put_32 (abfd, disp, contents + irel->r_offset);
2084 1.1.1.6 christos }
2085 1.1.1.6 christos else
2086 1.1.1.6 christos nop_offset = irel->r_offset - 2;
2087 1.1.1.6 christos }
2088 1.1.1.6 christos }
2089 1.1.1.6 christos bfd_put_8 (abfd, nop, contents + nop_offset);
2090 1.1.1.6 christos bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
2091 1.1.1.6 christos r_type = R_X86_64_PC32;
2092 1.1.1.6 christos }
2093 1.1.1.6 christos else
2094 1.1.1.6 christos {
2095 1.1.1.6 christos unsigned int rex;
2096 1.1.1.6 christos unsigned int rex_mask = REX_R;
2097 1.1.1.6 christos
2098 1.1.1.6 christos if (r_type == R_X86_64_REX_GOTPCRELX)
2099 1.1.1.6 christos rex = bfd_get_8 (abfd, contents + roff - 3);
2100 1.1.1.6 christos else
2101 1.1.1.6 christos rex = 0;
2102 1.1.1.6 christos
2103 1.1.1.6 christos if (opcode == 0x8b)
2104 1.1.1.6 christos {
2105 1.1.1.6 christos if (to_reloc_pc32)
2106 1.1.1.6 christos {
2107 1.1.1.6 christos /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2108 1.1.1.6 christos "lea foo(%rip), %reg". */
2109 1.1.1.6 christos opcode = 0x8d;
2110 1.1.1.6 christos r_type = R_X86_64_PC32;
2111 1.1.1.6 christos }
2112 1.1.1.6 christos else
2113 1.1.1.6 christos {
2114 1.1.1.6 christos /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2115 1.1.1.6 christos "mov $foo, %reg". */
2116 1.1.1.6 christos opcode = 0xc7;
2117 1.1.1.6 christos modrm = bfd_get_8 (abfd, contents + roff - 1);
2118 1.1.1.6 christos modrm = 0xc0 | (modrm & 0x38) >> 3;
2119 1.1.1.6 christos if ((rex & REX_W) != 0
2120 1.1.1.6 christos && ABI_64_P (link_info->output_bfd))
2121 1.1.1.6 christos {
2122 1.1.1.6 christos /* Keep the REX_W bit in REX byte for LP64. */
2123 1.1.1.6 christos r_type = R_X86_64_32S;
2124 1.1.1.6 christos goto rewrite_modrm_rex;
2125 1.1.1.6 christos }
2126 1.1.1.6 christos else
2127 1.1.1.6 christos {
2128 1.1.1.6 christos /* If the REX_W bit in REX byte isn't needed,
2129 1.1.1.6 christos use R_X86_64_32 and clear the W bit to avoid
2130 1.1.1.6 christos sign-extend imm32 to imm64. */
2131 1.1.1.6 christos r_type = R_X86_64_32;
2132 1.1.1.6 christos /* Clear the W bit in REX byte. */
2133 1.1.1.6 christos rex_mask |= REX_W;
2134 1.1.1.6 christos goto rewrite_modrm_rex;
2135 1.1.1.6 christos }
2136 1.1.1.6 christos }
2137 1.1.1.6 christos }
2138 1.1.1.6 christos else
2139 1.1.1.6 christos {
2140 1.1.1.6 christos /* R_X86_64_PC32 isn't supported. */
2141 1.1.1.6 christos if (to_reloc_pc32)
2142 1.1.1.6 christos return TRUE;
2143 1.1.1.6 christos
2144 1.1.1.6 christos modrm = bfd_get_8 (abfd, contents + roff - 1);
2145 1.1.1.6 christos if (opcode == 0x85)
2146 1.1.1.6 christos {
2147 1.1.1.6 christos /* Convert "test %reg, foo@GOTPCREL(%rip)" to
2148 1.1.1.6 christos "test $foo, %reg". */
2149 1.1.1.6 christos modrm = 0xc0 | (modrm & 0x38) >> 3;
2150 1.1.1.6 christos opcode = 0xf7;
2151 1.1.1.6 christos }
2152 1.1.1.6 christos else
2153 1.1.1.6 christos {
2154 1.1.1.6 christos /* Convert "binop foo@GOTPCREL(%rip), %reg" to
2155 1.1.1.6 christos "binop $foo, %reg". */
2156 1.1.1.6 christos modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
2157 1.1.1.6 christos opcode = 0x81;
2158 1.1.1.6 christos }
2159 1.1.1.6 christos
2160 1.1.1.6 christos /* Use R_X86_64_32 with 32-bit operand to avoid relocation
2161 1.1.1.6 christos overflow when sign-extending imm32 to imm64. */
2162 1.1.1.6 christos r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
2163 1.1.1.6 christos
2164 1.1.1.6 christos rewrite_modrm_rex:
2165 1.1.1.6 christos bfd_put_8 (abfd, modrm, contents + roff - 1);
2166 1.1.1.6 christos
2167 1.1.1.6 christos if (rex)
2168 1.1.1.6 christos {
2169 1.1.1.6 christos /* Move the R bit to the B bit in REX byte. */
2170 1.1.1.6 christos rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
2171 1.1.1.6 christos bfd_put_8 (abfd, rex, contents + roff - 3);
2172 1.1.1.6 christos }
2173 1.1.1.6 christos
2174 1.1.1.6 christos /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2175 1.1.1.6 christos irel->r_addend = 0;
2176 1.1.1.6 christos }
2177 1.1.1.6 christos
2178 1.1.1.6 christos bfd_put_8 (abfd, opcode, contents + roff - 2);
2179 1.1.1.6 christos }
2180 1.1.1.6 christos
2181 1.1.1.6 christos irel->r_info = htab->r_info (r_symndx, r_type);
2182 1.1.1.6 christos
2183 1.1.1.6 christos *converted = TRUE;
2184 1.1.1.6 christos
2185 1.1.1.5 christos return TRUE;
2186 1.1 christos }
2187 1.1 christos
2188 1.1 christos /* Look through the relocs for a section during the first phase, and
2189 1.1 christos calculate needed space in the global offset table, procedure
2190 1.1 christos linkage table, and dynamic reloc sections. */
2191 1.1 christos
2192 1.1 christos static bfd_boolean
2193 1.1 christos elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2194 1.1 christos asection *sec,
2195 1.1 christos const Elf_Internal_Rela *relocs)
2196 1.1 christos {
2197 1.1 christos struct elf_x86_64_link_hash_table *htab;
2198 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2199 1.1 christos struct elf_link_hash_entry **sym_hashes;
2200 1.1 christos const Elf_Internal_Rela *rel;
2201 1.1.1.6 christos const Elf_Internal_Rela *rel_end;
2202 1.1.1.4 christos asection *sreloc;
2203 1.1 christos bfd_byte *contents;
2204 1.1.1.6 christos bfd_boolean use_plt_got;
2205 1.1.1.6 christos
2206 1.1.1.6 christos if (bfd_link_relocatable (info))
2207 1.1.1.6 christos return TRUE;
2208 1.1.1.6 christos
2209 1.1.1.6 christos /* Don't do anything special with non-loaded, non-alloced sections.
2210 1.1.1.6 christos In particular, any relocs in such sections should not affect GOT
2211 1.1.1.6 christos and PLT reference counting (ie. we don't allow them to create GOT
2212 1.1.1.6 christos or PLT entries), there's no possibility or desire to optimize TLS
2213 1.1.1.6 christos relocs, and there's not much point in propagating relocs to shared
2214 1.1 christos libs that the dynamic linker won't relocate. */
2215 1.1 christos if ((sec->flags & SEC_ALLOC) == 0)
2216 1.1 christos return TRUE;
2217 1.1 christos
2218 1.1 christos BFD_ASSERT (is_x86_64_elf (abfd));
2219 1.1 christos
2220 1.1.1.6 christos htab = elf_x86_64_hash_table (info);
2221 1.1.1.6 christos if (htab == NULL)
2222 1.1.1.6 christos {
2223 1.1.1.6 christos sec->check_relocs_failed = 1;
2224 1.1.1.6 christos return FALSE;
2225 1.1.1.6 christos }
2226 1.1.1.6 christos
2227 1.1.1.6 christos /* Get the section contents. */
2228 1.1.1.6 christos if (elf_section_data (sec)->this_hdr.contents != NULL)
2229 1.1.1.6 christos contents = elf_section_data (sec)->this_hdr.contents;
2230 1.1.1.6 christos else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2231 1.1.1.6 christos {
2232 1.1.1.6 christos sec->check_relocs_failed = 1;
2233 1.1 christos return FALSE;
2234 1.1.1.4 christos }
2235 1.1.1.4 christos
2236 1.1 christos use_plt_got = get_elf_x86_64_backend_data (abfd) == &elf_x86_64_arch_bed;
2237 1.1 christos
2238 1.1 christos symtab_hdr = &elf_symtab_hdr (abfd);
2239 1.1 christos sym_hashes = elf_sym_hashes (abfd);
2240 1.1 christos
2241 1.1 christos sreloc = NULL;
2242 1.1 christos
2243 1.1 christos rel_end = relocs + sec->reloc_count;
2244 1.1 christos for (rel = relocs; rel < rel_end; rel++)
2245 1.1 christos {
2246 1.1 christos unsigned int r_type;
2247 1.1.1.6 christos unsigned long r_symndx;
2248 1.1 christos struct elf_link_hash_entry *h;
2249 1.1 christos struct elf_x86_64_link_hash_entry *eh;
2250 1.1.1.2 christos Elf_Internal_Sym *isym;
2251 1.1 christos const char *name;
2252 1.1 christos bfd_boolean size_reloc;
2253 1.1 christos
2254 1.1 christos r_symndx = htab->r_sym (rel->r_info);
2255 1.1 christos r_type = ELF32_R_TYPE (rel->r_info);
2256 1.1 christos
2257 1.1.1.7 christos if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2258 1.1.1.7 christos {
2259 1.1.1.7 christos /* xgettext:c-format */
2260 1.1.1.6 christos _bfd_error_handler (_("%B: bad symbol index: %d"),
2261 1.1 christos abfd, r_symndx);
2262 1.1 christos goto error_return;
2263 1.1 christos }
2264 1.1 christos
2265 1.1 christos if (r_symndx < symtab_hdr->sh_info)
2266 1.1 christos {
2267 1.1 christos /* A local symbol. */
2268 1.1 christos isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2269 1.1.1.6 christos abfd, r_symndx);
2270 1.1 christos if (isym == NULL)
2271 1.1 christos goto error_return;
2272 1.1 christos
2273 1.1 christos /* Check relocation against local STT_GNU_IFUNC symbol. */
2274 1.1 christos if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2275 1.1 christos {
2276 1.1 christos h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
2277 1.1.1.6 christos TRUE);
2278 1.1 christos if (h == NULL)
2279 1.1 christos goto error_return;
2280 1.1 christos
2281 1.1 christos /* Fake a STT_GNU_IFUNC symbol. */
2282 1.1 christos h->type = STT_GNU_IFUNC;
2283 1.1 christos h->def_regular = 1;
2284 1.1 christos h->ref_regular = 1;
2285 1.1 christos h->forced_local = 1;
2286 1.1 christos h->root.type = bfd_link_hash_defined;
2287 1.1 christos }
2288 1.1 christos else
2289 1.1 christos h = NULL;
2290 1.1 christos }
2291 1.1 christos else
2292 1.1 christos {
2293 1.1 christos isym = NULL;
2294 1.1 christos h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2295 1.1 christos while (h->root.type == bfd_link_hash_indirect
2296 1.1 christos || h->root.type == bfd_link_hash_warning)
2297 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
2298 1.1 christos }
2299 1.1 christos
2300 1.1 christos /* Check invalid x32 relocations. */
2301 1.1 christos if (!ABI_64_P (abfd))
2302 1.1 christos switch (r_type)
2303 1.1 christos {
2304 1.1 christos default:
2305 1.1 christos break;
2306 1.1 christos
2307 1.1 christos case R_X86_64_DTPOFF64:
2308 1.1 christos case R_X86_64_TPOFF64:
2309 1.1 christos case R_X86_64_PC64:
2310 1.1 christos case R_X86_64_GOTOFF64:
2311 1.1 christos case R_X86_64_GOT64:
2312 1.1 christos case R_X86_64_GOTPCREL64:
2313 1.1 christos case R_X86_64_GOTPC64:
2314 1.1 christos case R_X86_64_GOTPLT64:
2315 1.1 christos case R_X86_64_PLTOFF64:
2316 1.1 christos {
2317 1.1 christos if (h)
2318 1.1 christos name = h->root.root.string;
2319 1.1 christos else
2320 1.1.1.7 christos name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2321 1.1.1.7 christos NULL);
2322 1.1 christos _bfd_error_handler
2323 1.1 christos /* xgettext:c-format */
2324 1.1 christos (_("%B: relocation %s against symbol `%s' isn't "
2325 1.1 christos "supported in x32 mode"), abfd,
2326 1.1.1.6 christos x86_64_elf_howto_table[r_type].name, name);
2327 1.1 christos bfd_set_error (bfd_error_bad_value);
2328 1.1 christos goto error_return;
2329 1.1 christos }
2330 1.1 christos break;
2331 1.1 christos }
2332 1.1 christos
2333 1.1 christos if (h != NULL)
2334 1.1 christos {
2335 1.1 christos switch (r_type)
2336 1.1 christos {
2337 1.1 christos default:
2338 1.1.1.4 christos break;
2339 1.1.1.4 christos
2340 1.1.1.4 christos case R_X86_64_PC32_BND:
2341 1.1.1.4 christos case R_X86_64_PLT32_BND:
2342 1.1 christos case R_X86_64_PC32:
2343 1.1 christos case R_X86_64_PLT32:
2344 1.1.1.4 christos case R_X86_64_32:
2345 1.1.1.4 christos case R_X86_64_64:
2346 1.1.1.4 christos /* MPX PLT is supported only if elf_x86_64_arch_bed
2347 1.1.1.7 christos is used in 64-bit mode. */
2348 1.1.1.7 christos if (ABI_64_P (abfd)
2349 1.1.1.7 christos && info->bndplt
2350 1.1.1.4 christos && (get_elf_x86_64_backend_data (abfd)
2351 1.1.1.4 christos == &elf_x86_64_arch_bed))
2352 1.1.1.4 christos {
2353 1.1.1.4 christos elf_x86_64_hash_entry (h)->has_bnd_reloc = 1;
2354 1.1.1.4 christos
2355 1.1.1.4 christos /* Create the second PLT for Intel MPX support. */
2356 1.1.1.4 christos if (htab->plt_bnd == NULL)
2357 1.1.1.4 christos {
2358 1.1.1.4 christos const struct elf_backend_data *bed;
2359 1.1.1.4 christos
2360 1.1.1.4 christos bed = get_elf_backend_data (info->output_bfd);
2361 1.1.1.4 christos BFD_ASSERT (sizeof (elf_x86_64_bnd_plt2_entry) == 8
2362 1.1.1.4 christos && (sizeof (elf_x86_64_bnd_plt2_entry)
2363 1.1.1.4 christos == sizeof (elf_x86_64_legacy_plt2_entry)));
2364 1.1.1.4 christos
2365 1.1.1.4 christos if (htab->elf.dynobj == NULL)
2366 1.1.1.4 christos htab->elf.dynobj = abfd;
2367 1.1.1.4 christos htab->plt_bnd
2368 1.1.1.4 christos = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2369 1.1.1.4 christos ".plt.bnd",
2370 1.1.1.4 christos (bed->dynamic_sec_flags
2371 1.1.1.4 christos | SEC_ALLOC
2372 1.1.1.4 christos | SEC_CODE
2373 1.1.1.4 christos | SEC_LOAD
2374 1.1.1.4 christos | SEC_READONLY));
2375 1.1.1.4 christos if (htab->plt_bnd == NULL
2376 1.1.1.7 christos || !bfd_set_section_alignment (htab->elf.dynobj,
2377 1.1.1.7 christos htab->plt_bnd,
2378 1.1.1.7 christos 3))
2379 1.1.1.7 christos goto error_return;
2380 1.1.1.7 christos }
2381 1.1.1.7 christos
2382 1.1.1.7 christos if (!info->no_ld_generated_unwind_info
2383 1.1.1.7 christos && htab->plt_bnd_eh_frame == NULL)
2384 1.1.1.7 christos {
2385 1.1.1.7 christos flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2386 1.1.1.7 christos | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2387 1.1.1.7 christos | SEC_LINKER_CREATED);
2388 1.1.1.7 christos htab->plt_bnd_eh_frame
2389 1.1.1.7 christos = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2390 1.1.1.7 christos ".eh_frame",
2391 1.1.1.7 christos flags);
2392 1.1.1.7 christos if (htab->plt_bnd_eh_frame == NULL
2393 1.1.1.7 christos || !bfd_set_section_alignment (htab->elf.dynobj,
2394 1.1.1.6 christos htab->plt_bnd_eh_frame,
2395 1.1.1.4 christos 3))
2396 1.1.1.4 christos goto error_return;
2397 1.1.1.7 christos }
2398 1.1.1.4 christos }
2399 1.1.1.4 christos /* Fall through. */
2400 1.1 christos
2401 1.1 christos case R_X86_64_32S:
2402 1.1.1.6 christos case R_X86_64_PC64:
2403 1.1.1.6 christos case R_X86_64_GOTPCREL:
2404 1.1 christos case R_X86_64_GOTPCRELX:
2405 1.1.1.2 christos case R_X86_64_REX_GOTPCRELX:
2406 1.1.1.2 christos case R_X86_64_GOTPCREL64:
2407 1.1.1.6 christos if (htab->elf.dynobj == NULL)
2408 1.1.1.6 christos htab->elf.dynobj = abfd;
2409 1.1.1.6 christos /* Create the ifunc sections for static executables. */
2410 1.1.1.6 christos if (h->type == STT_GNU_IFUNC
2411 1.1.1.6 christos && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj,
2412 1.1 christos info))
2413 1.1 christos goto error_return;
2414 1.1 christos break;
2415 1.1.1.2 christos }
2416 1.1.1.2 christos
2417 1.1.1.3 christos /* It is referenced by a non-shared object. */
2418 1.1.1.6 christos h->ref_regular = 1;
2419 1.1.1.6 christos h->root.non_ir_ref = 1;
2420 1.1.1.6 christos
2421 1.1.1.6 christos if (h->type == STT_GNU_IFUNC)
2422 1.1 christos elf_tdata (info->output_bfd)->has_gnu_symbols
2423 1.1 christos |= elf_gnu_symbol_ifunc;
2424 1.1.1.6 christos }
2425 1.1 christos
2426 1.1 christos if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2427 1.1.1.6 christos symtab_hdr, sym_hashes,
2428 1.1.1.6 christos &r_type, GOT_UNKNOWN,
2429 1.1 christos rel, rel_end, h, r_symndx, FALSE))
2430 1.1.1.6 christos goto error_return;
2431 1.1 christos
2432 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) h;
2433 1.1 christos switch (r_type)
2434 1.1 christos {
2435 1.1 christos case R_X86_64_TLSLD:
2436 1.1 christos htab->tls_ld_got.refcount += 1;
2437 1.1 christos goto create_got;
2438 1.1.1.6 christos
2439 1.1.1.6 christos case R_X86_64_TPOFF32:
2440 1.1.1.6 christos if (!bfd_link_executable (info) && ABI_64_P (abfd))
2441 1.1.1.6 christos return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
2442 1.1.1.6 christos &x86_64_elf_howto_table[r_type]);
2443 1.1 christos if (eh != NULL)
2444 1.1 christos eh->has_got_reloc = 1;
2445 1.1 christos break;
2446 1.1.1.6 christos
2447 1.1 christos case R_X86_64_GOTTPOFF:
2448 1.1 christos if (!bfd_link_executable (info))
2449 1.1 christos info->flags |= DF_STATIC_TLS;
2450 1.1 christos /* Fall through */
2451 1.1 christos
2452 1.1.1.6 christos case R_X86_64_GOT32:
2453 1.1.1.6 christos case R_X86_64_GOTPCREL:
2454 1.1 christos case R_X86_64_GOTPCRELX:
2455 1.1 christos case R_X86_64_REX_GOTPCRELX:
2456 1.1 christos case R_X86_64_TLSGD:
2457 1.1 christos case R_X86_64_GOT64:
2458 1.1 christos case R_X86_64_GOTPCREL64:
2459 1.1 christos case R_X86_64_GOTPLT64:
2460 1.1 christos case R_X86_64_GOTPC32_TLSDESC:
2461 1.1 christos case R_X86_64_TLSDESC_CALL:
2462 1.1 christos /* This symbol requires a global offset table entry. */
2463 1.1 christos {
2464 1.1 christos int tls_type, old_tls_type;
2465 1.1 christos
2466 1.1 christos switch (r_type)
2467 1.1 christos {
2468 1.1 christos default: tls_type = GOT_NORMAL; break;
2469 1.1 christos case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
2470 1.1 christos case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
2471 1.1 christos case R_X86_64_GOTPC32_TLSDESC:
2472 1.1 christos case R_X86_64_TLSDESC_CALL:
2473 1.1 christos tls_type = GOT_TLS_GDESC; break;
2474 1.1 christos }
2475 1.1 christos
2476 1.1 christos if (h != NULL)
2477 1.1.1.6 christos {
2478 1.1 christos h->got.refcount += 1;
2479 1.1 christos old_tls_type = eh->tls_type;
2480 1.1 christos }
2481 1.1 christos else
2482 1.1 christos {
2483 1.1 christos bfd_signed_vma *local_got_refcounts;
2484 1.1 christos
2485 1.1 christos /* This is a global offset table entry for a local symbol. */
2486 1.1 christos local_got_refcounts = elf_local_got_refcounts (abfd);
2487 1.1 christos if (local_got_refcounts == NULL)
2488 1.1 christos {
2489 1.1 christos bfd_size_type size;
2490 1.1 christos
2491 1.1 christos size = symtab_hdr->sh_info;
2492 1.1 christos size *= sizeof (bfd_signed_vma)
2493 1.1 christos + sizeof (bfd_vma) + sizeof (char);
2494 1.1 christos local_got_refcounts = ((bfd_signed_vma *)
2495 1.1.1.6 christos bfd_zalloc (abfd, size));
2496 1.1 christos if (local_got_refcounts == NULL)
2497 1.1 christos goto error_return;
2498 1.1 christos elf_local_got_refcounts (abfd) = local_got_refcounts;
2499 1.1 christos elf_x86_64_local_tlsdesc_gotent (abfd)
2500 1.1 christos = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2501 1.1 christos elf_x86_64_local_got_tls_type (abfd)
2502 1.1 christos = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2503 1.1 christos }
2504 1.1 christos local_got_refcounts[r_symndx] += 1;
2505 1.1 christos old_tls_type
2506 1.1 christos = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
2507 1.1 christos }
2508 1.1 christos
2509 1.1 christos /* If a TLS symbol is accessed using IE at least once,
2510 1.1 christos there is no point to use dynamic model for it. */
2511 1.1 christos if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2512 1.1 christos && (! GOT_TLS_GD_ANY_P (old_tls_type)
2513 1.1 christos || tls_type != GOT_TLS_IE))
2514 1.1 christos {
2515 1.1 christos if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2516 1.1 christos tls_type = old_tls_type;
2517 1.1 christos else if (GOT_TLS_GD_ANY_P (old_tls_type)
2518 1.1 christos && GOT_TLS_GD_ANY_P (tls_type))
2519 1.1 christos tls_type |= old_tls_type;
2520 1.1 christos else
2521 1.1 christos {
2522 1.1 christos if (h)
2523 1.1 christos name = h->root.root.string;
2524 1.1 christos else
2525 1.1.1.7 christos name = bfd_elf_sym_name (abfd, symtab_hdr,
2526 1.1.1.7 christos isym, NULL);
2527 1.1.1.7 christos _bfd_error_handler
2528 1.1.1.7 christos /* xgettext:c-format */
2529 1.1 christos (_("%B: '%s' accessed both as normal and"
2530 1.1.1.2 christos " thread local symbol"),
2531 1.1.1.6 christos abfd, name);
2532 1.1 christos bfd_set_error (bfd_error_bad_value);
2533 1.1 christos goto error_return;
2534 1.1 christos }
2535 1.1 christos }
2536 1.1 christos
2537 1.1.1.6 christos if (old_tls_type != tls_type)
2538 1.1.1.6 christos {
2539 1.1 christos if (eh != NULL)
2540 1.1 christos eh->tls_type = tls_type;
2541 1.1 christos else
2542 1.1 christos elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
2543 1.1 christos }
2544 1.1 christos }
2545 1.1 christos /* Fall through */
2546 1.1 christos
2547 1.1 christos case R_X86_64_GOTOFF64:
2548 1.1 christos case R_X86_64_GOTPC32:
2549 1.1.1.6 christos case R_X86_64_GOTPC64:
2550 1.1.1.6 christos create_got:
2551 1.1 christos if (eh != NULL)
2552 1.1 christos eh->has_got_reloc = 1;
2553 1.1 christos if (htab->elf.sgot == NULL)
2554 1.1 christos {
2555 1.1 christos if (htab->elf.dynobj == NULL)
2556 1.1 christos htab->elf.dynobj = abfd;
2557 1.1.1.6 christos if (!_bfd_elf_create_got_section (htab->elf.dynobj,
2558 1.1 christos info))
2559 1.1 christos goto error_return;
2560 1.1 christos }
2561 1.1 christos break;
2562 1.1.1.3 christos
2563 1.1 christos case R_X86_64_PLT32:
2564 1.1 christos case R_X86_64_PLT32_BND:
2565 1.1 christos /* This symbol requires a procedure linkage table entry. We
2566 1.1 christos actually build the entry in adjust_dynamic_symbol,
2567 1.1 christos because this might be a case of linking PIC code which is
2568 1.1 christos never referenced by a dynamic object, in which case we
2569 1.1 christos don't need to generate a procedure linkage table entry
2570 1.1 christos after all. */
2571 1.1 christos
2572 1.1 christos /* If this is a local symbol, we resolve it directly without
2573 1.1 christos creating a procedure linkage table entry. */
2574 1.1 christos if (h == NULL)
2575 1.1.1.6 christos continue;
2576 1.1 christos
2577 1.1 christos eh->has_got_reloc = 1;
2578 1.1 christos h->needs_plt = 1;
2579 1.1 christos h->plt.refcount += 1;
2580 1.1 christos break;
2581 1.1 christos
2582 1.1 christos case R_X86_64_PLTOFF64:
2583 1.1 christos /* This tries to form the 'address' of a function relative
2584 1.1 christos to GOT. For global symbols we need a PLT entry. */
2585 1.1 christos if (h != NULL)
2586 1.1 christos {
2587 1.1 christos h->needs_plt = 1;
2588 1.1 christos h->plt.refcount += 1;
2589 1.1 christos }
2590 1.1.1.2 christos goto create_got;
2591 1.1.1.2 christos
2592 1.1.1.2 christos case R_X86_64_SIZE32:
2593 1.1.1.2 christos case R_X86_64_SIZE64:
2594 1.1.1.2 christos size_reloc = TRUE;
2595 1.1 christos goto do_size;
2596 1.1 christos
2597 1.1 christos case R_X86_64_32:
2598 1.1.1.7 christos if (!ABI_64_P (abfd))
2599 1.1 christos goto pointer;
2600 1.1 christos /* Fall through. */
2601 1.1 christos case R_X86_64_8:
2602 1.1.1.6 christos case R_X86_64_16:
2603 1.1.1.6 christos case R_X86_64_32S:
2604 1.1 christos /* Check relocation overflow as these relocs may lead to
2605 1.1.1.6 christos run-time relocation overflow. Don't error out for
2606 1.1.1.6 christos sections we don't care about, such as debug sections or
2607 1.1.1.6 christos when relocation overflow check is disabled. */
2608 1.1.1.6 christos if (!info->no_reloc_overflow_check
2609 1.1.1.6 christos && (bfd_link_pic (info)
2610 1.1.1.6 christos || (bfd_link_executable (info)
2611 1.1.1.6 christos && h != NULL
2612 1.1.1.6 christos && !h->def_regular
2613 1.1.1.6 christos && h->def_dynamic
2614 1.1.1.6 christos && (sec->flags & SEC_READONLY) == 0)))
2615 1.1 christos return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
2616 1.1 christos &x86_64_elf_howto_table[r_type]);
2617 1.1 christos /* Fall through. */
2618 1.1 christos
2619 1.1 christos case R_X86_64_PC8:
2620 1.1.1.3 christos case R_X86_64_PC16:
2621 1.1 christos case R_X86_64_PC32:
2622 1.1 christos case R_X86_64_PC32_BND:
2623 1.1 christos case R_X86_64_PC64:
2624 1.1.1.6 christos case R_X86_64_64:
2625 1.1.1.6 christos pointer:
2626 1.1.1.6 christos if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2627 1.1.1.6 christos eh->has_non_got_reloc = 1;
2628 1.1.1.6 christos /* We are called after all symbols have been resolved. Only
2629 1.1.1.6 christos relocation against STT_GNU_IFUNC symbol must go through
2630 1.1.1.6 christos PLT. */
2631 1.1.1.6 christos if (h != NULL
2632 1.1 christos && (bfd_link_executable (info)
2633 1.1 christos || h->type == STT_GNU_IFUNC))
2634 1.1 christos {
2635 1.1 christos /* If this reloc is in a read-only section, we might
2636 1.1 christos need a copy reloc. We can't check reliably at this
2637 1.1 christos stage whether the section is read-only, as input
2638 1.1 christos sections have not yet been mapped to output sections.
2639 1.1 christos Tentatively set the flag for now, and correct in
2640 1.1 christos adjust_dynamic_symbol. */
2641 1.1.1.6 christos h->non_got_ref = 1;
2642 1.1.1.6 christos
2643 1.1.1.6 christos /* We may need a .plt entry if the symbol is a function
2644 1.1.1.6 christos defined in a shared lib or is a STT_GNU_IFUNC function
2645 1.1.1.6 christos referenced from the code or read-only section. */
2646 1.1.1.6 christos if (!h->def_regular
2647 1.1.1.6 christos || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2648 1.1.1.6 christos h->plt.refcount += 1;
2649 1.1.1.6 christos
2650 1.1.1.6 christos if (r_type == R_X86_64_PC32)
2651 1.1.1.6 christos {
2652 1.1.1.6 christos /* Since something like ".long foo - ." may be used
2653 1.1.1.6 christos as pointer, make sure that PLT is used if foo is
2654 1.1.1.6 christos a function defined in a shared library. */
2655 1.1.1.6 christos if ((sec->flags & SEC_CODE) == 0)
2656 1.1.1.6 christos h->pointer_equality_needed = 1;
2657 1.1.1.6 christos }
2658 1.1.1.6 christos else if (r_type != R_X86_64_PC32_BND
2659 1.1.1.6 christos && r_type != R_X86_64_PC64)
2660 1.1.1.6 christos {
2661 1.1.1.6 christos h->pointer_equality_needed = 1;
2662 1.1.1.6 christos /* At run-time, R_X86_64_64 can be resolved for both
2663 1.1.1.6 christos x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2664 1.1.1.6 christos can only be resolved for x32. */
2665 1.1.1.6 christos if ((sec->flags & SEC_READONLY) == 0
2666 1.1.1.6 christos && (r_type == R_X86_64_64
2667 1.1.1.6 christos || (!ABI_64_P (abfd)
2668 1.1.1.6 christos && (r_type == R_X86_64_32
2669 1.1.1.6 christos || r_type == R_X86_64_32S))))
2670 1.1 christos eh->func_pointer_refcount += 1;
2671 1.1 christos }
2672 1.1.1.2 christos }
2673 1.1.1.2 christos
2674 1.1 christos size_reloc = FALSE;
2675 1.1 christos do_size:
2676 1.1 christos /* If we are creating a shared library, and this is a reloc
2677 1.1 christos against a global symbol, or a non PC relative reloc
2678 1.1 christos against a local symbol, then we need to copy the reloc
2679 1.1 christos into the shared library. However, if we are linking with
2680 1.1 christos -Bsymbolic, we do not need to copy a reloc against a
2681 1.1 christos global symbol which is defined in an object we are
2682 1.1 christos including in the link (i.e., DEF_REGULAR is set). At
2683 1.1 christos this point we have not seen all the input files, so it is
2684 1.1 christos possible that DEF_REGULAR is not set now but will be set
2685 1.1 christos later (it is never cleared). In case of a weak definition,
2686 1.1 christos DEF_REGULAR may be cleared later by a strong definition in
2687 1.1 christos a shared library. We account for that possibility below by
2688 1.1 christos storing information in the relocs_copied field of the hash
2689 1.1 christos table entry. A similar situation occurs when creating
2690 1.1 christos shared libraries and symbol visibility changes render the
2691 1.1 christos symbol local.
2692 1.1 christos
2693 1.1 christos If on the other hand, we are creating an executable, we
2694 1.1.1.6 christos may need to keep relocations for symbols satisfied by a
2695 1.1.1.6 christos dynamic library if we manage to avoid copy relocs for the
2696 1.1.1.6 christos symbol.
2697 1.1.1.6 christos
2698 1.1.1.6 christos Generate dynamic pointer relocation against STT_GNU_IFUNC
2699 1.1 christos symbol in the non-code section. */
2700 1.1 christos if ((bfd_link_pic (info)
2701 1.1.1.6 christos && (! IS_X86_64_PCREL_TYPE (r_type)
2702 1.1.1.6 christos || (h != NULL
2703 1.1 christos && (! (bfd_link_pie (info)
2704 1.1 christos || SYMBOLIC_BIND (info, h))
2705 1.1.1.6 christos || h->root.type == bfd_link_hash_defweak
2706 1.1.1.6 christos || !h->def_regular))))
2707 1.1.1.6 christos || (h != NULL
2708 1.1.1.6 christos && h->type == STT_GNU_IFUNC
2709 1.1 christos && r_type == htab->pointer_r_type
2710 1.1.1.6 christos && (sec->flags & SEC_CODE) == 0)
2711 1.1 christos || (ELIMINATE_COPY_RELOCS
2712 1.1 christos && !bfd_link_pic (info)
2713 1.1 christos && h != NULL
2714 1.1 christos && (h->root.type == bfd_link_hash_defweak
2715 1.1 christos || !h->def_regular)))
2716 1.1 christos {
2717 1.1 christos struct elf_dyn_relocs *p;
2718 1.1 christos struct elf_dyn_relocs **head;
2719 1.1 christos
2720 1.1 christos /* We must copy these reloc types into the output file.
2721 1.1 christos Create a reloc section in dynobj and make room for
2722 1.1 christos this reloc. */
2723 1.1 christos if (sreloc == NULL)
2724 1.1 christos {
2725 1.1 christos if (htab->elf.dynobj == NULL)
2726 1.1 christos htab->elf.dynobj = abfd;
2727 1.1 christos
2728 1.1 christos sreloc = _bfd_elf_make_dynamic_reloc_section
2729 1.1 christos (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2730 1.1 christos abfd, /*rela?*/ TRUE);
2731 1.1.1.6 christos
2732 1.1 christos if (sreloc == NULL)
2733 1.1 christos goto error_return;
2734 1.1 christos }
2735 1.1 christos
2736 1.1 christos /* If this is a global symbol, we count the number of
2737 1.1.1.6 christos relocations we need for this symbol. */
2738 1.1 christos if (h != NULL)
2739 1.1 christos head = &eh->dyn_relocs;
2740 1.1 christos else
2741 1.1 christos {
2742 1.1 christos /* Track dynamic relocs needed for local syms too.
2743 1.1 christos We really need local syms available to do this
2744 1.1 christos easily. Oh well. */
2745 1.1 christos asection *s;
2746 1.1 christos void **vpp;
2747 1.1 christos
2748 1.1 christos isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2749 1.1.1.6 christos abfd, r_symndx);
2750 1.1 christos if (isym == NULL)
2751 1.1 christos goto error_return;
2752 1.1 christos
2753 1.1 christos s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2754 1.1 christos if (s == NULL)
2755 1.1 christos s = sec;
2756 1.1 christos
2757 1.1 christos /* Beware of type punned pointers vs strict aliasing
2758 1.1 christos rules. */
2759 1.1 christos vpp = &(elf_section_data (s)->local_dynrel);
2760 1.1 christos head = (struct elf_dyn_relocs **)vpp;
2761 1.1 christos }
2762 1.1 christos
2763 1.1 christos p = *head;
2764 1.1 christos if (p == NULL || p->sec != sec)
2765 1.1 christos {
2766 1.1 christos bfd_size_type amt = sizeof *p;
2767 1.1 christos
2768 1.1 christos p = ((struct elf_dyn_relocs *)
2769 1.1.1.6 christos bfd_alloc (htab->elf.dynobj, amt));
2770 1.1 christos if (p == NULL)
2771 1.1 christos goto error_return;
2772 1.1 christos p->next = *head;
2773 1.1 christos *head = p;
2774 1.1 christos p->sec = sec;
2775 1.1 christos p->count = 0;
2776 1.1 christos p->pc_count = 0;
2777 1.1 christos }
2778 1.1.1.2 christos
2779 1.1.1.2 christos p->count += 1;
2780 1.1 christos /* Count size relocation as PC-relative relocation. */
2781 1.1 christos if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
2782 1.1 christos p->pc_count += 1;
2783 1.1 christos }
2784 1.1 christos break;
2785 1.1 christos
2786 1.1 christos /* This relocation describes the C++ object vtable hierarchy.
2787 1.1 christos Reconstruct it for later use during GC. */
2788 1.1.1.6 christos case R_X86_64_GNU_VTINHERIT:
2789 1.1 christos if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2790 1.1 christos goto error_return;
2791 1.1 christos break;
2792 1.1 christos
2793 1.1 christos /* This relocation describes which C++ vtable entries are actually
2794 1.1 christos used. Record for later use during GC. */
2795 1.1 christos case R_X86_64_GNU_VTENTRY:
2796 1.1 christos BFD_ASSERT (h != NULL);
2797 1.1.1.6 christos if (h != NULL
2798 1.1 christos && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2799 1.1 christos goto error_return;
2800 1.1 christos break;
2801 1.1 christos
2802 1.1 christos default:
2803 1.1.1.4 christos break;
2804 1.1.1.4 christos }
2805 1.1.1.4 christos
2806 1.1.1.4 christos if (use_plt_got
2807 1.1.1.5 christos && h != NULL
2808 1.1.1.5 christos && h->plt.refcount > 0
2809 1.1.1.4 christos && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2810 1.1.1.4 christos || h->got.refcount > 0)
2811 1.1.1.4 christos && htab->plt_got == NULL)
2812 1.1.1.4 christos {
2813 1.1.1.4 christos /* Create the GOT procedure linkage table. */
2814 1.1.1.4 christos unsigned int plt_got_align;
2815 1.1.1.4 christos const struct elf_backend_data *bed;
2816 1.1.1.4 christos
2817 1.1.1.4 christos bed = get_elf_backend_data (info->output_bfd);
2818 1.1.1.4 christos BFD_ASSERT (sizeof (elf_x86_64_legacy_plt2_entry) == 8
2819 1.1.1.4 christos && (sizeof (elf_x86_64_bnd_plt2_entry)
2820 1.1.1.4 christos == sizeof (elf_x86_64_legacy_plt2_entry)));
2821 1.1.1.4 christos plt_got_align = 3;
2822 1.1.1.4 christos
2823 1.1.1.4 christos if (htab->elf.dynobj == NULL)
2824 1.1.1.4 christos htab->elf.dynobj = abfd;
2825 1.1.1.4 christos htab->plt_got
2826 1.1.1.4 christos = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2827 1.1.1.4 christos ".plt.got",
2828 1.1.1.4 christos (bed->dynamic_sec_flags
2829 1.1.1.4 christos | SEC_ALLOC
2830 1.1.1.4 christos | SEC_CODE
2831 1.1.1.4 christos | SEC_LOAD
2832 1.1.1.4 christos | SEC_READONLY));
2833 1.1.1.4 christos if (htab->plt_got == NULL
2834 1.1.1.4 christos || !bfd_set_section_alignment (htab->elf.dynobj,
2835 1.1.1.6 christos htab->plt_got,
2836 1.1.1.7 christos plt_got_align))
2837 1.1.1.7 christos goto error_return;
2838 1.1.1.7 christos
2839 1.1.1.7 christos if (!info->no_ld_generated_unwind_info
2840 1.1.1.7 christos && htab->plt_got_eh_frame == NULL
2841 1.1.1.7 christos && get_elf_x86_64_backend_data (abfd)->eh_frame_plt_got != NULL)
2842 1.1.1.7 christos {
2843 1.1.1.7 christos flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2844 1.1.1.7 christos | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2845 1.1.1.7 christos | SEC_LINKER_CREATED);
2846 1.1.1.7 christos htab->plt_got_eh_frame
2847 1.1.1.7 christos = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2848 1.1.1.7 christos ".eh_frame",
2849 1.1.1.7 christos flags);
2850 1.1.1.7 christos if (htab->plt_got_eh_frame == NULL
2851 1.1.1.7 christos || !bfd_set_section_alignment (htab->elf.dynobj,
2852 1.1.1.7 christos htab->plt_got_eh_frame,
2853 1.1.1.7 christos ABI_64_P (htab->elf.dynobj) ? 3 : 2))
2854 1.1.1.4 christos goto error_return;
2855 1.1.1.5 christos }
2856 1.1.1.6 christos }
2857 1.1.1.6 christos
2858 1.1.1.6 christos if ((r_type == R_X86_64_GOTPCREL
2859 1.1.1.5 christos || r_type == R_X86_64_GOTPCRELX
2860 1.1.1.6 christos || r_type == R_X86_64_REX_GOTPCRELX)
2861 1.1.1.6 christos && (h == NULL || h->type != STT_GNU_IFUNC))
2862 1.1.1.6 christos sec->need_convert_load = 1;
2863 1.1.1.6 christos }
2864 1.1.1.6 christos
2865 1.1.1.6 christos if (elf_section_data (sec)->this_hdr.contents != contents)
2866 1.1.1.6 christos {
2867 1.1.1.6 christos if (!info->keep_memory)
2868 1.1.1.6 christos free (contents);
2869 1.1.1.6 christos else
2870 1.1.1.6 christos {
2871 1.1.1.6 christos /* Cache the section contents for elf_link_input_bfd. */
2872 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
2873 1.1 christos }
2874 1.1 christos }
2875 1.1.1.6 christos
2876 1.1.1.6 christos return TRUE;
2877 1.1.1.6 christos
2878 1.1.1.6 christos error_return:
2879 1.1.1.6 christos if (elf_section_data (sec)->this_hdr.contents != contents)
2880 1.1.1.6 christos free (contents);
2881 1.1 christos sec->check_relocs_failed = 1;
2882 1.1 christos return FALSE;
2883 1.1 christos }
2884 1.1 christos
2885 1.1 christos /* Return the section that should be marked against GC for a given
2886 1.1 christos relocation. */
2887 1.1 christos
2888 1.1 christos static asection *
2889 1.1 christos elf_x86_64_gc_mark_hook (asection *sec,
2890 1.1 christos struct bfd_link_info *info,
2891 1.1 christos Elf_Internal_Rela *rel,
2892 1.1 christos struct elf_link_hash_entry *h,
2893 1.1 christos Elf_Internal_Sym *sym)
2894 1.1 christos {
2895 1.1 christos if (h != NULL)
2896 1.1 christos switch (ELF32_R_TYPE (rel->r_info))
2897 1.1 christos {
2898 1.1 christos case R_X86_64_GNU_VTINHERIT:
2899 1.1 christos case R_X86_64_GNU_VTENTRY:
2900 1.1 christos return NULL;
2901 1.1 christos }
2902 1.1 christos
2903 1.1 christos return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2904 1.1.1.6 christos }
2905 1.1.1.6 christos
2906 1.1 christos /* Remove undefined weak symbol from the dynamic symbol table if it
2907 1.1 christos is resolved to 0. */
2908 1.1.1.6 christos
2909 1.1.1.6 christos static bfd_boolean
2910 1.1 christos elf_x86_64_fixup_symbol (struct bfd_link_info *info,
2911 1.1.1.6 christos struct elf_link_hash_entry *h)
2912 1.1.1.6 christos {
2913 1.1.1.6 christos if (h->dynindx != -1
2914 1.1.1.6 christos && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2915 1.1.1.6 christos elf_x86_64_hash_entry (h)->has_got_reloc,
2916 1.1.1.6 christos elf_x86_64_hash_entry (h)))
2917 1.1.1.6 christos {
2918 1.1.1.6 christos h->dynindx = -1;
2919 1.1 christos _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2920 1.1 christos h->dynstr_index);
2921 1.1 christos }
2922 1.1 christos return TRUE;
2923 1.1 christos }
2924 1.1 christos
2925 1.1 christos /* Adjust a symbol defined by a dynamic object and referenced by a
2926 1.1 christos regular object. The current definition is in some section of the
2927 1.1 christos dynamic object, but we're not including those sections. We have to
2928 1.1 christos change the definition to something the rest of the link can
2929 1.1 christos understand. */
2930 1.1 christos
2931 1.1 christos static bfd_boolean
2932 1.1 christos elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2933 1.1 christos struct elf_link_hash_entry *h)
2934 1.1.1.7 christos {
2935 1.1.1.2 christos struct elf_x86_64_link_hash_table *htab;
2936 1.1.1.2 christos asection *s, *srel;
2937 1.1 christos struct elf_x86_64_link_hash_entry *eh;
2938 1.1 christos struct elf_dyn_relocs *p;
2939 1.1 christos
2940 1.1 christos /* STT_GNU_IFUNC symbol must go through PLT. */
2941 1.1.1.2 christos if (h->type == STT_GNU_IFUNC)
2942 1.1.1.2 christos {
2943 1.1.1.2 christos /* All local STT_GNU_IFUNC references must be treate as local
2944 1.1.1.2 christos calls via local PLT. */
2945 1.1.1.2 christos if (h->ref_regular
2946 1.1.1.2 christos && SYMBOL_CALLS_LOCAL (info, h))
2947 1.1.1.2 christos {
2948 1.1.1.2 christos bfd_size_type pc_count = 0, count = 0;
2949 1.1.1.2 christos struct elf_dyn_relocs **pp;
2950 1.1.1.2 christos
2951 1.1.1.2 christos eh = (struct elf_x86_64_link_hash_entry *) h;
2952 1.1.1.2 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2953 1.1.1.2 christos {
2954 1.1.1.2 christos pc_count += p->pc_count;
2955 1.1.1.2 christos p->count -= p->pc_count;
2956 1.1.1.2 christos p->pc_count = 0;
2957 1.1.1.2 christos count += p->count;
2958 1.1.1.2 christos if (p->count == 0)
2959 1.1.1.2 christos *pp = p->next;
2960 1.1.1.2 christos else
2961 1.1.1.2 christos pp = &p->next;
2962 1.1.1.2 christos }
2963 1.1.1.2 christos
2964 1.1.1.2 christos if (pc_count || count)
2965 1.1.1.6 christos {
2966 1.1.1.6 christos h->non_got_ref = 1;
2967 1.1.1.6 christos if (pc_count)
2968 1.1.1.6 christos {
2969 1.1.1.6 christos /* Increment PLT reference count only for PC-relative
2970 1.1.1.6 christos references. */
2971 1.1.1.6 christos h->needs_plt = 1;
2972 1.1.1.6 christos if (h->plt.refcount <= 0)
2973 1.1.1.6 christos h->plt.refcount = 1;
2974 1.1.1.6 christos else
2975 1.1.1.2 christos h->plt.refcount += 1;
2976 1.1.1.2 christos }
2977 1.1.1.2 christos }
2978 1.1 christos }
2979 1.1 christos
2980 1.1 christos if (h->plt.refcount <= 0)
2981 1.1 christos {
2982 1.1 christos h->plt.offset = (bfd_vma) -1;
2983 1.1 christos h->needs_plt = 0;
2984 1.1 christos }
2985 1.1 christos return TRUE;
2986 1.1 christos }
2987 1.1 christos
2988 1.1 christos /* If this is a function, put it in the procedure linkage table. We
2989 1.1 christos will fill in the contents of the procedure linkage table later,
2990 1.1 christos when we know the address of the .got section. */
2991 1.1 christos if (h->type == STT_FUNC
2992 1.1 christos || h->needs_plt)
2993 1.1 christos {
2994 1.1 christos if (h->plt.refcount <= 0
2995 1.1 christos || SYMBOL_CALLS_LOCAL (info, h)
2996 1.1 christos || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2997 1.1 christos && h->root.type == bfd_link_hash_undefweak))
2998 1.1 christos {
2999 1.1 christos /* This case can occur if we saw a PLT32 reloc in an input
3000 1.1 christos file, but the symbol was never referred to by a dynamic
3001 1.1 christos object, or if all references were garbage collected. In
3002 1.1 christos such a case, we don't actually need to build a procedure
3003 1.1 christos linkage table, and we can just do a PC32 reloc instead. */
3004 1.1 christos h->plt.offset = (bfd_vma) -1;
3005 1.1 christos h->needs_plt = 0;
3006 1.1 christos }
3007 1.1 christos
3008 1.1 christos return TRUE;
3009 1.1 christos }
3010 1.1 christos else
3011 1.1 christos /* It's possible that we incorrectly decided a .plt reloc was
3012 1.1 christos needed for an R_X86_64_PC32 reloc to a non-function sym in
3013 1.1 christos check_relocs. We can't decide accurately between function and
3014 1.1 christos non-function syms in check-relocs; Objects loaded later in
3015 1.1 christos the link may change h->type. So fix it now. */
3016 1.1 christos h->plt.offset = (bfd_vma) -1;
3017 1.1 christos
3018 1.1 christos /* If this is a weak symbol, and there is a real definition, the
3019 1.1 christos processor independent code will have arranged for us to see the
3020 1.1 christos real definition first, and we can just use the same value. */
3021 1.1 christos if (h->u.weakdef != NULL)
3022 1.1 christos {
3023 1.1 christos BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3024 1.1 christos || h->u.weakdef->root.type == bfd_link_hash_defweak);
3025 1.1 christos h->root.u.def.section = h->u.weakdef->root.u.def.section;
3026 1.1.1.4 christos h->root.u.def.value = h->u.weakdef->root.u.def.value;
3027 1.1.1.4 christos if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
3028 1.1.1.4 christos {
3029 1.1.1.4 christos eh = (struct elf_x86_64_link_hash_entry *) h;
3030 1.1.1.4 christos h->non_got_ref = h->u.weakdef->non_got_ref;
3031 1.1 christos eh->needs_copy = h->u.weakdef->needs_copy;
3032 1.1 christos }
3033 1.1 christos return TRUE;
3034 1.1 christos }
3035 1.1 christos
3036 1.1 christos /* This is a reference to a symbol defined by a dynamic object which
3037 1.1 christos is not a function. */
3038 1.1 christos
3039 1.1 christos /* If we are creating a shared library, we must presume that the
3040 1.1 christos only references to the symbol are via the global offset table.
3041 1.1.1.6 christos For such cases we need not do anything here; the relocations will
3042 1.1 christos be handled correctly by relocate_section. */
3043 1.1 christos if (!bfd_link_executable (info))
3044 1.1 christos return TRUE;
3045 1.1 christos
3046 1.1 christos /* If there are no references to this symbol that do not use the
3047 1.1 christos GOT, we don't need to generate a copy reloc. */
3048 1.1 christos if (!h->non_got_ref)
3049 1.1 christos return TRUE;
3050 1.1 christos
3051 1.1 christos /* If -z nocopyreloc was given, we won't generate them either. */
3052 1.1 christos if (info->nocopyreloc)
3053 1.1 christos {
3054 1.1 christos h->non_got_ref = 0;
3055 1.1 christos return TRUE;
3056 1.1 christos }
3057 1.1 christos
3058 1.1 christos if (ELIMINATE_COPY_RELOCS)
3059 1.1 christos {
3060 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) h;
3061 1.1 christos for (p = eh->dyn_relocs; p != NULL; p = p->next)
3062 1.1 christos {
3063 1.1 christos s = p->sec->output_section;
3064 1.1 christos if (s != NULL && (s->flags & SEC_READONLY) != 0)
3065 1.1 christos break;
3066 1.1 christos }
3067 1.1 christos
3068 1.1 christos /* If we didn't find any dynamic relocs in read-only sections, then
3069 1.1 christos we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3070 1.1 christos if (p == NULL)
3071 1.1 christos {
3072 1.1 christos h->non_got_ref = 0;
3073 1.1 christos return TRUE;
3074 1.1 christos }
3075 1.1 christos }
3076 1.1 christos
3077 1.1 christos /* We must allocate the symbol in our .dynbss section, which will
3078 1.1 christos become part of the .bss section of the executable. There will be
3079 1.1 christos an entry for this symbol in the .dynsym section. The dynamic
3080 1.1 christos object will contain position independent code, so all references
3081 1.1 christos from the dynamic object to this symbol will go through the global
3082 1.1 christos offset table. The dynamic linker will use the .dynsym entry to
3083 1.1 christos determine the address it must put in the global offset table, so
3084 1.1 christos both the dynamic object and the regular object will refer to the
3085 1.1 christos same memory location for the variable. */
3086 1.1 christos
3087 1.1 christos htab = elf_x86_64_hash_table (info);
3088 1.1 christos if (htab == NULL)
3089 1.1 christos return FALSE;
3090 1.1 christos
3091 1.1 christos /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
3092 1.1.1.7 christos to copy the initial value out of the dynamic object and into the
3093 1.1.1.7 christos runtime process image. */
3094 1.1.1.7 christos if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3095 1.1.1.7 christos {
3096 1.1.1.7 christos s = htab->elf.sdynrelro;
3097 1.1.1.7 christos srel = htab->elf.sreldynrelro;
3098 1.1.1.7 christos }
3099 1.1.1.7 christos else
3100 1.1.1.7 christos {
3101 1.1.1.7 christos s = htab->elf.sdynbss;
3102 1.1.1.2 christos srel = htab->elf.srelbss;
3103 1.1 christos }
3104 1.1 christos if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3105 1.1 christos {
3106 1.1.1.7 christos const struct elf_backend_data *bed;
3107 1.1 christos bed = get_elf_backend_data (info->output_bfd);
3108 1.1 christos srel->size += bed->s->sizeof_rela;
3109 1.1 christos h->needs_copy = 1;
3110 1.1.1.4 christos }
3111 1.1 christos
3112 1.1 christos return _bfd_elf_adjust_dynamic_copy (info, h, s);
3113 1.1 christos }
3114 1.1 christos
3115 1.1 christos /* Allocate space in .plt, .got and associated reloc sections for
3116 1.1 christos dynamic relocs. */
3117 1.1 christos
3118 1.1 christos static bfd_boolean
3119 1.1 christos elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
3120 1.1 christos {
3121 1.1 christos struct bfd_link_info *info;
3122 1.1 christos struct elf_x86_64_link_hash_table *htab;
3123 1.1 christos struct elf_x86_64_link_hash_entry *eh;
3124 1.1.1.2 christos struct elf_dyn_relocs *p;
3125 1.1.1.6 christos const struct elf_backend_data *bed;
3126 1.1 christos unsigned int plt_entry_size;
3127 1.1 christos bfd_boolean resolved_to_zero;
3128 1.1 christos
3129 1.1 christos if (h->root.type == bfd_link_hash_indirect)
3130 1.1 christos return TRUE;
3131 1.1 christos
3132 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) h;
3133 1.1 christos
3134 1.1 christos info = (struct bfd_link_info *) inf;
3135 1.1 christos htab = elf_x86_64_hash_table (info);
3136 1.1 christos if (htab == NULL)
3137 1.1.1.2 christos return FALSE;
3138 1.1 christos bed = get_elf_backend_data (info->output_bfd);
3139 1.1.1.6 christos plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
3140 1.1.1.6 christos
3141 1.1.1.6 christos resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3142 1.1.1.6 christos eh->has_got_reloc,
3143 1.1.1.4 christos eh);
3144 1.1.1.4 christos
3145 1.1.1.4 christos /* We can't use the GOT PLT if pointer equality is needed since
3146 1.1.1.4 christos finish_dynamic_symbol won't clear symbol value and the dynamic
3147 1.1.1.4 christos linker won't update the GOT slot. We will get into an infinite
3148 1.1.1.4 christos loop at run-time. */
3149 1.1.1.4 christos if (htab->plt_got != NULL
3150 1.1.1.4 christos && h->type != STT_GNU_IFUNC
3151 1.1.1.4 christos && !h->pointer_equality_needed
3152 1.1.1.4 christos && h->plt.refcount > 0
3153 1.1.1.4 christos && h->got.refcount > 0)
3154 1.1.1.4 christos {
3155 1.1.1.4 christos /* Don't use the regular PLT if there are both GOT and GOTPLT
3156 1.1.1.4 christos reloctions. */
3157 1.1.1.4 christos h->plt.offset = (bfd_vma) -1;
3158 1.1.1.4 christos
3159 1.1.1.4 christos /* Use the GOT PLT. */
3160 1.1.1.4 christos eh->plt_got.refcount = 1;
3161 1.1.1.6 christos }
3162 1.1.1.6 christos
3163 1.1.1.6 christos /* Clear the reference count of function pointer relocations if
3164 1.1.1.6 christos symbol isn't a normal function. */
3165 1.1.1.6 christos if (h->type != STT_FUNC)
3166 1.1 christos eh->func_pointer_refcount = 0;
3167 1.1 christos
3168 1.1 christos /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
3169 1.1 christos here if it is defined and referenced in a non-shared object. */
3170 1.1.1.4 christos if (h->type == STT_GNU_IFUNC
3171 1.1.1.4 christos && h->def_regular)
3172 1.1.1.4 christos {
3173 1.1.1.6 christos if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
3174 1.1.1.4 christos &eh->dyn_relocs,
3175 1.1.1.4 christos &htab->readonly_dynrelocs_against_ifunc,
3176 1.1.1.6 christos plt_entry_size,
3177 1.1.1.4 christos plt_entry_size,
3178 1.1.1.4 christos GOT_ENTRY_SIZE, TRUE))
3179 1.1.1.4 christos {
3180 1.1.1.4 christos asection *s = htab->plt_bnd;
3181 1.1.1.4 christos if (h->plt.offset != (bfd_vma) -1 && s != NULL)
3182 1.1.1.4 christos {
3183 1.1.1.4 christos /* Use the .plt.bnd section if it is created. */
3184 1.1.1.4 christos eh->plt_bnd.offset = s->size;
3185 1.1.1.4 christos
3186 1.1.1.4 christos /* Make room for this entry in the .plt.bnd section. */
3187 1.1.1.4 christos s->size += sizeof (elf_x86_64_legacy_plt2_entry);
3188 1.1.1.4 christos }
3189 1.1.1.4 christos
3190 1.1.1.4 christos return TRUE;
3191 1.1.1.4 christos }
3192 1.1.1.4 christos else
3193 1.1.1.6 christos return FALSE;
3194 1.1.1.6 christos }
3195 1.1 christos /* Don't create the PLT entry if there are only function pointer
3196 1.1.1.6 christos relocations which can be resolved at run-time. */
3197 1.1.1.6 christos else if (htab->elf.dynamic_sections_created
3198 1.1 christos && (h->plt.refcount > eh->func_pointer_refcount
3199 1.1.1.5 christos || eh->plt_got.refcount > 0))
3200 1.1.1.5 christos {
3201 1.1.1.6 christos bfd_boolean use_plt_got;
3202 1.1.1.6 christos
3203 1.1.1.6 christos /* Clear the reference count of function pointer relocations
3204 1.1.1.6 christos if PLT is used. */
3205 1.1.1.5 christos eh->func_pointer_refcount = 0;
3206 1.1.1.5 christos
3207 1.1.1.5 christos if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
3208 1.1.1.5 christos {
3209 1.1.1.5 christos /* Don't use the regular PLT for DF_BIND_NOW. */
3210 1.1.1.5 christos h->plt.offset = (bfd_vma) -1;
3211 1.1.1.5 christos
3212 1.1.1.5 christos /* Use the GOT PLT. */
3213 1.1.1.5 christos h->got.refcount = 1;
3214 1.1.1.5 christos eh->plt_got.refcount = 1;
3215 1.1.1.5 christos }
3216 1.1.1.4 christos
3217 1.1 christos use_plt_got = eh->plt_got.refcount > 0;
3218 1.1 christos
3219 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
3220 1.1.1.6 christos Undefined weak syms won't yet be marked as dynamic. */
3221 1.1.1.6 christos if (h->dynindx == -1
3222 1.1 christos && !h->forced_local
3223 1.1 christos && !resolved_to_zero)
3224 1.1 christos {
3225 1.1 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
3226 1.1 christos return FALSE;
3227 1.1.1.6 christos }
3228 1.1 christos
3229 1.1 christos if (bfd_link_pic (info)
3230 1.1 christos || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3231 1.1.1.4 christos {
3232 1.1.1.4 christos asection *s = htab->elf.splt;
3233 1.1 christos asection *bnd_s = htab->plt_bnd;
3234 1.1 christos asection *got_s = htab->plt_got;
3235 1.1.1.5 christos
3236 1.1.1.5 christos /* If this is the first .plt entry, make room for the special
3237 1.1 christos first entry. The .plt section is used by prelink to undo
3238 1.1.1.3 christos prelinking for dynamic relocations. */
3239 1.1 christos if (s->size == 0)
3240 1.1.1.4 christos s->size = plt_entry_size;
3241 1.1.1.4 christos
3242 1.1.1.4 christos if (use_plt_got)
3243 1.1.1.4 christos eh->plt_got.offset = got_s->size;
3244 1.1.1.4 christos else
3245 1.1.1.4 christos {
3246 1.1.1.4 christos h->plt.offset = s->size;
3247 1.1.1.4 christos if (bnd_s)
3248 1.1 christos eh->plt_bnd.offset = bnd_s->size;
3249 1.1 christos }
3250 1.1 christos
3251 1.1 christos /* If this symbol is not defined in a regular file, and we are
3252 1.1 christos not generating a shared library, then set the symbol to this
3253 1.1 christos location in the .plt. This is required to make function
3254 1.1.1.6 christos pointers compare as equal between the normal executable and
3255 1.1 christos the shared library. */
3256 1.1 christos if (! bfd_link_pic (info)
3257 1.1.1.4 christos && !h->def_regular)
3258 1.1.1.4 christos {
3259 1.1.1.4 christos if (use_plt_got)
3260 1.1.1.4 christos {
3261 1.1.1.4 christos /* We need to make a call to the entry of the GOT PLT
3262 1.1.1.4 christos instead of regular PLT entry. */
3263 1.1.1.4 christos h->root.u.def.section = got_s;
3264 1.1.1.4 christos h->root.u.def.value = eh->plt_got.offset;
3265 1.1.1.4 christos }
3266 1.1.1.4 christos else
3267 1.1.1.4 christos {
3268 1.1.1.4 christos if (bnd_s)
3269 1.1.1.4 christos {
3270 1.1.1.4 christos /* We need to make a call to the entry of the second
3271 1.1.1.4 christos PLT instead of regular PLT entry. */
3272 1.1.1.4 christos h->root.u.def.section = bnd_s;
3273 1.1.1.4 christos h->root.u.def.value = eh->plt_bnd.offset;
3274 1.1.1.4 christos }
3275 1.1.1.4 christos else
3276 1.1.1.4 christos {
3277 1.1.1.4 christos h->root.u.def.section = s;
3278 1.1.1.4 christos h->root.u.def.value = h->plt.offset;
3279 1.1 christos }
3280 1.1 christos }
3281 1.1 christos }
3282 1.1.1.4 christos
3283 1.1.1.4 christos /* Make room for this entry. */
3284 1.1.1.4 christos if (use_plt_got)
3285 1.1.1.4 christos got_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
3286 1.1.1.4 christos else
3287 1.1.1.4 christos {
3288 1.1.1.4 christos s->size += plt_entry_size;
3289 1.1.1.4 christos if (bnd_s)
3290 1.1.1.4 christos bnd_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
3291 1.1.1.4 christos
3292 1.1.1.4 christos /* We also need to make an entry in the .got.plt section,
3293 1.1.1.4 christos which will be placed in the .got section by the linker
3294 1.1.1.4 christos script. */
3295 1.1.1.6 christos htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
3296 1.1.1.6 christos
3297 1.1.1.6 christos /* There should be no PLT relocation against resolved
3298 1.1.1.6 christos undefined weak symbol in executable. */
3299 1.1.1.6 christos if (!resolved_to_zero)
3300 1.1.1.6 christos {
3301 1.1.1.6 christos /* We also need to make an entry in the .rela.plt
3302 1.1.1.6 christos section. */
3303 1.1.1.6 christos htab->elf.srelplt->size += bed->s->sizeof_rela;
3304 1.1.1.4 christos htab->elf.srelplt->reloc_count++;
3305 1.1 christos }
3306 1.1 christos }
3307 1.1 christos }
3308 1.1.1.6 christos else
3309 1.1 christos {
3310 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
3311 1.1 christos h->plt.offset = (bfd_vma) -1;
3312 1.1 christos h->needs_plt = 0;
3313 1.1 christos }
3314 1.1 christos }
3315 1.1.1.6 christos else
3316 1.1 christos {
3317 1.1 christos eh->plt_got.offset = (bfd_vma) -1;
3318 1.1 christos h->plt.offset = (bfd_vma) -1;
3319 1.1 christos h->needs_plt = 0;
3320 1.1 christos }
3321 1.1 christos
3322 1.1 christos eh->tlsdesc_got = (bfd_vma) -1;
3323 1.1 christos
3324 1.1 christos /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
3325 1.1.1.6 christos make it a R_X86_64_TPOFF32 requiring no GOT entry. */
3326 1.1 christos if (h->got.refcount > 0
3327 1.1 christos && bfd_link_executable (info)
3328 1.1 christos && h->dynindx == -1
3329 1.1 christos && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
3330 1.1 christos {
3331 1.1 christos h->got.offset = (bfd_vma) -1;
3332 1.1 christos }
3333 1.1 christos else if (h->got.refcount > 0)
3334 1.1 christos {
3335 1.1 christos asection *s;
3336 1.1 christos bfd_boolean dyn;
3337 1.1 christos int tls_type = elf_x86_64_hash_entry (h)->tls_type;
3338 1.1 christos
3339 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
3340 1.1.1.6 christos Undefined weak syms won't yet be marked as dynamic. */
3341 1.1.1.6 christos if (h->dynindx == -1
3342 1.1 christos && !h->forced_local
3343 1.1 christos && !resolved_to_zero)
3344 1.1 christos {
3345 1.1 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
3346 1.1 christos return FALSE;
3347 1.1 christos }
3348 1.1 christos
3349 1.1 christos if (GOT_TLS_GDESC_P (tls_type))
3350 1.1 christos {
3351 1.1 christos eh->tlsdesc_got = htab->elf.sgotplt->size
3352 1.1 christos - elf_x86_64_compute_jump_table_size (htab);
3353 1.1 christos htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3354 1.1 christos h->got.offset = (bfd_vma) -2;
3355 1.1 christos }
3356 1.1 christos if (! GOT_TLS_GDESC_P (tls_type)
3357 1.1 christos || GOT_TLS_GD_P (tls_type))
3358 1.1 christos {
3359 1.1 christos s = htab->elf.sgot;
3360 1.1 christos h->got.offset = s->size;
3361 1.1 christos s->size += GOT_ENTRY_SIZE;
3362 1.1 christos if (GOT_TLS_GD_P (tls_type))
3363 1.1 christos s->size += GOT_ENTRY_SIZE;
3364 1.1 christos }
3365 1.1.1.6 christos dyn = htab->elf.dynamic_sections_created;
3366 1.1.1.6 christos /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
3367 1.1.1.6 christos and two if global. R_X86_64_GOTTPOFF needs one dynamic
3368 1.1 christos relocation. No dynamic relocation against resolved undefined
3369 1.1 christos weak symbol in executable. */
3370 1.1 christos if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
3371 1.1 christos || tls_type == GOT_TLS_IE)
3372 1.1 christos htab->elf.srelgot->size += bed->s->sizeof_rela;
3373 1.1 christos else if (GOT_TLS_GD_P (tls_type))
3374 1.1.1.6 christos htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
3375 1.1.1.6 christos else if (! GOT_TLS_GDESC_P (tls_type)
3376 1.1 christos && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3377 1.1.1.6 christos && !resolved_to_zero)
3378 1.1 christos || h->root.type != bfd_link_hash_undefweak)
3379 1.1 christos && (bfd_link_pic (info)
3380 1.1 christos || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3381 1.1 christos htab->elf.srelgot->size += bed->s->sizeof_rela;
3382 1.1 christos if (GOT_TLS_GDESC_P (tls_type))
3383 1.1 christos {
3384 1.1 christos htab->elf.srelplt->size += bed->s->sizeof_rela;
3385 1.1 christos htab->tlsdesc_plt = (bfd_vma) -1;
3386 1.1 christos }
3387 1.1 christos }
3388 1.1 christos else
3389 1.1 christos h->got.offset = (bfd_vma) -1;
3390 1.1 christos
3391 1.1 christos if (eh->dyn_relocs == NULL)
3392 1.1 christos return TRUE;
3393 1.1 christos
3394 1.1 christos /* In the shared -Bsymbolic case, discard space allocated for
3395 1.1 christos dynamic pc-relative relocs against symbols which turn out to be
3396 1.1 christos defined in regular objects. For the normal shared case, discard
3397 1.1 christos space for pc-relative relocs that have become local due to symbol
3398 1.1.1.6 christos visibility changes. */
3399 1.1 christos
3400 1.1 christos if (bfd_link_pic (info))
3401 1.1 christos {
3402 1.1 christos /* Relocs that use pc_count are those that appear on a call
3403 1.1 christos insn, or certain REL relocs that can generated via assembly.
3404 1.1 christos We want calls to protected symbols to resolve directly to the
3405 1.1 christos function rather than going via the plt. If people want
3406 1.1 christos function pointer comparisons to work as expected then they
3407 1.1 christos should avoid writing weird assembly. */
3408 1.1 christos if (SYMBOL_CALLS_LOCAL (info, h))
3409 1.1 christos {
3410 1.1 christos struct elf_dyn_relocs **pp;
3411 1.1 christos
3412 1.1 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3413 1.1 christos {
3414 1.1 christos p->count -= p->pc_count;
3415 1.1 christos p->pc_count = 0;
3416 1.1 christos if (p->count == 0)
3417 1.1 christos *pp = p->next;
3418 1.1 christos else
3419 1.1 christos pp = &p->next;
3420 1.1 christos }
3421 1.1 christos }
3422 1.1.1.6 christos
3423 1.1.1.4 christos /* Also discard relocs on undefined weak syms with non-default
3424 1.1 christos visibility or in PIE. */
3425 1.1.1.4 christos if (eh->dyn_relocs != NULL)
3426 1.1.1.4 christos {
3427 1.1.1.6 christos if (h->root.type == bfd_link_hash_undefweak)
3428 1.1.1.6 christos {
3429 1.1.1.6 christos /* Undefined weak symbol is never bound locally in shared
3430 1.1.1.6 christos library. */
3431 1.1.1.4 christos if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3432 1.1.1.4 christos || resolved_to_zero)
3433 1.1.1.4 christos eh->dyn_relocs = NULL;
3434 1.1.1.4 christos else if (h->dynindx == -1
3435 1.1.1.4 christos && ! h->forced_local
3436 1.1.1.4 christos && ! bfd_elf_link_record_dynamic_symbol (info, h))
3437 1.1.1.4 christos return FALSE;
3438 1.1.1.4 christos }
3439 1.1.1.6 christos /* For PIE, discard space for pc-relative relocs against
3440 1.1.1.4 christos symbols which turn out to need copy relocs. */
3441 1.1.1.4 christos else if (bfd_link_executable (info)
3442 1.1.1.4 christos && (h->needs_copy || eh->needs_copy)
3443 1.1.1.4 christos && h->def_dynamic
3444 1.1.1.4 christos && !h->def_regular)
3445 1.1 christos {
3446 1.1.1.4 christos struct elf_dyn_relocs **pp;
3447 1.1.1.4 christos
3448 1.1.1.4 christos for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3449 1.1.1.4 christos {
3450 1.1.1.4 christos if (p->pc_count != 0)
3451 1.1.1.4 christos *pp = p->next;
3452 1.1.1.4 christos else
3453 1.1.1.4 christos pp = &p->next;
3454 1.1.1.4 christos }
3455 1.1 christos }
3456 1.1 christos }
3457 1.1 christos }
3458 1.1 christos else if (ELIMINATE_COPY_RELOCS)
3459 1.1 christos {
3460 1.1.1.6 christos /* For the non-shared case, discard space for relocs against
3461 1.1.1.6 christos symbols which turn out to need copy relocs or are not
3462 1.1 christos dynamic. Keep dynamic relocations for run-time function
3463 1.1.1.6 christos pointer initialization. */
3464 1.1.1.6 christos
3465 1.1.1.6 christos if ((!h->non_got_ref
3466 1.1.1.6 christos || eh->func_pointer_refcount > 0
3467 1.1 christos || (h->root.type == bfd_link_hash_undefweak
3468 1.1 christos && !resolved_to_zero))
3469 1.1 christos && ((h->def_dynamic
3470 1.1 christos && !h->def_regular)
3471 1.1 christos || (htab->elf.dynamic_sections_created
3472 1.1 christos && (h->root.type == bfd_link_hash_undefweak
3473 1.1 christos || h->root.type == bfd_link_hash_undefined))))
3474 1.1 christos {
3475 1.1 christos /* Make sure this symbol is output as a dynamic symbol.
3476 1.1 christos Undefined weak syms won't yet be marked as dynamic. */
3477 1.1.1.6 christos if (h->dynindx == -1
3478 1.1 christos && ! h->forced_local
3479 1.1 christos && ! resolved_to_zero
3480 1.1 christos && ! bfd_elf_link_record_dynamic_symbol (info, h))
3481 1.1 christos return FALSE;
3482 1.1 christos
3483 1.1 christos /* If that succeeded, we know we'll be keeping all the
3484 1.1 christos relocs. */
3485 1.1 christos if (h->dynindx != -1)
3486 1.1 christos goto keep;
3487 1.1 christos }
3488 1.1.1.6 christos
3489 1.1 christos eh->dyn_relocs = NULL;
3490 1.1 christos eh->func_pointer_refcount = 0;
3491 1.1 christos
3492 1.1 christos keep: ;
3493 1.1 christos }
3494 1.1 christos
3495 1.1 christos /* Finally, allocate space. */
3496 1.1 christos for (p = eh->dyn_relocs; p != NULL; p = p->next)
3497 1.1 christos {
3498 1.1 christos asection * sreloc;
3499 1.1 christos
3500 1.1 christos sreloc = elf_section_data (p->sec)->sreloc;
3501 1.1 christos
3502 1.1 christos BFD_ASSERT (sreloc != NULL);
3503 1.1 christos
3504 1.1 christos sreloc->size += p->count * bed->s->sizeof_rela;
3505 1.1 christos }
3506 1.1 christos
3507 1.1 christos return TRUE;
3508 1.1 christos }
3509 1.1 christos
3510 1.1 christos /* Allocate space in .plt, .got and associated reloc sections for
3511 1.1 christos local dynamic relocs. */
3512 1.1 christos
3513 1.1 christos static bfd_boolean
3514 1.1 christos elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
3515 1.1 christos {
3516 1.1 christos struct elf_link_hash_entry *h
3517 1.1 christos = (struct elf_link_hash_entry *) *slot;
3518 1.1 christos
3519 1.1 christos if (h->type != STT_GNU_IFUNC
3520 1.1 christos || !h->def_regular
3521 1.1 christos || !h->ref_regular
3522 1.1 christos || !h->forced_local
3523 1.1 christos || h->root.type != bfd_link_hash_defined)
3524 1.1 christos abort ();
3525 1.1 christos
3526 1.1 christos return elf_x86_64_allocate_dynrelocs (h, inf);
3527 1.1 christos }
3528 1.1 christos
3529 1.1 christos /* Find any dynamic relocs that apply to read-only sections. */
3530 1.1 christos
3531 1.1 christos static bfd_boolean
3532 1.1 christos elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
3533 1.1 christos void * inf)
3534 1.1 christos {
3535 1.1 christos struct elf_x86_64_link_hash_entry *eh;
3536 1.1.1.2 christos struct elf_dyn_relocs *p;
3537 1.1.1.2 christos
3538 1.1.1.2 christos /* Skip local IFUNC symbols. */
3539 1.1 christos if (h->forced_local && h->type == STT_GNU_IFUNC)
3540 1.1 christos return TRUE;
3541 1.1 christos
3542 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) h;
3543 1.1 christos for (p = eh->dyn_relocs; p != NULL; p = p->next)
3544 1.1 christos {
3545 1.1 christos asection *s = p->sec->output_section;
3546 1.1 christos
3547 1.1 christos if (s != NULL && (s->flags & SEC_READONLY) != 0)
3548 1.1 christos {
3549 1.1 christos struct bfd_link_info *info = (struct bfd_link_info *) inf;
3550 1.1 christos
3551 1.1.1.6 christos info->flags |= DF_TEXTREL;
3552 1.1.1.5 christos
3553 1.1.1.7 christos if ((info->warn_shared_textrel && bfd_link_pic (info))
3554 1.1.1.5 christos || info->error_textrel)
3555 1.1.1.2 christos /* xgettext:c-format */
3556 1.1.1.2 christos info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3557 1.1.1.2 christos p->sec->owner, h->root.root.string,
3558 1.1 christos p->sec);
3559 1.1 christos
3560 1.1 christos /* Not an error, just cut short the traversal. */
3561 1.1 christos return FALSE;
3562 1.1 christos }
3563 1.1 christos }
3564 1.1 christos return TRUE;
3565 1.1.1.6 christos }
3566 1.1.1.2 christos
3567 1.1.1.2 christos /* Convert load via the GOT slot to load immediate. */
3568 1.1.1.6 christos
3569 1.1.1.6 christos static bfd_boolean
3570 1.1.1.2 christos elf_x86_64_convert_load (bfd *abfd, asection *sec,
3571 1.1.1.2 christos struct bfd_link_info *link_info)
3572 1.1.1.2 christos {
3573 1.1.1.2 christos Elf_Internal_Shdr *symtab_hdr;
3574 1.1.1.2 christos Elf_Internal_Rela *internal_relocs;
3575 1.1.1.2 christos Elf_Internal_Rela *irel, *irelend;
3576 1.1.1.6 christos bfd_byte *contents;
3577 1.1.1.2 christos struct elf_x86_64_link_hash_table *htab;
3578 1.1.1.2 christos bfd_boolean changed;
3579 1.1.1.2 christos bfd_signed_vma *local_got_refcounts;
3580 1.1.1.2 christos
3581 1.1.1.2 christos /* Don't even try to convert non-ELF outputs. */
3582 1.1.1.2 christos if (!is_elf_hash_table (link_info->hash))
3583 1.1.1.5 christos return FALSE;
3584 1.1.1.2 christos
3585 1.1.1.6 christos /* Nothing to do if there is no need or no output. */
3586 1.1.1.4 christos if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3587 1.1.1.2 christos || sec->need_convert_load == 0
3588 1.1.1.2 christos || bfd_is_abs_section (sec->output_section))
3589 1.1.1.2 christos return TRUE;
3590 1.1.1.2 christos
3591 1.1.1.2 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3592 1.1.1.2 christos
3593 1.1.1.2 christos /* Load the relocations for this section. */
3594 1.1.1.2 christos internal_relocs = (_bfd_elf_link_read_relocs
3595 1.1.1.2 christos (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3596 1.1.1.2 christos link_info->keep_memory));
3597 1.1.1.2 christos if (internal_relocs == NULL)
3598 1.1.1.6 christos return FALSE;
3599 1.1.1.2 christos
3600 1.1.1.2 christos changed = FALSE;
3601 1.1.1.2 christos htab = elf_x86_64_hash_table (link_info);
3602 1.1.1.2 christos local_got_refcounts = elf_local_got_refcounts (abfd);
3603 1.1.1.2 christos
3604 1.1.1.2 christos /* Get the section contents. */
3605 1.1.1.2 christos if (elf_section_data (sec)->this_hdr.contents != NULL)
3606 1.1.1.2 christos contents = elf_section_data (sec)->this_hdr.contents;
3607 1.1.1.2 christos else
3608 1.1.1.2 christos {
3609 1.1.1.2 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3610 1.1.1.2 christos goto error_return;
3611 1.1.1.2 christos }
3612 1.1.1.2 christos
3613 1.1.1.2 christos irelend = internal_relocs + sec->reloc_count;
3614 1.1.1.2 christos for (irel = internal_relocs; irel < irelend; irel++)
3615 1.1.1.6 christos {
3616 1.1.1.2 christos unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3617 1.1.1.6 christos unsigned int r_symndx;
3618 1.1.1.2 christos struct elf_link_hash_entry *h;
3619 1.1.1.6 christos bfd_boolean converted;
3620 1.1.1.6 christos
3621 1.1.1.6 christos if (r_type != R_X86_64_GOTPCRELX
3622 1.1.1.5 christos && r_type != R_X86_64_REX_GOTPCRELX
3623 1.1.1.5 christos && r_type != R_X86_64_GOTPCREL)
3624 1.1.1.6 christos continue;
3625 1.1.1.2 christos
3626 1.1.1.6 christos r_symndx = htab->r_sym (irel->r_info);
3627 1.1.1.6 christos if (r_symndx < symtab_hdr->sh_info)
3628 1.1.1.6 christos h = elf_x86_64_get_local_sym_hash (htab, sec->owner,
3629 1.1.1.5 christos (const Elf_Internal_Rela *) irel,
3630 1.1.1.5 christos FALSE);
3631 1.1.1.6 christos else
3632 1.1.1.5 christos {
3633 1.1.1.5 christos h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3634 1.1.1.5 christos while (h->root.type == bfd_link_hash_indirect
3635 1.1.1.5 christos || h->root.type == bfd_link_hash_warning)
3636 1.1.1.5 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
3637 1.1.1.6 christos }
3638 1.1.1.6 christos
3639 1.1.1.5 christos /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
3640 1.1.1.5 christos if (h != NULL && h->type == STT_GNU_IFUNC)
3641 1.1.1.6 christos continue;
3642 1.1.1.6 christos
3643 1.1.1.6 christos converted = FALSE;
3644 1.1.1.6 christos if (!elf_x86_64_convert_load_reloc (abfd, sec, contents, irel, h,
3645 1.1.1.5 christos &converted, link_info))
3646 1.1.1.6 christos goto error_return;
3647 1.1.1.5 christos
3648 1.1.1.6 christos if (converted)
3649 1.1.1.6 christos {
3650 1.1.1.5 christos changed = converted;
3651 1.1.1.6 christos if (h)
3652 1.1.1.6 christos {
3653 1.1.1.6 christos if (h->got.refcount > 0)
3654 1.1.1.6 christos h->got.refcount -= 1;
3655 1.1.1.6 christos }
3656 1.1.1.6 christos else
3657 1.1.1.6 christos {
3658 1.1.1.6 christos if (local_got_refcounts != NULL
3659 1.1.1.5 christos && local_got_refcounts[r_symndx] > 0)
3660 1.1.1.2 christos local_got_refcounts[r_symndx] -= 1;
3661 1.1.1.2 christos }
3662 1.1.1.2 christos }
3663 1.1.1.2 christos }
3664 1.1.1.2 christos
3665 1.1.1.2 christos if (contents != NULL
3666 1.1.1.6 christos && elf_section_data (sec)->this_hdr.contents != contents)
3667 1.1.1.2 christos {
3668 1.1.1.2 christos if (!changed && !link_info->keep_memory)
3669 1.1.1.2 christos free (contents);
3670 1.1.1.2 christos else
3671 1.1.1.2 christos {
3672 1.1.1.2 christos /* Cache the section contents for elf_link_input_bfd. */
3673 1.1.1.2 christos elf_section_data (sec)->this_hdr.contents = contents;
3674 1.1.1.2 christos }
3675 1.1.1.2 christos }
3676 1.1.1.2 christos
3677 1.1.1.6 christos if (elf_section_data (sec)->relocs != internal_relocs)
3678 1.1.1.2 christos {
3679 1.1.1.2 christos if (!changed)
3680 1.1.1.2 christos free (internal_relocs);
3681 1.1.1.2 christos else
3682 1.1.1.2 christos elf_section_data (sec)->relocs = internal_relocs;
3683 1.1.1.2 christos }
3684 1.1.1.2 christos
3685 1.1.1.2 christos return TRUE;
3686 1.1.1.2 christos
3687 1.1.1.2 christos error_return:
3688 1.1.1.2 christos if (contents != NULL
3689 1.1.1.2 christos && elf_section_data (sec)->this_hdr.contents != contents)
3690 1.1.1.2 christos free (contents);
3691 1.1.1.2 christos if (internal_relocs != NULL
3692 1.1.1.2 christos && elf_section_data (sec)->relocs != internal_relocs)
3693 1.1.1.2 christos free (internal_relocs);
3694 1.1.1.2 christos return FALSE;
3695 1.1 christos }
3696 1.1 christos
3697 1.1 christos /* Set the sizes of the dynamic sections. */
3698 1.1 christos
3699 1.1 christos static bfd_boolean
3700 1.1 christos elf_x86_64_size_dynamic_sections (bfd *output_bfd,
3701 1.1 christos struct bfd_link_info *info)
3702 1.1 christos {
3703 1.1 christos struct elf_x86_64_link_hash_table *htab;
3704 1.1 christos bfd *dynobj;
3705 1.1 christos asection *s;
3706 1.1 christos bfd_boolean relocs;
3707 1.1.1.7 christos bfd *ibfd;
3708 1.1 christos const struct elf_backend_data *bed;
3709 1.1 christos const struct elf_x86_64_backend_data *arch_data;
3710 1.1 christos
3711 1.1 christos htab = elf_x86_64_hash_table (info);
3712 1.1 christos if (htab == NULL)
3713 1.1 christos return FALSE;
3714 1.1 christos bed = get_elf_backend_data (output_bfd);
3715 1.1 christos
3716 1.1 christos dynobj = htab->elf.dynobj;
3717 1.1 christos if (dynobj == NULL)
3718 1.1 christos abort ();
3719 1.1 christos
3720 1.1.1.4 christos /* Set up .got offsets for local syms, and space for local dynamic
3721 1.1 christos relocs. */
3722 1.1 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3723 1.1 christos {
3724 1.1 christos bfd_signed_vma *local_got;
3725 1.1 christos bfd_signed_vma *end_local_got;
3726 1.1 christos char *local_tls_type;
3727 1.1 christos bfd_vma *local_tlsdesc_gotent;
3728 1.1 christos bfd_size_type locsymcount;
3729 1.1 christos Elf_Internal_Shdr *symtab_hdr;
3730 1.1 christos asection *srel;
3731 1.1 christos
3732 1.1 christos if (! is_x86_64_elf (ibfd))
3733 1.1 christos continue;
3734 1.1 christos
3735 1.1 christos for (s = ibfd->sections; s != NULL; s = s->next)
3736 1.1 christos {
3737 1.1.1.6 christos struct elf_dyn_relocs *p;
3738 1.1.1.2 christos
3739 1.1.1.2 christos if (!elf_x86_64_convert_load (ibfd, s, info))
3740 1.1 christos return FALSE;
3741 1.1 christos
3742 1.1 christos for (p = (struct elf_dyn_relocs *)
3743 1.1 christos (elf_section_data (s)->local_dynrel);
3744 1.1 christos p != NULL;
3745 1.1 christos p = p->next)
3746 1.1 christos {
3747 1.1 christos if (!bfd_is_abs_section (p->sec)
3748 1.1 christos && bfd_is_abs_section (p->sec->output_section))
3749 1.1 christos {
3750 1.1 christos /* Input section has been discarded, either because
3751 1.1 christos it is a copy of a linkonce section or due to
3752 1.1 christos linker script /DISCARD/, so we'll be discarding
3753 1.1 christos the relocs too. */
3754 1.1 christos }
3755 1.1 christos else if (p->count != 0)
3756 1.1 christos {
3757 1.1.1.2 christos srel = elf_section_data (p->sec)->sreloc;
3758 1.1.1.2 christos srel->size += p->count * bed->s->sizeof_rela;
3759 1.1.1.2 christos if ((p->sec->output_section->flags & SEC_READONLY) != 0
3760 1.1.1.2 christos && (info->flags & DF_TEXTREL) == 0)
3761 1.1.1.6 christos {
3762 1.1.1.5 christos info->flags |= DF_TEXTREL;
3763 1.1.1.7 christos if ((info->warn_shared_textrel && bfd_link_pic (info))
3764 1.1.1.5 christos || info->error_textrel)
3765 1.1.1.2 christos /* xgettext:c-format */
3766 1.1.1.2 christos info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3767 1.1 christos p->sec->owner, p->sec);
3768 1.1 christos }
3769 1.1 christos }
3770 1.1 christos }
3771 1.1 christos }
3772 1.1 christos
3773 1.1 christos local_got = elf_local_got_refcounts (ibfd);
3774 1.1 christos if (!local_got)
3775 1.1 christos continue;
3776 1.1 christos
3777 1.1 christos symtab_hdr = &elf_symtab_hdr (ibfd);
3778 1.1 christos locsymcount = symtab_hdr->sh_info;
3779 1.1 christos end_local_got = local_got + locsymcount;
3780 1.1 christos local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
3781 1.1 christos local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
3782 1.1 christos s = htab->elf.sgot;
3783 1.1 christos srel = htab->elf.srelgot;
3784 1.1 christos for (; local_got < end_local_got;
3785 1.1 christos ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3786 1.1 christos {
3787 1.1 christos *local_tlsdesc_gotent = (bfd_vma) -1;
3788 1.1 christos if (*local_got > 0)
3789 1.1 christos {
3790 1.1 christos if (GOT_TLS_GDESC_P (*local_tls_type))
3791 1.1 christos {
3792 1.1 christos *local_tlsdesc_gotent = htab->elf.sgotplt->size
3793 1.1 christos - elf_x86_64_compute_jump_table_size (htab);
3794 1.1 christos htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3795 1.1 christos *local_got = (bfd_vma) -2;
3796 1.1 christos }
3797 1.1 christos if (! GOT_TLS_GDESC_P (*local_tls_type)
3798 1.1 christos || GOT_TLS_GD_P (*local_tls_type))
3799 1.1 christos {
3800 1.1 christos *local_got = s->size;
3801 1.1 christos s->size += GOT_ENTRY_SIZE;
3802 1.1 christos if (GOT_TLS_GD_P (*local_tls_type))
3803 1.1.1.6 christos s->size += GOT_ENTRY_SIZE;
3804 1.1 christos }
3805 1.1 christos if (bfd_link_pic (info)
3806 1.1 christos || GOT_TLS_GD_ANY_P (*local_tls_type)
3807 1.1 christos || *local_tls_type == GOT_TLS_IE)
3808 1.1 christos {
3809 1.1 christos if (GOT_TLS_GDESC_P (*local_tls_type))
3810 1.1 christos {
3811 1.1 christos htab->elf.srelplt->size
3812 1.1 christos += bed->s->sizeof_rela;
3813 1.1 christos htab->tlsdesc_plt = (bfd_vma) -1;
3814 1.1 christos }
3815 1.1 christos if (! GOT_TLS_GDESC_P (*local_tls_type)
3816 1.1 christos || GOT_TLS_GD_P (*local_tls_type))
3817 1.1 christos srel->size += bed->s->sizeof_rela;
3818 1.1 christos }
3819 1.1 christos }
3820 1.1 christos else
3821 1.1 christos *local_got = (bfd_vma) -1;
3822 1.1 christos }
3823 1.1 christos }
3824 1.1 christos
3825 1.1 christos if (htab->tls_ld_got.refcount > 0)
3826 1.1 christos {
3827 1.1 christos /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3828 1.1 christos relocs. */
3829 1.1 christos htab->tls_ld_got.offset = htab->elf.sgot->size;
3830 1.1 christos htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
3831 1.1 christos htab->elf.srelgot->size += bed->s->sizeof_rela;
3832 1.1 christos }
3833 1.1 christos else
3834 1.1 christos htab->tls_ld_got.offset = -1;
3835 1.1 christos
3836 1.1 christos /* Allocate global sym .plt and .got entries, and space for global
3837 1.1 christos sym dynamic relocs. */
3838 1.1 christos elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
3839 1.1 christos info);
3840 1.1 christos
3841 1.1 christos /* Allocate .plt and .got entries, and space for local symbols. */
3842 1.1 christos htab_traverse (htab->loc_hash_table,
3843 1.1 christos elf_x86_64_allocate_local_dynrelocs,
3844 1.1 christos info);
3845 1.1 christos
3846 1.1 christos /* For every jump slot reserved in the sgotplt, reloc_count is
3847 1.1 christos incremented. However, when we reserve space for TLS descriptors,
3848 1.1.1.2 christos it's not incremented, so in order to compute the space reserved
3849 1.1.1.2 christos for them, it suffices to multiply the reloc count by the jump
3850 1.1.1.2 christos slot size.
3851 1.1.1.2 christos
3852 1.1 christos PR ld/13302: We start next_irelative_index at the end of .rela.plt
3853 1.1.1.2 christos so that R_X86_64_IRELATIVE entries come last. */
3854 1.1.1.2 christos if (htab->elf.srelplt)
3855 1.1.1.2 christos {
3856 1.1.1.2 christos htab->sgotplt_jump_table_size
3857 1.1.1.2 christos = elf_x86_64_compute_jump_table_size (htab);
3858 1.1.1.2 christos htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3859 1.1.1.2 christos }
3860 1.1 christos else if (htab->elf.irelplt)
3861 1.1 christos htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3862 1.1 christos
3863 1.1 christos if (htab->tlsdesc_plt)
3864 1.1 christos {
3865 1.1 christos /* If we're not using lazy TLS relocations, don't generate the
3866 1.1 christos PLT and GOT entries they require. */
3867 1.1 christos if ((info->flags & DF_BIND_NOW))
3868 1.1 christos htab->tlsdesc_plt = 0;
3869 1.1 christos else
3870 1.1 christos {
3871 1.1 christos htab->tlsdesc_got = htab->elf.sgot->size;
3872 1.1 christos htab->elf.sgot->size += GOT_ENTRY_SIZE;
3873 1.1 christos /* Reserve room for the initial entry.
3874 1.1.1.2 christos FIXME: we could probably do away with it in this case. */
3875 1.1 christos if (htab->elf.splt->size == 0)
3876 1.1.1.2 christos htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
3877 1.1 christos htab->tlsdesc_plt = htab->elf.splt->size;
3878 1.1 christos htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
3879 1.1 christos }
3880 1.1 christos }
3881 1.1 christos
3882 1.1 christos if (htab->elf.sgotplt)
3883 1.1.1.2 christos {
3884 1.1.1.2 christos /* Don't allocate .got.plt section if there are no GOT nor PLT
3885 1.1.1.2 christos entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
3886 1.1 christos if ((htab->elf.hgot == NULL
3887 1.1 christos || !htab->elf.hgot->ref_regular_nonweak)
3888 1.1 christos && (htab->elf.sgotplt->size
3889 1.1 christos == get_elf_backend_data (output_bfd)->got_header_size)
3890 1.1 christos && (htab->elf.splt == NULL
3891 1.1 christos || htab->elf.splt->size == 0)
3892 1.1 christos && (htab->elf.sgot == NULL
3893 1.1 christos || htab->elf.sgot->size == 0)
3894 1.1 christos && (htab->elf.iplt == NULL
3895 1.1 christos || htab->elf.iplt->size == 0)
3896 1.1 christos && (htab->elf.igotplt == NULL
3897 1.1 christos || htab->elf.igotplt->size == 0))
3898 1.1 christos htab->elf.sgotplt->size = 0;
3899 1.1.1.7 christos }
3900 1.1.1.7 christos
3901 1.1.1.7 christos arch_data = (htab->plt_bnd != NULL
3902 1.1.1.7 christos ? &elf_x86_64_bnd_arch_bed
3903 1.1.1.7 christos : get_elf_x86_64_arch_data (bed));
3904 1.1.1.7 christos
3905 1.1.1.7 christos if (_bfd_elf_eh_frame_present (info))
3906 1.1.1.7 christos {
3907 1.1.1.7 christos if (htab->plt_eh_frame != NULL
3908 1.1.1.7 christos && htab->elf.splt != NULL
3909 1.1.1.7 christos && htab->elf.splt->size != 0
3910 1.1.1.7 christos && !bfd_is_abs_section (htab->elf.splt->output_section))
3911 1.1.1.7 christos htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
3912 1.1.1.7 christos
3913 1.1.1.7 christos if (htab->plt_got_eh_frame != NULL
3914 1.1.1.7 christos && htab->plt_got != NULL
3915 1.1.1.7 christos && htab->plt_got->size != 0
3916 1.1.1.7 christos && !bfd_is_abs_section (htab->plt_got->output_section))
3917 1.1.1.7 christos htab->plt_got_eh_frame->size = arch_data->eh_frame_plt_got_size;
3918 1.1.1.7 christos
3919 1.1.1.7 christos /* Unwind info for .plt.bnd and .plt.got sections are
3920 1.1.1.7 christos identical. */
3921 1.1.1.7 christos if (htab->plt_bnd_eh_frame != NULL
3922 1.1.1.7 christos && htab->plt_bnd != NULL
3923 1.1.1.7 christos && htab->plt_bnd->size != 0
3924 1.1.1.2 christos && !bfd_is_abs_section (htab->plt_bnd->output_section))
3925 1.1.1.2 christos htab->plt_bnd_eh_frame->size = arch_data->eh_frame_plt_got_size;
3926 1.1 christos }
3927 1.1 christos
3928 1.1 christos /* We now have determined the sizes of the various dynamic sections.
3929 1.1 christos Allocate memory for them. */
3930 1.1 christos relocs = FALSE;
3931 1.1 christos for (s = dynobj->sections; s != NULL; s = s->next)
3932 1.1 christos {
3933 1.1 christos if ((s->flags & SEC_LINKER_CREATED) == 0)
3934 1.1 christos continue;
3935 1.1 christos
3936 1.1 christos if (s == htab->elf.splt
3937 1.1 christos || s == htab->elf.sgot
3938 1.1 christos || s == htab->elf.sgotplt
3939 1.1.1.4 christos || s == htab->elf.iplt
3940 1.1.1.4 christos || s == htab->elf.igotplt
3941 1.1.1.2 christos || s == htab->plt_bnd
3942 1.1.1.7 christos || s == htab->plt_got
3943 1.1.1.7 christos || s == htab->plt_eh_frame
3944 1.1.1.7 christos || s == htab->plt_got_eh_frame
3945 1.1.1.7 christos || s == htab->plt_bnd_eh_frame
3946 1.1 christos || s == htab->elf.sdynbss
3947 1.1 christos || s == htab->elf.sdynrelro)
3948 1.1 christos {
3949 1.1 christos /* Strip this section if we don't need it; see the
3950 1.1 christos comment below. */
3951 1.1 christos }
3952 1.1 christos else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3953 1.1 christos {
3954 1.1 christos if (s->size != 0 && s != htab->elf.srelplt)
3955 1.1 christos relocs = TRUE;
3956 1.1 christos
3957 1.1 christos /* We use the reloc_count field as a counter if we need
3958 1.1 christos to copy relocs into the output file. */
3959 1.1 christos if (s != htab->elf.srelplt)
3960 1.1 christos s->reloc_count = 0;
3961 1.1 christos }
3962 1.1 christos else
3963 1.1 christos {
3964 1.1 christos /* It's not one of our sections, so don't allocate space. */
3965 1.1 christos continue;
3966 1.1 christos }
3967 1.1 christos
3968 1.1 christos if (s->size == 0)
3969 1.1 christos {
3970 1.1 christos /* If we don't need this section, strip it from the
3971 1.1 christos output file. This is mostly to handle .rela.bss and
3972 1.1 christos .rela.plt. We must create both sections in
3973 1.1 christos create_dynamic_sections, because they must be created
3974 1.1 christos before the linker maps input sections to output
3975 1.1 christos sections. The linker does that before
3976 1.1 christos adjust_dynamic_symbol is called, and it is that
3977 1.1 christos function which decides whether anything needs to go
3978 1.1 christos into these sections. */
3979 1.1 christos
3980 1.1 christos s->flags |= SEC_EXCLUDE;
3981 1.1 christos continue;
3982 1.1 christos }
3983 1.1 christos
3984 1.1 christos if ((s->flags & SEC_HAS_CONTENTS) == 0)
3985 1.1 christos continue;
3986 1.1 christos
3987 1.1 christos /* Allocate memory for the section contents. We use bfd_zalloc
3988 1.1 christos here in case unused entries are not reclaimed before the
3989 1.1 christos section's contents are written out. This should not happen,
3990 1.1 christos but this way if it does, we get a R_X86_64_NONE reloc instead
3991 1.1 christos of garbage. */
3992 1.1 christos s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3993 1.1 christos if (s->contents == NULL)
3994 1.1 christos return FALSE;
3995 1.1.1.2 christos }
3996 1.1.1.2 christos
3997 1.1.1.2 christos if (htab->plt_eh_frame != NULL
3998 1.1.1.2 christos && htab->plt_eh_frame->contents != NULL)
3999 1.1.1.2 christos {
4000 1.1.1.2 christos memcpy (htab->plt_eh_frame->contents,
4001 1.1.1.2 christos arch_data->eh_frame_plt, htab->plt_eh_frame->size);
4002 1.1.1.2 christos bfd_put_32 (dynobj, htab->elf.splt->size,
4003 1.1.1.2 christos htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
4004 1.1.1.7 christos }
4005 1.1.1.7 christos
4006 1.1.1.7 christos if (htab->plt_got_eh_frame != NULL
4007 1.1.1.7 christos && htab->plt_got_eh_frame->contents != NULL)
4008 1.1.1.7 christos {
4009 1.1.1.7 christos memcpy (htab->plt_got_eh_frame->contents,
4010 1.1.1.7 christos arch_data->eh_frame_plt_got,
4011 1.1.1.7 christos htab->plt_got_eh_frame->size);
4012 1.1.1.7 christos bfd_put_32 (dynobj, htab->plt_got->size,
4013 1.1.1.7 christos (htab->plt_got_eh_frame->contents
4014 1.1.1.7 christos + PLT_FDE_LEN_OFFSET));
4015 1.1.1.7 christos }
4016 1.1.1.7 christos
4017 1.1.1.7 christos if (htab->plt_bnd_eh_frame != NULL
4018 1.1.1.7 christos && htab->plt_bnd_eh_frame->contents != NULL)
4019 1.1.1.7 christos {
4020 1.1.1.7 christos memcpy (htab->plt_bnd_eh_frame->contents,
4021 1.1.1.7 christos arch_data->eh_frame_plt_got,
4022 1.1.1.7 christos htab->plt_bnd_eh_frame->size);
4023 1.1.1.7 christos bfd_put_32 (dynobj, htab->plt_bnd->size,
4024 1.1.1.7 christos (htab->plt_bnd_eh_frame->contents
4025 1.1.1.7 christos + PLT_FDE_LEN_OFFSET));
4026 1.1 christos }
4027 1.1 christos
4028 1.1 christos if (htab->elf.dynamic_sections_created)
4029 1.1 christos {
4030 1.1 christos /* Add some entries to the .dynamic section. We fill in the
4031 1.1 christos values later, in elf_x86_64_finish_dynamic_sections, but we
4032 1.1 christos must add the entries now so that we get the correct size for
4033 1.1 christos the .dynamic section. The DT_DEBUG entry is filled in by the
4034 1.1 christos dynamic linker and used by the debugger. */
4035 1.1 christos #define add_dynamic_entry(TAG, VAL) \
4036 1.1.1.6 christos _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4037 1.1 christos
4038 1.1 christos if (bfd_link_executable (info))
4039 1.1 christos {
4040 1.1 christos if (!add_dynamic_entry (DT_DEBUG, 0))
4041 1.1 christos return FALSE;
4042 1.1 christos }
4043 1.1 christos
4044 1.1.1.5 christos if (htab->elf.splt->size != 0)
4045 1.1.1.5 christos {
4046 1.1.1.5 christos /* DT_PLTGOT is used by prelink even if there is no PLT
4047 1.1 christos relocation. */
4048 1.1 christos if (!add_dynamic_entry (DT_PLTGOT, 0))
4049 1.1.1.5 christos return FALSE;
4050 1.1.1.5 christos
4051 1.1.1.5 christos if (htab->elf.srelplt->size != 0)
4052 1.1.1.5 christos {
4053 1.1.1.5 christos if (!add_dynamic_entry (DT_PLTRELSZ, 0)
4054 1.1.1.5 christos || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4055 1.1.1.5 christos || !add_dynamic_entry (DT_JMPREL, 0))
4056 1.1.1.5 christos return FALSE;
4057 1.1 christos }
4058 1.1 christos
4059 1.1 christos if (htab->tlsdesc_plt
4060 1.1 christos && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4061 1.1 christos || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4062 1.1 christos return FALSE;
4063 1.1 christos }
4064 1.1 christos
4065 1.1 christos if (relocs)
4066 1.1 christos {
4067 1.1 christos if (!add_dynamic_entry (DT_RELA, 0)
4068 1.1 christos || !add_dynamic_entry (DT_RELASZ, 0)
4069 1.1 christos || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
4070 1.1 christos return FALSE;
4071 1.1 christos
4072 1.1 christos /* If any dynamic relocs apply to a read-only section,
4073 1.1.1.2 christos then we need a DT_TEXTREL entry. */
4074 1.1 christos if ((info->flags & DF_TEXTREL) == 0)
4075 1.1 christos elf_link_hash_traverse (&htab->elf,
4076 1.1 christos elf_x86_64_readonly_dynrelocs,
4077 1.1 christos info);
4078 1.1 christos
4079 1.1.1.6 christos if ((info->flags & DF_TEXTREL) != 0)
4080 1.1.1.6 christos {
4081 1.1.1.6 christos if (htab->readonly_dynrelocs_against_ifunc)
4082 1.1.1.6 christos {
4083 1.1.1.6 christos info->callbacks->einfo
4084 1.1.1.6 christos (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
4085 1.1.1.6 christos bfd_set_error (bfd_error_bad_value);
4086 1.1.1.6 christos return FALSE;
4087 1.1 christos }
4088 1.1 christos
4089 1.1 christos if (!add_dynamic_entry (DT_TEXTREL, 0))
4090 1.1 christos return FALSE;
4091 1.1 christos }
4092 1.1 christos }
4093 1.1 christos }
4094 1.1 christos #undef add_dynamic_entry
4095 1.1 christos
4096 1.1 christos return TRUE;
4097 1.1 christos }
4098 1.1 christos
4099 1.1 christos static bfd_boolean
4100 1.1 christos elf_x86_64_always_size_sections (bfd *output_bfd,
4101 1.1 christos struct bfd_link_info *info)
4102 1.1 christos {
4103 1.1 christos asection *tls_sec = elf_hash_table (info)->tls_sec;
4104 1.1 christos
4105 1.1 christos if (tls_sec)
4106 1.1 christos {
4107 1.1 christos struct elf_link_hash_entry *tlsbase;
4108 1.1 christos
4109 1.1 christos tlsbase = elf_link_hash_lookup (elf_hash_table (info),
4110 1.1 christos "_TLS_MODULE_BASE_",
4111 1.1 christos FALSE, FALSE, FALSE);
4112 1.1 christos
4113 1.1 christos if (tlsbase && tlsbase->type == STT_TLS)
4114 1.1 christos {
4115 1.1 christos struct elf_x86_64_link_hash_table *htab;
4116 1.1 christos struct bfd_link_hash_entry *bh = NULL;
4117 1.1 christos const struct elf_backend_data *bed
4118 1.1 christos = get_elf_backend_data (output_bfd);
4119 1.1 christos
4120 1.1 christos htab = elf_x86_64_hash_table (info);
4121 1.1 christos if (htab == NULL)
4122 1.1 christos return FALSE;
4123 1.1 christos
4124 1.1 christos if (!(_bfd_generic_link_add_one_symbol
4125 1.1 christos (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
4126 1.1 christos tls_sec, 0, NULL, FALSE,
4127 1.1 christos bed->collect, &bh)))
4128 1.1 christos return FALSE;
4129 1.1 christos
4130 1.1 christos htab->tls_module_base = bh;
4131 1.1 christos
4132 1.1 christos tlsbase = (struct elf_link_hash_entry *)bh;
4133 1.1.1.5 christos tlsbase->def_regular = 1;
4134 1.1 christos tlsbase->other = STV_HIDDEN;
4135 1.1 christos tlsbase->root.linker_def = 1;
4136 1.1 christos (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
4137 1.1 christos }
4138 1.1 christos }
4139 1.1 christos
4140 1.1 christos return TRUE;
4141 1.1 christos }
4142 1.1 christos
4143 1.1 christos /* _TLS_MODULE_BASE_ needs to be treated especially when linking
4144 1.1 christos executables. Rather than setting it to the beginning of the TLS
4145 1.1 christos section, we have to set it to the end. This function may be called
4146 1.1 christos multiple times, it is idempotent. */
4147 1.1 christos
4148 1.1 christos static void
4149 1.1 christos elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
4150 1.1 christos {
4151 1.1 christos struct elf_x86_64_link_hash_table *htab;
4152 1.1.1.6 christos struct bfd_link_hash_entry *base;
4153 1.1 christos
4154 1.1 christos if (!bfd_link_executable (info))
4155 1.1 christos return;
4156 1.1 christos
4157 1.1 christos htab = elf_x86_64_hash_table (info);
4158 1.1 christos if (htab == NULL)
4159 1.1 christos return;
4160 1.1 christos
4161 1.1 christos base = htab->tls_module_base;
4162 1.1 christos if (base == NULL)
4163 1.1 christos return;
4164 1.1 christos
4165 1.1 christos base->u.def.value = htab->elf.tls_size;
4166 1.1 christos }
4167 1.1 christos
4168 1.1 christos /* Return the base VMA address which should be subtracted from real addresses
4169 1.1 christos when resolving @dtpoff relocation.
4170 1.1 christos This is PT_TLS segment p_vaddr. */
4171 1.1 christos
4172 1.1 christos static bfd_vma
4173 1.1 christos elf_x86_64_dtpoff_base (struct bfd_link_info *info)
4174 1.1 christos {
4175 1.1 christos /* If tls_sec is NULL, we should have signalled an error already. */
4176 1.1 christos if (elf_hash_table (info)->tls_sec == NULL)
4177 1.1 christos return 0;
4178 1.1 christos return elf_hash_table (info)->tls_sec->vma;
4179 1.1 christos }
4180 1.1 christos
4181 1.1 christos /* Return the relocation value for @tpoff relocation
4182 1.1 christos if STT_TLS virtual address is ADDRESS. */
4183 1.1 christos
4184 1.1 christos static bfd_vma
4185 1.1 christos elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
4186 1.1 christos {
4187 1.1 christos struct elf_link_hash_table *htab = elf_hash_table (info);
4188 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
4189 1.1 christos bfd_vma static_tls_size;
4190 1.1 christos
4191 1.1 christos /* If tls_segment is NULL, we should have signalled an error already. */
4192 1.1 christos if (htab->tls_sec == NULL)
4193 1.1 christos return 0;
4194 1.1 christos
4195 1.1 christos /* Consider special static TLS alignment requirements. */
4196 1.1 christos static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
4197 1.1 christos return address - static_tls_size - htab->tls_sec->vma;
4198 1.1 christos }
4199 1.1 christos
4200 1.1 christos /* Is the instruction before OFFSET in CONTENTS a 32bit relative
4201 1.1 christos branch? */
4202 1.1 christos
4203 1.1 christos static bfd_boolean
4204 1.1 christos is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
4205 1.1 christos {
4206 1.1 christos /* Opcode Instruction
4207 1.1 christos 0xe8 call
4208 1.1 christos 0xe9 jump
4209 1.1 christos 0x0f 0x8x conditional jump */
4210 1.1 christos return ((offset > 0
4211 1.1 christos && (contents [offset - 1] == 0xe8
4212 1.1 christos || contents [offset - 1] == 0xe9))
4213 1.1 christos || (offset > 1
4214 1.1 christos && contents [offset - 2] == 0x0f
4215 1.1 christos && (contents [offset - 1] & 0xf0) == 0x80));
4216 1.1 christos }
4217 1.1 christos
4218 1.1 christos /* Relocate an x86_64 ELF section. */
4219 1.1 christos
4220 1.1 christos static bfd_boolean
4221 1.1 christos elf_x86_64_relocate_section (bfd *output_bfd,
4222 1.1 christos struct bfd_link_info *info,
4223 1.1 christos bfd *input_bfd,
4224 1.1 christos asection *input_section,
4225 1.1 christos bfd_byte *contents,
4226 1.1 christos Elf_Internal_Rela *relocs,
4227 1.1 christos Elf_Internal_Sym *local_syms,
4228 1.1 christos asection **local_sections)
4229 1.1 christos {
4230 1.1 christos struct elf_x86_64_link_hash_table *htab;
4231 1.1 christos Elf_Internal_Shdr *symtab_hdr;
4232 1.1 christos struct elf_link_hash_entry **sym_hashes;
4233 1.1 christos bfd_vma *local_got_offsets;
4234 1.1.1.6 christos bfd_vma *local_tlsdesc_gotents;
4235 1.1 christos Elf_Internal_Rela *rel;
4236 1.1.1.2 christos Elf_Internal_Rela *wrel;
4237 1.1 christos Elf_Internal_Rela *relend;
4238 1.1 christos const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
4239 1.1 christos
4240 1.1.1.6 christos BFD_ASSERT (is_x86_64_elf (input_bfd));
4241 1.1.1.6 christos
4242 1.1.1.6 christos /* Skip if check_relocs failed. */
4243 1.1.1.6 christos if (input_section->check_relocs_failed)
4244 1.1 christos return FALSE;
4245 1.1 christos
4246 1.1 christos htab = elf_x86_64_hash_table (info);
4247 1.1 christos if (htab == NULL)
4248 1.1 christos return FALSE;
4249 1.1 christos symtab_hdr = &elf_symtab_hdr (input_bfd);
4250 1.1 christos sym_hashes = elf_sym_hashes (input_bfd);
4251 1.1 christos local_got_offsets = elf_local_got_offsets (input_bfd);
4252 1.1 christos local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
4253 1.1 christos
4254 1.1.1.6 christos elf_x86_64_set_tls_module_base (info);
4255 1.1 christos
4256 1.1.1.6 christos rel = wrel = relocs;
4257 1.1 christos relend = relocs + input_section->reloc_count;
4258 1.1 christos for (; rel < relend; wrel++, rel++)
4259 1.1 christos {
4260 1.1 christos unsigned int r_type;
4261 1.1 christos reloc_howto_type *howto;
4262 1.1.1.4 christos unsigned long r_symndx;
4263 1.1 christos struct elf_link_hash_entry *h;
4264 1.1 christos struct elf_x86_64_link_hash_entry *eh;
4265 1.1.1.4 christos Elf_Internal_Sym *sym;
4266 1.1 christos asection *sec;
4267 1.1 christos bfd_vma off, offplt, plt_offset;
4268 1.1 christos bfd_vma relocation;
4269 1.1 christos bfd_boolean unresolved_reloc;
4270 1.1.1.4 christos bfd_reloc_status_type r;
4271 1.1.1.2 christos int tls_type;
4272 1.1.1.6 christos asection *base_got, *resolved_plt;
4273 1.1 christos bfd_vma st_size;
4274 1.1 christos bfd_boolean resolved_to_zero;
4275 1.1 christos
4276 1.1 christos r_type = ELF32_R_TYPE (rel->r_info);
4277 1.1.1.6 christos if (r_type == (int) R_X86_64_GNU_VTINHERIT
4278 1.1.1.6 christos || r_type == (int) R_X86_64_GNU_VTENTRY)
4279 1.1.1.6 christos {
4280 1.1.1.6 christos if (wrel != rel)
4281 1.1.1.6 christos *wrel = *rel;
4282 1.1 christos continue;
4283 1.1.1.2 christos }
4284 1.1 christos
4285 1.1.1.7 christos if (r_type >= (int) R_X86_64_standard)
4286 1.1.1.7 christos {
4287 1.1.1.2 christos _bfd_error_handler
4288 1.1.1.7 christos /* xgettext:c-format */
4289 1.1 christos (_("%B: unrecognized relocation (0x%x) in section `%A'"),
4290 1.1 christos input_bfd, r_type, input_section);
4291 1.1 christos bfd_set_error (bfd_error_bad_value);
4292 1.1 christos return FALSE;
4293 1.1.1.2 christos }
4294 1.1.1.2 christos
4295 1.1.1.2 christos if (r_type != (int) R_X86_64_32
4296 1.1.1.2 christos || ABI_64_P (output_bfd))
4297 1.1.1.2 christos howto = x86_64_elf_howto_table + r_type;
4298 1.1.1.2 christos else
4299 1.1 christos howto = (x86_64_elf_howto_table
4300 1.1 christos + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
4301 1.1 christos r_symndx = htab->r_sym (rel->r_info);
4302 1.1 christos h = NULL;
4303 1.1 christos sym = NULL;
4304 1.1 christos sec = NULL;
4305 1.1 christos unresolved_reloc = FALSE;
4306 1.1 christos if (r_symndx < symtab_hdr->sh_info)
4307 1.1 christos {
4308 1.1 christos sym = local_syms + r_symndx;
4309 1.1 christos sec = local_sections[r_symndx];
4310 1.1 christos
4311 1.1.1.2 christos relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
4312 1.1 christos &sec, rel);
4313 1.1 christos st_size = sym->st_size;
4314 1.1.1.6 christos
4315 1.1 christos /* Relocate against local STT_GNU_IFUNC symbol. */
4316 1.1 christos if (!bfd_link_relocatable (info)
4317 1.1 christos && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4318 1.1 christos {
4319 1.1 christos h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
4320 1.1 christos rel, FALSE);
4321 1.1 christos if (h == NULL)
4322 1.1.1.2 christos abort ();
4323 1.1 christos
4324 1.1 christos /* Set STT_GNU_IFUNC symbol value. */
4325 1.1 christos h->root.u.def.value = sym->st_value;
4326 1.1 christos h->root.u.def.section = sec;
4327 1.1 christos }
4328 1.1 christos }
4329 1.1 christos else
4330 1.1.1.3 christos {
4331 1.1 christos bfd_boolean warned ATTRIBUTE_UNUSED;
4332 1.1 christos bfd_boolean ignored ATTRIBUTE_UNUSED;
4333 1.1 christos
4334 1.1 christos RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4335 1.1.1.3 christos r_symndx, symtab_hdr, sym_hashes,
4336 1.1.1.2 christos h, sec, relocation,
4337 1.1 christos unresolved_reloc, warned, ignored);
4338 1.1 christos st_size = h->size;
4339 1.1.1.2 christos }
4340 1.1.1.6 christos
4341 1.1.1.6 christos if (sec != NULL && discarded_section (sec))
4342 1.1.1.6 christos {
4343 1.1.1.6 christos _bfd_clear_contents (howto, input_bfd, input_section,
4344 1.1.1.6 christos contents + rel->r_offset);
4345 1.1.1.6 christos wrel->r_offset = rel->r_offset;
4346 1.1.1.6 christos wrel->r_info = 0;
4347 1.1.1.6 christos wrel->r_addend = 0;
4348 1.1.1.6 christos
4349 1.1.1.6 christos /* For ld -r, remove relocations in debug sections against
4350 1.1.1.6 christos sections defined in discarded sections. Not done for
4351 1.1.1.6 christos eh_frame editing code expects to be present. */
4352 1.1.1.6 christos if (bfd_link_relocatable (info)
4353 1.1 christos && (input_section->flags & SEC_DEBUGGING))
4354 1.1.1.6 christos wrel--;
4355 1.1.1.6 christos
4356 1.1.1.6 christos continue;
4357 1.1.1.6 christos }
4358 1.1.1.6 christos
4359 1.1.1.6 christos if (bfd_link_relocatable (info))
4360 1.1.1.6 christos {
4361 1.1.1.6 christos if (wrel != rel)
4362 1.1.1.6 christos *wrel = *rel;
4363 1.1 christos continue;
4364 1.1.1.2 christos }
4365 1.1.1.2 christos
4366 1.1.1.2 christos if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
4367 1.1.1.2 christos {
4368 1.1.1.2 christos if (r_type == R_X86_64_64)
4369 1.1.1.2 christos {
4370 1.1.1.2 christos /* For x32, treat R_X86_64_64 like R_X86_64_32 and
4371 1.1.1.2 christos zero-extend it to 64bit if addend is zero. */
4372 1.1.1.2 christos r_type = R_X86_64_32;
4373 1.1.1.2 christos memset (contents + rel->r_offset + 4, 0, 4);
4374 1.1.1.2 christos }
4375 1.1.1.2 christos else if (r_type == R_X86_64_SIZE64)
4376 1.1.1.2 christos {
4377 1.1.1.2 christos /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
4378 1.1.1.2 christos zero-extend it to 64bit if addend is zero. */
4379 1.1.1.2 christos r_type = R_X86_64_SIZE32;
4380 1.1.1.2 christos memset (contents + rel->r_offset + 4, 0, 4);
4381 1.1.1.2 christos }
4382 1.1.1.4 christos }
4383 1.1.1.4 christos
4384 1.1 christos eh = (struct elf_x86_64_link_hash_entry *) h;
4385 1.1 christos
4386 1.1 christos /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4387 1.1 christos it here if it is defined in a non-shared object. */
4388 1.1 christos if (h != NULL
4389 1.1 christos && h->type == STT_GNU_IFUNC
4390 1.1 christos && h->def_regular)
4391 1.1 christos {
4392 1.1 christos bfd_vma plt_index;
4393 1.1.1.6 christos const char *name;
4394 1.1.1.6 christos
4395 1.1.1.6 christos if ((input_section->flags & SEC_ALLOC) == 0)
4396 1.1.1.6 christos {
4397 1.1.1.6 christos /* Dynamic relocs are not propagated for SEC_DEBUGGING
4398 1.1.1.6 christos sections because such sections are not SEC_ALLOC and
4399 1.1.1.6 christos thus ld.so will not process them. */
4400 1.1.1.6 christos if ((input_section->flags & SEC_DEBUGGING) != 0)
4401 1.1.1.6 christos continue;
4402 1.1.1.6 christos abort ();
4403 1.1.1.6 christos }
4404 1.1.1.6 christos
4405 1.1.1.6 christos switch (r_type)
4406 1.1.1.6 christos {
4407 1.1.1.6 christos default:
4408 1.1.1.6 christos break;
4409 1.1.1.6 christos
4410 1.1.1.6 christos case R_X86_64_GOTPCREL:
4411 1.1.1.6 christos case R_X86_64_GOTPCRELX:
4412 1.1.1.6 christos case R_X86_64_REX_GOTPCRELX:
4413 1.1.1.6 christos case R_X86_64_GOTPCREL64:
4414 1.1.1.6 christos base_got = htab->elf.sgot;
4415 1.1.1.6 christos off = h->got.offset;
4416 1.1.1.6 christos
4417 1.1.1.6 christos if (base_got == NULL)
4418 1.1.1.6 christos abort ();
4419 1.1.1.6 christos
4420 1.1.1.6 christos if (off == (bfd_vma) -1)
4421 1.1.1.6 christos {
4422 1.1.1.6 christos /* We can't use h->got.offset here to save state, or
4423 1.1.1.6 christos even just remember the offset, as finish_dynamic_symbol
4424 1.1.1.6 christos would use that as offset into .got. */
4425 1.1.1.6 christos
4426 1.1.1.6 christos if (h->plt.offset == (bfd_vma) -1)
4427 1.1.1.6 christos abort ();
4428 1.1.1.6 christos
4429 1.1.1.6 christos if (htab->elf.splt != NULL)
4430 1.1.1.6 christos {
4431 1.1.1.6 christos plt_index = h->plt.offset / plt_entry_size - 1;
4432 1.1.1.6 christos off = (plt_index + 3) * GOT_ENTRY_SIZE;
4433 1.1.1.6 christos base_got = htab->elf.sgotplt;
4434 1.1.1.6 christos }
4435 1.1.1.6 christos else
4436 1.1.1.6 christos {
4437 1.1.1.6 christos plt_index = h->plt.offset / plt_entry_size;
4438 1.1.1.6 christos off = plt_index * GOT_ENTRY_SIZE;
4439 1.1.1.6 christos base_got = htab->elf.igotplt;
4440 1.1.1.6 christos }
4441 1.1.1.6 christos
4442 1.1.1.6 christos if (h->dynindx == -1
4443 1.1.1.6 christos || h->forced_local
4444 1.1.1.6 christos || info->symbolic)
4445 1.1.1.6 christos {
4446 1.1.1.6 christos /* This references the local defitionion. We must
4447 1.1.1.6 christos initialize this entry in the global offset table.
4448 1.1.1.6 christos Since the offset must always be a multiple of 8,
4449 1.1.1.6 christos we use the least significant bit to record
4450 1.1.1.6 christos whether we have initialized it already.
4451 1.1.1.6 christos
4452 1.1.1.6 christos When doing a dynamic link, we create a .rela.got
4453 1.1.1.6 christos relocation entry to initialize the value. This
4454 1.1.1.6 christos is done in the finish_dynamic_symbol routine. */
4455 1.1.1.6 christos if ((off & 1) != 0)
4456 1.1.1.6 christos off &= ~1;
4457 1.1.1.6 christos else
4458 1.1.1.6 christos {
4459 1.1.1.6 christos bfd_put_64 (output_bfd, relocation,
4460 1.1.1.6 christos base_got->contents + off);
4461 1.1.1.6 christos /* Note that this is harmless for the GOTPLT64
4462 1.1.1.6 christos case, as -1 | 1 still is -1. */
4463 1.1.1.6 christos h->got.offset |= 1;
4464 1.1.1.6 christos }
4465 1.1.1.6 christos }
4466 1.1.1.6 christos }
4467 1.1.1.6 christos
4468 1.1.1.6 christos relocation = (base_got->output_section->vma
4469 1.1.1.6 christos + base_got->output_offset + off);
4470 1.1.1.6 christos
4471 1.1.1.6 christos goto do_relocation;
4472 1.1.1.6 christos }
4473 1.1.1.6 christos
4474 1.1.1.6 christos if (h->plt.offset == (bfd_vma) -1)
4475 1.1.1.6 christos {
4476 1.1.1.6 christos /* Handle static pointers of STT_GNU_IFUNC symbols. */
4477 1.1.1.6 christos if (r_type == htab->pointer_r_type
4478 1.1.1.6 christos && (input_section->flags & SEC_CODE) == 0)
4479 1.1.1.6 christos goto do_ifunc_pointer;
4480 1.1 christos goto bad_ifunc_reloc;
4481 1.1 christos }
4482 1.1.1.4 christos
4483 1.1.1.4 christos /* STT_GNU_IFUNC symbol must go through PLT. */
4484 1.1.1.4 christos if (htab->elf.splt != NULL)
4485 1.1.1.4 christos {
4486 1.1.1.4 christos if (htab->plt_bnd != NULL)
4487 1.1.1.4 christos {
4488 1.1.1.4 christos resolved_plt = htab->plt_bnd;
4489 1.1.1.4 christos plt_offset = eh->plt_bnd.offset;
4490 1.1.1.4 christos }
4491 1.1.1.4 christos else
4492 1.1.1.4 christos {
4493 1.1.1.4 christos resolved_plt = htab->elf.splt;
4494 1.1.1.4 christos plt_offset = h->plt.offset;
4495 1.1.1.4 christos }
4496 1.1.1.4 christos }
4497 1.1.1.4 christos else
4498 1.1.1.4 christos {
4499 1.1.1.4 christos resolved_plt = htab->elf.iplt;
4500 1.1.1.4 christos plt_offset = h->plt.offset;
4501 1.1.1.4 christos }
4502 1.1.1.4 christos
4503 1.1 christos relocation = (resolved_plt->output_section->vma
4504 1.1 christos + resolved_plt->output_offset + plt_offset);
4505 1.1 christos
4506 1.1 christos switch (r_type)
4507 1.1.1.6 christos {
4508 1.1 christos default:
4509 1.1 christos bad_ifunc_reloc:
4510 1.1 christos if (h->root.root.string)
4511 1.1 christos name = h->root.root.string;
4512 1.1 christos else
4513 1.1.1.7 christos name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4514 1.1.1.7 christos NULL);
4515 1.1 christos _bfd_error_handler
4516 1.1.1.6 christos /* xgettext:c-format */
4517 1.1.1.6 christos (_("%B: relocation %s against STT_GNU_IFUNC "
4518 1.1 christos "symbol `%s' isn't supported"), input_bfd,
4519 1.1 christos howto->name, name);
4520 1.1 christos bfd_set_error (bfd_error_bad_value);
4521 1.1 christos return FALSE;
4522 1.1.1.6 christos
4523 1.1 christos case R_X86_64_32S:
4524 1.1 christos if (bfd_link_pic (info))
4525 1.1 christos abort ();
4526 1.1 christos goto do_relocation;
4527 1.1 christos
4528 1.1 christos case R_X86_64_32:
4529 1.1 christos if (ABI_64_P (output_bfd))
4530 1.1.1.2 christos goto do_relocation;
4531 1.1.1.6 christos /* FALLTHROUGH */
4532 1.1 christos case R_X86_64_64:
4533 1.1 christos do_ifunc_pointer:
4534 1.1 christos if (rel->r_addend != 0)
4535 1.1 christos {
4536 1.1 christos if (h->root.root.string)
4537 1.1 christos name = h->root.root.string;
4538 1.1 christos else
4539 1.1.1.7 christos name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4540 1.1.1.7 christos sym, NULL);
4541 1.1 christos _bfd_error_handler
4542 1.1 christos /* xgettext:c-format */
4543 1.1.1.6 christos (_("%B: relocation %s against STT_GNU_IFUNC "
4544 1.1 christos "symbol `%s' has non-zero addend: %d"),
4545 1.1 christos input_bfd, howto->name, name, rel->r_addend);
4546 1.1 christos bfd_set_error (bfd_error_bad_value);
4547 1.1 christos return FALSE;
4548 1.1 christos }
4549 1.1.1.6 christos
4550 1.1.1.6 christos /* Generate dynamic relcoation only when there is a
4551 1.1.1.6 christos non-GOT reference in a shared object or there is no
4552 1.1.1.6 christos PLT. */
4553 1.1 christos if ((bfd_link_pic (info) && h->non_got_ref)
4554 1.1 christos || h->plt.offset == (bfd_vma) -1)
4555 1.1 christos {
4556 1.1 christos Elf_Internal_Rela outrel;
4557 1.1 christos asection *sreloc;
4558 1.1 christos
4559 1.1 christos /* Need a dynamic relocation to get the real function
4560 1.1 christos address. */
4561 1.1 christos outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4562 1.1 christos info,
4563 1.1 christos input_section,
4564 1.1 christos rel->r_offset);
4565 1.1 christos if (outrel.r_offset == (bfd_vma) -1
4566 1.1 christos || outrel.r_offset == (bfd_vma) -2)
4567 1.1 christos abort ();
4568 1.1 christos
4569 1.1 christos outrel.r_offset += (input_section->output_section->vma
4570 1.1 christos + input_section->output_offset);
4571 1.1 christos
4572 1.1.1.6 christos if (h->dynindx == -1
4573 1.1 christos || h->forced_local
4574 1.1 christos || bfd_link_executable (info))
4575 1.1 christos {
4576 1.1 christos /* This symbol is resolved locally. */
4577 1.1 christos outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4578 1.1 christos outrel.r_addend = (h->root.u.def.value
4579 1.1 christos + h->root.u.def.section->output_section->vma
4580 1.1 christos + h->root.u.def.section->output_offset);
4581 1.1 christos }
4582 1.1 christos else
4583 1.1 christos {
4584 1.1 christos outrel.r_info = htab->r_info (h->dynindx, r_type);
4585 1.1 christos outrel.r_addend = 0;
4586 1.1.1.6 christos }
4587 1.1.1.6 christos
4588 1.1.1.6 christos /* Dynamic relocations are stored in
4589 1.1.1.6 christos 1. .rela.ifunc section in PIC object.
4590 1.1.1.6 christos 2. .rela.got section in dynamic executable.
4591 1.1.1.6 christos 3. .rela.iplt section in static executable. */
4592 1.1.1.6 christos if (bfd_link_pic (info))
4593 1.1.1.6 christos sreloc = htab->elf.irelifunc;
4594 1.1.1.6 christos else if (htab->elf.splt != NULL)
4595 1.1.1.6 christos sreloc = htab->elf.srelgot;
4596 1.1 christos else
4597 1.1 christos sreloc = htab->elf.irelplt;
4598 1.1 christos elf_append_rela (output_bfd, sreloc, &outrel);
4599 1.1 christos
4600 1.1 christos /* If this reloc is against an external symbol, we
4601 1.1 christos do not want to fiddle with the addend. Otherwise,
4602 1.1 christos we need to include the symbol value so that it
4603 1.1 christos becomes an addend for the dynamic reloc. For an
4604 1.1 christos internal symbol, we have updated addend. */
4605 1.1 christos continue;
4606 1.1 christos }
4607 1.1.1.3 christos /* FALLTHROUGH */
4608 1.1 christos case R_X86_64_PC32:
4609 1.1 christos case R_X86_64_PC32_BND:
4610 1.1.1.3 christos case R_X86_64_PC64:
4611 1.1 christos case R_X86_64_PLT32:
4612 1.1 christos case R_X86_64_PLT32_BND:
4613 1.1 christos goto do_relocation;
4614 1.1 christos }
4615 1.1.1.6 christos }
4616 1.1.1.6 christos
4617 1.1.1.6 christos resolved_to_zero = (eh != NULL
4618 1.1.1.6 christos && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4619 1.1.1.6 christos eh->has_got_reloc,
4620 1.1 christos eh));
4621 1.1 christos
4622 1.1 christos /* When generating a shared object, the relocations handled here are
4623 1.1 christos copied into the output file to be resolved at run time. */
4624 1.1 christos switch (r_type)
4625 1.1 christos {
4626 1.1 christos case R_X86_64_GOT32:
4627 1.1 christos case R_X86_64_GOT64:
4628 1.1 christos /* Relocation is to the entry for this symbol in the global
4629 1.1.1.6 christos offset table. */
4630 1.1.1.6 christos case R_X86_64_GOTPCREL:
4631 1.1 christos case R_X86_64_GOTPCRELX:
4632 1.1 christos case R_X86_64_REX_GOTPCRELX:
4633 1.1 christos case R_X86_64_GOTPCREL64:
4634 1.1.1.4 christos /* Use global offset table entry as symbol value. */
4635 1.1 christos case R_X86_64_GOTPLT64:
4636 1.1 christos /* This is obsolete and treated the the same as GOT64. */
4637 1.1 christos base_got = htab->elf.sgot;
4638 1.1 christos
4639 1.1 christos if (htab->elf.sgot == NULL)
4640 1.1 christos abort ();
4641 1.1 christos
4642 1.1 christos if (h != NULL)
4643 1.1 christos {
4644 1.1 christos bfd_boolean dyn;
4645 1.1 christos
4646 1.1.1.2 christos off = h->got.offset;
4647 1.1 christos if (h->needs_plt
4648 1.1 christos && h->plt.offset != (bfd_vma)-1
4649 1.1 christos && off == (bfd_vma)-1)
4650 1.1 christos {
4651 1.1 christos /* We can't use h->got.offset here to save
4652 1.1 christos state, or even just remember the offset, as
4653 1.1.1.2 christos finish_dynamic_symbol would use that as offset into
4654 1.1 christos .got. */
4655 1.1 christos bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
4656 1.1 christos off = (plt_index + 3) * GOT_ENTRY_SIZE;
4657 1.1 christos base_got = htab->elf.sgotplt;
4658 1.1 christos }
4659 1.1 christos
4660 1.1.1.6 christos dyn = htab->elf.dynamic_sections_created;
4661 1.1.1.6 christos
4662 1.1 christos if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4663 1.1 christos || (bfd_link_pic (info)
4664 1.1 christos && SYMBOL_REFERENCES_LOCAL (info, h))
4665 1.1 christos || (ELF_ST_VISIBILITY (h->other)
4666 1.1 christos && h->root.type == bfd_link_hash_undefweak))
4667 1.1 christos {
4668 1.1 christos /* This is actually a static link, or it is a -Bsymbolic
4669 1.1 christos link and the symbol is defined locally, or the symbol
4670 1.1 christos was forced to be local because of a version file. We
4671 1.1 christos must initialize this entry in the global offset table.
4672 1.1 christos Since the offset must always be a multiple of 8, we
4673 1.1 christos use the least significant bit to record whether we
4674 1.1 christos have initialized it already.
4675 1.1 christos
4676 1.1 christos When doing a dynamic link, we create a .rela.got
4677 1.1 christos relocation entry to initialize the value. This is
4678 1.1 christos done in the finish_dynamic_symbol routine. */
4679 1.1 christos if ((off & 1) != 0)
4680 1.1 christos off &= ~1;
4681 1.1 christos else
4682 1.1 christos {
4683 1.1 christos bfd_put_64 (output_bfd, relocation,
4684 1.1.1.2 christos base_got->contents + off);
4685 1.1 christos /* Note that this is harmless for the GOTPLT64 case,
4686 1.1 christos as -1 | 1 still is -1. */
4687 1.1 christos h->got.offset |= 1;
4688 1.1 christos }
4689 1.1 christos }
4690 1.1 christos else
4691 1.1 christos unresolved_reloc = FALSE;
4692 1.1 christos }
4693 1.1 christos else
4694 1.1 christos {
4695 1.1 christos if (local_got_offsets == NULL)
4696 1.1 christos abort ();
4697 1.1 christos
4698 1.1 christos off = local_got_offsets[r_symndx];
4699 1.1 christos
4700 1.1 christos /* The offset must always be a multiple of 8. We use
4701 1.1 christos the least significant bit to record whether we have
4702 1.1 christos already generated the necessary reloc. */
4703 1.1 christos if ((off & 1) != 0)
4704 1.1 christos off &= ~1;
4705 1.1 christos else
4706 1.1 christos {
4707 1.1 christos bfd_put_64 (output_bfd, relocation,
4708 1.1.1.6 christos base_got->contents + off);
4709 1.1 christos
4710 1.1 christos if (bfd_link_pic (info))
4711 1.1 christos {
4712 1.1 christos asection *s;
4713 1.1 christos Elf_Internal_Rela outrel;
4714 1.1 christos
4715 1.1 christos /* We need to generate a R_X86_64_RELATIVE reloc
4716 1.1 christos for the dynamic linker. */
4717 1.1 christos s = htab->elf.srelgot;
4718 1.1 christos if (s == NULL)
4719 1.1 christos abort ();
4720 1.1 christos
4721 1.1 christos outrel.r_offset = (base_got->output_section->vma
4722 1.1 christos + base_got->output_offset
4723 1.1 christos + off);
4724 1.1 christos outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4725 1.1 christos outrel.r_addend = relocation;
4726 1.1 christos elf_append_rela (output_bfd, s, &outrel);
4727 1.1 christos }
4728 1.1 christos
4729 1.1 christos local_got_offsets[r_symndx] |= 1;
4730 1.1 christos }
4731 1.1 christos }
4732 1.1 christos
4733 1.1 christos if (off >= (bfd_vma) -2)
4734 1.1 christos abort ();
4735 1.1 christos
4736 1.1.1.6 christos relocation = base_got->output_section->vma
4737 1.1.1.6 christos + base_got->output_offset + off;
4738 1.1.1.6 christos if (r_type != R_X86_64_GOTPCREL
4739 1.1.1.6 christos && r_type != R_X86_64_GOTPCRELX
4740 1.1 christos && r_type != R_X86_64_REX_GOTPCRELX
4741 1.1 christos && r_type != R_X86_64_GOTPCREL64)
4742 1.1 christos relocation -= htab->elf.sgotplt->output_section->vma
4743 1.1 christos - htab->elf.sgotplt->output_offset;
4744 1.1 christos
4745 1.1 christos break;
4746 1.1 christos
4747 1.1 christos case R_X86_64_GOTOFF64:
4748 1.1 christos /* Relocation is relative to the start of the global offset
4749 1.1.1.5 christos table. */
4750 1.1.1.5 christos
4751 1.1.1.5 christos /* Check to make sure it isn't a protected function or data
4752 1.1.1.5 christos symbol for shared library since it may not be local when
4753 1.1.1.6 christos used as function address or with copy relocation. We also
4754 1.1 christos need to make sure that a symbol is referenced locally. */
4755 1.1.1.5 christos if (bfd_link_pic (info) && h)
4756 1.1.1.5 christos {
4757 1.1.1.5 christos if (!h->def_regular)
4758 1.1.1.5 christos {
4759 1.1.1.5 christos const char *v;
4760 1.1.1.5 christos
4761 1.1.1.5 christos switch (ELF_ST_VISIBILITY (h->other))
4762 1.1.1.5 christos {
4763 1.1.1.5 christos case STV_HIDDEN:
4764 1.1.1.5 christos v = _("hidden symbol");
4765 1.1.1.5 christos break;
4766 1.1.1.5 christos case STV_INTERNAL:
4767 1.1.1.5 christos v = _("internal symbol");
4768 1.1.1.5 christos break;
4769 1.1.1.5 christos case STV_PROTECTED:
4770 1.1.1.5 christos v = _("protected symbol");
4771 1.1.1.5 christos break;
4772 1.1.1.5 christos default:
4773 1.1.1.5 christos v = _("symbol");
4774 1.1.1.5 christos break;
4775 1.1.1.7 christos }
4776 1.1.1.7 christos
4777 1.1.1.7 christos _bfd_error_handler
4778 1.1.1.7 christos /* xgettext:c-format */
4779 1.1.1.5 christos (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s"
4780 1.1.1.5 christos " `%s' can not be used when making a shared object"),
4781 1.1.1.5 christos input_bfd, v, h->root.root.string);
4782 1.1.1.5 christos bfd_set_error (bfd_error_bad_value);
4783 1.1.1.6 christos return FALSE;
4784 1.1.1.5 christos }
4785 1.1.1.5 christos else if (!bfd_link_executable (info)
4786 1.1.1.5 christos && !SYMBOL_REFERENCES_LOCAL (info, h)
4787 1.1.1.5 christos && (h->type == STT_FUNC
4788 1.1.1.5 christos || h->type == STT_OBJECT)
4789 1.1.1.7 christos && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4790 1.1.1.7 christos {
4791 1.1.1.7 christos _bfd_error_handler
4792 1.1.1.7 christos /* xgettext:c-format */
4793 1.1.1.5 christos (_("%B: relocation R_X86_64_GOTOFF64 against protected %s"
4794 1.1.1.5 christos " `%s' can not be used when making a shared object"),
4795 1.1.1.5 christos input_bfd,
4796 1.1.1.5 christos h->type == STT_FUNC ? "function" : "data",
4797 1.1 christos h->root.root.string);
4798 1.1.1.5 christos bfd_set_error (bfd_error_bad_value);
4799 1.1 christos return FALSE;
4800 1.1 christos }
4801 1.1 christos }
4802 1.1 christos
4803 1.1 christos /* Note that sgot is not involved in this
4804 1.1 christos calculation. We always want the start of .got.plt. If we
4805 1.1 christos defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4806 1.1 christos permitted by the ABI, we might have to change this
4807 1.1 christos calculation. */
4808 1.1 christos relocation -= htab->elf.sgotplt->output_section->vma
4809 1.1 christos + htab->elf.sgotplt->output_offset;
4810 1.1 christos break;
4811 1.1 christos
4812 1.1 christos case R_X86_64_GOTPC32:
4813 1.1 christos case R_X86_64_GOTPC64:
4814 1.1 christos /* Use global offset table as symbol value. */
4815 1.1 christos relocation = htab->elf.sgotplt->output_section->vma
4816 1.1 christos + htab->elf.sgotplt->output_offset;
4817 1.1 christos unresolved_reloc = FALSE;
4818 1.1 christos break;
4819 1.1 christos
4820 1.1 christos case R_X86_64_PLTOFF64:
4821 1.1.1.2 christos /* Relocation is PLT entry relative to GOT. For local
4822 1.1 christos symbols it's the symbol itself relative to GOT. */
4823 1.1.1.7 christos if (h != NULL
4824 1.1.1.7 christos /* See PLT32 handling. */
4825 1.1 christos && (h->plt.offset != (bfd_vma) -1
4826 1.1 christos || eh->plt_got.offset != (bfd_vma) -1)
4827 1.1.1.7 christos && htab->elf.splt != NULL)
4828 1.1.1.7 christos {
4829 1.1.1.7 christos if (eh->plt_got.offset != (bfd_vma) -1)
4830 1.1.1.7 christos {
4831 1.1.1.7 christos /* Use the GOT PLT. */
4832 1.1.1.7 christos resolved_plt = htab->plt_got;
4833 1.1.1.7 christos plt_offset = eh->plt_got.offset;
4834 1.1.1.4 christos }
4835 1.1.1.4 christos else if (htab->plt_bnd != NULL)
4836 1.1.1.4 christos {
4837 1.1.1.4 christos resolved_plt = htab->plt_bnd;
4838 1.1.1.4 christos plt_offset = eh->plt_bnd.offset;
4839 1.1.1.4 christos }
4840 1.1.1.4 christos else
4841 1.1.1.4 christos {
4842 1.1.1.4 christos resolved_plt = htab->elf.splt;
4843 1.1.1.4 christos plt_offset = h->plt.offset;
4844 1.1.1.4 christos }
4845 1.1.1.4 christos
4846 1.1.1.4 christos relocation = (resolved_plt->output_section->vma
4847 1.1 christos + resolved_plt->output_offset
4848 1.1 christos + plt_offset);
4849 1.1 christos unresolved_reloc = FALSE;
4850 1.1 christos }
4851 1.1 christos
4852 1.1 christos relocation -= htab->elf.sgotplt->output_section->vma
4853 1.1 christos + htab->elf.sgotplt->output_offset;
4854 1.1 christos break;
4855 1.1.1.3 christos
4856 1.1 christos case R_X86_64_PLT32:
4857 1.1 christos case R_X86_64_PLT32_BND:
4858 1.1 christos /* Relocation is to the entry for this symbol in the
4859 1.1 christos procedure linkage table. */
4860 1.1 christos
4861 1.1 christos /* Resolve a PLT32 reloc against a local symbol directly,
4862 1.1 christos without using the procedure linkage table. */
4863 1.1 christos if (h == NULL)
4864 1.1.1.4 christos break;
4865 1.1.1.4 christos
4866 1.1 christos if ((h->plt.offset == (bfd_vma) -1
4867 1.1 christos && eh->plt_got.offset == (bfd_vma) -1)
4868 1.1 christos || htab->elf.splt == NULL)
4869 1.1 christos {
4870 1.1 christos /* We didn't make a PLT entry for this symbol. This
4871 1.1 christos happens when statically linking PIC code, or when
4872 1.1 christos using -Bsymbolic. */
4873 1.1 christos break;
4874 1.1.1.4 christos }
4875 1.1.1.4 christos
4876 1.1.1.4 christos if (h->plt.offset != (bfd_vma) -1)
4877 1.1.1.4 christos {
4878 1.1.1.4 christos if (htab->plt_bnd != NULL)
4879 1.1.1.4 christos {
4880 1.1.1.4 christos resolved_plt = htab->plt_bnd;
4881 1.1.1.4 christos plt_offset = eh->plt_bnd.offset;
4882 1.1.1.4 christos }
4883 1.1.1.4 christos else
4884 1.1.1.4 christos {
4885 1.1.1.4 christos resolved_plt = htab->elf.splt;
4886 1.1.1.4 christos plt_offset = h->plt.offset;
4887 1.1.1.4 christos }
4888 1.1.1.4 christos }
4889 1.1.1.4 christos else
4890 1.1.1.4 christos {
4891 1.1.1.4 christos /* Use the GOT PLT. */
4892 1.1.1.4 christos resolved_plt = htab->plt_got;
4893 1.1.1.4 christos plt_offset = eh->plt_got.offset;
4894 1.1.1.4 christos }
4895 1.1.1.4 christos
4896 1.1.1.4 christos relocation = (resolved_plt->output_section->vma
4897 1.1 christos + resolved_plt->output_offset
4898 1.1 christos + plt_offset);
4899 1.1 christos unresolved_reloc = FALSE;
4900 1.1.1.2 christos break;
4901 1.1.1.2 christos
4902 1.1.1.2 christos case R_X86_64_SIZE32:
4903 1.1.1.2 christos case R_X86_64_SIZE64:
4904 1.1.1.2 christos /* Set to symbol size. */
4905 1.1.1.2 christos relocation = st_size;
4906 1.1 christos goto direct;
4907 1.1 christos
4908 1.1 christos case R_X86_64_PC8:
4909 1.1.1.3 christos case R_X86_64_PC16:
4910 1.1.1.5 christos case R_X86_64_PC32:
4911 1.1.1.6 christos case R_X86_64_PC32_BND:
4912 1.1.1.6 christos /* Don't complain about -fPIC if the symbol is undefined when
4913 1.1 christos building executable unless it is unresolved weak symbol. */
4914 1.1.1.5 christos if ((input_section->flags & SEC_ALLOC) != 0
4915 1.1.1.6 christos && (input_section->flags & SEC_READONLY) != 0
4916 1.1.1.6 christos && h != NULL
4917 1.1.1.6 christos && ((bfd_link_executable (info)
4918 1.1.1.6 christos && h->root.type == bfd_link_hash_undefweak
4919 1.1.1.6 christos && !resolved_to_zero)
4920 1.1.1.6 christos || (bfd_link_pic (info)
4921 1.1 christos && !(bfd_link_pie (info)
4922 1.1 christos && h->root.type == bfd_link_hash_undefined))))
4923 1.1 christos {
4924 1.1.1.3 christos bfd_boolean fail = FALSE;
4925 1.1.1.3 christos bfd_boolean branch
4926 1.1 christos = ((r_type == R_X86_64_PC32
4927 1.1 christos || r_type == R_X86_64_PC32_BND)
4928 1.1 christos && is_32bit_relative_branch (contents, rel->r_offset));
4929 1.1 christos
4930 1.1 christos if (SYMBOL_REFERENCES_LOCAL (info, h))
4931 1.1 christos {
4932 1.1.1.7 christos /* Symbol is referenced locally. Make sure it is
4933 1.1.1.7 christos defined locally or for a branch. */
4934 1.1 christos fail = (!(h->def_regular || ELF_COMMON_DEF_P (h))
4935 1.1.1.6 christos && !branch);
4936 1.1.1.4 christos }
4937 1.1 christos else if (!(bfd_link_pie (info)
4938 1.1.1.4 christos && (h->needs_copy || eh->needs_copy)))
4939 1.1.1.4 christos {
4940 1.1.1.4 christos /* Symbol doesn't need copy reloc and isn't referenced
4941 1.1 christos locally. We only allow branch to symbol with
4942 1.1 christos non-default visibility. */
4943 1.1 christos fail = (!branch
4944 1.1 christos || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4945 1.1 christos }
4946 1.1.1.6 christos
4947 1.1.1.6 christos if (fail)
4948 1.1 christos return elf_x86_64_need_pic (input_bfd, input_section,
4949 1.1 christos h, NULL, NULL, howto);
4950 1.1 christos }
4951 1.1 christos /* Fall through. */
4952 1.1 christos
4953 1.1 christos case R_X86_64_8:
4954 1.1 christos case R_X86_64_16:
4955 1.1 christos case R_X86_64_32:
4956 1.1 christos case R_X86_64_PC64:
4957 1.1 christos case R_X86_64_64:
4958 1.1 christos /* FIXME: The ABI says the linker should make sure the value is
4959 1.1.1.2 christos the same when it's zeroextended to 64 bit. */
4960 1.1 christos
4961 1.1 christos direct:
4962 1.1 christos if ((input_section->flags & SEC_ALLOC) == 0)
4963 1.1.1.4 christos break;
4964 1.1.1.5 christos
4965 1.1.1.6 christos /* Don't copy a pc-relative relocation into the output file
4966 1.1.1.6 christos if the symbol needs copy reloc or the symbol is undefined
4967 1.1.1.6 christos when building executable. Copy dynamic function pointer
4968 1.1.1.6 christos relocations. Don't generate dynamic relocations against
4969 1.1.1.6 christos resolved undefined weak symbols in PIE. */
4970 1.1.1.4 christos if ((bfd_link_pic (info)
4971 1.1.1.5 christos && !(bfd_link_pie (info)
4972 1.1.1.5 christos && h != NULL
4973 1.1.1.5 christos && (h->needs_copy
4974 1.1.1.7 christos || eh->needs_copy
4975 1.1.1.7 christos || h->root.type == bfd_link_hash_undefined)
4976 1.1.1.7 christos && (IS_X86_64_PCREL_TYPE (r_type)
4977 1.1 christos || r_type == R_X86_64_SIZE32
4978 1.1.1.6 christos || r_type == R_X86_64_SIZE64))
4979 1.1.1.6 christos && (h == NULL
4980 1.1.1.6 christos || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4981 1.1.1.2 christos && !resolved_to_zero)
4982 1.1.1.2 christos || h->root.type != bfd_link_hash_undefweak))
4983 1.1.1.2 christos && ((! IS_X86_64_PCREL_TYPE (r_type)
4984 1.1 christos && r_type != R_X86_64_SIZE32
4985 1.1 christos && r_type != R_X86_64_SIZE64)
4986 1.1.1.6 christos || ! SYMBOL_CALLS_LOCAL (info, h)))
4987 1.1 christos || (ELIMINATE_COPY_RELOCS
4988 1.1 christos && !bfd_link_pic (info)
4989 1.1.1.6 christos && h != NULL
4990 1.1.1.6 christos && h->dynindx != -1
4991 1.1.1.6 christos && (!h->non_got_ref
4992 1.1.1.6 christos || eh->func_pointer_refcount > 0
4993 1.1.1.6 christos || (h->root.type == bfd_link_hash_undefweak
4994 1.1.1.6 christos && !resolved_to_zero))
4995 1.1.1.6 christos && ((h->def_dynamic && !h->def_regular)
4996 1.1 christos /* Undefined weak symbol is bound locally when
4997 1.1 christos PIC is false. */
4998 1.1 christos || h->root.type == bfd_link_hash_undefined)))
4999 1.1 christos {
5000 1.1 christos Elf_Internal_Rela outrel;
5001 1.1 christos bfd_boolean skip, relocate;
5002 1.1 christos asection *sreloc;
5003 1.1 christos
5004 1.1 christos /* When generating a shared object, these relocations
5005 1.1 christos are copied into the output file to be resolved at run
5006 1.1 christos time. */
5007 1.1 christos skip = FALSE;
5008 1.1 christos relocate = FALSE;
5009 1.1 christos
5010 1.1 christos outrel.r_offset =
5011 1.1 christos _bfd_elf_section_offset (output_bfd, info, input_section,
5012 1.1 christos rel->r_offset);
5013 1.1 christos if (outrel.r_offset == (bfd_vma) -1)
5014 1.1 christos skip = TRUE;
5015 1.1 christos else if (outrel.r_offset == (bfd_vma) -2)
5016 1.1 christos skip = TRUE, relocate = TRUE;
5017 1.1 christos
5018 1.1 christos outrel.r_offset += (input_section->output_section->vma
5019 1.1 christos + input_section->output_offset);
5020 1.1 christos
5021 1.1 christos if (skip)
5022 1.1 christos memset (&outrel, 0, sizeof outrel);
5023 1.1 christos
5024 1.1 christos /* h->dynindx may be -1 if this symbol was marked to
5025 1.1 christos become local. */
5026 1.1 christos else if (h != NULL
5027 1.1.1.6 christos && h->dynindx != -1
5028 1.1.1.6 christos && (IS_X86_64_PCREL_TYPE (r_type)
5029 1.1 christos || !(bfd_link_executable (info)
5030 1.1 christos || SYMBOLIC_BIND (info, h))
5031 1.1 christos || ! h->def_regular))
5032 1.1 christos {
5033 1.1 christos outrel.r_info = htab->r_info (h->dynindx, r_type);
5034 1.1 christos outrel.r_addend = rel->r_addend;
5035 1.1 christos }
5036 1.1.1.6 christos else
5037 1.1.1.6 christos {
5038 1.1.1.6 christos /* This symbol is local, or marked to become local.
5039 1.1.1.6 christos When relocation overflow check is disabled, we
5040 1.1.1.6 christos convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
5041 1.1.1.6 christos if (r_type == htab->pointer_r_type
5042 1.1 christos || (r_type == R_X86_64_32
5043 1.1 christos && info->no_reloc_overflow_check))
5044 1.1 christos {
5045 1.1 christos relocate = TRUE;
5046 1.1 christos outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
5047 1.1.1.2 christos outrel.r_addend = relocation + rel->r_addend;
5048 1.1.1.2 christos }
5049 1.1.1.2 christos else if (r_type == R_X86_64_64
5050 1.1.1.2 christos && !ABI_64_P (output_bfd))
5051 1.1.1.2 christos {
5052 1.1.1.2 christos relocate = TRUE;
5053 1.1.1.2 christos outrel.r_info = htab->r_info (0,
5054 1.1.1.2 christos R_X86_64_RELATIVE64);
5055 1.1.1.2 christos outrel.r_addend = relocation + rel->r_addend;
5056 1.1.1.2 christos /* Check addend overflow. */
5057 1.1.1.2 christos if ((outrel.r_addend & 0x80000000)
5058 1.1.1.2 christos != (rel->r_addend & 0x80000000))
5059 1.1.1.2 christos {
5060 1.1.1.2 christos const char *name;
5061 1.1.1.2 christos int addend = rel->r_addend;
5062 1.1.1.2 christos if (h && h->root.root.string)
5063 1.1.1.2 christos name = h->root.root.string;
5064 1.1.1.2 christos else
5065 1.1.1.2 christos name = bfd_elf_sym_name (input_bfd, symtab_hdr,
5066 1.1.1.7 christos sym, NULL);
5067 1.1.1.7 christos if (addend < 0)
5068 1.1.1.2 christos _bfd_error_handler
5069 1.1.1.2 christos /* xgettext:c-format */
5070 1.1.1.2 christos (_("%B: addend -0x%x in relocation %s against "
5071 1.1.1.7 christos "symbol `%s' at 0x%lx in section `%A' is "
5072 1.1.1.7 christos "out of range"),
5073 1.1.1.2 christos input_bfd, addend, howto->name, name,
5074 1.1.1.7 christos (unsigned long) rel->r_offset, input_section);
5075 1.1.1.7 christos else
5076 1.1.1.2 christos _bfd_error_handler
5077 1.1.1.2 christos /* xgettext:c-format */
5078 1.1.1.2 christos (_("%B: addend 0x%x in relocation %s against "
5079 1.1.1.7 christos "symbol `%s' at 0x%lx in section `%A' is "
5080 1.1.1.7 christos "out of range"),
5081 1.1.1.2 christos input_bfd, addend, howto->name, name,
5082 1.1.1.2 christos (unsigned long) rel->r_offset, input_section);
5083 1.1.1.2 christos bfd_set_error (bfd_error_bad_value);
5084 1.1.1.2 christos return FALSE;
5085 1.1 christos }
5086 1.1 christos }
5087 1.1 christos else
5088 1.1 christos {
5089 1.1 christos long sindx;
5090 1.1 christos
5091 1.1 christos if (bfd_is_abs_section (sec))
5092 1.1 christos sindx = 0;
5093 1.1 christos else if (sec == NULL || sec->owner == NULL)
5094 1.1 christos {
5095 1.1 christos bfd_set_error (bfd_error_bad_value);
5096 1.1 christos return FALSE;
5097 1.1 christos }
5098 1.1 christos else
5099 1.1 christos {
5100 1.1 christos asection *osec;
5101 1.1 christos
5102 1.1 christos /* We are turning this relocation into one
5103 1.1 christos against a section symbol. It would be
5104 1.1 christos proper to subtract the symbol's value,
5105 1.1 christos osec->vma, from the emitted reloc addend,
5106 1.1 christos but ld.so expects buggy relocs. */
5107 1.1 christos osec = sec->output_section;
5108 1.1 christos sindx = elf_section_data (osec)->dynindx;
5109 1.1 christos if (sindx == 0)
5110 1.1 christos {
5111 1.1 christos asection *oi = htab->elf.text_index_section;
5112 1.1 christos sindx = elf_section_data (oi)->dynindx;
5113 1.1 christos }
5114 1.1 christos BFD_ASSERT (sindx != 0);
5115 1.1 christos }
5116 1.1 christos
5117 1.1 christos outrel.r_info = htab->r_info (sindx, r_type);
5118 1.1 christos outrel.r_addend = relocation + rel->r_addend;
5119 1.1 christos }
5120 1.1 christos }
5121 1.1 christos
5122 1.1.1.2 christos sreloc = elf_section_data (input_section)->sreloc;
5123 1.1.1.2 christos
5124 1.1.1.2 christos if (sreloc == NULL || sreloc->contents == NULL)
5125 1.1.1.2 christos {
5126 1.1.1.2 christos r = bfd_reloc_notsupported;
5127 1.1 christos goto check_relocation_error;
5128 1.1 christos }
5129 1.1 christos
5130 1.1 christos elf_append_rela (output_bfd, sreloc, &outrel);
5131 1.1 christos
5132 1.1 christos /* If this reloc is against an external symbol, we do
5133 1.1 christos not want to fiddle with the addend. Otherwise, we
5134 1.1 christos need to include the symbol value so that it becomes
5135 1.1 christos an addend for the dynamic reloc. */
5136 1.1 christos if (! relocate)
5137 1.1 christos continue;
5138 1.1 christos }
5139 1.1 christos
5140 1.1 christos break;
5141 1.1 christos
5142 1.1 christos case R_X86_64_TLSGD:
5143 1.1 christos case R_X86_64_GOTPC32_TLSDESC:
5144 1.1 christos case R_X86_64_TLSDESC_CALL:
5145 1.1 christos case R_X86_64_GOTTPOFF:
5146 1.1 christos tls_type = GOT_UNKNOWN;
5147 1.1 christos if (h == NULL && local_got_offsets)
5148 1.1 christos tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
5149 1.1 christos else if (h != NULL)
5150 1.1 christos tls_type = elf_x86_64_hash_entry (h)->tls_type;
5151 1.1 christos
5152 1.1 christos if (! elf_x86_64_tls_transition (info, input_bfd,
5153 1.1 christos input_section, contents,
5154 1.1.1.6 christos symtab_hdr, sym_hashes,
5155 1.1 christos &r_type, tls_type, rel,
5156 1.1 christos relend, h, r_symndx, TRUE))
5157 1.1 christos return FALSE;
5158 1.1 christos
5159 1.1 christos if (r_type == R_X86_64_TPOFF32)
5160 1.1 christos {
5161 1.1 christos bfd_vma roff = rel->r_offset;
5162 1.1 christos
5163 1.1 christos BFD_ASSERT (! unresolved_reloc);
5164 1.1 christos
5165 1.1 christos if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5166 1.1.1.6 christos {
5167 1.1.1.6 christos /* GD->LE transition. For 64bit, change
5168 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5169 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
5170 1.1.1.6 christos or
5171 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5172 1.1.1.6 christos .byte 0x66; rex64
5173 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
5174 1.1 christos which may be converted to
5175 1.1.1.6 christos addr32 call __tls_get_addr
5176 1.1.1.6 christos into:
5177 1.1 christos movq %fs:0, %rax
5178 1.1.1.6 christos leaq foo@tpoff(%rax), %rax
5179 1.1.1.6 christos For 32bit, change
5180 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5181 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
5182 1.1.1.6 christos or
5183 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5184 1.1.1.6 christos .byte 0x66; rex64
5185 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
5186 1.1 christos which may be converted to
5187 1.1.1.6 christos addr32 call __tls_get_addr
5188 1.1.1.6 christos into:
5189 1.1.1.3 christos movl %fs:0, %eax
5190 1.1.1.6 christos leaq foo@tpoff(%rax), %rax
5191 1.1.1.6 christos For largepic, change:
5192 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5193 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
5194 1.1.1.3 christos addq %r15, %rax
5195 1.1.1.6 christos call *%rax
5196 1.1.1.6 christos into:
5197 1.1.1.6 christos movq %fs:0, %rax
5198 1.1.1.3 christos leaq foo@tpoff(%rax), %rax
5199 1.1.1.6 christos nopw 0x0(%rax,%rax,1) */
5200 1.1.1.3 christos int largepic = 0;
5201 1.1.1.6 christos if (ABI_64_P (output_bfd))
5202 1.1.1.6 christos {
5203 1.1.1.6 christos if (contents[roff + 5] == 0xb8)
5204 1.1.1.6 christos {
5205 1.1.1.6 christos memcpy (contents + roff - 3,
5206 1.1.1.6 christos "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
5207 1.1.1.6 christos "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5208 1.1.1.6 christos largepic = 1;
5209 1.1.1.6 christos }
5210 1.1.1.6 christos else
5211 1.1.1.6 christos memcpy (contents + roff - 4,
5212 1.1.1.3 christos "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5213 1.1 christos 16);
5214 1.1 christos }
5215 1.1 christos else
5216 1.1 christos memcpy (contents + roff - 3,
5217 1.1 christos "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5218 1.1 christos 15);
5219 1.1.1.3 christos bfd_put_32 (output_bfd,
5220 1.1.1.6 christos elf_x86_64_tpoff (info, relocation),
5221 1.1.1.6 christos contents + roff + 8 + largepic);
5222 1.1 christos /* Skip R_X86_64_PC32, R_X86_64_PLT32,
5223 1.1.1.6 christos R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
5224 1.1 christos rel++;
5225 1.1 christos wrel++;
5226 1.1 christos continue;
5227 1.1 christos }
5228 1.1 christos else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5229 1.1 christos {
5230 1.1 christos /* GDesc -> LE transition.
5231 1.1 christos It's originally something like:
5232 1.1 christos leaq x@tlsdesc(%rip), %rax
5233 1.1 christos
5234 1.1 christos Change it to:
5235 1.1 christos movl $x@tpoff, %rax. */
5236 1.1 christos
5237 1.1 christos unsigned int val, type;
5238 1.1 christos
5239 1.1 christos type = bfd_get_8 (input_bfd, contents + roff - 3);
5240 1.1 christos val = bfd_get_8 (input_bfd, contents + roff - 1);
5241 1.1 christos bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
5242 1.1 christos contents + roff - 3);
5243 1.1 christos bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
5244 1.1 christos bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
5245 1.1 christos contents + roff - 1);
5246 1.1 christos bfd_put_32 (output_bfd,
5247 1.1 christos elf_x86_64_tpoff (info, relocation),
5248 1.1 christos contents + roff);
5249 1.1 christos continue;
5250 1.1 christos }
5251 1.1 christos else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5252 1.1 christos {
5253 1.1 christos /* GDesc -> LE transition.
5254 1.1 christos It's originally:
5255 1.1 christos call *(%rax)
5256 1.1 christos Turn it into:
5257 1.1 christos xchg %ax,%ax. */
5258 1.1 christos bfd_put_8 (output_bfd, 0x66, contents + roff);
5259 1.1 christos bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5260 1.1 christos continue;
5261 1.1 christos }
5262 1.1 christos else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
5263 1.1.1.4 christos {
5264 1.1 christos /* IE->LE transition:
5265 1.1 christos For 64bit, originally it can be one of:
5266 1.1 christos movq foo@gottpoff(%rip), %reg
5267 1.1 christos addq foo@gottpoff(%rip), %reg
5268 1.1 christos We change it into:
5269 1.1.1.4 christos movq $foo, %reg
5270 1.1.1.4 christos leaq foo(%reg), %reg
5271 1.1.1.4 christos addq $foo, %reg.
5272 1.1.1.4 christos For 32bit, originally it can be one of:
5273 1.1.1.4 christos movq foo@gottpoff(%rip), %reg
5274 1.1.1.4 christos addl foo@gottpoff(%rip), %reg
5275 1.1.1.4 christos We change it into:
5276 1.1.1.4 christos movq $foo, %reg
5277 1.1 christos leal foo(%reg), %reg
5278 1.1 christos addl $foo, %reg. */
5279 1.1 christos
5280 1.1.1.4 christos unsigned int val, type, reg;
5281 1.1.1.4 christos
5282 1.1.1.4 christos if (roff >= 3)
5283 1.1.1.4 christos val = bfd_get_8 (input_bfd, contents + roff - 3);
5284 1.1 christos else
5285 1.1 christos val = 0;
5286 1.1 christos type = bfd_get_8 (input_bfd, contents + roff - 2);
5287 1.1 christos reg = bfd_get_8 (input_bfd, contents + roff - 1);
5288 1.1 christos reg >>= 3;
5289 1.1 christos if (type == 0x8b)
5290 1.1 christos {
5291 1.1 christos /* movq */
5292 1.1 christos if (val == 0x4c)
5293 1.1 christos bfd_put_8 (output_bfd, 0x49,
5294 1.1 christos contents + roff - 3);
5295 1.1 christos else if (!ABI_64_P (output_bfd) && val == 0x44)
5296 1.1 christos bfd_put_8 (output_bfd, 0x41,
5297 1.1 christos contents + roff - 3);
5298 1.1 christos bfd_put_8 (output_bfd, 0xc7,
5299 1.1 christos contents + roff - 2);
5300 1.1 christos bfd_put_8 (output_bfd, 0xc0 | reg,
5301 1.1 christos contents + roff - 1);
5302 1.1 christos }
5303 1.1.1.4 christos else if (reg == 4)
5304 1.1.1.4 christos {
5305 1.1 christos /* addq/addl -> addq/addl - addressing with %rsp/%r12
5306 1.1 christos is special */
5307 1.1 christos if (val == 0x4c)
5308 1.1 christos bfd_put_8 (output_bfd, 0x49,
5309 1.1 christos contents + roff - 3);
5310 1.1 christos else if (!ABI_64_P (output_bfd) && val == 0x44)
5311 1.1 christos bfd_put_8 (output_bfd, 0x41,
5312 1.1 christos contents + roff - 3);
5313 1.1 christos bfd_put_8 (output_bfd, 0x81,
5314 1.1 christos contents + roff - 2);
5315 1.1 christos bfd_put_8 (output_bfd, 0xc0 | reg,
5316 1.1 christos contents + roff - 1);
5317 1.1 christos }
5318 1.1.1.4 christos else
5319 1.1 christos {
5320 1.1 christos /* addq/addl -> leaq/leal */
5321 1.1 christos if (val == 0x4c)
5322 1.1 christos bfd_put_8 (output_bfd, 0x4d,
5323 1.1 christos contents + roff - 3);
5324 1.1 christos else if (!ABI_64_P (output_bfd) && val == 0x44)
5325 1.1 christos bfd_put_8 (output_bfd, 0x45,
5326 1.1 christos contents + roff - 3);
5327 1.1 christos bfd_put_8 (output_bfd, 0x8d,
5328 1.1 christos contents + roff - 2);
5329 1.1 christos bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
5330 1.1 christos contents + roff - 1);
5331 1.1 christos }
5332 1.1 christos bfd_put_32 (output_bfd,
5333 1.1 christos elf_x86_64_tpoff (info, relocation),
5334 1.1 christos contents + roff);
5335 1.1 christos continue;
5336 1.1 christos }
5337 1.1 christos else
5338 1.1 christos BFD_ASSERT (FALSE);
5339 1.1 christos }
5340 1.1 christos
5341 1.1 christos if (htab->elf.sgot == NULL)
5342 1.1 christos abort ();
5343 1.1 christos
5344 1.1 christos if (h != NULL)
5345 1.1 christos {
5346 1.1 christos off = h->got.offset;
5347 1.1 christos offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
5348 1.1 christos }
5349 1.1 christos else
5350 1.1 christos {
5351 1.1 christos if (local_got_offsets == NULL)
5352 1.1 christos abort ();
5353 1.1 christos
5354 1.1 christos off = local_got_offsets[r_symndx];
5355 1.1 christos offplt = local_tlsdesc_gotents[r_symndx];
5356 1.1 christos }
5357 1.1 christos
5358 1.1 christos if ((off & 1) != 0)
5359 1.1 christos off &= ~1;
5360 1.1 christos else
5361 1.1 christos {
5362 1.1 christos Elf_Internal_Rela outrel;
5363 1.1 christos int dr_type, indx;
5364 1.1 christos asection *sreloc;
5365 1.1 christos
5366 1.1 christos if (htab->elf.srelgot == NULL)
5367 1.1 christos abort ();
5368 1.1 christos
5369 1.1 christos indx = h && h->dynindx != -1 ? h->dynindx : 0;
5370 1.1 christos
5371 1.1 christos if (GOT_TLS_GDESC_P (tls_type))
5372 1.1 christos {
5373 1.1 christos outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
5374 1.1 christos BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
5375 1.1 christos + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
5376 1.1 christos outrel.r_offset = (htab->elf.sgotplt->output_section->vma
5377 1.1 christos + htab->elf.sgotplt->output_offset
5378 1.1 christos + offplt
5379 1.1 christos + htab->sgotplt_jump_table_size);
5380 1.1 christos sreloc = htab->elf.srelplt;
5381 1.1 christos if (indx == 0)
5382 1.1 christos outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
5383 1.1 christos else
5384 1.1 christos outrel.r_addend = 0;
5385 1.1 christos elf_append_rela (output_bfd, sreloc, &outrel);
5386 1.1 christos }
5387 1.1 christos
5388 1.1 christos sreloc = htab->elf.srelgot;
5389 1.1 christos
5390 1.1 christos outrel.r_offset = (htab->elf.sgot->output_section->vma
5391 1.1 christos + htab->elf.sgot->output_offset + off);
5392 1.1 christos
5393 1.1 christos if (GOT_TLS_GD_P (tls_type))
5394 1.1 christos dr_type = R_X86_64_DTPMOD64;
5395 1.1 christos else if (GOT_TLS_GDESC_P (tls_type))
5396 1.1 christos goto dr_done;
5397 1.1 christos else
5398 1.1 christos dr_type = R_X86_64_TPOFF64;
5399 1.1 christos
5400 1.1 christos bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
5401 1.1 christos outrel.r_addend = 0;
5402 1.1 christos if ((dr_type == R_X86_64_TPOFF64
5403 1.1 christos || dr_type == R_X86_64_TLSDESC) && indx == 0)
5404 1.1 christos outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
5405 1.1 christos outrel.r_info = htab->r_info (indx, dr_type);
5406 1.1 christos
5407 1.1 christos elf_append_rela (output_bfd, sreloc, &outrel);
5408 1.1 christos
5409 1.1 christos if (GOT_TLS_GD_P (tls_type))
5410 1.1 christos {
5411 1.1 christos if (indx == 0)
5412 1.1 christos {
5413 1.1 christos BFD_ASSERT (! unresolved_reloc);
5414 1.1 christos bfd_put_64 (output_bfd,
5415 1.1 christos relocation - elf_x86_64_dtpoff_base (info),
5416 1.1 christos htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5417 1.1 christos }
5418 1.1 christos else
5419 1.1 christos {
5420 1.1 christos bfd_put_64 (output_bfd, 0,
5421 1.1 christos htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5422 1.1 christos outrel.r_info = htab->r_info (indx,
5423 1.1 christos R_X86_64_DTPOFF64);
5424 1.1 christos outrel.r_offset += GOT_ENTRY_SIZE;
5425 1.1 christos elf_append_rela (output_bfd, sreloc,
5426 1.1 christos &outrel);
5427 1.1 christos }
5428 1.1 christos }
5429 1.1 christos
5430 1.1 christos dr_done:
5431 1.1 christos if (h != NULL)
5432 1.1 christos h->got.offset |= 1;
5433 1.1 christos else
5434 1.1 christos local_got_offsets[r_symndx] |= 1;
5435 1.1 christos }
5436 1.1 christos
5437 1.1 christos if (off >= (bfd_vma) -2
5438 1.1 christos && ! GOT_TLS_GDESC_P (tls_type))
5439 1.1 christos abort ();
5440 1.1 christos if (r_type == ELF32_R_TYPE (rel->r_info))
5441 1.1 christos {
5442 1.1 christos if (r_type == R_X86_64_GOTPC32_TLSDESC
5443 1.1 christos || r_type == R_X86_64_TLSDESC_CALL)
5444 1.1 christos relocation = htab->elf.sgotplt->output_section->vma
5445 1.1 christos + htab->elf.sgotplt->output_offset
5446 1.1 christos + offplt + htab->sgotplt_jump_table_size;
5447 1.1 christos else
5448 1.1 christos relocation = htab->elf.sgot->output_section->vma
5449 1.1 christos + htab->elf.sgot->output_offset + off;
5450 1.1 christos unresolved_reloc = FALSE;
5451 1.1 christos }
5452 1.1 christos else
5453 1.1 christos {
5454 1.1 christos bfd_vma roff = rel->r_offset;
5455 1.1 christos
5456 1.1 christos if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5457 1.1.1.6 christos {
5458 1.1.1.6 christos /* GD->IE transition. For 64bit, change
5459 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5460 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
5461 1.1.1.6 christos or
5462 1.1.1.6 christos .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5463 1.1.1.6 christos .byte 0x66; rex64
5464 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip
5465 1.1 christos which may be converted to
5466 1.1.1.6 christos addr32 call __tls_get_addr
5467 1.1.1.6 christos into:
5468 1.1 christos movq %fs:0, %rax
5469 1.1.1.6 christos addq foo@gottpoff(%rip), %rax
5470 1.1.1.6 christos For 32bit, change
5471 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5472 1.1.1.6 christos .word 0x6666; rex64; call __tls_get_addr@PLT
5473 1.1.1.6 christos or
5474 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5475 1.1.1.6 christos .byte 0x66; rex64;
5476 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
5477 1.1 christos which may be converted to
5478 1.1.1.6 christos addr32 call __tls_get_addr
5479 1.1.1.6 christos into:
5480 1.1.1.3 christos movl %fs:0, %eax
5481 1.1.1.6 christos addq foo@gottpoff(%rip), %rax
5482 1.1.1.6 christos For largepic, change:
5483 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5484 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
5485 1.1.1.3 christos addq %r15, %rax
5486 1.1.1.6 christos call *%rax
5487 1.1.1.6 christos into:
5488 1.1.1.6 christos movq %fs:0, %rax
5489 1.1.1.3 christos addq foo@gottpoff(%rax), %rax
5490 1.1.1.6 christos nopw 0x0(%rax,%rax,1) */
5491 1.1.1.3 christos int largepic = 0;
5492 1.1.1.6 christos if (ABI_64_P (output_bfd))
5493 1.1.1.6 christos {
5494 1.1.1.6 christos if (contents[roff + 5] == 0xb8)
5495 1.1.1.6 christos {
5496 1.1.1.6 christos memcpy (contents + roff - 3,
5497 1.1.1.6 christos "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5498 1.1.1.6 christos "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5499 1.1.1.6 christos largepic = 1;
5500 1.1.1.6 christos }
5501 1.1.1.6 christos else
5502 1.1.1.6 christos memcpy (contents + roff - 4,
5503 1.1.1.3 christos "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5504 1.1 christos 16);
5505 1.1 christos }
5506 1.1 christos else
5507 1.1 christos memcpy (contents + roff - 3,
5508 1.1 christos "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5509 1.1 christos 15);
5510 1.1 christos
5511 1.1 christos relocation = (htab->elf.sgot->output_section->vma
5512 1.1.1.3 christos + htab->elf.sgot->output_offset + off
5513 1.1 christos - roff
5514 1.1 christos - largepic
5515 1.1 christos - input_section->output_section->vma
5516 1.1 christos - input_section->output_offset
5517 1.1.1.3 christos - 12);
5518 1.1.1.3 christos bfd_put_32 (output_bfd, relocation,
5519 1.1 christos contents + roff + 8 + largepic);
5520 1.1.1.6 christos /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
5521 1.1 christos rel++;
5522 1.1 christos wrel++;
5523 1.1 christos continue;
5524 1.1 christos }
5525 1.1 christos else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5526 1.1 christos {
5527 1.1 christos /* GDesc -> IE transition.
5528 1.1 christos It's originally something like:
5529 1.1 christos leaq x@tlsdesc(%rip), %rax
5530 1.1 christos
5531 1.1 christos Change it to:
5532 1.1 christos movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
5533 1.1 christos
5534 1.1 christos /* Now modify the instruction as appropriate. To
5535 1.1 christos turn a leaq into a movq in the form we use it, it
5536 1.1 christos suffices to change the second byte from 0x8d to
5537 1.1 christos 0x8b. */
5538 1.1 christos bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5539 1.1 christos
5540 1.1 christos bfd_put_32 (output_bfd,
5541 1.1 christos htab->elf.sgot->output_section->vma
5542 1.1 christos + htab->elf.sgot->output_offset + off
5543 1.1 christos - rel->r_offset
5544 1.1 christos - input_section->output_section->vma
5545 1.1 christos - input_section->output_offset
5546 1.1 christos - 4,
5547 1.1 christos contents + roff);
5548 1.1 christos continue;
5549 1.1 christos }
5550 1.1 christos else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5551 1.1 christos {
5552 1.1 christos /* GDesc -> IE transition.
5553 1.1 christos It's originally:
5554 1.1 christos call *(%rax)
5555 1.1 christos
5556 1.1 christos Change it to:
5557 1.1 christos xchg %ax, %ax. */
5558 1.1 christos
5559 1.1 christos bfd_put_8 (output_bfd, 0x66, contents + roff);
5560 1.1 christos bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5561 1.1 christos continue;
5562 1.1 christos }
5563 1.1 christos else
5564 1.1 christos BFD_ASSERT (FALSE);
5565 1.1 christos }
5566 1.1 christos break;
5567 1.1 christos
5568 1.1 christos case R_X86_64_TLSLD:
5569 1.1 christos if (! elf_x86_64_tls_transition (info, input_bfd,
5570 1.1.1.6 christos input_section, contents,
5571 1.1.1.6 christos symtab_hdr, sym_hashes,
5572 1.1 christos &r_type, GOT_UNKNOWN, rel,
5573 1.1 christos relend, h, r_symndx, TRUE))
5574 1.1 christos return FALSE;
5575 1.1 christos
5576 1.1 christos if (r_type != R_X86_64_TLSLD)
5577 1.1.1.6 christos {
5578 1.1.1.6 christos /* LD->LE transition:
5579 1.1 christos leaq foo@tlsld(%rip), %rdi
5580 1.1.1.6 christos call __tls_get_addr@PLT
5581 1.1 christos For 64bit, we change it into:
5582 1.1.1.6 christos .word 0x6666; .byte 0x66; movq %fs:0, %rax
5583 1.1.1.6 christos For 32bit, we change it into:
5584 1.1.1.6 christos nopl 0x0(%rax); movl %fs:0, %eax
5585 1.1.1.6 christos Or
5586 1.1.1.6 christos leaq foo@tlsld(%rip), %rdi;
5587 1.1.1.6 christos call *__tls_get_addr@GOTPCREL(%rip)
5588 1.1.1.6 christos which may be converted to
5589 1.1.1.6 christos addr32 call __tls_get_addr
5590 1.1.1.6 christos For 64bit, we change it into:
5591 1.1.1.6 christos .word 0x6666; .word 0x6666; movq %fs:0, %rax
5592 1.1.1.3 christos For 32bit, we change it into:
5593 1.1.1.6 christos nopw 0x0(%rax); movl %fs:0, %eax
5594 1.1.1.6 christos For largepic, change:
5595 1.1.1.6 christos leaq foo@tlsgd(%rip), %rdi
5596 1.1.1.6 christos movabsq $__tls_get_addr@pltoff, %rax
5597 1.1.1.6 christos addq %rbx, %rax
5598 1.1.1.6 christos call *%rax
5599 1.1.1.6 christos into
5600 1.1 christos data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5601 1.1 christos movq %fs:0, %eax */
5602 1.1.1.6 christos
5603 1.1.1.6 christos BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5604 1.1.1.6 christos if (ABI_64_P (output_bfd))
5605 1.1.1.6 christos {
5606 1.1.1.6 christos if (contents[rel->r_offset + 5] == 0xb8)
5607 1.1.1.6 christos memcpy (contents + rel->r_offset - 3,
5608 1.1.1.6 christos "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5609 1.1.1.6 christos "\x64\x48\x8b\x04\x25\0\0\0", 22);
5610 1.1.1.6 christos else if (contents[rel->r_offset + 4] == 0xff
5611 1.1.1.6 christos || contents[rel->r_offset + 4] == 0x67)
5612 1.1.1.6 christos memcpy (contents + rel->r_offset - 3,
5613 1.1.1.6 christos "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5614 1.1.1.6 christos 13);
5615 1.1.1.6 christos else
5616 1.1.1.6 christos memcpy (contents + rel->r_offset - 3,
5617 1.1 christos "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5618 1.1.1.6 christos }
5619 1.1.1.6 christos else
5620 1.1.1.6 christos {
5621 1.1.1.6 christos if (contents[rel->r_offset + 4] == 0xff)
5622 1.1.1.6 christos memcpy (contents + rel->r_offset - 3,
5623 1.1.1.6 christos "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5624 1.1.1.6 christos 13);
5625 1.1.1.6 christos else
5626 1.1.1.6 christos memcpy (contents + rel->r_offset - 3,
5627 1.1.1.6 christos "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5628 1.1.1.6 christos }
5629 1.1 christos /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5630 1.1.1.6 christos and R_X86_64_PLTOFF64. */
5631 1.1 christos rel++;
5632 1.1 christos wrel++;
5633 1.1 christos continue;
5634 1.1 christos }
5635 1.1 christos
5636 1.1 christos if (htab->elf.sgot == NULL)
5637 1.1 christos abort ();
5638 1.1 christos
5639 1.1 christos off = htab->tls_ld_got.offset;
5640 1.1 christos if (off & 1)
5641 1.1 christos off &= ~1;
5642 1.1 christos else
5643 1.1 christos {
5644 1.1 christos Elf_Internal_Rela outrel;
5645 1.1 christos
5646 1.1 christos if (htab->elf.srelgot == NULL)
5647 1.1 christos abort ();
5648 1.1 christos
5649 1.1 christos outrel.r_offset = (htab->elf.sgot->output_section->vma
5650 1.1 christos + htab->elf.sgot->output_offset + off);
5651 1.1 christos
5652 1.1 christos bfd_put_64 (output_bfd, 0,
5653 1.1 christos htab->elf.sgot->contents + off);
5654 1.1 christos bfd_put_64 (output_bfd, 0,
5655 1.1 christos htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5656 1.1 christos outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5657 1.1 christos outrel.r_addend = 0;
5658 1.1 christos elf_append_rela (output_bfd, htab->elf.srelgot,
5659 1.1 christos &outrel);
5660 1.1 christos htab->tls_ld_got.offset |= 1;
5661 1.1 christos }
5662 1.1 christos relocation = htab->elf.sgot->output_section->vma
5663 1.1 christos + htab->elf.sgot->output_offset + off;
5664 1.1 christos unresolved_reloc = FALSE;
5665 1.1 christos break;
5666 1.1.1.6 christos
5667 1.1.1.6 christos case R_X86_64_DTPOFF32:
5668 1.1 christos if (!bfd_link_executable (info)
5669 1.1 christos || (input_section->flags & SEC_CODE) == 0)
5670 1.1 christos relocation -= elf_x86_64_dtpoff_base (info);
5671 1.1 christos else
5672 1.1 christos relocation = elf_x86_64_tpoff (info, relocation);
5673 1.1 christos break;
5674 1.1.1.2 christos
5675 1.1.1.6 christos case R_X86_64_TPOFF32:
5676 1.1 christos case R_X86_64_TPOFF64:
5677 1.1 christos BFD_ASSERT (bfd_link_executable (info));
5678 1.1 christos relocation = elf_x86_64_tpoff (info, relocation);
5679 1.1.1.3 christos break;
5680 1.1.1.3 christos
5681 1.1.1.3 christos case R_X86_64_DTPOFF64:
5682 1.1.1.3 christos BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5683 1.1.1.3 christos relocation -= elf_x86_64_dtpoff_base (info);
5684 1.1 christos break;
5685 1.1 christos
5686 1.1 christos default:
5687 1.1 christos break;
5688 1.1 christos }
5689 1.1 christos
5690 1.1 christos /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5691 1.1 christos because such sections are not SEC_ALLOC and thus ld.so will
5692 1.1 christos not process them. */
5693 1.1.1.2 christos if (unresolved_reloc
5694 1.1.1.2 christos && !((input_section->flags & SEC_DEBUGGING) != 0
5695 1.1.1.2 christos && h->def_dynamic)
5696 1.1.1.2 christos && _bfd_elf_section_offset (output_bfd, info, input_section,
5697 1.1.1.7 christos rel->r_offset) != (bfd_vma) -1)
5698 1.1.1.7 christos {
5699 1.1.1.2 christos _bfd_error_handler
5700 1.1.1.2 christos /* xgettext:c-format */
5701 1.1.1.2 christos (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5702 1.1.1.2 christos input_bfd,
5703 1.1.1.2 christos input_section,
5704 1.1.1.2 christos (long) rel->r_offset,
5705 1.1.1.2 christos howto->name,
5706 1.1.1.2 christos h->root.root.string);
5707 1.1 christos return FALSE;
5708 1.1 christos }
5709 1.1 christos
5710 1.1 christos do_relocation:
5711 1.1 christos r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5712 1.1 christos contents, rel->r_offset,
5713 1.1.1.2 christos relocation, rel->r_addend);
5714 1.1 christos
5715 1.1 christos check_relocation_error:
5716 1.1 christos if (r != bfd_reloc_ok)
5717 1.1 christos {
5718 1.1 christos const char *name;
5719 1.1 christos
5720 1.1 christos if (h != NULL)
5721 1.1 christos name = h->root.root.string;
5722 1.1 christos else
5723 1.1 christos {
5724 1.1 christos name = bfd_elf_string_from_elf_section (input_bfd,
5725 1.1 christos symtab_hdr->sh_link,
5726 1.1 christos sym->st_name);
5727 1.1 christos if (name == NULL)
5728 1.1 christos return FALSE;
5729 1.1 christos if (*name == '\0')
5730 1.1 christos name = bfd_section_name (input_bfd, sec);
5731 1.1 christos }
5732 1.1.1.6 christos
5733 1.1.1.6 christos if (r == bfd_reloc_overflow)
5734 1.1.1.6 christos (*info->callbacks->reloc_overflow)
5735 1.1 christos (info, (h ? &h->root : NULL), name, howto->name,
5736 1.1 christos (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5737 1.1.1.7 christos else
5738 1.1.1.7 christos {
5739 1.1 christos _bfd_error_handler
5740 1.1 christos /* xgettext:c-format */
5741 1.1 christos (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5742 1.1 christos input_bfd, input_section,
5743 1.1 christos (long) rel->r_offset, name, (int) r);
5744 1.1 christos return FALSE;
5745 1.1.1.6 christos }
5746 1.1.1.6 christos }
5747 1.1.1.6 christos
5748 1.1.1.6 christos if (wrel != rel)
5749 1.1.1.6 christos *wrel = *rel;
5750 1.1.1.6 christos }
5751 1.1.1.6 christos
5752 1.1.1.6 christos if (wrel != rel)
5753 1.1.1.6 christos {
5754 1.1.1.6 christos Elf_Internal_Shdr *rel_hdr;
5755 1.1.1.6 christos size_t deleted = rel - wrel;
5756 1.1.1.6 christos
5757 1.1.1.6 christos rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5758 1.1.1.6 christos rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5759 1.1.1.6 christos if (rel_hdr->sh_size == 0)
5760 1.1.1.6 christos {
5761 1.1.1.6 christos /* It is too late to remove an empty reloc section. Leave
5762 1.1.1.6 christos one NONE reloc.
5763 1.1.1.6 christos ??? What is wrong with an empty section??? */
5764 1.1.1.6 christos rel_hdr->sh_size = rel_hdr->sh_entsize;
5765 1.1.1.6 christos deleted -= 1;
5766 1.1.1.6 christos }
5767 1.1.1.6 christos rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5768 1.1 christos rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5769 1.1 christos input_section->reloc_count -= deleted;
5770 1.1 christos }
5771 1.1 christos
5772 1.1 christos return TRUE;
5773 1.1 christos }
5774 1.1 christos
5775 1.1 christos /* Finish up dynamic symbol handling. We set the contents of various
5776 1.1 christos dynamic sections here. */
5777 1.1 christos
5778 1.1 christos static bfd_boolean
5779 1.1 christos elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5780 1.1.1.6 christos struct bfd_link_info *info,
5781 1.1 christos struct elf_link_hash_entry *h,
5782 1.1 christos Elf_Internal_Sym *sym)
5783 1.1.1.4 christos {
5784 1.1.1.4 christos struct elf_x86_64_link_hash_table *htab;
5785 1.1.1.4 christos const struct elf_x86_64_backend_data *abed;
5786 1.1.1.6 christos bfd_boolean use_plt_bnd;
5787 1.1 christos struct elf_x86_64_link_hash_entry *eh;
5788 1.1 christos bfd_boolean local_undefweak;
5789 1.1 christos
5790 1.1 christos htab = elf_x86_64_hash_table (info);
5791 1.1 christos if (htab == NULL)
5792 1.1.1.4 christos return FALSE;
5793 1.1.1.4 christos
5794 1.1.1.4 christos /* Use MPX backend data in case of BND relocation. Use .plt_bnd
5795 1.1.1.4 christos section only if there is .plt section. */
5796 1.1.1.4 christos use_plt_bnd = htab->elf.splt != NULL && htab->plt_bnd != NULL;
5797 1.1.1.4 christos abed = (use_plt_bnd
5798 1.1.1.4 christos ? &elf_x86_64_bnd_arch_bed
5799 1.1.1.4 christos : get_elf_x86_64_backend_data (output_bfd));
5800 1.1.1.4 christos
5801 1.1.1.6 christos eh = (struct elf_x86_64_link_hash_entry *) h;
5802 1.1.1.6 christos
5803 1.1.1.6 christos /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5804 1.1.1.6 christos resolved undefined weak symbols in executable so that their
5805 1.1.1.6 christos references have value 0 at run-time. */
5806 1.1.1.6 christos local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5807 1.1.1.6 christos eh->has_got_reloc,
5808 1.1 christos eh);
5809 1.1 christos
5810 1.1 christos if (h->plt.offset != (bfd_vma) -1)
5811 1.1.1.4 christos {
5812 1.1.1.4 christos bfd_vma plt_index;
5813 1.1 christos bfd_vma got_offset, plt_offset, plt_plt_offset, plt_got_offset;
5814 1.1 christos bfd_vma plt_plt_insn_end, plt_got_insn_size;
5815 1.1.1.4 christos Elf_Internal_Rela rela;
5816 1.1 christos bfd_byte *loc;
5817 1.1.1.4 christos asection *plt, *gotplt, *relplt, *resolved_plt;
5818 1.1 christos const struct elf_backend_data *bed;
5819 1.1 christos bfd_vma plt_got_pcrel_offset;
5820 1.1 christos
5821 1.1 christos /* When building a static executable, use .iplt, .igot.plt and
5822 1.1 christos .rela.iplt sections for STT_GNU_IFUNC symbols. */
5823 1.1 christos if (htab->elf.splt != NULL)
5824 1.1 christos {
5825 1.1 christos plt = htab->elf.splt;
5826 1.1 christos gotplt = htab->elf.sgotplt;
5827 1.1 christos relplt = htab->elf.srelplt;
5828 1.1 christos }
5829 1.1 christos else
5830 1.1 christos {
5831 1.1 christos plt = htab->elf.iplt;
5832 1.1 christos gotplt = htab->elf.igotplt;
5833 1.1 christos relplt = htab->elf.irelplt;
5834 1.1 christos }
5835 1.1 christos
5836 1.1 christos /* This symbol has an entry in the procedure linkage table. Set
5837 1.1.1.6 christos it up. */
5838 1.1.1.6 christos if ((h->dynindx == -1
5839 1.1 christos && !local_undefweak
5840 1.1 christos && !((h->forced_local || bfd_link_executable (info))
5841 1.1 christos && h->def_regular
5842 1.1 christos && h->type == STT_GNU_IFUNC))
5843 1.1 christos || plt == NULL
5844 1.1 christos || gotplt == NULL
5845 1.1 christos || relplt == NULL)
5846 1.1 christos abort ();
5847 1.1 christos
5848 1.1 christos /* Get the index in the procedure linkage table which
5849 1.1 christos corresponds to this symbol. This is the index of this symbol
5850 1.1 christos in all the symbols for which we are making plt entries. The
5851 1.1 christos first entry in the procedure linkage table is reserved.
5852 1.1 christos
5853 1.1 christos Get the offset into the .got table of the entry that
5854 1.1 christos corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
5855 1.1 christos bytes. The first three are reserved for the dynamic linker.
5856 1.1 christos
5857 1.1 christos For static executables, we don't reserve anything. */
5858 1.1 christos
5859 1.1.1.2 christos if (plt == htab->elf.splt)
5860 1.1.1.2 christos {
5861 1.1 christos got_offset = h->plt.offset / abed->plt_entry_size - 1;
5862 1.1 christos got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5863 1.1 christos }
5864 1.1.1.2 christos else
5865 1.1.1.2 christos {
5866 1.1 christos got_offset = h->plt.offset / abed->plt_entry_size;
5867 1.1 christos got_offset = got_offset * GOT_ENTRY_SIZE;
5868 1.1.1.4 christos }
5869 1.1.1.4 christos
5870 1.1.1.4 christos plt_plt_insn_end = abed->plt_plt_insn_end;
5871 1.1.1.4 christos plt_plt_offset = abed->plt_plt_offset;
5872 1.1.1.4 christos plt_got_insn_size = abed->plt_got_insn_size;
5873 1.1.1.4 christos plt_got_offset = abed->plt_got_offset;
5874 1.1.1.4 christos if (use_plt_bnd)
5875 1.1.1.4 christos {
5876 1.1.1.4 christos /* Use the second PLT with BND relocations. */
5877 1.1.1.4 christos const bfd_byte *plt_entry, *plt2_entry;
5878 1.1.1.4 christos
5879 1.1.1.4 christos if (eh->has_bnd_reloc)
5880 1.1.1.4 christos {
5881 1.1.1.4 christos plt_entry = elf_x86_64_bnd_plt_entry;
5882 1.1.1.4 christos plt2_entry = elf_x86_64_bnd_plt2_entry;
5883 1.1.1.4 christos }
5884 1.1.1.4 christos else
5885 1.1.1.4 christos {
5886 1.1.1.4 christos plt_entry = elf_x86_64_legacy_plt_entry;
5887 1.1.1.4 christos plt2_entry = elf_x86_64_legacy_plt2_entry;
5888 1.1.1.4 christos
5889 1.1.1.4 christos /* Subtract 1 since there is no BND prefix. */
5890 1.1.1.4 christos plt_plt_insn_end -= 1;
5891 1.1.1.4 christos plt_plt_offset -= 1;
5892 1.1.1.4 christos plt_got_insn_size -= 1;
5893 1.1.1.4 christos plt_got_offset -= 1;
5894 1.1.1.4 christos }
5895 1.1.1.4 christos
5896 1.1.1.4 christos BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry)
5897 1.1.1.4 christos == sizeof (elf_x86_64_legacy_plt_entry));
5898 1.1.1.4 christos
5899 1.1.1.4 christos /* Fill in the entry in the procedure linkage table. */
5900 1.1.1.4 christos memcpy (plt->contents + h->plt.offset,
5901 1.1.1.4 christos plt_entry, sizeof (elf_x86_64_legacy_plt_entry));
5902 1.1.1.4 christos /* Fill in the entry in the second PLT. */
5903 1.1.1.4 christos memcpy (htab->plt_bnd->contents + eh->plt_bnd.offset,
5904 1.1.1.4 christos plt2_entry, sizeof (elf_x86_64_legacy_plt2_entry));
5905 1.1.1.4 christos
5906 1.1.1.4 christos resolved_plt = htab->plt_bnd;
5907 1.1.1.4 christos plt_offset = eh->plt_bnd.offset;
5908 1.1.1.4 christos }
5909 1.1.1.4 christos else
5910 1.1.1.4 christos {
5911 1.1.1.4 christos /* Fill in the entry in the procedure linkage table. */
5912 1.1.1.4 christos memcpy (plt->contents + h->plt.offset, abed->plt_entry,
5913 1.1.1.4 christos abed->plt_entry_size);
5914 1.1.1.4 christos
5915 1.1.1.4 christos resolved_plt = plt;
5916 1.1 christos plt_offset = h->plt.offset;
5917 1.1.1.2 christos }
5918 1.1 christos
5919 1.1.1.2 christos /* Insert the relocation positions of the plt section. */
5920 1.1.1.2 christos
5921 1.1.1.4 christos /* Put offset the PC-relative instruction referring to the GOT entry,
5922 1.1.1.4 christos subtracting the size of that instruction. */
5923 1.1.1.4 christos plt_got_pcrel_offset = (gotplt->output_section->vma
5924 1.1.1.4 christos + gotplt->output_offset
5925 1.1.1.4 christos + got_offset
5926 1.1.1.4 christos - resolved_plt->output_section->vma
5927 1.1.1.4 christos - resolved_plt->output_offset
5928 1.1.1.4 christos - plt_offset
5929 1.1.1.4 christos - plt_got_insn_size);
5930 1.1.1.4 christos
5931 1.1.1.7 christos /* Check PC-relative offset overflow in PLT entry. */
5932 1.1.1.4 christos if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5933 1.1.1.4 christos /* xgettext:c-format */
5934 1.1.1.4 christos info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5935 1.1.1.4 christos output_bfd, h->root.root.string);
5936 1.1.1.4 christos
5937 1.1 christos bfd_put_32 (output_bfd, plt_got_pcrel_offset,
5938 1.1 christos resolved_plt->contents + plt_offset + plt_got_offset);
5939 1.1.1.6 christos
5940 1.1.1.6 christos /* Fill in the entry in the global offset table, initially this
5941 1.1.1.6 christos points to the second part of the PLT entry. Leave the entry
5942 1.1.1.6 christos as zero for undefined weak symbol in PIE. No PLT relocation
5943 1.1.1.6 christos against undefined weak symbol in PIE. */
5944 1.1.1.6 christos if (!local_undefweak)
5945 1.1.1.6 christos {
5946 1.1.1.6 christos bfd_put_64 (output_bfd, (plt->output_section->vma
5947 1.1.1.6 christos + plt->output_offset
5948 1.1.1.6 christos + h->plt.offset
5949 1.1.1.6 christos + abed->plt_lazy_offset),
5950 1.1.1.6 christos gotplt->contents + got_offset);
5951 1.1.1.6 christos
5952 1.1.1.6 christos /* Fill in the entry in the .rela.plt section. */
5953 1.1.1.6 christos rela.r_offset = (gotplt->output_section->vma
5954 1.1.1.6 christos + gotplt->output_offset
5955 1.1.1.6 christos + got_offset);
5956 1.1.1.6 christos if (h->dynindx == -1
5957 1.1.1.6 christos || ((bfd_link_executable (info)
5958 1.1.1.6 christos || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5959 1.1.1.6 christos && h->def_regular
5960 1.1.1.6 christos && h->type == STT_GNU_IFUNC))
5961 1.1.1.6 christos {
5962 1.1.1.6 christos /* If an STT_GNU_IFUNC symbol is locally defined, generate
5963 1.1.1.6 christos R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5964 1.1.1.6 christos rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5965 1.1.1.6 christos rela.r_addend = (h->root.u.def.value
5966 1.1.1.6 christos + h->root.u.def.section->output_section->vma
5967 1.1.1.6 christos + h->root.u.def.section->output_offset);
5968 1.1.1.6 christos /* R_X86_64_IRELATIVE comes last. */
5969 1.1.1.6 christos plt_index = htab->next_irelative_index--;
5970 1.1.1.6 christos }
5971 1.1.1.6 christos else
5972 1.1.1.6 christos {
5973 1.1.1.6 christos rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5974 1.1.1.6 christos rela.r_addend = 0;
5975 1.1.1.6 christos plt_index = htab->next_jump_slot_index++;
5976 1.1.1.6 christos }
5977 1.1.1.6 christos
5978 1.1.1.6 christos /* Don't fill PLT entry for static executables. */
5979 1.1.1.6 christos if (plt == htab->elf.splt)
5980 1.1.1.6 christos {
5981 1.1.1.6 christos bfd_vma plt0_offset = h->plt.offset + plt_plt_insn_end;
5982 1.1.1.6 christos
5983 1.1.1.6 christos /* Put relocation index. */
5984 1.1.1.6 christos bfd_put_32 (output_bfd, plt_index,
5985 1.1.1.6 christos (plt->contents + h->plt.offset
5986 1.1.1.6 christos + abed->plt_reloc_offset));
5987 1.1.1.6 christos
5988 1.1.1.6 christos /* Put offset for jmp .PLT0 and check for overflow. We don't
5989 1.1.1.6 christos check relocation index for overflow since branch displacement
5990 1.1.1.7 christos will overflow first. */
5991 1.1.1.6 christos if (plt0_offset > 0x80000000)
5992 1.1.1.6 christos /* xgettext:c-format */
5993 1.1.1.6 christos info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5994 1.1.1.6 christos output_bfd, h->root.root.string);
5995 1.1.1.6 christos bfd_put_32 (output_bfd, - plt0_offset,
5996 1.1.1.6 christos plt->contents + h->plt.offset + plt_plt_offset);
5997 1.1.1.6 christos }
5998 1.1.1.6 christos
5999 1.1.1.6 christos bed = get_elf_backend_data (output_bfd);
6000 1.1.1.2 christos loc = relplt->contents + plt_index * bed->s->sizeof_rela;
6001 1.1.1.4 christos bed->s->swap_reloca_out (output_bfd, &rela, loc);
6002 1.1.1.4 christos }
6003 1.1.1.4 christos }
6004 1.1.1.4 christos else if (eh->plt_got.offset != (bfd_vma) -1)
6005 1.1.1.4 christos {
6006 1.1.1.4 christos bfd_vma got_offset, plt_offset, plt_got_offset, plt_got_insn_size;
6007 1.1.1.4 christos asection *plt, *got;
6008 1.1.1.4 christos bfd_boolean got_after_plt;
6009 1.1.1.4 christos int32_t got_pcrel_offset;
6010 1.1.1.4 christos const bfd_byte *got_plt_entry;
6011 1.1.1.4 christos
6012 1.1.1.4 christos /* Set the entry in the GOT procedure linkage table. */
6013 1.1.1.4 christos plt = htab->plt_got;
6014 1.1 christos got = htab->elf.sgot;
6015 1.1.1.4 christos got_offset = h->got.offset;
6016 1.1.1.4 christos
6017 1.1.1.4 christos if (got_offset == (bfd_vma) -1
6018 1.1.1.4 christos || h->type == STT_GNU_IFUNC
6019 1.1.1.4 christos || plt == NULL
6020 1.1.1.4 christos || got == NULL)
6021 1.1.1.4 christos abort ();
6022 1.1.1.4 christos
6023 1.1.1.4 christos /* Use the second PLT entry template for the GOT PLT since they
6024 1.1.1.4 christos are the identical. */
6025 1.1.1.4 christos plt_got_insn_size = elf_x86_64_bnd_arch_bed.plt_got_insn_size;
6026 1.1.1.4 christos plt_got_offset = elf_x86_64_bnd_arch_bed.plt_got_offset;
6027 1.1.1.4 christos if (eh->has_bnd_reloc)
6028 1.1 christos got_plt_entry = elf_x86_64_bnd_plt2_entry;
6029 1.1.1.4 christos else
6030 1.1.1.4 christos {
6031 1.1.1.4 christos got_plt_entry = elf_x86_64_legacy_plt2_entry;
6032 1.1.1.4 christos
6033 1.1.1.4 christos /* Subtract 1 since there is no BND prefix. */
6034 1.1.1.4 christos plt_got_insn_size -= 1;
6035 1.1.1.4 christos plt_got_offset -= 1;
6036 1.1.1.4 christos }
6037 1.1.1.4 christos
6038 1.1.1.4 christos /* Fill in the entry in the GOT procedure linkage table. */
6039 1.1.1.4 christos plt_offset = eh->plt_got.offset;
6040 1.1.1.4 christos memcpy (plt->contents + plt_offset,
6041 1.1.1.4 christos got_plt_entry, sizeof (elf_x86_64_legacy_plt2_entry));
6042 1.1.1.4 christos
6043 1.1.1.4 christos /* Put offset the PC-relative instruction referring to the GOT
6044 1.1.1.4 christos entry, subtracting the size of that instruction. */
6045 1.1.1.4 christos got_pcrel_offset = (got->output_section->vma
6046 1.1.1.4 christos + got->output_offset
6047 1.1.1.4 christos + got_offset
6048 1.1.1.4 christos - plt->output_section->vma
6049 1.1.1.4 christos - plt->output_offset
6050 1.1.1.4 christos - plt_offset
6051 1.1.1.4 christos - plt_got_insn_size);
6052 1.1.1.4 christos
6053 1.1.1.4 christos /* Check PC-relative offset overflow in GOT PLT entry. */
6054 1.1.1.4 christos got_after_plt = got->output_section->vma > plt->output_section->vma;
6055 1.1.1.7 christos if ((got_after_plt && got_pcrel_offset < 0)
6056 1.1.1.4 christos || (!got_after_plt && got_pcrel_offset > 0))
6057 1.1.1.4 christos /* xgettext:c-format */
6058 1.1.1.4 christos info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
6059 1.1.1.4 christos output_bfd, h->root.root.string);
6060 1.1.1.4 christos
6061 1.1.1.4 christos bfd_put_32 (output_bfd, got_pcrel_offset,
6062 1.1.1.4 christos plt->contents + plt_offset + plt_got_offset);
6063 1.1.1.6 christos }
6064 1.1.1.6 christos
6065 1.1.1.4 christos if (!local_undefweak
6066 1.1.1.4 christos && !h->def_regular
6067 1.1.1.4 christos && (h->plt.offset != (bfd_vma) -1
6068 1.1.1.4 christos || eh->plt_got.offset != (bfd_vma) -1))
6069 1.1.1.4 christos {
6070 1.1.1.4 christos /* Mark the symbol as undefined, rather than as defined in
6071 1.1.1.4 christos the .plt section. Leave the value if there were any
6072 1.1.1.4 christos relocations where pointer equality matters (this is a clue
6073 1.1.1.4 christos for the dynamic linker, to make function pointer
6074 1.1.1.4 christos comparisons work between an application and shared
6075 1.1.1.4 christos library), otherwise set it to zero. If a function is only
6076 1.1.1.4 christos called from a binary, there is no need to slow down
6077 1.1.1.4 christos shared libraries because of that. */
6078 1.1.1.4 christos sym->st_shndx = SHN_UNDEF;
6079 1.1 christos if (!h->pointer_equality_needed)
6080 1.1 christos sym->st_value = 0;
6081 1.1.1.6 christos }
6082 1.1.1.6 christos
6083 1.1 christos /* Don't generate dynamic GOT relocation against undefined weak
6084 1.1 christos symbol in executable. */
6085 1.1.1.6 christos if (h->got.offset != (bfd_vma) -1
6086 1.1.1.6 christos && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
6087 1.1 christos && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE
6088 1.1 christos && !local_undefweak)
6089 1.1.1.6 christos {
6090 1.1 christos Elf_Internal_Rela rela;
6091 1.1 christos asection *relgot = htab->elf.srelgot;
6092 1.1 christos
6093 1.1 christos /* This symbol has an entry in the global offset table. Set it
6094 1.1 christos up. */
6095 1.1 christos if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
6096 1.1 christos abort ();
6097 1.1 christos
6098 1.1 christos rela.r_offset = (htab->elf.sgot->output_section->vma
6099 1.1 christos + htab->elf.sgot->output_offset
6100 1.1 christos + (h->got.offset &~ (bfd_vma) 1));
6101 1.1 christos
6102 1.1 christos /* If this is a static link, or it is a -Bsymbolic link and the
6103 1.1 christos symbol is defined locally or was forced to be local because
6104 1.1 christos of a version file, we just want to emit a RELATIVE reloc.
6105 1.1 christos The entry in the global offset table will already have been
6106 1.1 christos initialized in the relocate_section function. */
6107 1.1 christos if (h->def_regular
6108 1.1.1.6 christos && h->type == STT_GNU_IFUNC)
6109 1.1.1.6 christos {
6110 1.1.1.6 christos if (h->plt.offset == (bfd_vma) -1)
6111 1.1.1.6 christos {
6112 1.1.1.6 christos /* STT_GNU_IFUNC is referenced without PLT. */
6113 1.1.1.6 christos if (htab->elf.splt == NULL)
6114 1.1.1.6 christos {
6115 1.1.1.6 christos /* use .rel[a].iplt section to store .got relocations
6116 1.1.1.6 christos in static executable. */
6117 1.1.1.6 christos relgot = htab->elf.irelplt;
6118 1.1.1.6 christos }
6119 1.1.1.6 christos if (SYMBOL_REFERENCES_LOCAL (info, h))
6120 1.1.1.6 christos {
6121 1.1.1.6 christos rela.r_info = htab->r_info (0,
6122 1.1.1.6 christos R_X86_64_IRELATIVE);
6123 1.1.1.6 christos rela.r_addend = (h->root.u.def.value
6124 1.1.1.6 christos + h->root.u.def.section->output_section->vma
6125 1.1.1.6 christos + h->root.u.def.section->output_offset);
6126 1.1.1.6 christos }
6127 1.1.1.6 christos else
6128 1.1.1.6 christos goto do_glob_dat;
6129 1.1 christos }
6130 1.1 christos else if (bfd_link_pic (info))
6131 1.1 christos {
6132 1.1 christos /* Generate R_X86_64_GLOB_DAT. */
6133 1.1 christos goto do_glob_dat;
6134 1.1 christos }
6135 1.1 christos else
6136 1.1 christos {
6137 1.1 christos asection *plt;
6138 1.1 christos
6139 1.1 christos if (!h->pointer_equality_needed)
6140 1.1 christos abort ();
6141 1.1 christos
6142 1.1 christos /* For non-shared object, we can't use .got.plt, which
6143 1.1 christos contains the real function addres if we need pointer
6144 1.1 christos equality. We load the GOT entry with the PLT entry. */
6145 1.1 christos plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
6146 1.1 christos bfd_put_64 (output_bfd, (plt->output_section->vma
6147 1.1 christos + plt->output_offset
6148 1.1 christos + h->plt.offset),
6149 1.1 christos htab->elf.sgot->contents + h->got.offset);
6150 1.1 christos return TRUE;
6151 1.1.1.6 christos }
6152 1.1 christos }
6153 1.1 christos else if (bfd_link_pic (info)
6154 1.1 christos && SYMBOL_REFERENCES_LOCAL (info, h))
6155 1.1 christos {
6156 1.1 christos if (!h->def_regular)
6157 1.1 christos return FALSE;
6158 1.1 christos BFD_ASSERT((h->got.offset & 1) != 0);
6159 1.1 christos rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
6160 1.1 christos rela.r_addend = (h->root.u.def.value
6161 1.1 christos + h->root.u.def.section->output_section->vma
6162 1.1 christos + h->root.u.def.section->output_offset);
6163 1.1 christos }
6164 1.1 christos else
6165 1.1 christos {
6166 1.1 christos BFD_ASSERT((h->got.offset & 1) == 0);
6167 1.1 christos do_glob_dat:
6168 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0,
6169 1.1 christos htab->elf.sgot->contents + h->got.offset);
6170 1.1 christos rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
6171 1.1 christos rela.r_addend = 0;
6172 1.1.1.6 christos }
6173 1.1 christos
6174 1.1 christos elf_append_rela (output_bfd, relgot, &rela);
6175 1.1 christos }
6176 1.1 christos
6177 1.1 christos if (h->needs_copy)
6178 1.1.1.7 christos {
6179 1.1 christos Elf_Internal_Rela rela;
6180 1.1 christos asection *s;
6181 1.1 christos
6182 1.1 christos /* This symbol needs a copy reloc. Set it up. */
6183 1.1 christos
6184 1.1 christos if (h->dynindx == -1
6185 1.1.1.7 christos || (h->root.type != bfd_link_hash_defined
6186 1.1.1.7 christos && h->root.type != bfd_link_hash_defweak)
6187 1.1 christos || htab->elf.srelbss == NULL
6188 1.1 christos || htab->elf.sreldynrelro == NULL)
6189 1.1 christos abort ();
6190 1.1 christos
6191 1.1 christos rela.r_offset = (h->root.u.def.value
6192 1.1 christos + h->root.u.def.section->output_section->vma
6193 1.1 christos + h->root.u.def.section->output_offset);
6194 1.1.1.7 christos rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
6195 1.1.1.7 christos rela.r_addend = 0;
6196 1.1.1.7 christos if (h->root.u.def.section == htab->elf.sdynrelro)
6197 1.1.1.7 christos s = htab->elf.sreldynrelro;
6198 1.1.1.7 christos else
6199 1.1 christos s = htab->elf.srelbss;
6200 1.1 christos elf_append_rela (output_bfd, s, &rela);
6201 1.1 christos }
6202 1.1 christos
6203 1.1 christos return TRUE;
6204 1.1 christos }
6205 1.1 christos
6206 1.1 christos /* Finish up local dynamic symbol handling. We set the contents of
6207 1.1 christos various dynamic sections here. */
6208 1.1 christos
6209 1.1 christos static bfd_boolean
6210 1.1 christos elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
6211 1.1 christos {
6212 1.1 christos struct elf_link_hash_entry *h
6213 1.1.1.2 christos = (struct elf_link_hash_entry *) *slot;
6214 1.1 christos struct bfd_link_info *info
6215 1.1 christos = (struct bfd_link_info *) inf;
6216 1.1 christos
6217 1.1 christos return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
6218 1.1 christos info, h, NULL);
6219 1.1.1.6 christos }
6220 1.1.1.6 christos
6221 1.1.1.6 christos /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
6222 1.1.1.6 christos here since undefined weak symbol may not be dynamic and may not be
6223 1.1.1.6 christos called for elf_x86_64_finish_dynamic_symbol. */
6224 1.1.1.6 christos
6225 1.1.1.6 christos static bfd_boolean
6226 1.1.1.6 christos elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
6227 1.1.1.6 christos void *inf)
6228 1.1.1.6 christos {
6229 1.1.1.6 christos struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
6230 1.1.1.6 christos struct bfd_link_info *info = (struct bfd_link_info *) inf;
6231 1.1.1.6 christos
6232 1.1.1.6 christos if (h->root.type != bfd_link_hash_undefweak
6233 1.1.1.6 christos || h->dynindx != -1)
6234 1.1.1.6 christos return TRUE;
6235 1.1.1.6 christos
6236 1.1.1.6 christos return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
6237 1.1.1.6 christos info, h, NULL);
6238 1.1 christos }
6239 1.1 christos
6240 1.1 christos /* Used to decide how to sort relocs in an optimal manner for the
6241 1.1 christos dynamic linker, before writing them out. */
6242 1.1.1.6 christos
6243 1.1.1.3 christos static enum elf_reloc_type_class
6244 1.1.1.3 christos elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
6245 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED,
6246 1.1.1.6 christos const Elf_Internal_Rela *rela)
6247 1.1.1.6 christos {
6248 1.1.1.6 christos bfd *abfd = info->output_bfd;
6249 1.1.1.6 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6250 1.1.1.6 christos struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
6251 1.1.1.6 christos
6252 1.1.1.6 christos if (htab->elf.dynsym != NULL
6253 1.1.1.6 christos && htab->elf.dynsym->contents != NULL)
6254 1.1.1.6 christos {
6255 1.1.1.6 christos /* Check relocation against STT_GNU_IFUNC symbol if there are
6256 1.1.1.6 christos dynamic symbols. */
6257 1.1.1.6 christos unsigned long r_symndx = htab->r_sym (rela->r_info);
6258 1.1.1.6 christos if (r_symndx != STN_UNDEF)
6259 1.1.1.6 christos {
6260 1.1.1.6 christos Elf_Internal_Sym sym;
6261 1.1.1.6 christos if (!bed->s->swap_symbol_in (abfd,
6262 1.1.1.6 christos (htab->elf.dynsym->contents
6263 1.1.1.6 christos + r_symndx * bed->s->sizeof_sym),
6264 1.1.1.6 christos 0, &sym))
6265 1.1.1.6 christos abort ();
6266 1.1.1.6 christos
6267 1.1.1.6 christos if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
6268 1.1.1.6 christos return reloc_class_ifunc;
6269 1.1.1.6 christos }
6270 1.1 christos }
6271 1.1 christos
6272 1.1.1.6 christos switch ((int) ELF32_R_TYPE (rela->r_info))
6273 1.1.1.6 christos {
6274 1.1 christos case R_X86_64_IRELATIVE:
6275 1.1.1.2 christos return reloc_class_ifunc;
6276 1.1 christos case R_X86_64_RELATIVE:
6277 1.1 christos case R_X86_64_RELATIVE64:
6278 1.1 christos return reloc_class_relative;
6279 1.1 christos case R_X86_64_JUMP_SLOT:
6280 1.1 christos return reloc_class_plt;
6281 1.1 christos case R_X86_64_COPY:
6282 1.1 christos return reloc_class_copy;
6283 1.1 christos default:
6284 1.1 christos return reloc_class_normal;
6285 1.1 christos }
6286 1.1 christos }
6287 1.1 christos
6288 1.1 christos /* Finish up the dynamic sections. */
6289 1.1 christos
6290 1.1 christos static bfd_boolean
6291 1.1 christos elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
6292 1.1 christos struct bfd_link_info *info)
6293 1.1 christos {
6294 1.1 christos struct elf_x86_64_link_hash_table *htab;
6295 1.1.1.4 christos bfd *dynobj;
6296 1.1 christos asection *sdyn;
6297 1.1 christos const struct elf_x86_64_backend_data *abed;
6298 1.1 christos
6299 1.1 christos htab = elf_x86_64_hash_table (info);
6300 1.1 christos if (htab == NULL)
6301 1.1.1.4 christos return FALSE;
6302 1.1.1.4 christos
6303 1.1.1.4 christos /* Use MPX backend data in case of BND relocation. Use .plt_bnd
6304 1.1.1.4 christos section only if there is .plt section. */
6305 1.1.1.4 christos abed = (htab->elf.splt != NULL && htab->plt_bnd != NULL
6306 1.1.1.4 christos ? &elf_x86_64_bnd_arch_bed
6307 1.1 christos : get_elf_x86_64_backend_data (output_bfd));
6308 1.1.1.2 christos
6309 1.1 christos dynobj = htab->elf.dynobj;
6310 1.1 christos sdyn = bfd_get_linker_section (dynobj, ".dynamic");
6311 1.1 christos
6312 1.1 christos if (htab->elf.dynamic_sections_created)
6313 1.1 christos {
6314 1.1 christos bfd_byte *dyncon, *dynconend;
6315 1.1 christos const struct elf_backend_data *bed;
6316 1.1 christos bfd_size_type sizeof_dyn;
6317 1.1 christos
6318 1.1 christos if (sdyn == NULL || htab->elf.sgot == NULL)
6319 1.1 christos abort ();
6320 1.1 christos
6321 1.1 christos bed = get_elf_backend_data (dynobj);
6322 1.1 christos sizeof_dyn = bed->s->sizeof_dyn;
6323 1.1 christos dyncon = sdyn->contents;
6324 1.1 christos dynconend = sdyn->contents + sdyn->size;
6325 1.1 christos for (; dyncon < dynconend; dyncon += sizeof_dyn)
6326 1.1 christos {
6327 1.1 christos Elf_Internal_Dyn dyn;
6328 1.1 christos asection *s;
6329 1.1 christos
6330 1.1 christos (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
6331 1.1 christos
6332 1.1 christos switch (dyn.d_tag)
6333 1.1 christos {
6334 1.1 christos default:
6335 1.1 christos continue;
6336 1.1 christos
6337 1.1 christos case DT_PLTGOT:
6338 1.1 christos s = htab->elf.sgotplt;
6339 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6340 1.1 christos break;
6341 1.1 christos
6342 1.1 christos case DT_JMPREL:
6343 1.1 christos dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
6344 1.1 christos break;
6345 1.1 christos
6346 1.1 christos case DT_PLTRELSZ:
6347 1.1 christos s = htab->elf.srelplt->output_section;
6348 1.1 christos dyn.d_un.d_val = s->size;
6349 1.1 christos break;
6350 1.1 christos
6351 1.1 christos case DT_TLSDESC_PLT:
6352 1.1 christos s = htab->elf.splt;
6353 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6354 1.1 christos + htab->tlsdesc_plt;
6355 1.1 christos break;
6356 1.1 christos
6357 1.1 christos case DT_TLSDESC_GOT:
6358 1.1 christos s = htab->elf.sgot;
6359 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6360 1.1 christos + htab->tlsdesc_got;
6361 1.1 christos break;
6362 1.1 christos }
6363 1.1 christos
6364 1.1 christos (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
6365 1.1 christos }
6366 1.1 christos
6367 1.1 christos /* Fill in the special first entry in the procedure linkage table. */
6368 1.1 christos if (htab->elf.splt && htab->elf.splt->size > 0)
6369 1.1.1.2 christos {
6370 1.1.1.2 christos /* Fill in the first entry in the procedure linkage table. */
6371 1.1 christos memcpy (htab->elf.splt->contents,
6372 1.1 christos abed->plt0_entry, abed->plt_entry_size);
6373 1.1 christos /* Add offset for pushq GOT+8(%rip), since the instruction
6374 1.1 christos uses 6 bytes subtract this value. */
6375 1.1 christos bfd_put_32 (output_bfd,
6376 1.1 christos (htab->elf.sgotplt->output_section->vma
6377 1.1 christos + htab->elf.sgotplt->output_offset
6378 1.1 christos + 8
6379 1.1 christos - htab->elf.splt->output_section->vma
6380 1.1.1.2 christos - htab->elf.splt->output_offset
6381 1.1.1.2 christos - 6),
6382 1.1.1.2 christos htab->elf.splt->contents + abed->plt0_got1_offset);
6383 1.1 christos /* Add offset for the PC-relative instruction accessing GOT+16,
6384 1.1 christos subtracting the offset to the end of that instruction. */
6385 1.1 christos bfd_put_32 (output_bfd,
6386 1.1 christos (htab->elf.sgotplt->output_section->vma
6387 1.1 christos + htab->elf.sgotplt->output_offset
6388 1.1 christos + 16
6389 1.1.1.2 christos - htab->elf.splt->output_section->vma
6390 1.1.1.2 christos - htab->elf.splt->output_offset
6391 1.1 christos - abed->plt0_got2_insn_end),
6392 1.1.1.2 christos htab->elf.splt->contents + abed->plt0_got2_offset);
6393 1.1.1.2 christos
6394 1.1 christos elf_section_data (htab->elf.splt->output_section)
6395 1.1 christos ->this_hdr.sh_entsize = abed->plt_entry_size;
6396 1.1 christos
6397 1.1 christos if (htab->tlsdesc_plt)
6398 1.1 christos {
6399 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0,
6400 1.1 christos htab->elf.sgot->contents + htab->tlsdesc_got);
6401 1.1.1.2 christos
6402 1.1 christos memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
6403 1.1 christos abed->plt0_entry, abed->plt_entry_size);
6404 1.1 christos
6405 1.1 christos /* Add offset for pushq GOT+8(%rip), since the
6406 1.1 christos instruction uses 6 bytes subtract this value. */
6407 1.1 christos bfd_put_32 (output_bfd,
6408 1.1 christos (htab->elf.sgotplt->output_section->vma
6409 1.1 christos + htab->elf.sgotplt->output_offset
6410 1.1 christos + 8
6411 1.1 christos - htab->elf.splt->output_section->vma
6412 1.1 christos - htab->elf.splt->output_offset
6413 1.1.1.2 christos - htab->tlsdesc_plt
6414 1.1.1.2 christos - 6),
6415 1.1.1.2 christos htab->elf.splt->contents
6416 1.1.1.2 christos + htab->tlsdesc_plt + abed->plt0_got1_offset);
6417 1.1.1.2 christos /* Add offset for the PC-relative instruction accessing GOT+TDG,
6418 1.1 christos where TGD stands for htab->tlsdesc_got, subtracting the offset
6419 1.1 christos to the end of that instruction. */
6420 1.1 christos bfd_put_32 (output_bfd,
6421 1.1 christos (htab->elf.sgot->output_section->vma
6422 1.1 christos + htab->elf.sgot->output_offset
6423 1.1 christos + htab->tlsdesc_got
6424 1.1 christos - htab->elf.splt->output_section->vma
6425 1.1.1.2 christos - htab->elf.splt->output_offset
6426 1.1.1.2 christos - htab->tlsdesc_plt
6427 1.1.1.2 christos - abed->plt0_got2_insn_end),
6428 1.1 christos htab->elf.splt->contents
6429 1.1 christos + htab->tlsdesc_plt + abed->plt0_got2_offset);
6430 1.1 christos }
6431 1.1 christos }
6432 1.1.1.4 christos }
6433 1.1.1.4 christos
6434 1.1.1.4 christos if (htab->plt_bnd != NULL)
6435 1.1.1.4 christos elf_section_data (htab->plt_bnd->output_section)
6436 1.1 christos ->this_hdr.sh_entsize = sizeof (elf_x86_64_bnd_plt2_entry);
6437 1.1 christos
6438 1.1 christos if (htab->elf.sgotplt)
6439 1.1 christos {
6440 1.1.1.7 christos if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6441 1.1 christos {
6442 1.1 christos _bfd_error_handler
6443 1.1 christos (_("discarded output section: `%A'"), htab->elf.sgotplt);
6444 1.1 christos return FALSE;
6445 1.1 christos }
6446 1.1 christos
6447 1.1 christos /* Fill in the first three entries in the global offset table. */
6448 1.1 christos if (htab->elf.sgotplt->size > 0)
6449 1.1 christos {
6450 1.1 christos /* Set the first entry in the global offset table to the address of
6451 1.1 christos the dynamic section. */
6452 1.1 christos if (sdyn == NULL)
6453 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
6454 1.1 christos else
6455 1.1 christos bfd_put_64 (output_bfd,
6456 1.1 christos sdyn->output_section->vma + sdyn->output_offset,
6457 1.1 christos htab->elf.sgotplt->contents);
6458 1.1 christos /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6459 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6460 1.1 christos bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
6461 1.1 christos }
6462 1.1 christos
6463 1.1 christos elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
6464 1.1 christos GOT_ENTRY_SIZE;
6465 1.1.1.2 christos }
6466 1.1.1.2 christos
6467 1.1.1.2 christos /* Adjust .eh_frame for .plt section. */
6468 1.1.1.2 christos if (htab->plt_eh_frame != NULL
6469 1.1.1.2 christos && htab->plt_eh_frame->contents != NULL)
6470 1.1.1.2 christos {
6471 1.1.1.2 christos if (htab->elf.splt != NULL
6472 1.1.1.2 christos && htab->elf.splt->size != 0
6473 1.1.1.2 christos && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6474 1.1.1.2 christos && htab->elf.splt->output_section != NULL
6475 1.1.1.2 christos && htab->plt_eh_frame->output_section != NULL)
6476 1.1.1.2 christos {
6477 1.1.1.2 christos bfd_vma plt_start = htab->elf.splt->output_section->vma;
6478 1.1.1.2 christos bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6479 1.1.1.2 christos + htab->plt_eh_frame->output_offset
6480 1.1.1.2 christos + PLT_FDE_START_OFFSET;
6481 1.1.1.2 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6482 1.1.1.2 christos htab->plt_eh_frame->contents
6483 1.1.1.2 christos + PLT_FDE_START_OFFSET);
6484 1.1.1.2 christos }
6485 1.1.1.2 christos if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6486 1.1.1.2 christos {
6487 1.1.1.2 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6488 1.1.1.2 christos htab->plt_eh_frame,
6489 1.1.1.2 christos htab->plt_eh_frame->contents))
6490 1.1.1.2 christos return FALSE;
6491 1.1.1.2 christos }
6492 1.1.1.7 christos }
6493 1.1.1.7 christos
6494 1.1.1.7 christos /* Adjust .eh_frame for .plt.got section. */
6495 1.1.1.7 christos if (htab->plt_got_eh_frame != NULL
6496 1.1.1.7 christos && htab->plt_got_eh_frame->contents != NULL)
6497 1.1.1.7 christos {
6498 1.1.1.7 christos if (htab->plt_got != NULL
6499 1.1.1.7 christos && htab->plt_got->size != 0
6500 1.1.1.7 christos && (htab->plt_got->flags & SEC_EXCLUDE) == 0
6501 1.1.1.7 christos && htab->plt_got->output_section != NULL
6502 1.1.1.7 christos && htab->plt_got_eh_frame->output_section != NULL)
6503 1.1.1.7 christos {
6504 1.1.1.7 christos bfd_vma plt_start = htab->plt_got->output_section->vma;
6505 1.1.1.7 christos bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6506 1.1.1.7 christos + htab->plt_got_eh_frame->output_offset
6507 1.1.1.7 christos + PLT_FDE_START_OFFSET;
6508 1.1.1.7 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6509 1.1.1.7 christos htab->plt_got_eh_frame->contents
6510 1.1.1.7 christos + PLT_FDE_START_OFFSET);
6511 1.1.1.7 christos }
6512 1.1.1.7 christos if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6513 1.1.1.7 christos {
6514 1.1.1.7 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6515 1.1.1.7 christos htab->plt_got_eh_frame,
6516 1.1.1.7 christos htab->plt_got_eh_frame->contents))
6517 1.1.1.7 christos return FALSE;
6518 1.1.1.7 christos }
6519 1.1.1.7 christos }
6520 1.1.1.7 christos
6521 1.1.1.7 christos /* Adjust .eh_frame for .plt.bnd section. */
6522 1.1.1.7 christos if (htab->plt_bnd_eh_frame != NULL
6523 1.1.1.7 christos && htab->plt_bnd_eh_frame->contents != NULL)
6524 1.1.1.7 christos {
6525 1.1.1.7 christos if (htab->plt_bnd != NULL
6526 1.1.1.7 christos && htab->plt_bnd->size != 0
6527 1.1.1.7 christos && (htab->plt_bnd->flags & SEC_EXCLUDE) == 0
6528 1.1.1.7 christos && htab->plt_bnd->output_section != NULL
6529 1.1.1.7 christos && htab->plt_bnd_eh_frame->output_section != NULL)
6530 1.1.1.7 christos {
6531 1.1.1.7 christos bfd_vma plt_start = htab->plt_bnd->output_section->vma;
6532 1.1.1.7 christos bfd_vma eh_frame_start = htab->plt_bnd_eh_frame->output_section->vma
6533 1.1.1.7 christos + htab->plt_bnd_eh_frame->output_offset
6534 1.1.1.7 christos + PLT_FDE_START_OFFSET;
6535 1.1.1.7 christos bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6536 1.1.1.7 christos htab->plt_bnd_eh_frame->contents
6537 1.1.1.7 christos + PLT_FDE_START_OFFSET);
6538 1.1.1.7 christos }
6539 1.1.1.7 christos if (htab->plt_bnd_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6540 1.1.1.7 christos {
6541 1.1.1.7 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6542 1.1.1.7 christos htab->plt_bnd_eh_frame,
6543 1.1.1.7 christos htab->plt_bnd_eh_frame->contents))
6544 1.1.1.7 christos return FALSE;
6545 1.1.1.7 christos }
6546 1.1 christos }
6547 1.1 christos
6548 1.1 christos if (htab->elf.sgot && htab->elf.sgot->size > 0)
6549 1.1 christos elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
6550 1.1.1.6 christos = GOT_ENTRY_SIZE;
6551 1.1.1.6 christos
6552 1.1.1.6 christos /* Fill PLT entries for undefined weak symbols in PIE. */
6553 1.1.1.6 christos if (bfd_link_pie (info))
6554 1.1.1.6 christos bfd_hash_traverse (&info->hash->table,
6555 1.1.1.6 christos elf_x86_64_pie_finish_undefweak_symbol,
6556 1.1.1.6 christos info);
6557 1.1.1.6 christos
6558 1.1.1.6 christos return TRUE;
6559 1.1.1.6 christos }
6560 1.1.1.6 christos
6561 1.1.1.6 christos /* Fill PLT/GOT entries and allocate dynamic relocations for local
6562 1.1.1.6 christos STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6563 1.1.1.6 christos It has to be done before elf_link_sort_relocs is called so that
6564 1.1.1.6 christos dynamic relocations are properly sorted. */
6565 1.1.1.6 christos
6566 1.1.1.6 christos static bfd_boolean
6567 1.1.1.6 christos elf_x86_64_output_arch_local_syms
6568 1.1.1.6 christos (bfd *output_bfd ATTRIBUTE_UNUSED,
6569 1.1.1.6 christos struct bfd_link_info *info,
6570 1.1.1.6 christos void *flaginfo ATTRIBUTE_UNUSED,
6571 1.1.1.6 christos int (*func) (void *, const char *,
6572 1.1.1.6 christos Elf_Internal_Sym *,
6573 1.1.1.6 christos asection *,
6574 1.1.1.6 christos struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6575 1.1.1.6 christos {
6576 1.1.1.6 christos struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
6577 1.1.1.6 christos if (htab == NULL)
6578 1.1 christos return FALSE;
6579 1.1 christos
6580 1.1 christos /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6581 1.1 christos htab_traverse (htab->loc_hash_table,
6582 1.1 christos elf_x86_64_finish_local_dynamic_symbol,
6583 1.1 christos info);
6584 1.1 christos
6585 1.1 christos return TRUE;
6586 1.1.1.4 christos }
6587 1.1 christos
6588 1.1.1.4 christos /* Return an array of PLT entry symbol values. */
6589 1.1.1.4 christos
6590 1.1.1.4 christos static bfd_vma *
6591 1.1.1.4 christos elf_x86_64_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6592 1.1.1.4 christos asection *relplt)
6593 1.1.1.4 christos {
6594 1.1.1.4 christos bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6595 1.1.1.4 christos arelent *p;
6596 1.1.1.4 christos long count, i;
6597 1.1.1.4 christos bfd_vma *plt_sym_val;
6598 1.1.1.4 christos bfd_vma plt_offset;
6599 1.1.1.4 christos bfd_byte *plt_contents;
6600 1.1.1.4 christos const struct elf_x86_64_backend_data *bed;
6601 1.1.1.4 christos Elf_Internal_Shdr *hdr;
6602 1.1.1.4 christos asection *plt_bnd;
6603 1.1.1.4 christos
6604 1.1.1.4 christos /* Get the .plt section contents. PLT passed down may point to the
6605 1.1.1.4 christos .plt.bnd section. Make sure that PLT always points to the .plt
6606 1.1.1.4 christos section. */
6607 1.1.1.4 christos plt_bnd = bfd_get_section_by_name (abfd, ".plt.bnd");
6608 1.1.1.4 christos if (plt_bnd)
6609 1.1.1.4 christos {
6610 1.1.1.4 christos if (plt != plt_bnd)
6611 1.1.1.4 christos abort ();
6612 1.1.1.4 christos plt = bfd_get_section_by_name (abfd, ".plt");
6613 1.1.1.4 christos if (plt == NULL)
6614 1.1.1.4 christos abort ();
6615 1.1.1.4 christos bed = &elf_x86_64_bnd_arch_bed;
6616 1.1.1.4 christos }
6617 1.1.1.4 christos else
6618 1.1.1.4 christos bed = get_elf_x86_64_backend_data (abfd);
6619 1.1.1.4 christos
6620 1.1.1.4 christos plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6621 1.1.1.4 christos if (plt_contents == NULL)
6622 1.1.1.4 christos return NULL;
6623 1.1.1.4 christos if (!bfd_get_section_contents (abfd, (asection *) plt,
6624 1.1.1.4 christos plt_contents, 0, plt->size))
6625 1.1.1.4 christos {
6626 1.1.1.4 christos bad_return:
6627 1.1.1.4 christos free (plt_contents);
6628 1.1.1.4 christos return NULL;
6629 1.1.1.4 christos }
6630 1.1.1.4 christos
6631 1.1.1.4 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6632 1.1.1.4 christos if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6633 1.1.1.4 christos goto bad_return;
6634 1.1.1.4 christos
6635 1.1.1.4 christos hdr = &elf_section_data (relplt)->this_hdr;
6636 1.1.1.4 christos count = relplt->size / hdr->sh_entsize;
6637 1.1.1.4 christos
6638 1.1.1.4 christos plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6639 1.1.1.4 christos if (plt_sym_val == NULL)
6640 1.1.1.5 christos goto bad_return;
6641 1.1.1.4 christos
6642 1.1.1.4 christos for (i = 0; i < count; i++)
6643 1.1.1.4 christos plt_sym_val[i] = -1;
6644 1.1.1.4 christos
6645 1.1.1.4 christos plt_offset = bed->plt_entry_size;
6646 1.1.1.4 christos p = relplt->relocation;
6647 1.1.1.4 christos for (i = 0; i < count; i++, p++)
6648 1.1.1.4 christos {
6649 1.1.1.4 christos long reloc_index;
6650 1.1.1.4 christos
6651 1.1.1.4 christos /* Skip unknown relocation. */
6652 1.1.1.4 christos if (p->howto == NULL)
6653 1.1.1.4 christos continue;
6654 1.1.1.4 christos
6655 1.1.1.4 christos if (p->howto->type != R_X86_64_JUMP_SLOT
6656 1.1.1.4 christos && p->howto->type != R_X86_64_IRELATIVE)
6657 1.1.1.4 christos continue;
6658 1.1.1.4 christos
6659 1.1.1.6 christos reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6660 1.1.1.4 christos + bed->plt_reloc_offset));
6661 1.1.1.6 christos if (reloc_index < count)
6662 1.1.1.6 christos {
6663 1.1.1.6 christos if (plt_bnd)
6664 1.1.1.6 christos {
6665 1.1.1.6 christos /* This is the index in .plt section. */
6666 1.1.1.6 christos long plt_index = plt_offset / bed->plt_entry_size;
6667 1.1.1.6 christos /* Store VMA + the offset in .plt.bnd section. */
6668 1.1.1.6 christos plt_sym_val[reloc_index] =
6669 1.1.1.6 christos (plt_bnd->vma
6670 1.1.1.6 christos + (plt_index - 1) * sizeof (elf_x86_64_legacy_plt2_entry));
6671 1.1.1.6 christos }
6672 1.1.1.4 christos else
6673 1.1.1.4 christos plt_sym_val[reloc_index] = plt->vma + plt_offset;
6674 1.1.1.5 christos }
6675 1.1.1.5 christos plt_offset += bed->plt_entry_size;
6676 1.1.1.5 christos
6677 1.1.1.5 christos /* PR binutils/18437: Skip extra relocations in the .rela.plt
6678 1.1.1.5 christos section. */
6679 1.1.1.4 christos if (plt_offset >= plt->size)
6680 1.1.1.4 christos break;
6681 1.1.1.4 christos }
6682 1.1.1.4 christos
6683 1.1.1.4 christos free (plt_contents);
6684 1.1.1.4 christos
6685 1.1.1.4 christos return plt_sym_val;
6686 1.1.1.4 christos }
6687 1.1.1.4 christos
6688 1.1.1.4 christos /* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
6689 1.1.1.4 christos support. */
6690 1.1.1.4 christos
6691 1.1.1.4 christos static long
6692 1.1.1.4 christos elf_x86_64_get_synthetic_symtab (bfd *abfd,
6693 1.1.1.4 christos long symcount,
6694 1.1.1.4 christos asymbol **syms,
6695 1.1.1.4 christos long dynsymcount,
6696 1.1.1.4 christos asymbol **dynsyms,
6697 1.1.1.4 christos asymbol **ret)
6698 1.1.1.4 christos {
6699 1.1.1.4 christos /* Pass the .plt.bnd section to _bfd_elf_ifunc_get_synthetic_symtab
6700 1.1.1.4 christos as PLT if it exists. */
6701 1.1.1.4 christos asection *plt = bfd_get_section_by_name (abfd, ".plt.bnd");
6702 1.1.1.4 christos if (plt == NULL)
6703 1.1.1.4 christos plt = bfd_get_section_by_name (abfd, ".plt");
6704 1.1.1.4 christos return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6705 1.1.1.4 christos dynsymcount, dynsyms, ret,
6706 1.1 christos plt,
6707 1.1 christos elf_x86_64_get_plt_sym_val);
6708 1.1 christos }
6709 1.1 christos
6710 1.1 christos /* Handle an x86-64 specific section when reading an object file. This
6711 1.1 christos is called when elfcode.h finds a section with an unknown type. */
6712 1.1.1.3 christos
6713 1.1.1.3 christos static bfd_boolean
6714 1.1 christos elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6715 1.1 christos const char *name, int shindex)
6716 1.1 christos {
6717 1.1 christos if (hdr->sh_type != SHT_X86_64_UNWIND)
6718 1.1 christos return FALSE;
6719 1.1 christos
6720 1.1 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6721 1.1 christos return FALSE;
6722 1.1 christos
6723 1.1 christos return TRUE;
6724 1.1 christos }
6725 1.1 christos
6726 1.1 christos /* Hook called by the linker routine which adds symbols from an object
6727 1.1 christos file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6728 1.1 christos of .bss. */
6729 1.1 christos
6730 1.1.1.6 christos static bfd_boolean
6731 1.1 christos elf_x86_64_add_symbol_hook (bfd *abfd,
6732 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
6733 1.1 christos Elf_Internal_Sym *sym,
6734 1.1 christos const char **namep ATTRIBUTE_UNUSED,
6735 1.1 christos flagword *flagsp ATTRIBUTE_UNUSED,
6736 1.1 christos asection **secp,
6737 1.1 christos bfd_vma *valp)
6738 1.1 christos {
6739 1.1 christos asection *lcomm;
6740 1.1 christos
6741 1.1 christos switch (sym->st_shndx)
6742 1.1 christos {
6743 1.1 christos case SHN_X86_64_LCOMMON:
6744 1.1 christos lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6745 1.1 christos if (lcomm == NULL)
6746 1.1 christos {
6747 1.1 christos lcomm = bfd_make_section_with_flags (abfd,
6748 1.1 christos "LARGE_COMMON",
6749 1.1 christos (SEC_ALLOC
6750 1.1 christos | SEC_IS_COMMON
6751 1.1 christos | SEC_LINKER_CREATED));
6752 1.1 christos if (lcomm == NULL)
6753 1.1 christos return FALSE;
6754 1.1 christos elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6755 1.1 christos }
6756 1.1 christos *secp = lcomm;
6757 1.1 christos *valp = sym->st_size;
6758 1.1 christos return TRUE;
6759 1.1 christos }
6760 1.1 christos
6761 1.1 christos return TRUE;
6762 1.1 christos }
6763 1.1 christos
6764 1.1 christos
6765 1.1 christos /* Given a BFD section, try to locate the corresponding ELF section
6766 1.1 christos index. */
6767 1.1 christos
6768 1.1 christos static bfd_boolean
6769 1.1 christos elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6770 1.1 christos asection *sec, int *index_return)
6771 1.1 christos {
6772 1.1 christos if (sec == &_bfd_elf_large_com_section)
6773 1.1 christos {
6774 1.1 christos *index_return = SHN_X86_64_LCOMMON;
6775 1.1 christos return TRUE;
6776 1.1 christos }
6777 1.1 christos return FALSE;
6778 1.1 christos }
6779 1.1 christos
6780 1.1 christos /* Process a symbol. */
6781 1.1 christos
6782 1.1 christos static void
6783 1.1 christos elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6784 1.1 christos asymbol *asym)
6785 1.1 christos {
6786 1.1 christos elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6787 1.1 christos
6788 1.1 christos switch (elfsym->internal_elf_sym.st_shndx)
6789 1.1 christos {
6790 1.1 christos case SHN_X86_64_LCOMMON:
6791 1.1 christos asym->section = &_bfd_elf_large_com_section;
6792 1.1 christos asym->value = elfsym->internal_elf_sym.st_size;
6793 1.1 christos /* Common symbol doesn't set BSF_GLOBAL. */
6794 1.1 christos asym->flags &= ~BSF_GLOBAL;
6795 1.1 christos break;
6796 1.1 christos }
6797 1.1 christos }
6798 1.1 christos
6799 1.1 christos static bfd_boolean
6800 1.1 christos elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6801 1.1 christos {
6802 1.1 christos return (sym->st_shndx == SHN_COMMON
6803 1.1 christos || sym->st_shndx == SHN_X86_64_LCOMMON);
6804 1.1 christos }
6805 1.1 christos
6806 1.1 christos static unsigned int
6807 1.1 christos elf_x86_64_common_section_index (asection *sec)
6808 1.1 christos {
6809 1.1 christos if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6810 1.1 christos return SHN_COMMON;
6811 1.1 christos else
6812 1.1 christos return SHN_X86_64_LCOMMON;
6813 1.1 christos }
6814 1.1 christos
6815 1.1 christos static asection *
6816 1.1 christos elf_x86_64_common_section (asection *sec)
6817 1.1 christos {
6818 1.1 christos if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6819 1.1 christos return bfd_com_section_ptr;
6820 1.1 christos else
6821 1.1 christos return &_bfd_elf_large_com_section;
6822 1.1 christos }
6823 1.1.1.3 christos
6824 1.1.1.3 christos static bfd_boolean
6825 1.1 christos elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6826 1.1.1.3 christos const Elf_Internal_Sym *sym,
6827 1.1.1.3 christos asection **psec,
6828 1.1 christos bfd_boolean newdef,
6829 1.1.1.3 christos bfd_boolean olddef,
6830 1.1 christos bfd *oldbfd,
6831 1.1 christos const asection *oldsec)
6832 1.1 christos {
6833 1.1 christos /* A normal common symbol and a large common symbol result in a
6834 1.1.1.3 christos normal common symbol. We turn the large common symbol into a
6835 1.1 christos normal one. */
6836 1.1.1.3 christos if (!olddef
6837 1.1.1.3 christos && h->root.type == bfd_link_hash_common
6838 1.1.1.3 christos && !newdef
6839 1.1 christos && bfd_is_com_section (*psec)
6840 1.1 christos && oldsec != *psec)
6841 1.1.1.3 christos {
6842 1.1 christos if (sym->st_shndx == SHN_COMMON
6843 1.1 christos && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6844 1.1 christos {
6845 1.1 christos h->root.u.c.p->section
6846 1.1 christos = bfd_make_section_old_way (oldbfd, "COMMON");
6847 1.1 christos h->root.u.c.p->section->flags = SEC_ALLOC;
6848 1.1.1.3 christos }
6849 1.1.1.3 christos else if (sym->st_shndx == SHN_X86_64_LCOMMON
6850 1.1 christos && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6851 1.1 christos *psec = bfd_com_section_ptr;
6852 1.1 christos }
6853 1.1 christos
6854 1.1 christos return TRUE;
6855 1.1 christos }
6856 1.1 christos
6857 1.1 christos static int
6858 1.1 christos elf_x86_64_additional_program_headers (bfd *abfd,
6859 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED)
6860 1.1 christos {
6861 1.1 christos asection *s;
6862 1.1 christos int count = 0;
6863 1.1 christos
6864 1.1 christos /* Check to see if we need a large readonly segment. */
6865 1.1 christos s = bfd_get_section_by_name (abfd, ".lrodata");
6866 1.1 christos if (s && (s->flags & SEC_LOAD))
6867 1.1 christos count++;
6868 1.1 christos
6869 1.1 christos /* Check to see if we need a large data segment. Since .lbss sections
6870 1.1 christos is placed right after the .bss section, there should be no need for
6871 1.1 christos a large data segment just because of .lbss. */
6872 1.1 christos s = bfd_get_section_by_name (abfd, ".ldata");
6873 1.1 christos if (s && (s->flags & SEC_LOAD))
6874 1.1 christos count++;
6875 1.1 christos
6876 1.1 christos return count;
6877 1.1 christos }
6878 1.1 christos
6879 1.1 christos /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6880 1.1 christos
6881 1.1 christos static bfd_boolean
6882 1.1 christos elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
6883 1.1 christos {
6884 1.1 christos if (h->plt.offset != (bfd_vma) -1
6885 1.1 christos && !h->def_regular
6886 1.1 christos && !h->pointer_equality_needed)
6887 1.1 christos return FALSE;
6888 1.1 christos
6889 1.1 christos return _bfd_elf_hash_symbol (h);
6890 1.1 christos }
6891 1.1 christos
6892 1.1 christos /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6893 1.1 christos
6894 1.1 christos static bfd_boolean
6895 1.1 christos elf_x86_64_relocs_compatible (const bfd_target *input,
6896 1.1 christos const bfd_target *output)
6897 1.1 christos {
6898 1.1 christos return ((xvec_get_elf_backend_data (input)->s->elfclass
6899 1.1 christos == xvec_get_elf_backend_data (output)->s->elfclass)
6900 1.1 christos && _bfd_elf_relocs_compatible (input, output));
6901 1.1.1.7 christos }
6902 1.1.1.7 christos
6903 1.1.1.7 christos /* Parse x86-64 GNU properties. */
6904 1.1.1.7 christos
6905 1.1.1.7 christos static enum elf_property_kind
6906 1.1.1.7 christos elf_x86_64_parse_gnu_properties (bfd *abfd, unsigned int type,
6907 1.1.1.7 christos bfd_byte *ptr, unsigned int datasz)
6908 1.1.1.7 christos {
6909 1.1.1.7 christos elf_property *prop;
6910 1.1.1.7 christos
6911 1.1.1.7 christos switch (type)
6912 1.1.1.7 christos {
6913 1.1.1.7 christos case GNU_PROPERTY_X86_ISA_1_USED:
6914 1.1.1.7 christos case GNU_PROPERTY_X86_ISA_1_NEEDED:
6915 1.1.1.7 christos if (datasz != 4)
6916 1.1.1.7 christos {
6917 1.1.1.7 christos _bfd_error_handler
6918 1.1.1.7 christos ((type == GNU_PROPERTY_X86_ISA_1_USED
6919 1.1.1.7 christos ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
6920 1.1.1.7 christos : _("error: %B: <corrupt x86 ISA needed size: 0x%x>")),
6921 1.1.1.7 christos abfd, datasz);
6922 1.1.1.7 christos return property_corrupt;
6923 1.1.1.7 christos }
6924 1.1.1.7 christos prop = _bfd_elf_get_property (abfd, type, datasz);
6925 1.1.1.7 christos prop->u.number = bfd_h_get_32 (abfd, ptr);
6926 1.1.1.7 christos prop->pr_kind = property_number;
6927 1.1.1.7 christos break;
6928 1.1.1.7 christos
6929 1.1.1.7 christos default:
6930 1.1.1.7 christos return property_ignored;
6931 1.1.1.7 christos }
6932 1.1.1.7 christos
6933 1.1.1.7 christos return property_number;
6934 1.1.1.7 christos }
6935 1.1.1.7 christos
6936 1.1.1.7 christos /* Merge x86-64 GNU property BPROP with APROP. If APROP isn't NULL,
6937 1.1.1.7 christos return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
6938 1.1.1.7 christos should be merged with ABFD. */
6939 1.1.1.7 christos
6940 1.1.1.7 christos static bfd_boolean
6941 1.1.1.7 christos elf_x86_64_merge_gnu_properties (bfd *abfd ATTRIBUTE_UNUSED,
6942 1.1.1.7 christos elf_property *aprop,
6943 1.1.1.7 christos elf_property *bprop)
6944 1.1.1.7 christos {
6945 1.1.1.7 christos unsigned int number;
6946 1.1.1.7 christos bfd_boolean updated = FALSE;
6947 1.1.1.7 christos unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
6948 1.1.1.7 christos
6949 1.1.1.7 christos switch (pr_type)
6950 1.1.1.7 christos {
6951 1.1.1.7 christos case GNU_PROPERTY_X86_ISA_1_USED:
6952 1.1.1.7 christos case GNU_PROPERTY_X86_ISA_1_NEEDED:
6953 1.1.1.7 christos if (aprop != NULL && bprop != NULL)
6954 1.1.1.7 christos {
6955 1.1.1.7 christos number = aprop->u.number;
6956 1.1.1.7 christos aprop->u.number = number | bprop->u.number;
6957 1.1.1.7 christos updated = number != (unsigned int) aprop->u.number;
6958 1.1.1.7 christos }
6959 1.1.1.7 christos else
6960 1.1.1.7 christos {
6961 1.1.1.7 christos /* Return TRUE if APROP is NULL to indicate that BPROP should
6962 1.1.1.7 christos be added to ABFD. */
6963 1.1.1.7 christos updated = aprop == NULL;
6964 1.1.1.7 christos }
6965 1.1.1.7 christos break;
6966 1.1.1.7 christos
6967 1.1.1.7 christos default:
6968 1.1.1.7 christos /* Never should happen. */
6969 1.1.1.7 christos abort ();
6970 1.1.1.7 christos }
6971 1.1.1.7 christos
6972 1.1.1.7 christos return updated;
6973 1.1 christos }
6974 1.1.1.7 christos
6975 1.1 christos static const struct bfd_elf_special_section
6976 1.1 christos elf_x86_64_special_sections[]=
6977 1.1 christos {
6978 1.1 christos { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6979 1.1 christos { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6980 1.1 christos { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
6981 1.1 christos { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6982 1.1 christos { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6983 1.1 christos { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6984 1.1 christos { NULL, 0, 0, 0, 0 }
6985 1.1.1.4 christos };
6986 1.1 christos
6987 1.1 christos #define TARGET_LITTLE_SYM x86_64_elf64_vec
6988 1.1 christos #define TARGET_LITTLE_NAME "elf64-x86-64"
6989 1.1 christos #define ELF_ARCH bfd_arch_i386
6990 1.1 christos #define ELF_TARGET_ID X86_64_ELF_DATA
6991 1.1 christos #define ELF_MACHINE_CODE EM_X86_64
6992 1.1 christos #define ELF_MAXPAGESIZE 0x200000
6993 1.1 christos #define ELF_MINPAGESIZE 0x1000
6994 1.1 christos #define ELF_COMMONPAGESIZE 0x1000
6995 1.1 christos
6996 1.1 christos #define elf_backend_can_gc_sections 1
6997 1.1 christos #define elf_backend_can_refcount 1
6998 1.1 christos #define elf_backend_want_got_plt 1
6999 1.1 christos #define elf_backend_plt_readonly 1
7000 1.1 christos #define elf_backend_want_plt_sym 0
7001 1.1.1.2 christos #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
7002 1.1.1.5 christos #define elf_backend_rela_normal 1
7003 1.1.1.6 christos #define elf_backend_plt_alignment 4
7004 1.1.1.7 christos #define elf_backend_extern_protected_data 1
7005 1.1.1.7 christos #define elf_backend_caches_rawsize 1
7006 1.1 christos #define elf_backend_dtrel_excludes_plt 1
7007 1.1 christos #define elf_backend_want_dynrelro 1
7008 1.1 christos
7009 1.1 christos #define elf_info_to_howto elf_x86_64_info_to_howto
7010 1.1 christos
7011 1.1 christos #define bfd_elf64_bfd_link_hash_table_create \
7012 1.1 christos elf_x86_64_link_hash_table_create
7013 1.1 christos #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
7014 1.1 christos #define bfd_elf64_bfd_reloc_name_lookup \
7015 1.1 christos elf_x86_64_reloc_name_lookup
7016 1.1 christos
7017 1.1 christos #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
7018 1.1 christos #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
7019 1.1 christos #define elf_backend_check_relocs elf_x86_64_check_relocs
7020 1.1 christos #define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
7021 1.1 christos #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
7022 1.1.1.6 christos #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
7023 1.1 christos #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
7024 1.1 christos #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
7025 1.1 christos #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
7026 1.1.1.2 christos #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
7027 1.1.1.2 christos #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
7028 1.1.1.2 christos #ifdef CORE_HEADER
7029 1.1 christos #define elf_backend_write_core_note elf_x86_64_write_core_note
7030 1.1 christos #endif
7031 1.1 christos #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
7032 1.1 christos #define elf_backend_relocate_section elf_x86_64_relocate_section
7033 1.1 christos #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
7034 1.1 christos #define elf_backend_always_size_sections elf_x86_64_always_size_sections
7035 1.1 christos #define elf_backend_init_index_section _bfd_elf_init_1_index_section
7036 1.1.1.4 christos #define elf_backend_object_p elf64_x86_64_elf_object_p
7037 1.1 christos #define bfd_elf64_mkobject elf_x86_64_mkobject
7038 1.1 christos #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
7039 1.1 christos
7040 1.1 christos #define elf_backend_section_from_shdr \
7041 1.1 christos elf_x86_64_section_from_shdr
7042 1.1 christos
7043 1.1 christos #define elf_backend_section_from_bfd_section \
7044 1.1 christos elf_x86_64_elf_section_from_bfd_section
7045 1.1 christos #define elf_backend_add_symbol_hook \
7046 1.1 christos elf_x86_64_add_symbol_hook
7047 1.1 christos #define elf_backend_symbol_processing \
7048 1.1 christos elf_x86_64_symbol_processing
7049 1.1 christos #define elf_backend_common_section_index \
7050 1.1 christos elf_x86_64_common_section_index
7051 1.1 christos #define elf_backend_common_section \
7052 1.1 christos elf_x86_64_common_section
7053 1.1 christos #define elf_backend_common_definition \
7054 1.1 christos elf_x86_64_common_definition
7055 1.1 christos #define elf_backend_merge_symbol \
7056 1.1 christos elf_x86_64_merge_symbol
7057 1.1 christos #define elf_backend_special_sections \
7058 1.1 christos elf_x86_64_special_sections
7059 1.1 christos #define elf_backend_additional_program_headers \
7060 1.1 christos elf_x86_64_additional_program_headers
7061 1.1.1.6 christos #define elf_backend_hash_symbol \
7062 1.1.1.6 christos elf_x86_64_hash_symbol
7063 1.1.1.6 christos #define elf_backend_omit_section_dynsym \
7064 1.1.1.6 christos ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
7065 1.1.1.7 christos #define elf_backend_fixup_symbol \
7066 1.1.1.7 christos elf_x86_64_fixup_symbol
7067 1.1.1.7 christos #define elf_backend_parse_gnu_properties \
7068 1.1.1.7 christos elf_x86_64_parse_gnu_properties
7069 1.1 christos #define elf_backend_merge_gnu_properties \
7070 1.1 christos elf_x86_64_merge_gnu_properties
7071 1.1 christos
7072 1.1.1.5 christos #include "elf64-target.h"
7073 1.1.1.5 christos
7074 1.1.1.5 christos /* CloudABI support. */
7075 1.1.1.5 christos
7076 1.1.1.5 christos #undef TARGET_LITTLE_SYM
7077 1.1.1.5 christos #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
7078 1.1.1.5 christos #undef TARGET_LITTLE_NAME
7079 1.1.1.5 christos #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
7080 1.1.1.5 christos
7081 1.1.1.5 christos #undef ELF_OSABI
7082 1.1.1.5 christos #define ELF_OSABI ELFOSABI_CLOUDABI
7083 1.1.1.5 christos
7084 1.1.1.5 christos #undef elf64_bed
7085 1.1.1.5 christos #define elf64_bed elf64_x86_64_cloudabi_bed
7086 1.1.1.5 christos
7087 1.1 christos #include "elf64-target.h"
7088 1.1 christos
7089 1.1 christos /* FreeBSD support. */
7090 1.1.1.4 christos
7091 1.1 christos #undef TARGET_LITTLE_SYM
7092 1.1 christos #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
7093 1.1 christos #undef TARGET_LITTLE_NAME
7094 1.1 christos #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
7095 1.1 christos
7096 1.1 christos #undef ELF_OSABI
7097 1.1 christos #define ELF_OSABI ELFOSABI_FREEBSD
7098 1.1 christos
7099 1.1 christos #undef elf64_bed
7100 1.1 christos #define elf64_bed elf64_x86_64_fbsd_bed
7101 1.1 christos
7102 1.1 christos #include "elf64-target.h"
7103 1.1 christos
7104 1.1 christos /* Solaris 2 support. */
7105 1.1.1.4 christos
7106 1.1 christos #undef TARGET_LITTLE_SYM
7107 1.1 christos #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
7108 1.1 christos #undef TARGET_LITTLE_NAME
7109 1.1 christos #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
7110 1.1 christos
7111 1.1 christos /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
7112 1.1 christos objects won't be recognized. */
7113 1.1 christos #undef ELF_OSABI
7114 1.1 christos
7115 1.1 christos #undef elf64_bed
7116 1.1 christos #define elf64_bed elf64_x86_64_sol2_bed
7117 1.1 christos
7118 1.1.1.6 christos /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
7119 1.1 christos boundary. */
7120 1.1 christos #undef elf_backend_static_tls_alignment
7121 1.1 christos #define elf_backend_static_tls_alignment 16
7122 1.1 christos
7123 1.1 christos /* The Solaris 2 ABI requires a plt symbol on all platforms.
7124 1.1 christos
7125 1.1.1.6 christos Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
7126 1.1 christos File, p.63. */
7127 1.1 christos #undef elf_backend_want_plt_sym
7128 1.1.1.6 christos #define elf_backend_want_plt_sym 1
7129 1.1.1.6 christos
7130 1.1.1.6 christos #undef elf_backend_strtab_flags
7131 1.1.1.6 christos #define elf_backend_strtab_flags SHF_STRINGS
7132 1.1.1.6 christos
7133 1.1.1.6 christos static bfd_boolean
7134 1.1.1.6 christos elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
7135 1.1.1.6 christos bfd *obfd ATTRIBUTE_UNUSED,
7136 1.1.1.6 christos const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
7137 1.1.1.6 christos Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
7138 1.1.1.6 christos {
7139 1.1.1.6 christos /* PR 19938: FIXME: Need to add code for setting the sh_info
7140 1.1.1.6 christos and sh_link fields of Solaris specific section types. */
7141 1.1.1.6 christos return FALSE;
7142 1.1.1.6 christos }
7143 1.1.1.6 christos
7144 1.1.1.6 christos #undef elf_backend_copy_special_section_fields
7145 1.1 christos #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
7146 1.1 christos
7147 1.1.1.2 christos #include "elf64-target.h"
7148 1.1.1.2 christos
7149 1.1.1.3 christos /* Native Client support. */
7150 1.1.1.3 christos
7151 1.1.1.3 christos static bfd_boolean
7152 1.1.1.3 christos elf64_x86_64_nacl_elf_object_p (bfd *abfd)
7153 1.1.1.3 christos {
7154 1.1.1.3 christos /* Set the right machine number for a NaCl x86-64 ELF64 file. */
7155 1.1.1.3 christos bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
7156 1.1.1.3 christos return TRUE;
7157 1.1.1.2 christos }
7158 1.1.1.4 christos
7159 1.1.1.2 christos #undef TARGET_LITTLE_SYM
7160 1.1.1.2 christos #define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
7161 1.1.1.2 christos #undef TARGET_LITTLE_NAME
7162 1.1.1.2 christos #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
7163 1.1.1.2 christos #undef elf64_bed
7164 1.1.1.2 christos #define elf64_bed elf64_x86_64_nacl_bed
7165 1.1.1.2 christos
7166 1.1.1.2 christos #undef ELF_MAXPAGESIZE
7167 1.1.1.2 christos #undef ELF_MINPAGESIZE
7168 1.1.1.2 christos #undef ELF_COMMONPAGESIZE
7169 1.1.1.2 christos #define ELF_MAXPAGESIZE 0x10000
7170 1.1.1.2 christos #define ELF_MINPAGESIZE 0x10000
7171 1.1.1.2 christos #define ELF_COMMONPAGESIZE 0x10000
7172 1.1.1.2 christos
7173 1.1.1.2 christos /* Restore defaults. */
7174 1.1.1.2 christos #undef ELF_OSABI
7175 1.1.1.2 christos #undef elf_backend_static_tls_alignment
7176 1.1.1.6 christos #undef elf_backend_want_plt_sym
7177 1.1.1.6 christos #define elf_backend_want_plt_sym 0
7178 1.1.1.2 christos #undef elf_backend_strtab_flags
7179 1.1.1.2 christos #undef elf_backend_copy_special_section_fields
7180 1.1.1.2 christos
7181 1.1.1.2 christos /* NaCl uses substantially different PLT entries for the same effects. */
7182 1.1.1.2 christos
7183 1.1.1.2 christos #undef elf_backend_plt_alignment
7184 1.1.1.2 christos #define elf_backend_plt_alignment 5
7185 1.1.1.2 christos #define NACL_PLT_ENTRY_SIZE 64
7186 1.1.1.2 christos #define NACLMASK 0xe0 /* 32-byte alignment mask. */
7187 1.1.1.2 christos
7188 1.1.1.2 christos static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
7189 1.1.1.2 christos {
7190 1.1.1.2 christos 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
7191 1.1.1.2 christos 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
7192 1.1.1.2 christos 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7193 1.1.1.2 christos 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7194 1.1.1.2 christos 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7195 1.1.1.3 christos
7196 1.1.1.2 christos /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
7197 1.1.1.2 christos 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
7198 1.1.1.6 christos
7199 1.1.1.2 christos /* 32 bytes of nop to pad out to the standard size. */
7200 1.1.1.6 christos 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7201 1.1.1.2 christos 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7202 1.1.1.6 christos 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7203 1.1.1.2 christos 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7204 1.1.1.2 christos 0x66, /* excess data16 prefix */
7205 1.1.1.2 christos 0x90 /* nop */
7206 1.1.1.2 christos };
7207 1.1.1.2 christos
7208 1.1.1.2 christos static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
7209 1.1.1.2 christos {
7210 1.1.1.2 christos 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
7211 1.1.1.2 christos 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7212 1.1.1.2 christos 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7213 1.1.1.2 christos 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7214 1.1.1.6 christos
7215 1.1.1.2 christos /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
7216 1.1.1.2 christos 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7217 1.1.1.2 christos 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7218 1.1.1.2 christos
7219 1.1.1.2 christos /* Lazy GOT entries point here (32-byte aligned). */
7220 1.1.1.2 christos 0x68, /* pushq immediate */
7221 1.1.1.2 christos 0, 0, 0, 0, /* replaced with index into relocation table. */
7222 1.1.1.2 christos 0xe9, /* jmp relative */
7223 1.1.1.2 christos 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
7224 1.1.1.6 christos
7225 1.1.1.2 christos /* 22 bytes of nop to pad out to the standard size. */
7226 1.1.1.2 christos 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7227 1.1.1.2 christos 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7228 1.1.1.2 christos 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
7229 1.1.1.2 christos };
7230 1.1.1.2 christos
7231 1.1.1.2 christos /* .eh_frame covering the .plt section. */
7232 1.1.1.2 christos
7233 1.1.1.2 christos static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
7234 1.1.1.2 christos {
7235 1.1.1.2 christos #if (PLT_CIE_LENGTH != 20 \
7236 1.1.1.2 christos || PLT_FDE_LENGTH != 36 \
7237 1.1.1.2 christos || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
7238 1.1.1.2 christos || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
7239 1.1.1.2 christos # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
7240 1.1.1.2 christos #endif
7241 1.1.1.2 christos PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
7242 1.1.1.2 christos 0, 0, 0, 0, /* CIE ID */
7243 1.1.1.2 christos 1, /* CIE version */
7244 1.1.1.2 christos 'z', 'R', 0, /* Augmentation string */
7245 1.1.1.2 christos 1, /* Code alignment factor */
7246 1.1.1.2 christos 0x78, /* Data alignment factor */
7247 1.1.1.2 christos 16, /* Return address column */
7248 1.1.1.2 christos 1, /* Augmentation size */
7249 1.1.1.2 christos DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
7250 1.1.1.2 christos DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
7251 1.1.1.2 christos DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
7252 1.1.1.2 christos DW_CFA_nop, DW_CFA_nop,
7253 1.1.1.2 christos
7254 1.1.1.2 christos PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
7255 1.1.1.2 christos PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
7256 1.1.1.2 christos 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
7257 1.1.1.2 christos 0, 0, 0, 0, /* .plt size goes here */
7258 1.1.1.2 christos 0, /* Augmentation size */
7259 1.1.1.2 christos DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
7260 1.1.1.2 christos DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7261 1.1.1.2 christos DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
7262 1.1.1.2 christos DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7263 1.1.1.2 christos DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
7264 1.1.1.2 christos 13, /* Block length */
7265 1.1.1.2 christos DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
7266 1.1.1.2 christos DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
7267 1.1.1.2 christos DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
7268 1.1.1.2 christos DW_OP_lit3, DW_OP_shl, DW_OP_plus,
7269 1.1.1.2 christos DW_CFA_nop, DW_CFA_nop
7270 1.1.1.2 christos };
7271 1.1.1.2 christos
7272 1.1.1.2 christos static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
7273 1.1.1.2 christos {
7274 1.1.1.2 christos elf_x86_64_nacl_plt0_entry, /* plt0_entry */
7275 1.1.1.2 christos elf_x86_64_nacl_plt_entry, /* plt_entry */
7276 1.1.1.2 christos NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
7277 1.1.1.2 christos 2, /* plt0_got1_offset */
7278 1.1.1.2 christos 9, /* plt0_got2_offset */
7279 1.1.1.2 christos 13, /* plt0_got2_insn_end */
7280 1.1.1.2 christos 3, /* plt_got_offset */
7281 1.1.1.2 christos 33, /* plt_reloc_offset */
7282 1.1.1.2 christos 38, /* plt_plt_offset */
7283 1.1.1.2 christos 7, /* plt_got_insn_size */
7284 1.1.1.2 christos 42, /* plt_plt_insn_end */
7285 1.1.1.2 christos 32, /* plt_lazy_offset */
7286 1.1.1.7 christos elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
7287 1.1.1.7 christos sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
7288 1.1.1.2 christos NULL, /* eh_frame_plt_got */
7289 1.1.1.2 christos 0, /* eh_frame_plt_got_size */
7290 1.1.1.2 christos };
7291 1.1.1.2 christos
7292 1.1.1.2 christos #undef elf_backend_arch_data
7293 1.1.1.3 christos #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7294 1.1.1.3 christos
7295 1.1.1.2 christos #undef elf_backend_object_p
7296 1.1.1.2 christos #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
7297 1.1.1.2 christos #undef elf_backend_modify_segment_map
7298 1.1.1.2 christos #define elf_backend_modify_segment_map nacl_modify_segment_map
7299 1.1.1.3 christos #undef elf_backend_modify_program_headers
7300 1.1.1.3 christos #define elf_backend_modify_program_headers nacl_modify_program_headers
7301 1.1.1.2 christos #undef elf_backend_final_write_processing
7302 1.1.1.2 christos #define elf_backend_final_write_processing nacl_final_write_processing
7303 1.1.1.2 christos
7304 1.1.1.2 christos #include "elf64-target.h"
7305 1.1.1.2 christos
7306 1.1.1.3 christos /* Native Client x32 support. */
7307 1.1.1.3 christos
7308 1.1.1.3 christos static bfd_boolean
7309 1.1.1.3 christos elf32_x86_64_nacl_elf_object_p (bfd *abfd)
7310 1.1.1.3 christos {
7311 1.1.1.3 christos /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7312 1.1.1.3 christos bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
7313 1.1.1.3 christos return TRUE;
7314 1.1.1.2 christos }
7315 1.1.1.4 christos
7316 1.1.1.2 christos #undef TARGET_LITTLE_SYM
7317 1.1.1.2 christos #define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
7318 1.1.1.2 christos #undef TARGET_LITTLE_NAME
7319 1.1.1.2 christos #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7320 1.1.1.2 christos #undef elf32_bed
7321 1.1.1.2 christos #define elf32_bed elf32_x86_64_nacl_bed
7322 1.1.1.2 christos
7323 1.1.1.2 christos #define bfd_elf32_bfd_link_hash_table_create \
7324 1.1.1.2 christos elf_x86_64_link_hash_table_create
7325 1.1.1.2 christos #define bfd_elf32_bfd_reloc_type_lookup \
7326 1.1.1.2 christos elf_x86_64_reloc_type_lookup
7327 1.1.1.2 christos #define bfd_elf32_bfd_reloc_name_lookup \
7328 1.1.1.2 christos elf_x86_64_reloc_name_lookup
7329 1.1.1.4 christos #define bfd_elf32_mkobject \
7330 1.1.1.4 christos elf_x86_64_mkobject
7331 1.1.1.2 christos #define bfd_elf32_get_synthetic_symtab \
7332 1.1.1.2 christos elf_x86_64_get_synthetic_symtab
7333 1.1.1.2 christos
7334 1.1.1.3 christos #undef elf_backend_object_p
7335 1.1.1.2 christos #define elf_backend_object_p \
7336 1.1.1.2 christos elf32_x86_64_nacl_elf_object_p
7337 1.1.1.2 christos
7338 1.1.1.2 christos #undef elf_backend_bfd_from_remote_memory
7339 1.1.1.2 christos #define elf_backend_bfd_from_remote_memory \
7340 1.1.1.2 christos _bfd_elf32_bfd_from_remote_memory
7341 1.1.1.2 christos
7342 1.1.1.2 christos #undef elf_backend_size_info
7343 1.1.1.2 christos #define elf_backend_size_info \
7344 1.1.1.2 christos _bfd_elf32_size_info
7345 1.1.1.2 christos
7346 1.1.1.2 christos #include "elf32-target.h"
7347 1.1.1.2 christos
7348 1.1.1.2 christos /* Restore defaults. */
7349 1.1.1.2 christos #undef elf_backend_object_p
7350 1.1.1.2 christos #define elf_backend_object_p elf64_x86_64_elf_object_p
7351 1.1.1.2 christos #undef elf_backend_bfd_from_remote_memory
7352 1.1.1.2 christos #undef elf_backend_size_info
7353 1.1.1.3 christos #undef elf_backend_modify_segment_map
7354 1.1.1.2 christos #undef elf_backend_modify_program_headers
7355 1.1 christos #undef elf_backend_final_write_processing
7356 1.1 christos
7357 1.1 christos /* Intel L1OM support. */
7358 1.1 christos
7359 1.1 christos static bfd_boolean
7360 1.1 christos elf64_l1om_elf_object_p (bfd *abfd)
7361 1.1 christos {
7362 1.1 christos /* Set the right machine number for an L1OM elf64 file. */
7363 1.1 christos bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
7364 1.1 christos return TRUE;
7365 1.1 christos }
7366 1.1.1.4 christos
7367 1.1 christos #undef TARGET_LITTLE_SYM
7368 1.1 christos #define TARGET_LITTLE_SYM l1om_elf64_vec
7369 1.1 christos #undef TARGET_LITTLE_NAME
7370 1.1 christos #define TARGET_LITTLE_NAME "elf64-l1om"
7371 1.1 christos #undef ELF_ARCH
7372 1.1 christos #define ELF_ARCH bfd_arch_l1om
7373 1.1 christos
7374 1.1 christos #undef ELF_MACHINE_CODE
7375 1.1 christos #define ELF_MACHINE_CODE EM_L1OM
7376 1.1 christos
7377 1.1 christos #undef ELF_OSABI
7378 1.1 christos
7379 1.1 christos #undef elf64_bed
7380 1.1 christos #define elf64_bed elf64_l1om_bed
7381 1.1 christos
7382 1.1 christos #undef elf_backend_object_p
7383 1.1.1.2 christos #define elf_backend_object_p elf64_l1om_elf_object_p
7384 1.1.1.2 christos
7385 1.1.1.2 christos /* Restore defaults. */
7386 1.1.1.2 christos #undef ELF_MAXPAGESIZE
7387 1.1.1.2 christos #undef ELF_MINPAGESIZE
7388 1.1.1.2 christos #undef ELF_COMMONPAGESIZE
7389 1.1.1.2 christos #define ELF_MAXPAGESIZE 0x200000
7390 1.1.1.2 christos #define ELF_MINPAGESIZE 0x1000
7391 1.1.1.2 christos #define ELF_COMMONPAGESIZE 0x1000
7392 1.1.1.2 christos #undef elf_backend_plt_alignment
7393 1.1.1.2 christos #define elf_backend_plt_alignment 4
7394 1.1 christos #undef elf_backend_arch_data
7395 1.1 christos #define elf_backend_arch_data &elf_x86_64_arch_bed
7396 1.1 christos
7397 1.1 christos #include "elf64-target.h"
7398 1.1 christos
7399 1.1 christos /* FreeBSD L1OM support. */
7400 1.1.1.4 christos
7401 1.1 christos #undef TARGET_LITTLE_SYM
7402 1.1 christos #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
7403 1.1 christos #undef TARGET_LITTLE_NAME
7404 1.1 christos #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7405 1.1 christos
7406 1.1 christos #undef ELF_OSABI
7407 1.1 christos #define ELF_OSABI ELFOSABI_FREEBSD
7408 1.1 christos
7409 1.1 christos #undef elf64_bed
7410 1.1 christos #define elf64_bed elf64_l1om_fbsd_bed
7411 1.1 christos
7412 1.1.1.2 christos #include "elf64-target.h"
7413 1.1 christos
7414 1.1 christos /* Intel K1OM support. */
7415 1.1.1.2 christos
7416 1.1 christos static bfd_boolean
7417 1.1.1.2 christos elf64_k1om_elf_object_p (bfd *abfd)
7418 1.1.1.2 christos {
7419 1.1 christos /* Set the right machine number for an K1OM elf64 file. */
7420 1.1 christos bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
7421 1.1 christos return TRUE;
7422 1.1 christos }
7423 1.1.1.4 christos
7424 1.1.1.2 christos #undef TARGET_LITTLE_SYM
7425 1.1.1.2 christos #define TARGET_LITTLE_SYM k1om_elf64_vec
7426 1.1.1.2 christos #undef TARGET_LITTLE_NAME
7427 1.1.1.2 christos #define TARGET_LITTLE_NAME "elf64-k1om"
7428 1.1.1.2 christos #undef ELF_ARCH
7429 1.1.1.2 christos #define ELF_ARCH bfd_arch_k1om
7430 1.1.1.2 christos
7431 1.1.1.2 christos #undef ELF_MACHINE_CODE
7432 1.1.1.2 christos #define ELF_MACHINE_CODE EM_K1OM
7433 1.1.1.2 christos
7434 1.1.1.2 christos #undef ELF_OSABI
7435 1.1.1.2 christos
7436 1.1.1.2 christos #undef elf64_bed
7437 1.1.1.2 christos #define elf64_bed elf64_k1om_bed
7438 1.1.1.2 christos
7439 1.1.1.2 christos #undef elf_backend_object_p
7440 1.1.1.2 christos #define elf_backend_object_p elf64_k1om_elf_object_p
7441 1.1.1.2 christos
7442 1.1.1.2 christos #undef elf_backend_static_tls_alignment
7443 1.1.1.2 christos
7444 1.1.1.2 christos #undef elf_backend_want_plt_sym
7445 1.1.1.2 christos #define elf_backend_want_plt_sym 0
7446 1.1.1.2 christos
7447 1.1.1.2 christos #include "elf64-target.h"
7448 1.1.1.2 christos
7449 1.1.1.2 christos /* FreeBSD K1OM support. */
7450 1.1.1.4 christos
7451 1.1.1.2 christos #undef TARGET_LITTLE_SYM
7452 1.1.1.2 christos #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7453 1.1.1.2 christos #undef TARGET_LITTLE_NAME
7454 1.1.1.2 christos #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7455 1.1.1.2 christos
7456 1.1.1.2 christos #undef ELF_OSABI
7457 1.1.1.2 christos #define ELF_OSABI ELFOSABI_FREEBSD
7458 1.1.1.2 christos
7459 1.1.1.2 christos #undef elf64_bed
7460 1.1.1.2 christos #define elf64_bed elf64_k1om_fbsd_bed
7461 1.1.1.2 christos
7462 1.1.1.2 christos #include "elf64-target.h"
7463 1.1.1.2 christos
7464 1.1.1.2 christos /* 32bit x86-64 support. */
7465 1.1.1.4 christos
7466 1.1 christos #undef TARGET_LITTLE_SYM
7467 1.1 christos #define TARGET_LITTLE_SYM x86_64_elf32_vec
7468 1.1.1.2 christos #undef TARGET_LITTLE_NAME
7469 1.1 christos #define TARGET_LITTLE_NAME "elf32-x86-64"
7470 1.1 christos #undef elf32_bed
7471 1.1 christos
7472 1.1 christos #undef ELF_ARCH
7473 1.1 christos #define ELF_ARCH bfd_arch_i386
7474 1.1 christos
7475 1.1 christos #undef ELF_MACHINE_CODE
7476 1.1 christos #define ELF_MACHINE_CODE EM_X86_64
7477 1.1 christos
7478 1.1 christos #undef ELF_OSABI
7479 1.1 christos
7480 1.1 christos #undef elf_backend_object_p
7481 1.1 christos #define elf_backend_object_p \
7482 1.1 christos elf32_x86_64_elf_object_p
7483 1.1 christos
7484 1.1 christos #undef elf_backend_bfd_from_remote_memory
7485 1.1 christos #define elf_backend_bfd_from_remote_memory \
7486 1.1 christos _bfd_elf32_bfd_from_remote_memory
7487 1.1 christos
7488 1.1 christos #undef elf_backend_size_info
7489 1.1 christos #define elf_backend_size_info \
7490 1.1 christos _bfd_elf32_size_info
7491
7492 #include "elf32-target.h"
7493