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