gcc 以编程方式导致未定义指令异常

w8biq8rn  于 2023-08-06  发布在  其他
关注(0)|答案(4)|浏览(134)

我想为我的测试夹具的测试导致ARM Cortex-M3未定义指令异常。IAR编译器支持这样的内联汇编:

  1. asm("udf.w #0");

字符串
不幸的是,GNU CC内联汇编程序不知道NXP LPC 177 x8 x的此操作码。它编写诊断:

  1. ccw3kZ46.s:404: Error: bad instruction `udf.w #0'


如何创建一个导致未定义指令异常的函数?

kse8i1jr

kse8i1jr1#

基于 * Masta 79 * 的回答:
在ARMv7-M架构参考手册- ARM DDI 0403 D(documentation placeholder, registration required)中列出了一种“永久未定义”的编码。编码为0xf 7 fXaXXX(其中忽略“X”)。当然,指令提取是little-endian的,所以(没有测试):
第一个月
应该在任何ARMv7-M或更高版本的处理器上产生有保证的未定义指令。

t9aqgxwy

t9aqgxwy2#

一些额外的信息…
GCC的builtins之一是
void __builtin_trap(void)

此函数会导致程序异常退出。GCC通过使用依赖于目标的机制(例如故意执行非法指令)或调用abort来实现此功能。使用的机制可能因版本而异,因此您不应依赖任何特定的实现。

implementation for ARMv7 is

  1. (define_insn "trap"
  2. [(trap_if (const_int 1) (const_int 0))]
  3. ""
  4. "*
  5. if (TARGET_ARM)
  6. return \".inst\\t0xe7f000f0\";
  7. else
  8. return \".inst\\t0xdeff\";
  9. "
  10. [(set (attr "length")
  11. (if_then_else (eq_attr "is_thumb" "yes")
  12. (const_int 2)
  13. (const_int 4)))
  14. (set_attr "type" "trap")
  15. (set_attr "conds" "unconditional")]
  16. )

字符串
因此,对于ARM模式,gcc将生成0x7f000f0 (f0 00 f0 07),对于其他模式,将生成0xdeff (ff de)(在反汇编/调试时很方便)。
Also note that

  1. these encodings match the UDF instruction that is defined in the most
  2. recent edition of the ARM architecture reference manual.
  3. Thumb: 0xde00 | imm8 (we chose 0xff for the imm8)
  4. ARM: 0xe7f000f0 | (imm12 << 8) | imm4 (we chose to use 0 for both imms)


对于LLVM__builtin_trap,生成的值为0xe7ffdefe0xdefe

  1. case ARM::TRAP: {
  2. // Non-Darwin binutils don't yet support the "trap" mnemonic.
  3. // FIXME: Remove this special case when they do.
  4. if (!Subtarget->isTargetDarwin()) {
  5. //.long 0xe7ffdefe @ trap
  6. uint32_t Val = 0xe7ffdefeUL;
  7. OutStreamer.AddComment("trap");
  8. OutStreamer.EmitIntValue(Val, 4);
  9. return;
  10. }
  11. break;
  12. }
  13. case ARM::tTRAP: {
  14. // Non-Darwin binutils don't yet support the "trap" mnemonic.
  15. // FIXME: Remove this special case when they do.
  16. if (!Subtarget->isTargetDarwin()) {
  17. //.short 57086 @ trap
  18. uint16_t Val = 0xdefe;
  19. OutStreamer.AddComment("trap");
  20. OutStreamer.EmitIntValue(Val, 2);
  21. return;
  22. }
  23. break;
  24. }

展开查看全部
vltsax25

vltsax253#

在[1]中有不同的官方未定义指令(查找UDF;下面的.可以用任何十六进制数字替换):

  • 0xe7f...f.- ARM或A1编码(ARMv 4 T、ARMv 5 T、ARMv 6、ARMv7、ARMv 8)
  • 0xde..- Thumb或T1编码(ARMv 4 T、ARMv 5 T、ARMv 6、ARMv7、ARMv 8)
  • 0xf7f.a...-Thumb 2或T2编码(ARMv 6 T2、ARMv7、ARMv 8)
  • 0x0000....-ARMv 8-A永久未定义

还有其他的,但这些可能是未来的证明,因为它得到。
至于生成触发它的代码,您可以使用.short.word,如下所示:

  • asm volatile (".short 0xde00\n");/* thumb非法指令 */
  • asm volatile (".word 0xe7f000f0\n");/* arm非法指令 */
  • asm volatile (".word 0xe7f0def0\n");/* arm+thumb非法指令 */

注意,最后一个将在thumb上下文中解码为0xdef00xe7f0,因此也会导致未定义的指令异常。
[1][http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html)
[2] DDI0487D_b_armv8_arm.pdf

展开查看全部
xuo3flqw

xuo3flqw4#

永久未定义指令的thumb-16版本是0xDExx。因此,您可以在代码中执行以下操作来触发异常:

  1. .inst 0xde00

字符串
参考:ARMv7-M架构参考手册,第A5.2.6节。
(Note 0xF 7 Fx,0xAxxx编码也是永久未定义的,但它是32位指令。)

相关问题