]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/cpu-i386.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / cpu-i386.c
CommitLineData
252b5132 1/* BFD support for the Intel 386 architecture.
fd67aa11 2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
252b5132 3
cd123cb7 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
cd123cb7
NC
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.
252b5132 10
cd123cb7
NC
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.
252b5132 15
cd123cb7
NC
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. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132 23#include "libbfd.h"
b7761f11
L
24#include "libiberty.h"
25
0a1b45a2
AM
26extern void * bfd_arch_i386_short_nop_fill (bfd_size_type, bool,
27 bool);
252b5132 28
889a4d3e
L
29static const bfd_arch_info_type *
30bfd_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. */
d7921315
L
36 if (compat
37 && (a->mach & bfd_mach_x64_32) != (b->mach & bfd_mach_x64_32))
889a4d3e
L
38 compat = NULL;
39
40 return compat;
41}
42
923f257f
L
43/* Fill the buffer with zero or nop instruction if CODE is TRUE. Use
44 multi byte nop instructions if LONG_NOP is TRUE. */
b7761f11 45
923f257f 46static void *
0a1b45a2
AM
47bfd_arch_i386_fill (bfd_size_type count, bool code,
48 bool long_nop)
b7761f11
L
49{
50 /* nop */
68ffbac6 51 static const char nop_1[] = { 0x90 };
923f257f 52 /* xchg %ax,%ax */
b7761f11
L
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 };
923f257f 76 bfd_size_type nop_size = long_nop ? ARRAY_SIZE (nops) : 2;
b7761f11
L
77
78 void *fill = bfd_malloc (count);
79 if (fill == NULL)
80 return fill;
81
82 if (code)
83 {
84 bfd_byte *p = fill;
923f257f 85 while (count >= nop_size)
b7761f11 86 {
923f257f
L
87 memcpy (p, nops[nop_size - 1], nop_size);
88 p += nop_size;
89 count -= nop_size;
b7761f11
L
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
0a1b45a2 100/* Fill the buffer with zero or short nop instruction if CODE is true. */
923f257f
L
101
102void *
103bfd_arch_i386_short_nop_fill (bfd_size_type count,
0a1b45a2
AM
104 bool is_bigendian ATTRIBUTE_UNUSED,
105 bool code)
923f257f 106{
0a1b45a2 107 return bfd_arch_i386_fill (count, code, false);
923f257f
L
108}
109
110/* Fill the buffer with zero or long nop instruction if CODE is TRUE. */
111
112static void *
113bfd_arch_i386_long_nop_fill (bfd_size_type count,
0a1b45a2
AM
114 bool is_bigendian ATTRIBUTE_UNUSED,
115 bool code)
923f257f 116{
0a1b45a2 117 return bfd_arch_i386_fill (count, code, true);
923f257f
L
118}
119
aebcfb76
NC
120#define N(BITS, MACH, NAME, PRINT, DEF, FILL, NEXT) \
121 { BITS, /* Bits in a word. */ \
122 BITS, /* Bits in an address. */ \
123 8, /* Bits in a byte. */ \
124 bfd_arch_i386, \
125 MACH, /* Machine number. */ \
126 NAME, \
127 PRINT, \
128 3, /* Section alignment power. */ \
129 DEF, /* Default architecture version ? */ \
130 bfd_i386_compatible, \
131 bfd_default_scan, \
132 FILL, \
133 NEXT, \
134 0 /* Maximum instruction length. */ \
135 }
136
1098fd41 137
351f65ca 138static const bfd_arch_info_type bfd_x64_32_arch_intel_syntax =
aebcfb76 139 N (64, bfd_mach_x64_32_intel_syntax, "i386:intel", "i386:x64-32:intel",
0a1b45a2 140 false, bfd_arch_i386_long_nop_fill, NULL);
351f65ca 141
34274ccc 142static const bfd_arch_info_type bfd_x86_64_arch_intel_syntax =
aebcfb76 143 N (64, bfd_mach_x86_64_intel_syntax, "i386:intel", "i386:x86-64:intel",
0a1b45a2 144 false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch_intel_syntax);
1abc8597 145
34274ccc 146static const bfd_arch_info_type bfd_i386_arch_intel_syntax =
aebcfb76 147 N (32, bfd_mach_i386_i386_intel_syntax, "i386:intel", "i386:intel",
0a1b45a2 148 true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch_intel_syntax);
aebcfb76 149
1abc8597 150
34274ccc 151static const bfd_arch_info_type i8086_arch =
aebcfb76 152 N (32, bfd_mach_i386_i8086, "i8086", "i8086",
0a1b45a2 153 false, bfd_arch_i386_short_nop_fill, &bfd_i386_arch_intel_syntax);
8d88c4ca 154
351f65ca 155static const bfd_arch_info_type bfd_x64_32_arch =
aebcfb76 156 N (64, bfd_mach_x64_32, "i386", "i386:x64-32",
0a1b45a2 157 false, bfd_arch_i386_long_nop_fill, &i8086_arch);
351f65ca 158
34274ccc 159static const bfd_arch_info_type bfd_x86_64_arch =
aebcfb76 160 N (64, bfd_mach_x86_64, "i386", "i386:x86-64",
0a1b45a2 161 false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch);
252b5132
RH
162
163const bfd_arch_info_type bfd_i386_arch =
aebcfb76 164 N (32, bfd_mach_i386_i386, "i386", "i386",
0a1b45a2 165 true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch);