llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

MC Assembler generates ARM NOP instructions instead of Thumb NOP instructions when aligning

Open kbeyls opened this issue 15 years ago • 2 comments

Bugzilla Link 10632
Resolution FIXED
Resolved on Oct 20, 2011 02:50
Version trunk
OS All
CC @efriedma-quic,@jmolloy

Extended Description

The following assembler file gets mis-assembled:

$ cat ArmNopsInThumbCode.s .section __TEXT //,__text,regular,pure_instructions .text .code 16 f: $t: add r0, r0, #​1 .align 3 add r0, r0, #​2 bx lr

When using the following command line: $ build/Debug+Asserts/bin/llvm-mc -arch=thumb -mcpu=arm926ej-s ArmNopsInThumbCode.s -filetype=obj -o ArmNopsInThumbCode.o

Disassembling the generated ELF file with GNU objdump shows that ARM NOP instructions were generated instead of Thumb NOP instructions:

$ arm-none-eabi-objdump -d ArmNopsInThumbCode.o

ArmNopsInThumbCode.o: file format elf32-littlearm

Disassembly of section .text:

00000000 : 0: 1c40 adds r0, r0, #​1 2: 0000 movs r0, r0 4: 0e1a lsrs r2, r3, #​24 6: 0000 movs r0, r0 8: 1c80 adds r0, r0, #​2 a: 4770 bx lr

The decoded instruction at offsets 2-6 should be 16-bit Thumb NOP instructions. However, when looking at the encoding "0000 0e1a 0000", it seems that the ARM pseudo-NOP instruction (i.e. MOV r0,r0) was encoded.

kbeyls avatar Aug 10 '11 11:08 kbeyls

Was fixed in r137723.

jmolloy avatar Oct 20 '11 09:10 jmolloy

I think that the root cause of the bug originates in this area of the code:

Index: lib/MC/MCAssembler.cpp

--- lib/MC/MCAssembler.cpp (revision 136769) +++ lib/MC/MCAssembler.cpp (working copy) @@ -412,6 +412,12 @@ // bytes left to fill use the the Value and ValueSize to fill the rest. // If we are aligning with nops, ask that target to emit the right data. if (AF.hasEmitNops()) {

  •   // FIXME: this assumes that the NOP data to be written is independent
    
  •   // of the instructions/fragments before or after it.
    
  •   // I believe that to write NOP data, the ASM writer should be set in
    
  •   // the correct state, e.g. for ARM compilation, in Thumb state so as
    
  •   // to produce Thumb NOP's when the NOP data is in the middle of a
    
  •   // Thumb function.
     if (!Asm.getBackend().WriteNopData(Count, OW))
       report_fatal_error("unable to write nop sequence of " +
                         Twine(Count) + " bytes");
    

kbeyls avatar Aug 10 '11 17:08 kbeyls