mach-o-i386.c revision 1.1 1 1.1 christos /* Intel i386 Mach-O support for BFD.
2 1.1 christos Copyright 2009
3 1.1 christos Free Software Foundation, Inc.
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 "mach-o.h"
24 1.1 christos #include "bfd.h"
25 1.1 christos #include "libbfd.h"
26 1.1 christos #include "libiberty.h"
27 1.1 christos
28 1.1 christos #define bfd_mach_o_object_p bfd_mach_o_i386_object_p
29 1.1 christos #define bfd_mach_o_core_p bfd_mach_o_i386_core_p
30 1.1 christos #define bfd_mach_o_mkobject bfd_mach_o_i386_mkobject
31 1.1 christos
32 1.1 christos static const bfd_target *
33 1.1 christos bfd_mach_o_i386_object_p (bfd *abfd)
34 1.1 christos {
35 1.1 christos return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_CPU_TYPE_I386);
36 1.1 christos }
37 1.1 christos
38 1.1 christos static const bfd_target *
39 1.1 christos bfd_mach_o_i386_core_p (bfd *abfd)
40 1.1 christos {
41 1.1 christos return bfd_mach_o_header_p (abfd,
42 1.1 christos BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_I386);
43 1.1 christos }
44 1.1 christos
45 1.1 christos static bfd_boolean
46 1.1 christos bfd_mach_o_i386_mkobject (bfd *abfd)
47 1.1 christos {
48 1.1 christos bfd_mach_o_data_struct *mdata;
49 1.1 christos
50 1.1 christos if (!bfd_mach_o_mkobject_init (abfd))
51 1.1 christos return FALSE;
52 1.1 christos
53 1.1 christos mdata = bfd_mach_o_get_data (abfd);
54 1.1 christos mdata->header.magic = BFD_MACH_O_MH_MAGIC;
55 1.1 christos mdata->header.cputype = BFD_MACH_O_CPU_TYPE_I386;
56 1.1 christos mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL;
57 1.1 christos mdata->header.byteorder = BFD_ENDIAN_LITTLE;
58 1.1 christos mdata->header.version = 1;
59 1.1 christos
60 1.1 christos return TRUE;
61 1.1 christos }
62 1.1 christos
63 1.1 christos static reloc_howto_type i386_howto_table[]=
64 1.1 christos {
65 1.1 christos HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0,
66 1.1 christos complain_overflow_bitfield,
67 1.1 christos NULL, "32",
68 1.1 christos FALSE, 0xffffffff, 0xffffffff, FALSE),
69 1.1 christos HOWTO(BFD_RELOC_16, 0, 1, 16, FALSE, 0,
70 1.1 christos complain_overflow_bitfield,
71 1.1 christos NULL, "16",
72 1.1 christos FALSE, 0xffff, 0xffff, FALSE),
73 1.1 christos HOWTO(BFD_RELOC_8, 0, 0, 8, FALSE, 0,
74 1.1 christos complain_overflow_bitfield,
75 1.1 christos NULL, "8",
76 1.1 christos FALSE, 0xff, 0xff, FALSE),
77 1.1 christos HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0,
78 1.1 christos complain_overflow_bitfield,
79 1.1 christos NULL, "DISP32",
80 1.1 christos FALSE, 0xffffffff, 0xffffffff, TRUE),
81 1.1 christos HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0,
82 1.1 christos complain_overflow_bitfield,
83 1.1 christos NULL, "DISP16",
84 1.1 christos FALSE, 0xffff, 0xffff, TRUE),
85 1.1 christos HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0,
86 1.1 christos complain_overflow_bitfield,
87 1.1 christos NULL, "SECTDIFF_32",
88 1.1 christos FALSE, 0xffffffff, 0xffffffff, FALSE),
89 1.1 christos HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0,
90 1.1 christos complain_overflow_bitfield,
91 1.1 christos NULL, "PAIR_32",
92 1.1 christos FALSE, 0xffffffff, 0xffffffff, FALSE),
93 1.1 christos };
94 1.1 christos
95 1.1 christos static bfd_boolean
96 1.1 christos bfd_mach_o_i386_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc)
97 1.1 christos {
98 1.1 christos if (reloc->r_scattered)
99 1.1 christos {
100 1.1 christos switch (reloc->r_type)
101 1.1 christos {
102 1.1 christos case BFD_MACH_O_GENERIC_RELOC_PAIR:
103 1.1 christos if (reloc->r_length != 2)
104 1.1 christos return FALSE;
105 1.1 christos res->howto = &i386_howto_table[6];
106 1.1 christos res->address = res[-1].address;
107 1.1 christos return TRUE;
108 1.1 christos case BFD_MACH_O_GENERIC_RELOC_SECTDIFF:
109 1.1 christos case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF:
110 1.1 christos if (reloc->r_length != 2)
111 1.1 christos return FALSE;
112 1.1 christos res->howto = &i386_howto_table[5];
113 1.1 christos return TRUE;
114 1.1 christos default:
115 1.1 christos return FALSE;
116 1.1 christos }
117 1.1 christos }
118 1.1 christos else
119 1.1 christos {
120 1.1 christos switch (reloc->r_type)
121 1.1 christos {
122 1.1 christos case BFD_MACH_O_GENERIC_RELOC_VANILLA:
123 1.1 christos switch ((reloc->r_length << 1) | reloc->r_pcrel)
124 1.1 christos {
125 1.1 christos case 0: /* len = 0, pcrel = 0 */
126 1.1 christos res->howto = &i386_howto_table[2];
127 1.1 christos return TRUE;
128 1.1 christos case 2: /* len = 1, pcrel = 0 */
129 1.1 christos res->howto = &i386_howto_table[1];
130 1.1 christos return TRUE;
131 1.1 christos case 3: /* len = 1, pcrel = 1 */
132 1.1 christos res->howto = &i386_howto_table[4];
133 1.1 christos return TRUE;
134 1.1 christos case 4: /* len = 2, pcrel = 0 */
135 1.1 christos res->howto = &i386_howto_table[0];
136 1.1 christos return TRUE;
137 1.1 christos case 5: /* len = 2, pcrel = 1 */
138 1.1 christos res->howto = &i386_howto_table[3];
139 1.1 christos return TRUE;
140 1.1 christos default:
141 1.1 christos return FALSE;
142 1.1 christos }
143 1.1 christos break;
144 1.1 christos default:
145 1.1 christos return FALSE;
146 1.1 christos }
147 1.1 christos }
148 1.1 christos }
149 1.1 christos
150 1.1 christos static bfd_boolean
151 1.1 christos bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
152 1.1 christos {
153 1.1 christos rinfo->r_address = rel->address;
154 1.1 christos switch (rel->howto->type)
155 1.1 christos {
156 1.1 christos case BFD_RELOC_32:
157 1.1 christos case BFD_RELOC_32_PCREL:
158 1.1 christos case BFD_RELOC_16:
159 1.1 christos case BFD_RELOC_16_PCREL:
160 1.1 christos case BFD_RELOC_8:
161 1.1 christos rinfo->r_scattered = 0;
162 1.1 christos rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA;
163 1.1 christos rinfo->r_pcrel = rel->howto->pc_relative;
164 1.1 christos rinfo->r_length = rel->howto->size; /* Correct in practice. */
165 1.1 christos if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
166 1.1 christos {
167 1.1 christos rinfo->r_extern = 0;
168 1.1 christos rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index;
169 1.1 christos }
170 1.1 christos else
171 1.1 christos {
172 1.1 christos rinfo->r_extern = 1;
173 1.1 christos rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
174 1.1 christos }
175 1.1 christos break;
176 1.1 christos case BFD_RELOC_MACH_O_SECTDIFF:
177 1.1 christos rinfo->r_scattered = 1;
178 1.1 christos rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF;
179 1.1 christos rinfo->r_pcrel = 0;
180 1.1 christos rinfo->r_length = 2;
181 1.1 christos rinfo->r_extern = 0;
182 1.1 christos rinfo->r_value = (*rel->sym_ptr_ptr)->value
183 1.1 christos + (*rel->sym_ptr_ptr)->section->vma;
184 1.1 christos break;
185 1.1 christos case BFD_RELOC_MACH_O_PAIR:
186 1.1 christos rinfo->r_address = 0;
187 1.1 christos rinfo->r_scattered = 1;
188 1.1 christos rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR;
189 1.1 christos rinfo->r_pcrel = 0;
190 1.1 christos rinfo->r_length = 2;
191 1.1 christos rinfo->r_extern = 0;
192 1.1 christos rinfo->r_value = (*rel->sym_ptr_ptr)->value
193 1.1 christos + (*rel->sym_ptr_ptr)->section->vma;
194 1.1 christos break;
195 1.1 christos default:
196 1.1 christos return FALSE;
197 1.1 christos }
198 1.1 christos return TRUE;
199 1.1 christos }
200 1.1 christos
201 1.1 christos static reloc_howto_type *
202 1.1 christos bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
203 1.1 christos bfd_reloc_code_real_type code)
204 1.1 christos {
205 1.1 christos unsigned int i;
206 1.1 christos
207 1.1 christos for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++)
208 1.1 christos if (code == i386_howto_table[i].type)
209 1.1 christos return &i386_howto_table[i];
210 1.1 christos return NULL;
211 1.1 christos }
212 1.1 christos
213 1.1 christos static reloc_howto_type *
214 1.1 christos bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
215 1.1 christos const char *name ATTRIBUTE_UNUSED)
216 1.1 christos {
217 1.1 christos return NULL;
218 1.1 christos }
219 1.1 christos
220 1.1 christos static bfd_boolean
221 1.1 christos bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread,
222 1.1 christos void *vfile, char *buf)
223 1.1 christos {
224 1.1 christos FILE *file = (FILE *)vfile;
225 1.1 christos
226 1.1 christos switch (thread->flavour)
227 1.1 christos {
228 1.1 christos case BFD_MACH_O_x86_THREAD_STATE:
229 1.1 christos if (thread->size < (8 + 16 * 4))
230 1.1 christos return FALSE;
231 1.1 christos fprintf (file, " x86_THREAD_STATE:\n");
232 1.1 christos fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
233 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 0),
234 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 4));
235 1.1 christos fprintf (file, " eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
236 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 8),
237 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 12),
238 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 16),
239 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 20));
240 1.1 christos fprintf (file, " edi: %08lx esi: %08lx ebp: %08lx esp: %08lx\n",
241 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 24),
242 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 28),
243 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 32),
244 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 36));
245 1.1 christos fprintf (file, " ss: %08lx flg: %08lx eip: %08lx cs: %08lx\n",
246 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 40),
247 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 44),
248 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 48),
249 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 52));
250 1.1 christos fprintf (file, " ds: %08lx es: %08lx fs: %08lx gs: %08lx\n",
251 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 56),
252 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 60),
253 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 64),
254 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 68));
255 1.1 christos return TRUE;
256 1.1 christos case BFD_MACH_O_x86_FLOAT_STATE:
257 1.1 christos if (thread->size < 8)
258 1.1 christos return FALSE;
259 1.1 christos fprintf (file, " x86_FLOAT_STATE:\n");
260 1.1 christos fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
261 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 0),
262 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 4));
263 1.1 christos return TRUE;
264 1.1 christos case BFD_MACH_O_x86_EXCEPTION_STATE:
265 1.1 christos if (thread->size < 8 + 3 * 4)
266 1.1 christos return FALSE;
267 1.1 christos fprintf (file, " x86_EXCEPTION_STATE:\n");
268 1.1 christos fprintf (file, " flavor: 0x%08lx count: 0x%08lx\n",
269 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 0),
270 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 4));
271 1.1 christos fprintf (file, " trapno: %08lx err: %08lx faultaddr: %08lx\n",
272 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 8),
273 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 12),
274 1.1 christos (unsigned long)bfd_get_32 (abfd, buf + 16));
275 1.1 christos return TRUE;
276 1.1 christos default:
277 1.1 christos break;
278 1.1 christos }
279 1.1 christos return FALSE;
280 1.1 christos }
281 1.1 christos
282 1.1 christos #define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
283 1.1 christos #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
284 1.1 christos #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread
285 1.1 christos
286 1.1 christos #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup
287 1.1 christos #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
288 1.1 christos
289 1.1 christos #define TARGET_NAME mach_o_i386_vec
290 1.1 christos #define TARGET_STRING "mach-o-i386"
291 1.1 christos #define TARGET_ARCHITECTURE bfd_arch_i386
292 1.1 christos #define TARGET_BIG_ENDIAN 0
293 1.1 christos #define TARGET_ARCHIVE 0
294 1.1 christos #include "mach-o-target.c"
295