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

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

问题描述

假设有两种可能的体系结构,即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

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

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 ...

然后,您可以在地址04h处放置更详细的x86代码,在地址0bb4h处放置ARM代码.

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

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天全站免登陆