cpu-i386.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* BFD support for the Intel 386 architecture.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "libiberty.h"
  20. extern void * bfd_arch_i386_short_nop_fill (bfd_size_type, bool,
  21. bool);
  22. static const bfd_arch_info_type *
  23. bfd_i386_compatible (const bfd_arch_info_type *a,
  24. const bfd_arch_info_type *b)
  25. {
  26. const bfd_arch_info_type *compat = bfd_default_compatible (a, b);
  27. /* Don't allow mixing x64_32 with x86_64. */
  28. if (compat
  29. && (a->mach & bfd_mach_x64_32) != (b->mach & bfd_mach_x64_32))
  30. compat = NULL;
  31. return compat;
  32. }
  33. /* Fill the buffer with zero or nop instruction if CODE is TRUE. Use
  34. multi byte nop instructions if LONG_NOP is TRUE. */
  35. static void *
  36. bfd_arch_i386_fill (bfd_size_type count, bool code,
  37. bool long_nop)
  38. {
  39. /* nop */
  40. static const char nop_1[] = { 0x90 };
  41. /* xchg %ax,%ax */
  42. static const char nop_2[] = { 0x66, 0x90 };
  43. /* nopl (%[re]ax) */
  44. static const char nop_3[] = { 0x0f, 0x1f, 0x00 };
  45. /* nopl 0(%[re]ax) */
  46. static const char nop_4[] = { 0x0f, 0x1f, 0x40, 0x00 };
  47. /* nopl 0(%[re]ax,%[re]ax,1) */
  48. static const char nop_5[] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
  49. /* nopw 0(%[re]ax,%[re]ax,1) */
  50. static const char nop_6[] = { 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
  51. /* nopl 0L(%[re]ax) */
  52. static const char nop_7[] = { 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 };
  53. /* nopl 0L(%[re]ax,%[re]ax,1) */
  54. static const char nop_8[] =
  55. { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00};
  56. /* nopw 0L(%[re]ax,%[re]ax,1) */
  57. static const char nop_9[] =
  58. { 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
  59. /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
  60. static const char nop_10[] =
  61. { 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
  62. static const char *const nops[] =
  63. { nop_1, nop_2, nop_3, nop_4, nop_5,
  64. nop_6, nop_7, nop_8, nop_9, nop_10 };
  65. bfd_size_type nop_size = long_nop ? ARRAY_SIZE (nops) : 2;
  66. void *fill = bfd_malloc (count);
  67. if (fill == NULL)
  68. return fill;
  69. if (code)
  70. {
  71. bfd_byte *p = fill;
  72. while (count >= nop_size)
  73. {
  74. memcpy (p, nops[nop_size - 1], nop_size);
  75. p += nop_size;
  76. count -= nop_size;
  77. }
  78. if (count != 0)
  79. memcpy (p, nops[count - 1], count);
  80. }
  81. else
  82. memset (fill, 0, count);
  83. return fill;
  84. }
  85. /* Fill the buffer with zero or short nop instruction if CODE is true. */
  86. void *
  87. bfd_arch_i386_short_nop_fill (bfd_size_type count,
  88. bool is_bigendian ATTRIBUTE_UNUSED,
  89. bool code)
  90. {
  91. return bfd_arch_i386_fill (count, code, false);
  92. }
  93. /* Fill the buffer with zero or long nop instruction if CODE is TRUE. */
  94. static void *
  95. bfd_arch_i386_long_nop_fill (bfd_size_type count,
  96. bool is_bigendian ATTRIBUTE_UNUSED,
  97. bool code)
  98. {
  99. return bfd_arch_i386_fill (count, code, true);
  100. }
  101. #define N(BITS, MACH, NAME, PRINT, DEF, FILL, NEXT) \
  102. { BITS, /* Bits in a word. */ \
  103. BITS, /* Bits in an address. */ \
  104. 8, /* Bits in a byte. */ \
  105. bfd_arch_i386, \
  106. MACH, /* Machine number. */ \
  107. NAME, \
  108. PRINT, \
  109. 3, /* Section alignment power. */ \
  110. DEF, /* Default architecture version ? */ \
  111. bfd_i386_compatible, \
  112. bfd_default_scan, \
  113. FILL, \
  114. NEXT, \
  115. 0 /* Maximum instruction length. */ \
  116. }
  117. static const bfd_arch_info_type bfd_x64_32_arch_intel_syntax =
  118. N (64, bfd_mach_x64_32_intel_syntax, "i386:intel", "i386:x64-32:intel",
  119. false, bfd_arch_i386_long_nop_fill, NULL);
  120. static const bfd_arch_info_type bfd_x86_64_arch_intel_syntax =
  121. N (64, bfd_mach_x86_64_intel_syntax, "i386:intel", "i386:x86-64:intel",
  122. false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch_intel_syntax);
  123. static const bfd_arch_info_type bfd_i386_arch_intel_syntax =
  124. N (32, bfd_mach_i386_i386_intel_syntax, "i386:intel", "i386:intel",
  125. true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch_intel_syntax);
  126. static const bfd_arch_info_type i8086_arch =
  127. N (32, bfd_mach_i386_i8086, "i8086", "i8086",
  128. false, bfd_arch_i386_short_nop_fill, &bfd_i386_arch_intel_syntax);
  129. static const bfd_arch_info_type bfd_x64_32_arch =
  130. N (64, bfd_mach_x64_32, "i386", "i386:x64-32",
  131. false, bfd_arch_i386_long_nop_fill, &i8086_arch);
  132. static const bfd_arch_info_type bfd_x86_64_arch =
  133. N (64, bfd_mach_x86_64, "i386", "i386:x86-64",
  134. false, bfd_arch_i386_long_nop_fill, &bfd_x64_32_arch);
  135. const bfd_arch_info_type bfd_i386_arch =
  136. N (32, bfd_mach_i386_i386, "i386", "i386",
  137. true, bfd_arch_i386_short_nop_fill, &bfd_x86_64_arch);