175fd0b74Schristos /* BFD support for the Intel 386 architecture.
2*e992f068Schristos Copyright (C) 1992-2022 Free Software Foundation, Inc.
375fd0b74Schristos
475fd0b74Schristos This file is part of BFD, the Binary File Descriptor library.
575fd0b74Schristos
675fd0b74Schristos This program is free software; you can redistribute it and/or modify
775fd0b74Schristos it under the terms of the GNU General Public License as published by
875fd0b74Schristos the Free Software Foundation; either version 3 of the License, or
975fd0b74Schristos (at your option) any later version.
1075fd0b74Schristos
1175fd0b74Schristos This program is distributed in the hope that it will be useful,
1275fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1375fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1475fd0b74Schristos GNU General Public License for more details.
1575fd0b74Schristos
1675fd0b74Schristos You should have received a copy of the GNU General Public License
1775fd0b74Schristos along with this program; if not, write to the Free Software
1875fd0b74Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
1975fd0b74Schristos MA 02110-1301, USA. */
2075fd0b74Schristos
2175fd0b74Schristos #include "sysdep.h"
2275fd0b74Schristos #include "bfd.h"
2375fd0b74Schristos #include "libbfd.h"
2475fd0b74Schristos #include "libiberty.h"
2575fd0b74Schristos
26*e992f068Schristos extern void * bfd_arch_i386_short_nop_fill (bfd_size_type, bool,
27*e992f068Schristos bool);
2875fd0b74Schristos
2975fd0b74Schristos static const bfd_arch_info_type *
bfd_i386_compatible(const bfd_arch_info_type * a,const bfd_arch_info_type * b)3075fd0b74Schristos bfd_i386_compatible (const bfd_arch_info_type *a,
3175fd0b74Schristos const bfd_arch_info_type *b)
3275fd0b74Schristos {
3375fd0b74Schristos const bfd_arch_info_type *compat = bfd_default_compatible (a, b);
3475fd0b74Schristos
3575fd0b74Schristos /* Don't allow mixing x64_32 with x86_64. */
3675fd0b74Schristos if (compat
3775fd0b74Schristos && (a->mach & bfd_mach_x64_32) != (b->mach & bfd_mach_x64_32))
3875fd0b74Schristos compat = NULL;
3975fd0b74Schristos
4075fd0b74Schristos return compat;
4175fd0b74Schristos }
4275fd0b74Schristos
4375fd0b74Schristos /* Fill the buffer with zero or nop instruction if CODE is TRUE. Use
4475fd0b74Schristos multi byte nop instructions if LONG_NOP is TRUE. */
4575fd0b74Schristos
4675fd0b74Schristos static void *
bfd_arch_i386_fill(bfd_size_type count,bool code,bool long_nop)47*e992f068Schristos bfd_arch_i386_fill (bfd_size_type count, bool code,
48*e992f068Schristos bool long_nop)
4975fd0b74Schristos {
5075fd0b74Schristos /* nop */
5175fd0b74Schristos static const char nop_1[] = { 0x90 };
5275fd0b74Schristos /* xchg %ax,%ax */
5375fd0b74Schristos static const char nop_2[] = { 0x66, 0x90 };
5475fd0b74Schristos /* nopl (%[re]ax) */
5575fd0b74Schristos static const char nop_3[] = { 0x0f, 0x1f, 0x00 };
5675fd0b74Schristos /* nopl 0(%[re]ax) */
5775fd0b74Schristos static const char nop_4[] = { 0x0f, 0x1f, 0x40, 0x00 };
5875fd0b74Schristos /* nopl 0(%[re]ax,%[re]ax,1) */
5975fd0b74Schristos static const char nop_5[] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
6075fd0b74Schristos /* nopw 0(%[re]ax,%[re]ax,1) */
6175fd0b74Schristos static const char nop_6[] = { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
6275fd0b74Schristos /* nopl 0L(%[re]ax) */
6375fd0b74Schristos static const char nop_7[] = { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 };
6475fd0b74Schristos /* nopl 0L(%[re]ax,%[re]ax,1) */
6575fd0b74Schristos static const char nop_8[] =
6675fd0b74Schristos { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00};
6775fd0b74Schristos /* nopw 0L(%[re]ax,%[re]ax,1) */
6875fd0b74Schristos static const char nop_9[] =
6975fd0b74Schristos { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
7075fd0b74Schristos /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
7175fd0b74Schristos static const char nop_10[] =
7275fd0b74Schristos { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
7375fd0b74Schristos static const char *const nops[] =
7475fd0b74Schristos { nop_1, nop_2, nop_3, nop_4, nop_5,
7575fd0b74Schristos nop_6, nop_7, nop_8, nop_9, nop_10 };
7675fd0b74Schristos bfd_size_type nop_size = long_nop ? ARRAY_SIZE (nops) : 2;
7775fd0b74Schristos
7875fd0b74Schristos void *fill = bfd_malloc (count);
7975fd0b74Schristos if (fill == NULL)
8075fd0b74Schristos return fill;
8175fd0b74Schristos
8275fd0b74Schristos if (code)
8375fd0b74Schristos {
8475fd0b74Schristos bfd_byte *p = fill;
8575fd0b74Schristos while (count >= nop_size)
8675fd0b74Schristos {
8775fd0b74Schristos memcpy (p, nops[nop_size - 1], nop_size);
8875fd0b74Schristos p += nop_size;
8975fd0b74Schristos count -= nop_size;
9075fd0b74Schristos }
9175fd0b74Schristos if (count != 0)
9275fd0b74Schristos memcpy (p, nops[count - 1], count);
9375fd0b74Schristos }
9475fd0b74Schristos else
9575fd0b74Schristos memset (fill, 0, count);
9675fd0b74Schristos
9775fd0b74Schristos return fill;
9875fd0b74Schristos }
9975fd0b74Schristos
100*e992f068Schristos /* Fill the buffer with zero or short nop instruction if CODE is true. */
10175fd0b74Schristos
10275fd0b74Schristos void *
bfd_arch_i386_short_nop_fill(bfd_size_type count,bool is_bigendian ATTRIBUTE_UNUSED,bool code)10375fd0b74Schristos bfd_arch_i386_short_nop_fill (bfd_size_type count,
104*e992f068Schristos bool is_bigendian ATTRIBUTE_UNUSED,
105*e992f068Schristos bool code)
10675fd0b74Schristos {
107*e992f068Schristos return bfd_arch_i386_fill (count, code, false);
10875fd0b74Schristos }
10975fd0b74Schristos
11075fd0b74Schristos /* Fill the buffer with zero or long nop instruction if CODE is TRUE. */
11175fd0b74Schristos
11275fd0b74Schristos static void *
bfd_arch_i386_long_nop_fill(bfd_size_type count,bool is_bigendian ATTRIBUTE_UNUSED,bool code)11375fd0b74Schristos bfd_arch_i386_long_nop_fill (bfd_size_type count,
114*e992f068Schristos bool is_bigendian ATTRIBUTE_UNUSED,
115*e992f068Schristos bool code)
11675fd0b74Schristos {
117*e992f068Schristos return bfd_arch_i386_fill (count, code, true);
11875fd0b74Schristos }
11975fd0b74Schristos
120012573ebSchristos #define N(BITS, MACH, NAME, PRINT, DEF, FILL, NEXT) \
121012573ebSchristos { BITS, /* Bits in a word. */ \
122012573ebSchristos BITS, /* Bits in an address. */ \
123012573ebSchristos 8, /* Bits in a byte. */ \
124012573ebSchristos bfd_arch_i386, \
125012573ebSchristos MACH, /* Machine number. */ \
126012573ebSchristos NAME, \
127012573ebSchristos PRINT, \
128012573ebSchristos 3, /* Section alignment power. */ \
129012573ebSchristos DEF, /* Default architecture version ? */ \
130012573ebSchristos bfd_i386_compatible, \
131012573ebSchristos bfd_default_scan, \
132012573ebSchristos FILL, \
133012573ebSchristos NEXT, \
134012573ebSchristos 0 /* Maximum instruction length. */ \
135012573ebSchristos }
136012573ebSchristos
13775fd0b74Schristos
13875fd0b74Schristos static const bfd_arch_info_type bfd_x64_32_arch_intel_syntax =
139012573ebSchristos N (64, bfd_mach_x64_32_intel_syntax, "i386:intel", "i386:x64-32:intel",
140*e992f068Schristos false, bfd_arch_i386_long_nop_fill, NULL);
14175fd0b74Schristos
14275fd0b74Schristos static const bfd_arch_info_type bfd_x86_64_arch_intel_syntax =
143012573ebSchristos N (64, bfd_mach_x86_64_intel_syntax, "i386:intel", "i386:x86-64:intel",
144*e992f068Schristos false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch_intel_syntax);
14575fd0b74Schristos
14675fd0b74Schristos static const bfd_arch_info_type bfd_i386_arch_intel_syntax =
147012573ebSchristos N (32, bfd_mach_i386_i386_intel_syntax, "i386:intel", "i386:intel",
148*e992f068Schristos true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch_intel_syntax);
149012573ebSchristos
15075fd0b74Schristos
15175fd0b74Schristos static const bfd_arch_info_type i8086_arch =
152012573ebSchristos N (32, bfd_mach_i386_i8086, "i8086", "i8086",
153*e992f068Schristos false, bfd_arch_i386_short_nop_fill, &bfd_i386_arch_intel_syntax);
15475fd0b74Schristos
15575fd0b74Schristos static const bfd_arch_info_type bfd_x64_32_arch =
156012573ebSchristos N (64, bfd_mach_x64_32, "i386", "i386:x64-32",
157*e992f068Schristos false, bfd_arch_i386_long_nop_fill, &i8086_arch);
15875fd0b74Schristos
15975fd0b74Schristos static const bfd_arch_info_type bfd_x86_64_arch =
160012573ebSchristos N (64, bfd_mach_x86_64, "i386", "i386:x86-64",
161*e992f068Schristos false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch);
16275fd0b74Schristos
16375fd0b74Schristos const bfd_arch_info_type bfd_i386_arch =
164012573ebSchristos N (32, bfd_mach_i386_i386, "i386", "i386",
165*e992f068Schristos true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch);
166