2

This code compiles just fine on gcc, but when using llvm (llvm-gcc), it says "constant expression expected" on the line with ldr

The problem is the syntax: How do I specify the place where my array is? I do not want to hard-code the displacement in bytes: ldr r7, [pc, #some_shift] but to use a literal to keep the code clean and safe.

Any idea how to make it working?

.globl func_name

func_name:
     push   {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}

//[Some stripped code]

     add    r6, r6, sl, lsl #2
     sub    ip, ip, sl
     ldr    r7, =maskTable           // Here it crashes
     add    sl, sl, #4  @ 0x4

// Some stripped code here

     mov    r0, #0  @ 0x0 // return 0
     pop    {r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}

     .word  0x00000000

.data
.align 5
maskTable:

    .word  0x00000000, 0x00000000, 0x00000000, 0x00000000
    .word  0x0000FFFF, 0x00000000, 0x00000000, 0x00000000
    .word  0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000
7
  • Did you ask on the llvm mailing list? LLVM 3.1 will have an ARM assembler, but in the mean time they are the best resource for question about LLVM's behavior. Or reading the source code, of course. llvm.org/docs/CodeGenerator.html#targetfeatures says ARM asm parser is not supported, so it's using gas for your code. Commented Mar 10, 2012 at 15:37
  • Can you tell the command you are issuing to build this code? Like in gcc -fpic hello.c ? Commented Mar 12, 2012 at 20:20
  • @vasile Any luck? I wanted to know your build command so that I can exactly reproduce your situation Commented Mar 13, 2012 at 6:51
  • I as able to compile with a slightly modified code based on your sugestion (removed the line with .data, and use name instead of .name). But now I have some problems with linking. I will try to better understand the problem and then I'll post some more details Commented Mar 13, 2012 at 8:20
  • I compile with some default parameters: -x -assembler-with-cpp -mdynamic-no-pic -arch armv7 and some other, unrelated flags Commented Mar 13, 2012 at 8:22

3 Answers 3

2
+150

Try changing

ldr r7, =maskTable

to

ldr r7, maskTable

and remove

.data

section. It seems to be a bug/missing capability of gcc < 4.6 to deal with .data section

Sign up to request clarification or add additional context in comments.

Comments

1

There are two things you can try:

  1. Change ldr r7, =maskTable into adr r7, maskTable.
  2. Store the address of the table under a separate label and load it manually like follows:

.globl func_name

func_name:
     push   {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}

//[Some stripped code]

     add    r6, r6, sl, lsl #2
     sub    ip, ip, sl
     ldr    r7, maskTable_adr           // Here it crashes
     add    sl, sl, #4  @ 0x4

// Some stripped code here

     mov    r0, #0  @ 0x0 // return 0
     pop    {r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}

     .word  0x00000000

.data
.align 5
maskTable_adr:
    .word   maskTable

maskTable:

    .word  0x00000000, 0x00000000, 0x00000000, 0x00000000
    .word  0x0000FFFF, 0x00000000, 0x00000000, 0x00000000
    .word  0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000

Comments

0

I don't know the answer myself, but if it was me, I'd look at some compiled C code, and see how the compiler does it. Make sure that the compiler isn't in PIC mode, or something, or it'll do something more complicated and unnecessary.

2 Comments

I need the PIC flag, so I cannot simplify the case. And the compiled code has a hardcoded value there: ldr r7 [pc #138] wchich I really do not want, because if I change something in the .S file, it will need an update to the shift position. And this is ugly.
I find it unlikely that the compiler output has hard offsets (but then I've not used LLVM much). Are you looking at the assembler code, or a disassembly? Normally these are filled in by the assembler, if they are within the same section, or by the linker, if the symbol is in a different section, or a different file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.