Commit | Line | Data |
---|---|---|
154a1ee5 | 1 | /* Intel i386 Mach-O support for BFD. |
b90efa5b | 2 | Copyright (C) 2009-2015 Free Software Foundation, Inc. |
154a1ee5 TG |
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 "mach-o.h" | |
23 | #include "bfd.h" | |
24 | #include "libbfd.h" | |
25 | #include "libiberty.h" | |
46d1c23b | 26 | #include "mach-o/reloc.h" |
154a1ee5 TG |
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 const bfd_target * | |
33 | bfd_mach_o_i386_object_p (bfd *abfd) | |
34 | { | |
47daa70f | 35 | return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_I386); |
154a1ee5 TG |
36 | } |
37 | ||
38 | static const bfd_target * | |
39 | bfd_mach_o_i386_core_p (bfd *abfd) | |
40 | { | |
47daa70f | 41 | return bfd_mach_o_header_p (abfd, 0, |
154a1ee5 TG |
42 | BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_I386); |
43 | } | |
44 | ||
45 | static bfd_boolean | |
46 | bfd_mach_o_i386_mkobject (bfd *abfd) | |
47 | { | |
48 | bfd_mach_o_data_struct *mdata; | |
49 | ||
50 | if (!bfd_mach_o_mkobject_init (abfd)) | |
51 | return FALSE; | |
52 | ||
046b007d | 53 | mdata = bfd_mach_o_get_data (abfd); |
154a1ee5 TG |
54 | mdata->header.magic = BFD_MACH_O_MH_MAGIC; |
55 | mdata->header.cputype = BFD_MACH_O_CPU_TYPE_I386; | |
56 | mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL; | |
154a1ee5 TG |
57 | mdata->header.byteorder = BFD_ENDIAN_LITTLE; |
58 | mdata->header.version = 1; | |
59 | ||
60 | return TRUE; | |
61 | } | |
62 | ||
92bc0e80 TG |
63 | static reloc_howto_type i386_howto_table[]= |
64 | { | |
e1e81ed3 | 65 | /* 0 */ |
92bc0e80 TG |
66 | HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0, |
67 | complain_overflow_bitfield, | |
68 | NULL, "32", | |
69 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
70 | HOWTO(BFD_RELOC_16, 0, 1, 16, FALSE, 0, | |
71 | complain_overflow_bitfield, | |
72 | NULL, "16", | |
73 | FALSE, 0xffff, 0xffff, FALSE), | |
74 | HOWTO(BFD_RELOC_8, 0, 0, 8, FALSE, 0, | |
75 | complain_overflow_bitfield, | |
76 | NULL, "8", | |
77 | FALSE, 0xff, 0xff, FALSE), | |
78 | HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0, | |
79 | complain_overflow_bitfield, | |
80 | NULL, "DISP32", | |
81 | FALSE, 0xffffffff, 0xffffffff, TRUE), | |
e1e81ed3 | 82 | /* 4 */ |
92bc0e80 TG |
83 | HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0, |
84 | complain_overflow_bitfield, | |
85 | NULL, "DISP16", | |
86 | FALSE, 0xffff, 0xffff, TRUE), | |
87 | HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0, | |
88 | complain_overflow_bitfield, | |
89 | NULL, "SECTDIFF_32", | |
90 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
e1e81ed3 IS |
91 | HOWTO(BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 2, 32, FALSE, 0, |
92 | complain_overflow_bitfield, | |
93 | NULL, "LSECTDIFF_32", | |
94 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
92bc0e80 TG |
95 | HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0, |
96 | complain_overflow_bitfield, | |
97 | NULL, "PAIR_32", | |
98 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
6639d37a IS |
99 | /* 8 */ |
100 | HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 1, 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, 1, 16, FALSE, 0, | |
105 | complain_overflow_bitfield, | |
106 | NULL, "LSECTDIFF_16", | |
107 | FALSE, 0xffff, 0xffff, FALSE), | |
108 | HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 1, 16, FALSE, 0, | |
109 | complain_overflow_bitfield, | |
110 | NULL, "PAIR_16", | |
111 | FALSE, 0xffff, 0xffff, FALSE), | |
92bc0e80 TG |
112 | }; |
113 | ||
114 | static bfd_boolean | |
bcb51645 TG |
115 | bfd_mach_o_i386_canonicalize_one_reloc (bfd *abfd, |
116 | struct mach_o_reloc_info_external *raw, | |
117 | arelent *res, asymbol **syms) | |
92bc0e80 | 118 | { |
bcb51645 TG |
119 | bfd_mach_o_reloc_info reloc; |
120 | ||
121 | if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms)) | |
122 | return FALSE; | |
123 | ||
124 | if (reloc.r_scattered) | |
92bc0e80 | 125 | { |
bcb51645 | 126 | switch (reloc.r_type) |
92bc0e80 TG |
127 | { |
128 | case BFD_MACH_O_GENERIC_RELOC_PAIR: | |
bcb51645 | 129 | if (reloc.r_length == 2) |
6639d37a IS |
130 | { |
131 | res->howto = &i386_howto_table[7]; | |
132 | res->address = res[-1].address; | |
133 | return TRUE; | |
134 | } | |
bcb51645 | 135 | else if (reloc.r_length == 1) |
6639d37a IS |
136 | { |
137 | res->howto = &i386_howto_table[10]; | |
138 | res->address = res[-1].address; | |
139 | return TRUE; | |
140 | } | |
141 | return FALSE; | |
92bc0e80 | 142 | case BFD_MACH_O_GENERIC_RELOC_SECTDIFF: |
bcb51645 | 143 | if (reloc.r_length == 2) |
6639d37a IS |
144 | { |
145 | res->howto = &i386_howto_table[5]; | |
146 | return TRUE; | |
147 | } | |
bcb51645 | 148 | else if (reloc.r_length == 1) |
6639d37a IS |
149 | { |
150 | res->howto = &i386_howto_table[8]; | |
151 | return TRUE; | |
152 | } | |
153 | return FALSE; | |
e1e81ed3 | 154 | case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF: |
bcb51645 | 155 | if (reloc.r_length == 2) |
6639d37a IS |
156 | { |
157 | res->howto = &i386_howto_table[6]; | |
158 | return TRUE; | |
159 | } | |
bcb51645 | 160 | else if (reloc.r_length == 1) |
6639d37a IS |
161 | { |
162 | res->howto = &i386_howto_table[9]; | |
163 | return TRUE; | |
164 | } | |
165 | return FALSE; | |
92bc0e80 TG |
166 | default: |
167 | return FALSE; | |
168 | } | |
169 | } | |
170 | else | |
171 | { | |
bcb51645 | 172 | switch (reloc.r_type) |
92bc0e80 TG |
173 | { |
174 | case BFD_MACH_O_GENERIC_RELOC_VANILLA: | |
bcb51645 | 175 | switch ((reloc.r_length << 1) | reloc.r_pcrel) |
92bc0e80 TG |
176 | { |
177 | case 0: /* len = 0, pcrel = 0 */ | |
178 | res->howto = &i386_howto_table[2]; | |
179 | return TRUE; | |
180 | case 2: /* len = 1, pcrel = 0 */ | |
181 | res->howto = &i386_howto_table[1]; | |
182 | return TRUE; | |
183 | case 3: /* len = 1, pcrel = 1 */ | |
184 | res->howto = &i386_howto_table[4]; | |
185 | return TRUE; | |
186 | case 4: /* len = 2, pcrel = 0 */ | |
187 | res->howto = &i386_howto_table[0]; | |
188 | return TRUE; | |
189 | case 5: /* len = 2, pcrel = 1 */ | |
190 | res->howto = &i386_howto_table[3]; | |
191 | return TRUE; | |
192 | default: | |
193 | return FALSE; | |
194 | } | |
195 | break; | |
196 | default: | |
197 | return FALSE; | |
198 | } | |
199 | } | |
200 | } | |
201 | ||
202 | static bfd_boolean | |
203 | bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo) | |
204 | { | |
205 | rinfo->r_address = rel->address; | |
206 | switch (rel->howto->type) | |
207 | { | |
208 | case BFD_RELOC_32: | |
209 | case BFD_RELOC_32_PCREL: | |
210 | case BFD_RELOC_16: | |
211 | case BFD_RELOC_16_PCREL: | |
212 | case BFD_RELOC_8: | |
213 | rinfo->r_scattered = 0; | |
214 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA; | |
215 | rinfo->r_pcrel = rel->howto->pc_relative; | |
216 | rinfo->r_length = rel->howto->size; /* Correct in practice. */ | |
217 | if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) | |
218 | { | |
219 | rinfo->r_extern = 0; | |
47c50932 TG |
220 | rinfo->r_value = |
221 | (*rel->sym_ptr_ptr)->section->output_section->target_index; | |
92bc0e80 TG |
222 | } |
223 | else | |
224 | { | |
225 | rinfo->r_extern = 1; | |
226 | rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i; | |
227 | } | |
228 | break; | |
229 | case BFD_RELOC_MACH_O_SECTDIFF: | |
230 | rinfo->r_scattered = 1; | |
231 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF; | |
232 | rinfo->r_pcrel = 0; | |
6639d37a | 233 | rinfo->r_length = rel->howto->size; |
92bc0e80 | 234 | rinfo->r_extern = 0; |
6639d37a | 235 | rinfo->r_value = rel->addend; |
92bc0e80 | 236 | break; |
e1e81ed3 IS |
237 | case BFD_RELOC_MACH_O_LOCAL_SECTDIFF: |
238 | rinfo->r_scattered = 1; | |
239 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF; | |
240 | rinfo->r_pcrel = 0; | |
6639d37a | 241 | rinfo->r_length = rel->howto->size; |
e1e81ed3 | 242 | rinfo->r_extern = 0; |
6639d37a | 243 | rinfo->r_value = rel->addend; |
e1e81ed3 | 244 | break; |
92bc0e80 TG |
245 | case BFD_RELOC_MACH_O_PAIR: |
246 | rinfo->r_address = 0; | |
247 | rinfo->r_scattered = 1; | |
248 | rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR; | |
249 | rinfo->r_pcrel = 0; | |
6639d37a | 250 | rinfo->r_length = rel->howto->size; |
92bc0e80 | 251 | rinfo->r_extern = 0; |
6639d37a | 252 | rinfo->r_value = rel->addend; |
92bc0e80 TG |
253 | break; |
254 | default: | |
255 | return FALSE; | |
256 | } | |
257 | return TRUE; | |
258 | } | |
259 | ||
260 | static reloc_howto_type * | |
261 | bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
262 | bfd_reloc_code_real_type code) | |
263 | { | |
264 | unsigned int i; | |
265 | ||
266 | for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++) | |
267 | if (code == i386_howto_table[i].type) | |
268 | return &i386_howto_table[i]; | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | static reloc_howto_type * | |
273 | bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
274 | const char *name ATTRIBUTE_UNUSED) | |
275 | { | |
276 | return NULL; | |
277 | } | |
278 | ||
b32e07d7 TG |
279 | static bfd_boolean |
280 | bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread, | |
281 | void *vfile, char *buf) | |
282 | { | |
283 | FILE *file = (FILE *)vfile; | |
284 | ||
285 | switch (thread->flavour) | |
286 | { | |
287 | case BFD_MACH_O_x86_THREAD_STATE: | |
288 | if (thread->size < (8 + 16 * 4)) | |
289 | return FALSE; | |
290 | fprintf (file, " x86_THREAD_STATE:\n"); | |
291 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", | |
292 | (unsigned long)bfd_get_32 (abfd, buf + 0), | |
293 | (unsigned long)bfd_get_32 (abfd, buf + 4)); | |
294 | fprintf (file, " eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", | |
295 | (unsigned long)bfd_get_32 (abfd, buf + 8), | |
296 | (unsigned long)bfd_get_32 (abfd, buf + 12), | |
297 | (unsigned long)bfd_get_32 (abfd, buf + 16), | |
298 | (unsigned long)bfd_get_32 (abfd, buf + 20)); | |
299 | fprintf (file, " edi: %08lx esi: %08lx ebp: %08lx esp: %08lx\n", | |
300 | (unsigned long)bfd_get_32 (abfd, buf + 24), | |
301 | (unsigned long)bfd_get_32 (abfd, buf + 28), | |
302 | (unsigned long)bfd_get_32 (abfd, buf + 32), | |
303 | (unsigned long)bfd_get_32 (abfd, buf + 36)); | |
304 | fprintf (file, " ss: %08lx flg: %08lx eip: %08lx cs: %08lx\n", | |
305 | (unsigned long)bfd_get_32 (abfd, buf + 40), | |
306 | (unsigned long)bfd_get_32 (abfd, buf + 44), | |
307 | (unsigned long)bfd_get_32 (abfd, buf + 48), | |
308 | (unsigned long)bfd_get_32 (abfd, buf + 52)); | |
309 | fprintf (file, " ds: %08lx es: %08lx fs: %08lx gs: %08lx\n", | |
310 | (unsigned long)bfd_get_32 (abfd, buf + 56), | |
311 | (unsigned long)bfd_get_32 (abfd, buf + 60), | |
312 | (unsigned long)bfd_get_32 (abfd, buf + 64), | |
313 | (unsigned long)bfd_get_32 (abfd, buf + 68)); | |
314 | return TRUE; | |
315 | case BFD_MACH_O_x86_FLOAT_STATE: | |
316 | if (thread->size < 8) | |
317 | return FALSE; | |
318 | fprintf (file, " x86_FLOAT_STATE:\n"); | |
319 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", | |
320 | (unsigned long)bfd_get_32 (abfd, buf + 0), | |
321 | (unsigned long)bfd_get_32 (abfd, buf + 4)); | |
322 | return TRUE; | |
323 | case BFD_MACH_O_x86_EXCEPTION_STATE: | |
324 | if (thread->size < 8 + 3 * 4) | |
325 | return FALSE; | |
326 | fprintf (file, " x86_EXCEPTION_STATE:\n"); | |
327 | fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n", | |
328 | (unsigned long)bfd_get_32 (abfd, buf + 0), | |
329 | (unsigned long)bfd_get_32 (abfd, buf + 4)); | |
330 | fprintf (file, " trapno: %08lx err: %08lx faultaddr: %08lx\n", | |
331 | (unsigned long)bfd_get_32 (abfd, buf + 8), | |
332 | (unsigned long)bfd_get_32 (abfd, buf + 12), | |
333 | (unsigned long)bfd_get_32 (abfd, buf + 16)); | |
334 | return TRUE; | |
335 | default: | |
336 | break; | |
337 | } | |
338 | return FALSE; | |
339 | } | |
340 | ||
a4551119 TG |
341 | static const mach_o_section_name_xlat text_section_names_xlat[] = |
342 | { | |
343 | { ".symbol_stub", "__symbol_stub", | |
632039e0 | 344 | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
a4551119 TG |
345 | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, |
346 | 0}, | |
347 | { ".picsymbol_stub", "__picsymbol_stub", | |
632039e0 | 348 | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
a4551119 TG |
349 | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, |
350 | 0}, | |
351 | { NULL, NULL, 0, 0, 0, 0} | |
352 | }; | |
353 | ||
354 | static const mach_o_section_name_xlat data_section_names_xlat[] = | |
355 | { | |
356 | /* The first two are recognized by i386, but not emitted for x86 by | |
357 | modern GCC. */ | |
358 | { ".non_lazy_symbol_pointer", "__nl_symbol_ptr", | |
359 | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS, | |
360 | BFD_MACH_O_S_ATTR_NONE, 2}, | |
361 | { ".lazy_symbol_pointer", "__la_symbol_ptr", | |
362 | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, | |
363 | BFD_MACH_O_S_ATTR_NONE, 2}, | |
364 | { ".lazy_symbol_pointer2", "__la_sym_ptr2", | |
365 | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, | |
366 | BFD_MACH_O_S_ATTR_NONE, 2}, | |
367 | { ".lazy_symbol_pointer3", "__la_sym_ptr3", | |
368 | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS, | |
369 | BFD_MACH_O_S_ATTR_NONE, 2}, | |
370 | { NULL, NULL, 0, 0, 0, 0} | |
371 | }; | |
372 | ||
373 | static const mach_o_section_name_xlat import_section_names_xlat[] = | |
374 | { | |
375 | { ".picsymbol_stub3", "__jump_table", | |
632039e0 | 376 | SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS, |
68ffbac6 | 377 | BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS |
a4551119 TG |
378 | | BFD_MACH_O_S_SELF_MODIFYING_CODE, |
379 | 6}, | |
380 | { ".non_lazy_symbol_pointer_x86", "__pointers", | |
381 | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS, | |
382 | BFD_MACH_O_S_ATTR_NONE, 2}, | |
383 | { NULL, NULL, 0, 0, 0, 0} | |
384 | }; | |
385 | ||
386 | const mach_o_segment_name_xlat mach_o_i386_segsec_names_xlat[] = | |
387 | { | |
388 | { "__TEXT", text_section_names_xlat }, | |
389 | { "__DATA", data_section_names_xlat }, | |
390 | { "__IMPORT", import_section_names_xlat }, | |
391 | { NULL, NULL } | |
392 | }; | |
393 | ||
bcb51645 | 394 | #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_i386_canonicalize_one_reloc |
92bc0e80 | 395 | #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out |
b32e07d7 | 396 | #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread |
92bc0e80 | 397 | |
a4551119 | 398 | #define bfd_mach_o_tgt_seg_table mach_o_i386_segsec_names_xlat |
ab76eeaf | 399 | #define bfd_mach_o_section_type_valid_for_tgt NULL |
a4551119 | 400 | |
68ffbac6 | 401 | #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup |
92bc0e80 TG |
402 | #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup |
403 | ||
6d00b590 | 404 | #define TARGET_NAME i386_mach_o_vec |
154a1ee5 | 405 | #define TARGET_STRING "mach-o-i386" |
42fa0891 | 406 | #define TARGET_ARCHITECTURE bfd_arch_i386 |
4384b284 | 407 | #define TARGET_PAGESIZE 4096 |
154a1ee5 TG |
408 | #define TARGET_BIG_ENDIAN 0 |
409 | #define TARGET_ARCHIVE 0 | |
b93a1992 | 410 | #define TARGET_PRIORITY 0 |
154a1ee5 | 411 | #include "mach-o-target.c" |