Is it possible to detect the CPU architecture from machine code?

后端 未结 3 1295
谎友^
谎友^ 2021-01-14 02:41

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/ma

3条回答
  •  [愿得一人]
    2021-01-14 03:09

    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
    

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

    00000000: b 0xbb4
    00000004: ...
    

    But for x86 (real mode), translates to:

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

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

提交回复
热议问题