llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27k stars 11.06k forks source link

ARM Thumb2 disassemble error #88

Open williamleecn opened 4 years ago

williamleecn commented 4 years ago
(lldb) dis -b -A thumb-2 

   0xd1929ba8 <+0>:  0x4ff0e92d   .long  0x4ff0e92d                ; unknown opcode
    0xd1929bac <+4>:  0xaf03       add    r7, sp, #0xc
    0xd1929bae <+6>:  0x7d61f5ad   .long  0x7d61f5ad                ; unknown opcode

The correct should be

.text:00003BA8 2D E9 F0 4F                             PUSH.W          {R4-R11,LR}
.text:00003BAC 03 AF                                   ADD             R7, SP, #0xC
.text:00003BAE AD F5 61 7D                             SUB.W           SP, SP, #0x384
dcci commented 4 years ago

This is a bug in MC, not in lldb, for what it's worth. you might want too provide a small example where this reproduces (showing that objdump -d is incorrect is probably the best option).

williamleecn commented 4 years ago

I upload and shared the file: https://drive.google.com/open?id=1hZ-B3iWnbxNpREem2nauFuI01C3vDYIq

function offset : .text:00003BA8

DavidSpickett commented 4 years ago

If we assemble those instructions with MC it says it requires Thumb2, which makes sense.

$ ./llvm-mc --triple thumb --assemble <<< "push.w{r4-r11, lr}; sub.w sp, sp, #900"
    .text
<stdin>:1:1: error: instruction requires: thumb2
push.w{r4-r11, lr}; sub.w sp, sp, #900
^
<stdin>:1:21: error: instruction requires: thumb2
push.w{r4-r11, lr}; sub.w sp, sp, #900
                    ^

You can use "--triple thumbv8" and it will succeed, presumably that enables Thumb2. So somewhere along the way from lldb to MC Thumb2 is not being enabled.

First guess is that lldb passes "thumb-2" directly to MC which doesn't actually enable Thumb2 it seems (indeed, you can use a triple of "thumb-foo" and it doesn't reject it either). I'd have to find the source for the "dis" command, I'm not very familiar with lldb's layout.

DavidSpickett commented 4 years ago

llvm-mc understands triple of thumb/thumbeb or thumbv[0-9]. I found that thumbv7 is where it enabled Thumb2.

@williamleecn could you see if this works for you? (lldb) dis -b -A thumbv7

williamleecn commented 4 years ago

@DavidSpickett It's work, but it's still wrong to display it when pause by single step, and set break point in the thumb2 code occur error: signal SIGSEGV: address access protected

DavidSpickett commented 4 years ago

My uneducated guess is that lldb is either using incorrect arguments to MC, or it's not been told specifically what the target is and so is using a default. (maybe from the program file?)

I see from the .ARM.attributes it's built for cortex-a8 which is v7 and has Thumb2. (I don't know if anything here is paying attention to that though)

For clarity can you tell us:

llvmbot commented 5 months ago

@llvm/issue-subscribers-lldb

Author: None (williamleecn)

``` (lldb) dis -b -A thumb-2 0xd1929ba8 <+0>: 0x4ff0e92d .long 0x4ff0e92d ; unknown opcode 0xd1929bac <+4>: 0xaf03 add r7, sp, #0xc 0xd1929bae <+6>: 0x7d61f5ad .long 0x7d61f5ad ; unknown opcode ``` The correct should be ``` .text:00003BA8 2D E9 F0 4F PUSH.W {R4-R11,LR} .text:00003BAC 03 AF ADD R7, SP, #0xC .text:00003BAE AD F5 61 7D SUB.W SP, SP, #0x384 ```