Update copyright years
[platform/upstream/binutils.git] / bfd / mach-o-i386.c
1 /* Intel i386 Mach-O support for BFD.
2    Copyright (C) 2009-2014 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 "mach-o.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "libiberty.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 const bfd_target *
33 bfd_mach_o_i386_object_p (bfd *abfd)
34 {
35   return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_CPU_TYPE_I386);
36 }
37
38 static const bfd_target *
39 bfd_mach_o_i386_core_p (bfd *abfd)
40 {
41   return bfd_mach_o_header_p (abfd,
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
53   mdata = bfd_mach_o_get_data (abfd);
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;
57   mdata->header.byteorder = BFD_ENDIAN_LITTLE;
58   mdata->header.version = 1;
59
60   return TRUE;
61 }
62
63 static reloc_howto_type i386_howto_table[]=
64 {
65   /* 0 */
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),
82   /* 4 */
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),
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),
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),
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),
112 };
113
114 static bfd_boolean
115 bfd_mach_o_i386_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc)
116 {
117   if (reloc->r_scattered)
118     {
119       switch (reloc->r_type)
120         {
121         case BFD_MACH_O_GENERIC_RELOC_PAIR:
122           if (reloc->r_length == 2)
123             {
124               res->howto = &i386_howto_table[7];
125               res->address = res[-1].address;
126               return TRUE;
127             }
128           else if (reloc->r_length == 1)
129             {
130               res->howto = &i386_howto_table[10];
131               res->address = res[-1].address;
132               return TRUE;
133             }
134           return FALSE;
135         case BFD_MACH_O_GENERIC_RELOC_SECTDIFF:
136           if (reloc->r_length == 2)
137             {
138               res->howto = &i386_howto_table[5];
139               return TRUE;
140             }
141           else if (reloc->r_length == 1)
142             {
143               res->howto = &i386_howto_table[8];
144               return TRUE;
145             }
146           return FALSE;
147         case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF:
148           if (reloc->r_length == 2)
149             {
150               res->howto = &i386_howto_table[6];
151               return TRUE;
152             }
153           else if (reloc->r_length == 1)
154             {
155               res->howto = &i386_howto_table[9];
156               return TRUE;
157             }
158           return FALSE;
159         default:
160           return FALSE;
161         }
162     }
163   else
164     {
165       switch (reloc->r_type)
166         {
167         case BFD_MACH_O_GENERIC_RELOC_VANILLA:
168           switch ((reloc->r_length << 1) | reloc->r_pcrel)
169             {
170             case 0: /* len = 0, pcrel = 0  */
171               res->howto = &i386_howto_table[2];
172               return TRUE;
173             case 2: /* len = 1, pcrel = 0  */
174               res->howto = &i386_howto_table[1];
175               return TRUE;
176             case 3: /* len = 1, pcrel = 1  */
177               res->howto = &i386_howto_table[4];
178               return TRUE;
179             case 4: /* len = 2, pcrel = 0  */
180               res->howto = &i386_howto_table[0];
181               return TRUE;
182             case 5: /* len = 2, pcrel = 1  */
183               res->howto = &i386_howto_table[3];
184               return TRUE;
185             default:
186               return FALSE;
187             }
188           break;
189         default:
190           return FALSE;
191         }
192     }
193 }
194
195 static bfd_boolean
196 bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
197 {
198   rinfo->r_address = rel->address;
199   switch (rel->howto->type)
200     {
201     case BFD_RELOC_32:
202     case BFD_RELOC_32_PCREL:
203     case BFD_RELOC_16:
204     case BFD_RELOC_16_PCREL:
205     case BFD_RELOC_8:
206       rinfo->r_scattered = 0;
207       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA;
208       rinfo->r_pcrel = rel->howto->pc_relative;
209       rinfo->r_length = rel->howto->size; /* Correct in practice.  */
210       if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
211         {
212           rinfo->r_extern = 0;
213           rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index;
214         }
215       else
216         {
217           rinfo->r_extern = 1;
218           rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
219         }
220       break;
221     case BFD_RELOC_MACH_O_SECTDIFF:
222       rinfo->r_scattered = 1;
223       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF;
224       rinfo->r_pcrel = 0;
225       rinfo->r_length = rel->howto->size;
226       rinfo->r_extern = 0;
227       rinfo->r_value = rel->addend;
228       break;
229     case BFD_RELOC_MACH_O_LOCAL_SECTDIFF:
230       rinfo->r_scattered = 1;
231       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF;
232       rinfo->r_pcrel = 0;
233       rinfo->r_length = rel->howto->size;
234       rinfo->r_extern = 0;
235       rinfo->r_value = rel->addend;
236       break;
237     case BFD_RELOC_MACH_O_PAIR:
238       rinfo->r_address = 0;
239       rinfo->r_scattered = 1;
240       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR;
241       rinfo->r_pcrel = 0;
242       rinfo->r_length = rel->howto->size;
243       rinfo->r_extern = 0;
244       rinfo->r_value = rel->addend;
245       break;
246     default:
247       return FALSE;
248     }
249   return TRUE;
250 }
251
252 static reloc_howto_type *
253 bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
254                                        bfd_reloc_code_real_type code)
255 {
256   unsigned int i;
257
258   for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++)
259     if (code == i386_howto_table[i].type)
260       return &i386_howto_table[i];
261   return NULL;
262 }
263
264 static reloc_howto_type *
265 bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
266                                        const char *name ATTRIBUTE_UNUSED)
267 {
268   return NULL;
269 }
270
271 static bfd_boolean
272 bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread,
273                               void *vfile, char *buf)
274 {
275   FILE *file = (FILE *)vfile;
276
277   switch (thread->flavour)
278     {
279     case BFD_MACH_O_x86_THREAD_STATE:
280       if (thread->size < (8 + 16 * 4))
281         return FALSE;
282       fprintf (file, "   x86_THREAD_STATE:\n");
283       fprintf (file, "    flavor: 0x%08lx  count: 0x%08lx\n",
284                (unsigned long)bfd_get_32 (abfd, buf + 0),
285                (unsigned long)bfd_get_32 (abfd, buf + 4));
286       fprintf (file, "     eax: %08lx  ebx: %08lx  ecx: %08lx  edx: %08lx\n",
287                (unsigned long)bfd_get_32 (abfd, buf + 8),
288                (unsigned long)bfd_get_32 (abfd, buf + 12),
289                (unsigned long)bfd_get_32 (abfd, buf + 16),
290                (unsigned long)bfd_get_32 (abfd, buf + 20));
291       fprintf (file, "     edi: %08lx  esi: %08lx  ebp: %08lx  esp: %08lx\n",
292                (unsigned long)bfd_get_32 (abfd, buf + 24),
293                (unsigned long)bfd_get_32 (abfd, buf + 28),
294                (unsigned long)bfd_get_32 (abfd, buf + 32),
295                (unsigned long)bfd_get_32 (abfd, buf + 36));
296       fprintf (file, "      ss: %08lx  flg: %08lx  eip: %08lx   cs: %08lx\n",
297                (unsigned long)bfd_get_32 (abfd, buf + 40),
298                (unsigned long)bfd_get_32 (abfd, buf + 44),
299                (unsigned long)bfd_get_32 (abfd, buf + 48),
300                (unsigned long)bfd_get_32 (abfd, buf + 52));
301       fprintf (file, "      ds: %08lx   es: %08lx   fs: %08lx   gs: %08lx\n",
302                (unsigned long)bfd_get_32 (abfd, buf + 56),
303                (unsigned long)bfd_get_32 (abfd, buf + 60),
304                (unsigned long)bfd_get_32 (abfd, buf + 64),
305                (unsigned long)bfd_get_32 (abfd, buf + 68));
306       return TRUE;
307     case BFD_MACH_O_x86_FLOAT_STATE:
308       if (thread->size < 8)
309         return FALSE;
310       fprintf (file, "   x86_FLOAT_STATE:\n");
311       fprintf (file, "    flavor: 0x%08lx  count: 0x%08lx\n",
312                (unsigned long)bfd_get_32 (abfd, buf + 0),
313                (unsigned long)bfd_get_32 (abfd, buf + 4));
314       return TRUE;
315     case BFD_MACH_O_x86_EXCEPTION_STATE:
316       if (thread->size < 8 + 3 * 4)
317         return FALSE;
318       fprintf (file, "   x86_EXCEPTION_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       fprintf (file, "    trapno: %08lx  err: %08lx  faultaddr: %08lx\n",
323                (unsigned long)bfd_get_32 (abfd, buf + 8),
324                (unsigned long)bfd_get_32 (abfd, buf + 12),
325                (unsigned long)bfd_get_32 (abfd, buf + 16));
326       return TRUE;
327     default:
328       break;
329     }
330   return FALSE;
331 }
332
333 static const mach_o_section_name_xlat text_section_names_xlat[] =
334   {
335     {   ".symbol_stub",                 "__symbol_stub",
336         SEC_CODE | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
337         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
338                                         0},
339     {   ".picsymbol_stub",              "__picsymbol_stub",
340         SEC_CODE | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
341         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
342                                         0},
343     { NULL, NULL, 0, 0, 0, 0}
344   };
345
346 static const mach_o_section_name_xlat data_section_names_xlat[] =
347   {
348     /* The first two are recognized by i386, but not emitted for x86 by
349        modern GCC.  */
350     {   ".non_lazy_symbol_pointer",     "__nl_symbol_ptr",
351         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
352         BFD_MACH_O_S_ATTR_NONE,         2},
353     {   ".lazy_symbol_pointer",         "__la_symbol_ptr",
354         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
355         BFD_MACH_O_S_ATTR_NONE,         2},
356     {   ".lazy_symbol_pointer2",        "__la_sym_ptr2",
357         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
358         BFD_MACH_O_S_ATTR_NONE,         2},
359     {   ".lazy_symbol_pointer3",        "__la_sym_ptr3",
360         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
361         BFD_MACH_O_S_ATTR_NONE,         2},
362     { NULL, NULL, 0, 0, 0, 0}
363   };
364
365 static const mach_o_section_name_xlat import_section_names_xlat[] =
366   {
367     {   ".picsymbol_stub3",             "__jump_table",
368         SEC_CODE | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
369         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
370         | BFD_MACH_O_S_SELF_MODIFYING_CODE,
371                                         6},
372     {   ".non_lazy_symbol_pointer_x86", "__pointers",
373         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
374         BFD_MACH_O_S_ATTR_NONE,         2},
375     { NULL, NULL, 0, 0, 0, 0}
376   };
377
378 const mach_o_segment_name_xlat mach_o_i386_segsec_names_xlat[] =
379   {
380     { "__TEXT", text_section_names_xlat },
381     { "__DATA", data_section_names_xlat },
382     { "__IMPORT", import_section_names_xlat },
383     { NULL, NULL }
384   };
385
386 #define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
387 #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
388 #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread
389
390 #define bfd_mach_o_tgt_seg_table mach_o_i386_segsec_names_xlat
391 #define bfd_mach_o_section_type_valid_for_tgt NULL
392
393 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup
394 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
395
396 #define TARGET_NAME             mach_o_i386_vec
397 #define TARGET_STRING           "mach-o-i386"
398 #define TARGET_ARCHITECTURE     bfd_arch_i386
399 #define TARGET_BIG_ENDIAN       0
400 #define TARGET_ARCHIVE          0
401 #define TARGET_PRIORITY         0
402 #include "mach-o-target.c"