/src/binutils-gdb/bfd/mach-o-i386.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Intel i386 Mach-O support for BFD. |
2 | | Copyright (C) 2009-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of BFD, the Binary File Descriptor library. |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | | MA 02110-1301, USA. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "bfd.h" |
23 | | #include "libbfd.h" |
24 | | #include "libiberty.h" |
25 | | #include "mach-o.h" |
26 | | #include "mach-o/reloc.h" |
27 | | |
28 | | #define bfd_mach_o_object_p bfd_mach_o_i386_object_p |
29 | | #define bfd_mach_o_core_p bfd_mach_o_i386_core_p |
30 | | #define bfd_mach_o_mkobject bfd_mach_o_i386_mkobject |
31 | | |
32 | | static bfd_cleanup |
33 | | bfd_mach_o_i386_object_p (bfd *abfd) |
34 | 3.41M | { |
35 | 3.41M | return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_I386); |
36 | 3.41M | } |
37 | | |
38 | | static bfd_cleanup |
39 | | bfd_mach_o_i386_core_p (bfd *abfd) |
40 | 16.6k | { |
41 | 16.6k | return bfd_mach_o_header_p (abfd, 0, |
42 | 16.6k | BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_I386); |
43 | 16.6k | } |
44 | | |
45 | | static bool |
46 | | bfd_mach_o_i386_mkobject (bfd *abfd) |
47 | 87 | { |
48 | 87 | bfd_mach_o_data_struct *mdata; |
49 | | |
50 | 87 | if (!bfd_mach_o_mkobject_init (abfd)) |
51 | 0 | return false; |
52 | | |
53 | 87 | mdata = bfd_mach_o_get_data (abfd); |
54 | 87 | mdata->header.magic = BFD_MACH_O_MH_MAGIC; |
55 | 87 | mdata->header.cputype = BFD_MACH_O_CPU_TYPE_I386; |
56 | 87 | mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL; |
57 | 87 | mdata->header.byteorder = BFD_ENDIAN_LITTLE; |
58 | 87 | mdata->header.version = 1; |
59 | | |
60 | 87 | return true; |
61 | 87 | } |
62 | | |
63 | | static reloc_howto_type i386_howto_table[]= |
64 | | { |
65 | | /* 0 */ |
66 | | HOWTO(BFD_RELOC_32, 0, 4, 32, false, 0, |
67 | | complain_overflow_bitfield, |
68 | | NULL, "32", |
69 | | false, 0xffffffff, 0xffffffff, false), |
70 | | HOWTO(BFD_RELOC_16, 0, 2, 16, false, 0, |
71 | | complain_overflow_bitfield, |
72 | | NULL, "16", |
73 | | false, 0xffff, 0xffff, false), |
74 | | HOWTO(BFD_RELOC_8, 0, 1, 8, false, 0, |
75 | | complain_overflow_bitfield, |
76 | | NULL, "8", |
77 | | false, 0xff, 0xff, false), |
78 | | HOWTO(BFD_RELOC_32_PCREL, 0, 4, 32, true, 0, |
79 | | complain_overflow_bitfield, |
80 | | NULL, "DISP32", |
81 | | false, 0xffffffff, 0xffffffff, true), |
82 | | /* 4 */ |
83 | | HOWTO(BFD_RELOC_16_PCREL, 0, 2, 16, true, 0, |
84 | | complain_overflow_bitfield, |
85 | | NULL, "DISP16", |
86 | | false, 0xffff, 0xffff, true), |
87 | | HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 4, 32, false, 0, |
88 | | complain_overflow_bitfield, |
89 | | NULL, "SECTDIFF_32", |
90 | | false, 0xffffffff, 0xffffffff, false), |
91 | | HOWTO(BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 4, 32, false, 0, |
92 | | complain_overflow_bitfield, |
93 | | NULL, "LSECTDIFF_32", |
94 | | false, 0xffffffff, 0xffffffff, false), |
95 | | HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 4, 32, false, 0, |
96 | | complain_overflow_bitfield, |
97 | | NULL, "PAIR_32", |
98 | | false, 0xffffffff, 0xffffffff, false), |
99 | | /* 8 */ |
100 | | HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 16, false, 0, |
101 | | complain_overflow_bitfield, |
102 | | NULL, "SECTDIFF_16", |
103 | | false, 0xffff, 0xffff, false), |
104 | | HOWTO(BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 2, 16, false, 0, |
105 | | complain_overflow_bitfield, |
106 | | NULL, "LSECTDIFF_16", |
107 | | false, 0xffff, 0xffff, false), |
108 | | HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 16, false, 0, |
109 | | complain_overflow_bitfield, |
110 | | NULL, "PAIR_16", |
111 | | false, 0xffff, 0xffff, false), |
112 | | }; |
113 | | |
114 | | static bool |
115 | | bfd_mach_o_i386_canonicalize_one_reloc (bfd * abfd, |
116 | | struct mach_o_reloc_info_external * raw, |
117 | | arelent * res, |
118 | | asymbol ** syms, |
119 | | arelent * res_base) |
120 | 5.48M | { |
121 | 5.48M | bfd_mach_o_reloc_info reloc; |
122 | | |
123 | 5.48M | if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms)) |
124 | 10.4k | return false; |
125 | | |
126 | 5.47M | if (reloc.r_scattered) |
127 | 5.41M | { |
128 | 5.41M | switch (reloc.r_type) |
129 | 5.41M | { |
130 | 1.56k | case BFD_MACH_O_GENERIC_RELOC_PAIR: |
131 | | /* PR 21813: Check for a corrupt PAIR reloc at the start. */ |
132 | 1.56k | if (res == res_base) |
133 | 266 | return false; |
134 | 1.29k | if (reloc.r_length == 2) |
135 | 603 | { |
136 | 603 | res->howto = &i386_howto_table[7]; |
137 | 603 | res->address = res[-1].address; |
138 | 603 | return true; |
139 | 603 | } |
140 | 691 | else if (reloc.r_length == 1) |
141 | 432 | { |
142 | 432 | res->howto = &i386_howto_table[10]; |
143 | 432 | res->address = res[-1].address; |
144 | 432 | return true; |
145 | 432 | } |
146 | 259 | return false; |
147 | 5.40M | case BFD_MACH_O_GENERIC_RELOC_SECTDIFF: |
148 | 5.40M | if (reloc.r_length == 2) |
149 | 5.36M | { |
150 | 5.36M | res->howto = &i386_howto_table[5]; |
151 | 5.36M | return true; |
152 | 5.36M | } |
153 | 31.2k | else if (reloc.r_length == 1) |
154 | 30.8k | { |
155 | 30.8k | res->howto = &i386_howto_table[8]; |
156 | 30.8k | return true; |
157 | 30.8k | } |
158 | 354 | return false; |
159 | 2.12k | case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF: |
160 | 2.12k | if (reloc.r_length == 2) |
161 | 1.63k | { |
162 | 1.63k | res->howto = &i386_howto_table[6]; |
163 | 1.63k | return true; |
164 | 1.63k | } |
165 | 498 | else if (reloc.r_length == 1) |
166 | 253 | { |
167 | 253 | res->howto = &i386_howto_table[9]; |
168 | 253 | return true; |
169 | 253 | } |
170 | 245 | return false; |
171 | 12.9k | default: |
172 | 12.9k | break; |
173 | 5.41M | } |
174 | 5.41M | } |
175 | 52.6k | else |
176 | 52.6k | { |
177 | 52.6k | switch (reloc.r_type) |
178 | 52.6k | { |
179 | 48.1k | case BFD_MACH_O_GENERIC_RELOC_VANILLA: |
180 | 48.1k | switch ((reloc.r_length << 1) | reloc.r_pcrel) |
181 | 48.1k | { |
182 | 41.9k | case 0: /* len = 0, pcrel = 0 */ |
183 | 41.9k | res->howto = &i386_howto_table[2]; |
184 | 41.9k | return true; |
185 | 457 | case 2: /* len = 1, pcrel = 0 */ |
186 | 457 | res->howto = &i386_howto_table[1]; |
187 | 457 | return true; |
188 | 3.15k | case 3: /* len = 1, pcrel = 1 */ |
189 | 3.15k | res->howto = &i386_howto_table[4]; |
190 | 3.15k | return true; |
191 | 232 | case 4: /* len = 2, pcrel = 0 */ |
192 | 232 | res->howto = &i386_howto_table[0]; |
193 | 232 | return true; |
194 | 418 | case 5: /* len = 2, pcrel = 1 */ |
195 | 418 | res->howto = &i386_howto_table[3]; |
196 | 418 | return true; |
197 | 1.96k | default: |
198 | 1.96k | return false; |
199 | 48.1k | } |
200 | 4.52k | default: |
201 | 4.52k | break; |
202 | 52.6k | } |
203 | 52.6k | } |
204 | 17.4k | return false; |
205 | 5.47M | } |
206 | | |
207 | | static bool |
208 | | bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo) |
209 | 0 | { |
210 | 0 | rinfo->r_address = rel->address; |
211 | 0 | switch (rel->howto->type) |
212 | 0 | { |
213 | 0 | case BFD_RELOC_32: |
214 | 0 | case BFD_RELOC_32_PCREL: |
215 | 0 | case BFD_RELOC_16: |
216 | 0 | case BFD_RELOC_16_PCREL: |
217 | 0 | case BFD_RELOC_8: |
218 | 0 | rinfo->r_scattered = 0; |
219 | 0 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA; |
220 | 0 | rinfo->r_pcrel = rel->howto->pc_relative; |
221 | 0 | rinfo->r_length = bfd_log2 (bfd_get_reloc_size (rel->howto)); |
222 | 0 | if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) |
223 | 0 | { |
224 | 0 | rinfo->r_extern = 0; |
225 | 0 | rinfo->r_value = |
226 | 0 | (*rel->sym_ptr_ptr)->section->output_section->target_index; |
227 | 0 | } |
228 | 0 | else |
229 | 0 | { |
230 | 0 | rinfo->r_extern = 1; |
231 | 0 | rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i; |
232 | 0 | } |
233 | 0 | break; |
234 | 0 | case BFD_RELOC_MACH_O_SECTDIFF: |
235 | 0 | rinfo->r_scattered = 1; |
236 | 0 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF; |
237 | 0 | rinfo->r_pcrel = 0; |
238 | 0 | rinfo->r_length = bfd_log2 (bfd_get_reloc_size (rel->howto)); |
239 | 0 | rinfo->r_extern = 0; |
240 | 0 | rinfo->r_value = rel->addend; |
241 | 0 | break; |
242 | 0 | case BFD_RELOC_MACH_O_LOCAL_SECTDIFF: |
243 | 0 | rinfo->r_scattered = 1; |
244 | 0 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF; |
245 | 0 | rinfo->r_pcrel = 0; |
246 | 0 | rinfo->r_length = bfd_log2 (bfd_get_reloc_size (rel->howto)); |
247 | 0 | rinfo->r_extern = 0; |
248 | 0 | rinfo->r_value = rel->addend; |
249 | 0 | break; |
250 | 0 | case BFD_RELOC_MACH_O_PAIR: |
251 | 0 | rinfo->r_address = 0; |
252 | 0 | rinfo->r_scattered = 1; |
253 | 0 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR; |
254 | 0 | rinfo->r_pcrel = 0; |
255 | 0 | rinfo->r_length = bfd_log2 (bfd_get_reloc_size (rel->howto)); |
256 | 0 | rinfo->r_extern = 0; |
257 | 0 | rinfo->r_value = rel->addend; |
258 | 0 | break; |
259 | 0 | default: |
260 | 0 | return false; |
261 | 0 | } |
262 | 0 | return true; |
263 | 0 | } |
264 | | |
265 | | static reloc_howto_type * |
266 | | bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
267 | | bfd_reloc_code_real_type code) |
268 | 0 | { |
269 | 0 | unsigned int i; |
270 | |
|
271 | 0 | for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++) |
272 | 0 | if (code == i386_howto_table[i].type) |
273 | 0 | return &i386_howto_table[i]; |
274 | 0 | return NULL; |
275 | 0 | } |
276 | | |
277 | | static reloc_howto_type * |
278 | | bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
279 | | const char *name ATTRIBUTE_UNUSED) |
280 | 0 | { |
281 | 0 | return NULL; |
282 | 0 | } |
283 | | |
284 | | static bool |
285 | | bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread, |
286 | | void *vfile, char *buf) |
287 | 0 | { |
288 | 0 | FILE *file = (FILE *)vfile; |
289 | |
|
290 | 0 | switch (thread->flavour) |
291 | 0 | { |
292 | 0 | case BFD_MACH_O_x86_THREAD_STATE: |
293 | 0 | if (thread->size < (8 + 16 * 4)) |
294 | 0 | return false; |
295 | 0 | fprintf (file, " x86_THREAD_STATE:\n"); |
296 | 0 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", |
297 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 0), |
298 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 4)); |
299 | 0 | fprintf (file, " eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", |
300 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 8), |
301 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 12), |
302 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 16), |
303 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 20)); |
304 | 0 | fprintf (file, " edi: %08lx esi: %08lx ebp: %08lx esp: %08lx\n", |
305 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 24), |
306 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 28), |
307 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 32), |
308 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 36)); |
309 | 0 | fprintf (file, " ss: %08lx flg: %08lx eip: %08lx cs: %08lx\n", |
310 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 40), |
311 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 44), |
312 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 48), |
313 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 52)); |
314 | 0 | fprintf (file, " ds: %08lx es: %08lx fs: %08lx gs: %08lx\n", |
315 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 56), |
316 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 60), |
317 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 64), |
318 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 68)); |
319 | 0 | return true; |
320 | 0 | case BFD_MACH_O_x86_FLOAT_STATE: |
321 | 0 | if (thread->size < 8) |
322 | 0 | return false; |
323 | 0 | fprintf (file, " x86_FLOAT_STATE:\n"); |
324 | 0 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", |
325 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 0), |
326 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 4)); |
327 | 0 | return true; |
328 | 0 | case BFD_MACH_O_x86_EXCEPTION_STATE: |
329 | 0 | if (thread->size < 8 + 3 * 4) |
330 | 0 | return false; |
331 | 0 | fprintf (file, " x86_EXCEPTION_STATE:\n"); |
332 | 0 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", |
333 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 0), |
334 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 4)); |
335 | 0 | fprintf (file, " trapno: %08lx err: %08lx faultaddr: %08lx\n", |
336 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 8), |
337 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 12), |
338 | 0 | (unsigned long)bfd_get_32 (abfd, buf + 16)); |
339 | 0 | return true; |
340 | 0 | default: |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | return false; |
344 | 0 | } |
345 | | |
346 | | static const mach_o_section_name_xlat text_section_names_xlat[] = |
347 | | { |
348 | | { ".symbol_stub", "__symbol_stub", |
349 | | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
350 | | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, |
351 | | 0}, |
352 | | { ".picsymbol_stub", "__picsymbol_stub", |
353 | | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
354 | | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, |
355 | | 0}, |
356 | | { NULL, NULL, 0, 0, 0, 0} |
357 | | }; |
358 | | |
359 | | static const mach_o_section_name_xlat data_section_names_xlat[] = |
360 | | { |
361 | | /* The first two are recognized by i386, but not emitted for x86 by |
362 | | modern GCC. */ |
363 | | { ".non_lazy_symbol_pointer", "__nl_symbol_ptr", |
364 | | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS, |
365 | | BFD_MACH_O_S_ATTR_NONE, 2}, |
366 | | { ".lazy_symbol_pointer", "__la_symbol_ptr", |
367 | | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, |
368 | | BFD_MACH_O_S_ATTR_NONE, 2}, |
369 | | { ".lazy_symbol_pointer2", "__la_sym_ptr2", |
370 | | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, |
371 | | BFD_MACH_O_S_ATTR_NONE, 2}, |
372 | | { ".lazy_symbol_pointer3", "__la_sym_ptr3", |
373 | | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, |
374 | | BFD_MACH_O_S_ATTR_NONE, 2}, |
375 | | { NULL, NULL, 0, 0, 0, 0} |
376 | | }; |
377 | | |
378 | | static const mach_o_section_name_xlat import_section_names_xlat[] = |
379 | | { |
380 | | { ".picsymbol_stub3", "__jump_table", |
381 | | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
382 | | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS |
383 | | | BFD_MACH_O_S_SELF_MODIFYING_CODE, |
384 | | 6}, |
385 | | { ".non_lazy_symbol_pointer_x86", "__pointers", |
386 | | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS, |
387 | | BFD_MACH_O_S_ATTR_NONE, 2}, |
388 | | { NULL, NULL, 0, 0, 0, 0} |
389 | | }; |
390 | | |
391 | | const mach_o_segment_name_xlat mach_o_i386_segsec_names_xlat[] = |
392 | | { |
393 | | { "__TEXT", text_section_names_xlat }, |
394 | | { "__DATA", data_section_names_xlat }, |
395 | | { "__IMPORT", import_section_names_xlat }, |
396 | | { NULL, NULL } |
397 | | }; |
398 | | |
399 | | #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_i386_canonicalize_one_reloc |
400 | | #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out |
401 | | #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread |
402 | | |
403 | | #define bfd_mach_o_tgt_seg_table mach_o_i386_segsec_names_xlat |
404 | | #define bfd_mach_o_section_type_valid_for_tgt NULL |
405 | | |
406 | | #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup |
407 | | #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup |
408 | | |
409 | | #define TARGET_NAME i386_mach_o_vec |
410 | | #define TARGET_STRING "mach-o-i386" |
411 | | #define TARGET_ARCHITECTURE bfd_arch_i386 |
412 | | #define TARGET_PAGESIZE 4096 |
413 | | #define TARGET_BIG_ENDIAN 0 |
414 | | #define TARGET_ARCHIVE 0 |
415 | | #define TARGET_PRIORITY 0 |
416 | | #include "mach-o-target.c" |