1 /* Intel i386 Mach-O support for BFD.
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "mach-o/reloc.h"
29 #define bfd_mach_o_object_p bfd_mach_o_i386_object_p
30 #define bfd_mach_o_core_p bfd_mach_o_i386_core_p
31 #define bfd_mach_o_mkobject bfd_mach_o_i386_mkobject
33 static const bfd_target *
34 bfd_mach_o_i386_object_p (bfd *abfd)
36 return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_CPU_TYPE_I386);
39 static const bfd_target *
40 bfd_mach_o_i386_core_p (bfd *abfd)
42 return bfd_mach_o_header_p (abfd,
43 BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_I386);
47 bfd_mach_o_i386_mkobject (bfd *abfd)
49 bfd_mach_o_data_struct *mdata;
51 if (!bfd_mach_o_mkobject_init (abfd))
54 mdata = bfd_mach_o_get_data (abfd);
55 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
56 mdata->header.cputype = BFD_MACH_O_CPU_TYPE_I386;
57 mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL;
58 mdata->header.byteorder = BFD_ENDIAN_LITTLE;
59 mdata->header.version = 1;
64 static reloc_howto_type i386_howto_table[]=
67 HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0,
68 complain_overflow_bitfield,
70 FALSE, 0xffffffff, 0xffffffff, FALSE),
71 HOWTO(BFD_RELOC_16, 0, 1, 16, FALSE, 0,
72 complain_overflow_bitfield,
74 FALSE, 0xffff, 0xffff, FALSE),
75 HOWTO(BFD_RELOC_8, 0, 0, 8, FALSE, 0,
76 complain_overflow_bitfield,
78 FALSE, 0xff, 0xff, FALSE),
79 HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0,
80 complain_overflow_bitfield,
82 FALSE, 0xffffffff, 0xffffffff, TRUE),
84 HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0,
85 complain_overflow_bitfield,
87 FALSE, 0xffff, 0xffff, TRUE),
88 HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0,
89 complain_overflow_bitfield,
91 FALSE, 0xffffffff, 0xffffffff, FALSE),
92 HOWTO(BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 2, 32, FALSE, 0,
93 complain_overflow_bitfield,
95 FALSE, 0xffffffff, 0xffffffff, FALSE),
96 HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0,
97 complain_overflow_bitfield,
99 FALSE, 0xffffffff, 0xffffffff, FALSE),
103 bfd_mach_o_i386_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc)
105 if (reloc->r_scattered)
107 switch (reloc->r_type)
109 case BFD_MACH_O_GENERIC_RELOC_PAIR:
110 if (reloc->r_length != 2)
112 res->howto = &i386_howto_table[7];
113 res->address = res[-1].address;
115 case BFD_MACH_O_GENERIC_RELOC_SECTDIFF:
116 if (reloc->r_length != 2)
118 res->howto = &i386_howto_table[5];
120 case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF:
121 if (reloc->r_length != 2)
123 res->howto = &i386_howto_table[6];
131 switch (reloc->r_type)
133 case BFD_MACH_O_GENERIC_RELOC_VANILLA:
134 switch ((reloc->r_length << 1) | reloc->r_pcrel)
136 case 0: /* len = 0, pcrel = 0 */
137 res->howto = &i386_howto_table[2];
139 case 2: /* len = 1, pcrel = 0 */
140 res->howto = &i386_howto_table[1];
142 case 3: /* len = 1, pcrel = 1 */
143 res->howto = &i386_howto_table[4];
145 case 4: /* len = 2, pcrel = 0 */
146 res->howto = &i386_howto_table[0];
148 case 5: /* len = 2, pcrel = 1 */
149 res->howto = &i386_howto_table[3];
162 bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
164 rinfo->r_address = rel->address;
165 switch (rel->howto->type)
168 case BFD_RELOC_32_PCREL:
170 case BFD_RELOC_16_PCREL:
172 rinfo->r_scattered = 0;
173 rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA;
174 rinfo->r_pcrel = rel->howto->pc_relative;
175 rinfo->r_length = rel->howto->size; /* Correct in practice. */
176 if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
179 rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index;
184 rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
187 case BFD_RELOC_MACH_O_SECTDIFF:
188 rinfo->r_scattered = 1;
189 rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF;
193 rinfo->r_value = (*rel->sym_ptr_ptr)->value
194 + (*rel->sym_ptr_ptr)->section->vma;
196 case BFD_RELOC_MACH_O_LOCAL_SECTDIFF:
197 rinfo->r_scattered = 1;
198 rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF;
202 rinfo->r_value = (*rel->sym_ptr_ptr)->value
203 + (*rel->sym_ptr_ptr)->section->vma;
205 case BFD_RELOC_MACH_O_PAIR:
206 rinfo->r_address = 0;
207 rinfo->r_scattered = 1;
208 rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR;
212 rinfo->r_value = (*rel->sym_ptr_ptr)->value
213 + (*rel->sym_ptr_ptr)->section->vma;
221 static reloc_howto_type *
222 bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
223 bfd_reloc_code_real_type code)
227 for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++)
228 if (code == i386_howto_table[i].type)
229 return &i386_howto_table[i];
233 static reloc_howto_type *
234 bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
235 const char *name ATTRIBUTE_UNUSED)
241 bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread,
242 void *vfile, char *buf)
244 FILE *file = (FILE *)vfile;
246 switch (thread->flavour)
248 case BFD_MACH_O_x86_THREAD_STATE:
249 if (thread->size < (8 + 16 * 4))
251 fprintf (file, " x86_THREAD_STATE:\n");
252 fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
253 (unsigned long)bfd_get_32 (abfd, buf + 0),
254 (unsigned long)bfd_get_32 (abfd, buf + 4));
255 fprintf (file, " eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
256 (unsigned long)bfd_get_32 (abfd, buf + 8),
257 (unsigned long)bfd_get_32 (abfd, buf + 12),
258 (unsigned long)bfd_get_32 (abfd, buf + 16),
259 (unsigned long)bfd_get_32 (abfd, buf + 20));
260 fprintf (file, " edi: %08lx esi: %08lx ebp: %08lx esp: %08lx\n",
261 (unsigned long)bfd_get_32 (abfd, buf + 24),
262 (unsigned long)bfd_get_32 (abfd, buf + 28),
263 (unsigned long)bfd_get_32 (abfd, buf + 32),
264 (unsigned long)bfd_get_32 (abfd, buf + 36));
265 fprintf (file, " ss: %08lx flg: %08lx eip: %08lx cs: %08lx\n",
266 (unsigned long)bfd_get_32 (abfd, buf + 40),
267 (unsigned long)bfd_get_32 (abfd, buf + 44),
268 (unsigned long)bfd_get_32 (abfd, buf + 48),
269 (unsigned long)bfd_get_32 (abfd, buf + 52));
270 fprintf (file, " ds: %08lx es: %08lx fs: %08lx gs: %08lx\n",
271 (unsigned long)bfd_get_32 (abfd, buf + 56),
272 (unsigned long)bfd_get_32 (abfd, buf + 60),
273 (unsigned long)bfd_get_32 (abfd, buf + 64),
274 (unsigned long)bfd_get_32 (abfd, buf + 68));
276 case BFD_MACH_O_x86_FLOAT_STATE:
277 if (thread->size < 8)
279 fprintf (file, " x86_FLOAT_STATE:\n");
280 fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
281 (unsigned long)bfd_get_32 (abfd, buf + 0),
282 (unsigned long)bfd_get_32 (abfd, buf + 4));
284 case BFD_MACH_O_x86_EXCEPTION_STATE:
285 if (thread->size < 8 + 3 * 4)
287 fprintf (file, " x86_EXCEPTION_STATE:\n");
288 fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
289 (unsigned long)bfd_get_32 (abfd, buf + 0),
290 (unsigned long)bfd_get_32 (abfd, buf + 4));
291 fprintf (file, " trapno: %08lx err: %08lx faultaddr: %08lx\n",
292 (unsigned long)bfd_get_32 (abfd, buf + 8),
293 (unsigned long)bfd_get_32 (abfd, buf + 12),
294 (unsigned long)bfd_get_32 (abfd, buf + 16));
302 static const mach_o_section_name_xlat text_section_names_xlat[] =
304 { ".symbol_stub", "__symbol_stub",
305 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS,
306 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
308 { ".picsymbol_stub", "__picsymbol_stub",
309 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS,
310 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
312 { NULL, NULL, 0, 0, 0, 0}
315 static const mach_o_section_name_xlat data_section_names_xlat[] =
317 /* The first two are recognized by i386, but not emitted for x86 by
319 { ".non_lazy_symbol_pointer", "__nl_symbol_ptr",
320 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
321 BFD_MACH_O_S_ATTR_NONE, 2},
322 { ".lazy_symbol_pointer", "__la_symbol_ptr",
323 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
324 BFD_MACH_O_S_ATTR_NONE, 2},
325 { ".lazy_symbol_pointer2", "__la_sym_ptr2",
326 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
327 BFD_MACH_O_S_ATTR_NONE, 2},
328 { ".lazy_symbol_pointer3", "__la_sym_ptr3",
329 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
330 BFD_MACH_O_S_ATTR_NONE, 2},
331 { NULL, NULL, 0, 0, 0, 0}
334 static const mach_o_section_name_xlat import_section_names_xlat[] =
336 { ".picsymbol_stub3", "__jump_table",
337 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_SYMBOL_STUBS,
338 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
339 | BFD_MACH_O_S_SELF_MODIFYING_CODE,
341 { ".non_lazy_symbol_pointer_x86", "__pointers",
342 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
343 BFD_MACH_O_S_ATTR_NONE, 2},
344 { NULL, NULL, 0, 0, 0, 0}
347 const mach_o_segment_name_xlat mach_o_i386_segsec_names_xlat[] =
349 { "__TEXT", text_section_names_xlat },
350 { "__DATA", data_section_names_xlat },
351 { "__IMPORT", import_section_names_xlat },
355 #define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
356 #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
357 #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread
359 #define bfd_mach_o_tgt_seg_table mach_o_i386_segsec_names_xlat
360 #define bfd_mach_o_section_type_valid_for_tgt NULL
362 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup
363 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
365 #define TARGET_NAME mach_o_i386_vec
366 #define TARGET_STRING "mach-o-i386"
367 #define TARGET_ARCHITECTURE bfd_arch_i386
368 #define TARGET_BIG_ENDIAN 0
369 #define TARGET_ARCHIVE 0
370 #define TARGET_PRIORITY 0
371 #include "mach-o-target.c"