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"
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
32 static const bfd_target
*
33 bfd_mach_o_i386_object_p (bfd
*abfd
)
35 return bfd_mach_o_header_p (abfd
, 0, BFD_MACH_O_CPU_TYPE_I386
);
38 static const bfd_target
*
39 bfd_mach_o_i386_core_p (bfd
*abfd
)
41 return bfd_mach_o_header_p (abfd
,
42 BFD_MACH_O_MH_CORE
, BFD_MACH_O_CPU_TYPE_I386
);
46 bfd_mach_o_i386_mkobject (bfd
*abfd
)
48 bfd_mach_o_data_struct
*mdata
;
50 if (!bfd_mach_o_mkobject_init (abfd
))
53 mdata
= abfd
->tdata
.mach_o_data
;
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;
63 static reloc_howto_type i386_howto_table
[]=
65 HOWTO(BFD_RELOC_32
, 0, 2, 32, FALSE
, 0,
66 complain_overflow_bitfield
,
68 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
69 HOWTO(BFD_RELOC_16
, 0, 1, 16, FALSE
, 0,
70 complain_overflow_bitfield
,
72 FALSE
, 0xffff, 0xffff, FALSE
),
73 HOWTO(BFD_RELOC_8
, 0, 0, 8, FALSE
, 0,
74 complain_overflow_bitfield
,
76 FALSE
, 0xff, 0xff, FALSE
),
77 HOWTO(BFD_RELOC_32_PCREL
, 0, 2, 32, TRUE
, 0,
78 complain_overflow_bitfield
,
80 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
81 HOWTO(BFD_RELOC_16_PCREL
, 0, 1, 16, TRUE
, 0,
82 complain_overflow_bitfield
,
84 FALSE
, 0xffff, 0xffff, TRUE
),
85 HOWTO(BFD_RELOC_MACH_O_SECTDIFF
, 0, 2, 32, FALSE
, 0,
86 complain_overflow_bitfield
,
88 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
89 HOWTO(BFD_RELOC_MACH_O_PAIR
, 0, 2, 32, FALSE
, 0,
90 complain_overflow_bitfield
,
92 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
96 bfd_mach_o_i386_swap_reloc_in (arelent
*res
, bfd_mach_o_reloc_info
*reloc
)
98 if (reloc
->r_scattered
)
100 switch (reloc
->r_type
)
102 case BFD_MACH_O_GENERIC_RELOC_PAIR
:
103 if (reloc
->r_length
!= 2)
105 res
->howto
= &i386_howto_table
[3];
106 res
->address
= res
[-1].address
;
108 case BFD_MACH_O_GENERIC_RELOC_SECTDIFF
:
109 case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF
:
110 if (reloc
->r_length
!= 2)
112 res
->howto
= &i386_howto_table
[2];
120 switch (reloc
->r_type
)
122 case BFD_MACH_O_GENERIC_RELOC_VANILLA
:
123 switch ((reloc
->r_length
<< 1) | reloc
->r_pcrel
)
125 case 0: /* len = 0, pcrel = 0 */
126 res
->howto
= &i386_howto_table
[2];
128 case 2: /* len = 1, pcrel = 0 */
129 res
->howto
= &i386_howto_table
[1];
131 case 3: /* len = 1, pcrel = 1 */
132 res
->howto
= &i386_howto_table
[4];
134 case 4: /* len = 2, pcrel = 0 */
135 res
->howto
= &i386_howto_table
[0];
137 case 5: /* len = 2, pcrel = 1 */
138 res
->howto
= &i386_howto_table
[3];
151 bfd_mach_o_i386_swap_reloc_out (arelent
*rel
, bfd_mach_o_reloc_info
*rinfo
)
153 rinfo
->r_address
= rel
->address
;
154 switch (rel
->howto
->type
)
157 case BFD_RELOC_32_PCREL
:
159 case BFD_RELOC_16_PCREL
:
161 rinfo
->r_scattered
= 0;
162 rinfo
->r_type
= BFD_MACH_O_GENERIC_RELOC_VANILLA
;
163 rinfo
->r_pcrel
= rel
->howto
->pc_relative
;
164 rinfo
->r_length
= rel
->howto
->size
; /* Correct in practice. */
165 if ((*rel
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
)
168 rinfo
->r_value
= (*rel
->sym_ptr_ptr
)->section
->target_index
;
173 rinfo
->r_value
= (*rel
->sym_ptr_ptr
)->udata
.i
;
176 case BFD_RELOC_MACH_O_SECTDIFF
:
177 rinfo
->r_scattered
= 1;
178 rinfo
->r_type
= BFD_MACH_O_GENERIC_RELOC_SECTDIFF
;
182 rinfo
->r_value
= (*rel
->sym_ptr_ptr
)->value
183 + (*rel
->sym_ptr_ptr
)->section
->vma
;
185 case BFD_RELOC_MACH_O_PAIR
:
186 rinfo
->r_address
= 0;
187 rinfo
->r_scattered
= 1;
188 rinfo
->r_type
= BFD_MACH_O_GENERIC_RELOC_PAIR
;
192 rinfo
->r_value
= (*rel
->sym_ptr_ptr
)->value
193 + (*rel
->sym_ptr_ptr
)->section
->vma
;
201 static reloc_howto_type
*
202 bfd_mach_o_i386_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
203 bfd_reloc_code_real_type code
)
207 for (i
= 0; i
< sizeof (i386_howto_table
) / sizeof (*i386_howto_table
); i
++)
208 if (code
== i386_howto_table
[i
].type
)
209 return &i386_howto_table
[i
];
213 static reloc_howto_type
*
214 bfd_mach_o_i386_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
215 const char *name ATTRIBUTE_UNUSED
)
220 #define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
221 #define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
223 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup
224 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
226 #define TARGET_NAME mach_o_i386_vec
227 #define TARGET_STRING "mach-o-i386"
228 #define TARGET_BIG_ENDIAN 0
229 #define TARGET_ARCHIVE 0
230 #include "mach-o-target.c"