是否可以从机器代码中检测 CPU 架构? [英] Is it possible to detect the CPU architecture from machine code?

查看:22
本文介绍了是否可以从机器代码中检测 CPU 架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有 2 种可能的架构,ARM 和 x86.有没有办法检测代码在哪个系统上运行,从汇编/机器代码中实现这样的目标?

Let's say that there are 2 possible architectures, ARM and x86. Is there a way to detect what system the code is running on, to achieve something like this from assembly/machine code?

if (isArm)
    jmp to arm machine code
if (isX86)
    jmp to x86 machine code

我知道 ARM 机器代码与 x86 机器代码有很大不同.我在想的是一些精心设计的汇编指令,它们会产生相同的二进制机器代码.

I know that ARM machine code differs from x86 machine code significantly. What I'm thinking about is some well crafted assembly instructions that would result in the same binary machine code.

推荐答案

假设您已经处理了所有其他差异1 并且您还需要编写一个小型的多语言蹦床,您可以使用这些操作码:

Assuming you have already taken care of all other differences1 and you are left with writing a small polyglot trampoline, you can use these opcodes:

EB 02 00 EA

当放在地址 0 时,对于 ARM(非拇指),转换为:

Which, when put at address 0, for ARM (non thumb), translates into:

00000000: b 0xbb4
00000004: ...

但对于 x86(实模式),转换为:

But for x86 (real mode), translates to:

0000:0000 jmp 04h
0000:0002 add dl, ch
0000:0004 ...

然后您可以将更复杂的 x86 代码放在地址 04h 上,将 ARM 代码放在地址 0bb4h 上.

You can then put more elaborate x86 code at address 04h and ARM code at address 0bb4h.

当然,重定位基地址时,一定要重定位跳转目标.

Of course, when relocating the base address, make sure to relocate the jump targets too.

1 例如,ARM 从地址 0 开始 而 x86 从地址 0fffffff0h 开始,因此您需要特定的硬件/固件支持来抽象启动地址.

1 For example, ARM starts at address 0 while x86 starts at address 0fffffff0h, so you need a specific hardware/firmware support to abstract the boot address.

这篇关于是否可以从机器代码中检测 CPU 架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆