OSDN Git Service

Use datarootdir for locales.
[pf3gnuchains/pf3gnuchains4x.git] / bfd / mach-o-i386.c
1 /* Intel i386 Mach-O support for BFD.
2    Copyright 2009
3    Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
21
22 #include "sysdep.h"
23 #include "mach-o.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "mach-o/reloc.h"
28
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
32
33 static const bfd_target *
34 bfd_mach_o_i386_object_p (bfd *abfd)
35 {
36   return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_CPU_TYPE_I386);
37 }
38
39 static const bfd_target *
40 bfd_mach_o_i386_core_p (bfd *abfd)
41 {
42   return bfd_mach_o_header_p (abfd,
43                               BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_I386);
44 }
45
46 static bfd_boolean
47 bfd_mach_o_i386_mkobject (bfd *abfd)
48 {
49   bfd_mach_o_data_struct *mdata;
50
51   if (!bfd_mach_o_mkobject_init (abfd))
52     return FALSE;
53
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;
60
61   return TRUE;
62 }
63
64 static reloc_howto_type i386_howto_table[]=
65 {
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   HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0,
83         complain_overflow_bitfield,
84         NULL, "DISP16",
85         FALSE, 0xffff, 0xffff, TRUE),
86   HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0,
87         complain_overflow_bitfield,
88         NULL, "SECTDIFF_32",
89         FALSE, 0xffffffff, 0xffffffff, FALSE),
90   HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0,
91         complain_overflow_bitfield,
92         NULL, "PAIR_32",
93         FALSE, 0xffffffff, 0xffffffff, FALSE),
94 };
95
96 static bfd_boolean
97 bfd_mach_o_i386_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc)
98 {
99   if (reloc->r_scattered)
100     {
101       switch (reloc->r_type)
102         {
103         case BFD_MACH_O_GENERIC_RELOC_PAIR:
104           if (reloc->r_length != 2)
105             return FALSE;
106           res->howto = &i386_howto_table[6];
107           res->address = res[-1].address;
108           return TRUE;
109         case BFD_MACH_O_GENERIC_RELOC_SECTDIFF:
110         case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF:
111           if (reloc->r_length != 2)
112             return FALSE;
113           res->howto = &i386_howto_table[5];
114           return TRUE;
115         default:
116           return FALSE;
117         }
118     }
119   else
120     {
121       switch (reloc->r_type)
122         {
123         case BFD_MACH_O_GENERIC_RELOC_VANILLA:
124           switch ((reloc->r_length << 1) | reloc->r_pcrel)
125             {
126             case 0: /* len = 0, pcrel = 0  */
127               res->howto = &i386_howto_table[2];
128               return TRUE;
129             case 2: /* len = 1, pcrel = 0  */
130               res->howto = &i386_howto_table[1];
131               return TRUE;
132             case 3: /* len = 1, pcrel = 1  */
133               res->howto = &i386_howto_table[4];
134               return TRUE;
135             case 4: /* len = 2, pcrel = 0  */
136               res->howto = &i386_howto_table[0];
137               return TRUE;
138             case 5: /* len = 2, pcrel = 1  */
139               res->howto = &i386_howto_table[3];
140               return TRUE;
141             default:
142               return FALSE;
143             }
144           break;
145         default:
146           return FALSE;
147         }
148     }
149 }
150
151 static bfd_boolean
152 bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
153 {
154   rinfo->r_address = rel->address;
155   switch (rel->howto->type)
156     {
157     case BFD_RELOC_32:
158     case BFD_RELOC_32_PCREL:
159     case BFD_RELOC_16:
160     case BFD_RELOC_16_PCREL:
161     case BFD_RELOC_8:
162       rinfo->r_scattered = 0;
163       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA;
164       rinfo->r_pcrel = rel->howto->pc_relative;
165       rinfo->r_length = rel->howto->size; /* Correct in practice.  */
166       if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
167         {
168           rinfo->r_extern = 0;
169           rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index;
170         }
171       else
172         {
173           rinfo->r_extern = 1;
174           rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
175         }
176       break;
177     case BFD_RELOC_MACH_O_SECTDIFF:
178       rinfo->r_scattered = 1;
179       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF;
180       rinfo->r_pcrel = 0;
181       rinfo->r_length = 2;
182       rinfo->r_extern = 0;
183       rinfo->r_value = (*rel->sym_ptr_ptr)->value 
184         + (*rel->sym_ptr_ptr)->section->vma;
185       break;
186     case BFD_RELOC_MACH_O_PAIR:
187       rinfo->r_address = 0;
188       rinfo->r_scattered = 1;
189       rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR;
190       rinfo->r_pcrel = 0;
191       rinfo->r_length = 2;
192       rinfo->r_extern = 0;
193       rinfo->r_value = (*rel->sym_ptr_ptr)->value 
194         + (*rel->sym_ptr_ptr)->section->vma;
195       break;
196     default:
197       return FALSE;
198     }
199   return TRUE;
200 }
201
202 static reloc_howto_type *
203 bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
204                                        bfd_reloc_code_real_type code)
205 {
206   unsigned int i;
207
208   for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++)
209     if (code == i386_howto_table[i].type)
210       return &i386_howto_table[i];
211   return NULL;
212 }
213
214 static reloc_howto_type *
215 bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
216                                        const char *name ATTRIBUTE_UNUSED)
217 {
218   return NULL;
219 }
220
221 static bfd_boolean
222 bfd_mach_o_i386_print_thread (bfd *abfd, bfd_mach_o_thread_flavour *thread,
223                               void *vfile, char *buf)
224 {
225   FILE *file = (FILE *)vfile;
226
227   switch (thread->flavour)
228     {
229     case BFD_MACH_O_x86_THREAD_STATE:
230       if (thread->size < (8 + 16 * 4))
231         return FALSE;
232       fprintf (file, "   x86_THREAD_STATE:\n");
233       fprintf (file, "    flavor: 0x%08lx  count: 0x%08lx\n",
234                (unsigned long)bfd_get_32 (abfd, buf + 0),
235                (unsigned long)bfd_get_32 (abfd, buf + 4));
236       fprintf (file, "     eax: %08lx  ebx: %08lx  ecx: %08lx  edx: %08lx\n",
237                (unsigned long)bfd_get_32 (abfd, buf + 8),
238                (unsigned long)bfd_get_32 (abfd, buf + 12),
239                (unsigned long)bfd_get_32 (abfd, buf + 16),
240                (unsigned long)bfd_get_32 (abfd, buf + 20));
241       fprintf (file, "     edi: %08lx  esi: %08lx  ebp: %08lx  esp: %08lx\n",
242                (unsigned long)bfd_get_32 (abfd, buf + 24),
243                (unsigned long)bfd_get_32 (abfd, buf + 28),
244                (unsigned long)bfd_get_32 (abfd, buf + 32),
245                (unsigned long)bfd_get_32 (abfd, buf + 36));
246       fprintf (file, "      ss: %08lx  flg: %08lx  eip: %08lx   cs: %08lx\n",
247                (unsigned long)bfd_get_32 (abfd, buf + 40),
248                (unsigned long)bfd_get_32 (abfd, buf + 44),
249                (unsigned long)bfd_get_32 (abfd, buf + 48),
250                (unsigned long)bfd_get_32 (abfd, buf + 52));
251       fprintf (file, "      ds: %08lx   es: %08lx   fs: %08lx   gs: %08lx\n",
252                (unsigned long)bfd_get_32 (abfd, buf + 56),
253                (unsigned long)bfd_get_32 (abfd, buf + 60),
254                (unsigned long)bfd_get_32 (abfd, buf + 64),
255                (unsigned long)bfd_get_32 (abfd, buf + 68));
256       return TRUE;
257     case BFD_MACH_O_x86_FLOAT_STATE:
258       if (thread->size < 8)
259         return FALSE;
260       fprintf (file, "   x86_FLOAT_STATE:\n");
261       fprintf (file, "    flavor: 0x%08lx  count: 0x%08lx\n",
262                (unsigned long)bfd_get_32 (abfd, buf + 0),
263                (unsigned long)bfd_get_32 (abfd, buf + 4));
264       return TRUE;
265     case BFD_MACH_O_x86_EXCEPTION_STATE:
266       if (thread->size < 8 + 3 * 4)
267         return FALSE;
268       fprintf (file, "   x86_EXCEPTION_STATE:\n");
269       fprintf (file, "    flavor: 0x%08lx  count: 0x%08lx\n",
270                (unsigned long)bfd_get_32 (abfd, buf + 0),
271                (unsigned long)bfd_get_32 (abfd, buf + 4));
272       fprintf (file, "    trapno: %08lx  err: %08lx  faultaddr: %08lx\n",
273                (unsigned long)bfd_get_32 (abfd, buf + 8),
274                (unsigned long)bfd_get_32 (abfd, buf + 12),
275                (unsigned long)bfd_get_32 (abfd, buf + 16));
276       return TRUE;
277     default:
278       break;
279     }
280   return FALSE;
281 }
282
283 static const mach_o_section_name_xlat text_section_names_xlat[] =
284   {
285     {   ".symbol_stub",                 "__symbol_stub",
286         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
287         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
288                                         0},
289     {   ".picsymbol_stub",              "__picsymbol_stub",
290         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
291         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS,
292                                         0},
293     { NULL, NULL, 0, 0, 0, 0}
294   };
295
296 static const mach_o_section_name_xlat data_section_names_xlat[] =
297   {
298     /* The first two are recognized by i386, but not emitted for x86 by
299        modern GCC.  */
300     {   ".non_lazy_symbol_pointer",     "__nl_symbol_ptr",
301         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
302         BFD_MACH_O_S_ATTR_NONE,         2},
303     {   ".lazy_symbol_pointer",         "__la_symbol_ptr",
304         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
305         BFD_MACH_O_S_ATTR_NONE,         2},
306     {   ".lazy_symbol_pointer2",        "__la_sym_ptr2",
307         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
308         BFD_MACH_O_S_ATTR_NONE,         2},
309     {   ".lazy_symbol_pointer3",        "__la_sym_ptr3",
310         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_LAZY_SYMBOL_POINTERS,
311         BFD_MACH_O_S_ATTR_NONE,         2},
312     { NULL, NULL, 0, 0, 0, 0}
313   };
314
315 static const mach_o_section_name_xlat import_section_names_xlat[] =
316   {
317     {   ".picsymbol_stub3",             "__jump_table",
318         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_SYMBOL_STUBS,
319         BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS 
320         | BFD_MACH_O_S_SELF_MODIFYING_CODE,
321                                         6},
322     {   ".non_lazy_symbol_pointer_x86", "__pointers",
323         SEC_DATA | SEC_LOAD,            BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS,
324         BFD_MACH_O_S_ATTR_NONE,         2},
325     { NULL, NULL, 0, 0, 0, 0}
326   };
327
328 const mach_o_segment_name_xlat mach_o_i386_segsec_names_xlat[] =
329   {
330     { "__TEXT", text_section_names_xlat },
331     { "__DATA", data_section_names_xlat },
332     { "__IMPORT", import_section_names_xlat },
333     { NULL, NULL }
334   };
335
336 #define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
337 #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
338 #define bfd_mach_o_print_thread bfd_mach_o_i386_print_thread
339
340 #define bfd_mach_o_tgt_seg_table mach_o_i386_segsec_names_xlat
341 #define bfd_mach_o_section_type_valid_for_tgt NULL
342
343 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup 
344 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
345
346 #define TARGET_NAME             mach_o_i386_vec
347 #define TARGET_STRING           "mach-o-i386"
348 #define TARGET_ARCHITECTURE     bfd_arch_i386
349 #define TARGET_BIG_ENDIAN       0
350 #define TARGET_ARCHIVE          0
351 #define TARGET_PRIORITY         0
352 #include "mach-o-target.c"