gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / bfd / cpu-i386.c
1 /* BFD support for the Intel 386 architecture.
2 Copyright (C) 1992-2020 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 "bfd.h"
23 #include "libbfd.h"
24 #include "libiberty.h"
25
26 extern void * bfd_arch_i386_short_nop_fill (bfd_size_type, bfd_boolean,
27 bfd_boolean);
28
29 static const bfd_arch_info_type *
30 bfd_i386_compatible (const bfd_arch_info_type *a,
31 const bfd_arch_info_type *b)
32 {
33 const bfd_arch_info_type *compat = bfd_default_compatible (a, b);
34
35 /* Don't allow mixing x64_32 with x86_64. */
36 if (compat
37 && (a->mach & bfd_mach_x64_32) != (b->mach & bfd_mach_x64_32))
38 compat = NULL;
39
40 return compat;
41 }
42
43 /* Fill the buffer with zero or nop instruction if CODE is TRUE. Use
44 multi byte nop instructions if LONG_NOP is TRUE. */
45
46 static void *
47 bfd_arch_i386_fill (bfd_size_type count, bfd_boolean code,
48 bfd_boolean long_nop)
49 {
50 /* nop */
51 static const char nop_1[] = { 0x90 };
52 /* xchg %ax,%ax */
53 static const char nop_2[] = { 0x66, 0x90 };
54 /* nopl (%[re]ax) */
55 static const char nop_3[] = { 0x0f, 0x1f, 0x00 };
56 /* nopl 0(%[re]ax) */
57 static const char nop_4[] = { 0x0f, 0x1f, 0x40, 0x00 };
58 /* nopl 0(%[re]ax,%[re]ax,1) */
59 static const char nop_5[] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
60 /* nopw 0(%[re]ax,%[re]ax,1) */
61 static const char nop_6[] = { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
62 /* nopl 0L(%[re]ax) */
63 static const char nop_7[] = { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 };
64 /* nopl 0L(%[re]ax,%[re]ax,1) */
65 static const char nop_8[] =
66 { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00};
67 /* nopw 0L(%[re]ax,%[re]ax,1) */
68 static const char nop_9[] =
69 { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
70 /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
71 static const char nop_10[] =
72 { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
73 static const char *const nops[] =
74 { nop_1, nop_2, nop_3, nop_4, nop_5,
75 nop_6, nop_7, nop_8, nop_9, nop_10 };
76 bfd_size_type nop_size = long_nop ? ARRAY_SIZE (nops) : 2;
77
78 void *fill = bfd_malloc (count);
79 if (fill == NULL)
80 return fill;
81
82 if (code)
83 {
84 bfd_byte *p = fill;
85 while (count >= nop_size)
86 {
87 memcpy (p, nops[nop_size - 1], nop_size);
88 p += nop_size;
89 count -= nop_size;
90 }
91 if (count != 0)
92 memcpy (p, nops[count - 1], count);
93 }
94 else
95 memset (fill, 0, count);
96
97 return fill;
98 }
99
100 /* Fill the buffer with zero or short nop instruction if CODE is TRUE. */
101
102 void *
103 bfd_arch_i386_short_nop_fill (bfd_size_type count,
104 bfd_boolean is_bigendian ATTRIBUTE_UNUSED,
105 bfd_boolean code)
106 {
107 return bfd_arch_i386_fill (count, code, FALSE);
108 }
109
110 /* Fill the buffer with zero or long nop instruction if CODE is TRUE. */
111
112 static void *
113 bfd_arch_i386_long_nop_fill (bfd_size_type count,
114 bfd_boolean is_bigendian ATTRIBUTE_UNUSED,
115 bfd_boolean code)
116 {
117 return bfd_arch_i386_fill (count, code, TRUE);
118 }
119
120 /* Fill the buffer with zero, or one-byte nop instructions if CODE is TRUE. */
121
122 static void *
123 bfd_arch_i386_onebyte_nop_fill (bfd_size_type count,
124 bfd_boolean is_bigendian ATTRIBUTE_UNUSED,
125 bfd_boolean code)
126 {
127 void *fill = bfd_malloc (count);
128 if (fill != NULL)
129 memset (fill, code ? 0x90 : 0, count);
130 return fill;
131 }
132
133 #define N(BITS, MACH, NAME, PRINT, DEF, FILL, NEXT) \
134 { BITS, /* Bits in a word. */ \
135 BITS, /* Bits in an address. */ \
136 8, /* Bits in a byte. */ \
137 bfd_arch_i386, \
138 MACH, /* Machine number. */ \
139 NAME, \
140 PRINT, \
141 3, /* Section alignment power. */ \
142 DEF, /* Default architecture version ? */ \
143 bfd_i386_compatible, \
144 bfd_default_scan, \
145 FILL, \
146 NEXT, \
147 0 /* Maximum instruction length. */ \
148 }
149
150
151 static const bfd_arch_info_type bfd_x64_32_nacl_arch =
152 N (64, bfd_mach_x64_32_nacl, "i386", "i386:x64-32:nacl",
153 FALSE, bfd_arch_i386_onebyte_nop_fill, NULL);
154
155 static const bfd_arch_info_type bfd_x86_64_nacl_arch =
156 N (64, bfd_mach_x86_64_nacl, "i386", "i386:x86-64:nacl",
157 FALSE, bfd_arch_i386_onebyte_nop_fill, &bfd_x64_32_nacl_arch);
158
159 const bfd_arch_info_type bfd_i386_nacl_arch =
160 N (32, bfd_mach_i386_i386_nacl, "i386", "i386:nacl",
161 TRUE, bfd_arch_i386_onebyte_nop_fill, &bfd_x86_64_nacl_arch);
162
163
164 static const bfd_arch_info_type bfd_x64_32_arch_intel_syntax =
165 N (64, bfd_mach_x64_32_intel_syntax, "i386:intel", "i386:x64-32:intel",
166 FALSE, bfd_arch_i386_long_nop_fill, &bfd_i386_nacl_arch);
167
168 static const bfd_arch_info_type bfd_x86_64_arch_intel_syntax =
169 N (64, bfd_mach_x86_64_intel_syntax, "i386:intel", "i386:x86-64:intel",
170 FALSE, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch_intel_syntax);
171
172 static const bfd_arch_info_type bfd_i386_arch_intel_syntax =
173 N (32, bfd_mach_i386_i386_intel_syntax, "i386:intel", "i386:intel",
174 TRUE, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch_intel_syntax);
175
176
177 static const bfd_arch_info_type i8086_arch =
178 N (32, bfd_mach_i386_i8086, "i8086", "i8086",
179 FALSE, bfd_arch_i386_short_nop_fill, &bfd_i386_arch_intel_syntax);
180
181 static const bfd_arch_info_type bfd_x64_32_arch =
182 N (64, bfd_mach_x64_32, "i386", "i386:x64-32",
183 FALSE, bfd_arch_i386_long_nop_fill, &i8086_arch);
184
185 static const bfd_arch_info_type bfd_x86_64_arch =
186 N (64, bfd_mach_x86_64, "i386", "i386:x86-64",
187 FALSE, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch);
188
189 const bfd_arch_info_type bfd_i386_arch =
190 N (32, bfd_mach_i386_i386, "i386", "i386",
191 TRUE, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch);
This page took 0.033208 seconds and 4 git commands to generate.