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

[ARMv7] ldrb.w syntax issue

Open eblot opened this issue 7 years ago • 15 comments

Bugzilla Link 43382
Version 9.0
OS All
CC @jcai19,@eblot,@efriedma-quic,@kbeyls,@lalozano,@momchil-velikov,@nickdesaulniers,@smithp35,@stephenhines,@agners

Extended Description

[foreword: I did not find the proper component for clang's integrated-as component. I picked up "C" which is likely not the right sub-component, please reassign to the proper component]

Using clang/llvm 9.0.0 final with an ARMv7-M (ELF) target, the following syntax is not accepted by clang assembler:

ldrb.w  r2, [r3], #​1

Full example:

.text .arch armv7 .thumb .thumb_func .align 1 .syntax unified

mov r3, r0 1: ldrb.w r2, [r3], #​1 cmp r2, #​0 bne 1b subs r0, r3, r0 subs r0, #​1 bx lr

Using "clang -target armv7em-none-eabi -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb -mabi=aapcs -c strlen.S":

strlen.S:7:21: error: too many operands for instruction
1: ldrb.w r2, [r3], #​1

From ARM®v7-M Architecture Reference Manual [ARM DDI 0403E.b], Section A7.7.45 LDRB (immediate), Encoding T2 ARMv7-M,

LDRB<c>.W <Rt>,[<Rn>{,#<imm12>}]

seems to be a valid syntax for the selected architecture.

If the opcode is changed to

ldrb r2, [r3], #​1

clang does not complain anymore, and generate the .o output file

Now reading back this generated object file with llvm's objdump:

$ arm-none-eabi-objdump -S strlen.o

strlen.o:     file format elf32-littlearm


Disassembly of section .text:

00000000 <.text>:
   0: 4603        mov   r3, r0
   2: f813 2b01   ldrb.w   r2, [r3], #&#8203;1
   6: 2a00        cmp   r2, #&#8203;0
   8: d1fb        bne.n 2 <.text+0x2>
   a: 1a18        subs  r0, r3, r0
   c: 3801        subs  r0, #&#8203;1
   e: 4770        bx lr

objdump decodes the ldrb instruction as ldrb.w, which is the syntax clang rejects.

eblot avatar Sep 20 '19 14:09 eblot

mentioned in issue llvm/llvm-bugzilla-archive#44858

efriedma-quic avatar Nov 27 '21 02:11 efriedma-quic

Thanks Peter and Momchil. I will take a look at Momchil's patch and see if I can continue from there.

jcai19 avatar Feb 19 '20 18:02 jcai19

I've got one fix here https://reviews.llvm.org/D68916 , but reviewers are suggesting a different approach, which is much more involved; I have no idea when and if I'll have time for it.

momchil-velikov avatar Feb 19 '20 09:02 momchil-velikov

I'm not aware of a fix. As I understand it this would be a matter of finding the tablegen pattern in ARMInstrThumb2.td and adding support for an optional .w.

For example the t2LDRB inherits from the T2I_ld class which has support for .w, but the t2LDRB_PRE and t2_LDRB_POST variants (along with, it seems with a casual glance, all the indexed loads) do not.

smithp35 avatar Feb 19 '20 09:02 smithp35

Is there a fix for this issue? I was investigating on a similar issue https://github.com/ClangBuiltLinux/linux/issues/857 and ended up here. I can take a look at it if no one is actively working on this.

jcai19 avatar Feb 19 '20 05:02 jcai19

If this is only about accepting .w, then this is the same issue as llvm/llvm-project#40317

agners avatar Feb 09 '20 21:02 agners

There is actually one more instance where this is a problem in the Linux code base: dsb.w is used. Also in this case there is only a wide variant, and hence can be worked around.

agners avatar Feb 09 '20 21:02 agners

*** Bug llvm/llvm-bugzilla-archive#44858 has been marked as a duplicate of this bug. ***

efriedma-quic avatar Feb 09 '20 21:02 efriedma-quic

Fair enough.

momchil-velikov avatar Sep 30 '19 09:09 momchil-velikov

In this context, ldrb is unambiguously a wide instruction, so the ".w" isn't really useful, but we should still accept it in the assembler. From the architecture reference:

LDRB{}{} , [], #+/-

"" is allowed to be ".w".

efriedma-quic avatar Sep 28 '19 18:09 efriedma-quic

It seems clang 9 works as expected:

$ llvm-objdump -d lib_a-strlen.o

lib_a-strlen.o: file format ELF32-arm-little

Disassembly of section .text:

00000000 strlen: 0: 03 46 mov r3, r0 2: 13 f8 01 2b ldrb r2, [r3], #​1 6: 00 2a cmp r2, #​0 8: fb d1 bne #-10 <strlen+0x6> a: 18 1a subs r0, r3, r0 c: 01 38 subs r0, #​1 e: 70 47 bx lr

However the GNU binutils do not:

$ arm-none-eabi-objdump -d lib_a-strlen.o

lib_a-strlen.o: file format elf32-littlearm

Disassembly of section .text:

00000000 : 0: 4603 mov r3, r0 2: f813 2b01 ldrb.w r2, [r3], #​1 6: 2a00 cmp r2, #​0 8: d1fb bne.n 2 <strlen+0x2> a: 1a18 subs r0, r3, r0 c: 3801 subs r0, #​1 e: 4770 bx lr

Maybe I used the wrong version while troubleshooting this issue...

Sorry for the trouble. I guess this issue can be closed.

Thanks.

eblot avatar Sep 27 '19 14:09 eblot

Indeed it should.

I don't have a clang 9 around, but with a recent trunk build, both llvm-mc and llvm-objdump disassemble the instruction correctly.

$ ./bin/llvm-objdump --disassemble ldr.o

ldr.o:	file format ELF32-arm-little


Disassembly of section .text:

00000000 $t.0:
       0: 03 46                        	mov	r3, r0
       2: 13 f8 01 2b                  	ldrb	r2, [r3], #&#8203;1
       6: 00 2a                        	cmp	r2, #&#8203;0
       8: fb d1                        	bne	#-10 <$t.0+0x6>
       a: 18 1a                        	subs	r0, r3, r0
       c: 01 38                        	subs	r0, #&#8203;1
       e: 70 47                        	bx	lr
$ ./bin/llvm-mc -triple armv7m-eabi --show-encoding <<< "ldrb  r2, [r3], #&#8203;1" 
	.text
	ldrb	r2, [r3], #&#8203;1            @ encoding: [0x13,0xf8,0x01,0x2b]
$ ./bin/llvm-mc  -triple armv7m-eabi  --disassemble <<< "[0x13,0xf8,0x01,0x2b]"
	.text
	ldrb	r2, [r3], #&#8203;1
$ 

momchil-velikov avatar Sep 27 '19 14:09 momchil-velikov

Hi,

Sorry, I did misread the T2 syntax, thanks.

So is there a decoding issue with objdump that decodes ldrb r2, [r3], #​1 as ldrb.w r2, [r3], #​1

Should it not decode it as

ldrb r2, [r3], #​1

Thanks.

eblot avatar Sep 27 '19 14:09 eblot

Hello,

There's no such form of ldrb.w with post-increment.

LDRB.W ,[{,#}] takes 12-bit immediate offset, note the placement of square brackets.

But there are forms with 8-bit signed immediate offset and pre- and post-increment, which should be fine for you case. You can use

ldrb r2, [r3], #​1

i.e. just delete the ".w" part. That's encoding T3 in the manual.

HTH, ~chill

momchil-velikov avatar Sep 27 '19 14:09 momchil-velikov

assigned to @jcai19

eblot avatar Sep 20 '19 14:09 eblot