[LLVMdev] question about alignment of structures on the stack (arm 32)

Tim Northover t.p.northover at gmail.com
Tue Apr 21 09:15:02 PDT 2015


> I'm using MachO loader (https://github.com/LubosD/darling/). I'm trying to make it work on ARM.
> The scenario is to load MachO binary (e.g. compiled in xCode) that binary is invoking function from
> ELF library which implements libobjc2 and CoreFoundation.
>
>  in MachO on the ARM stack is 4-bytes aligned. Code produced for ELF expects 8-bytes alignment.
> So in 50% cases when call made from MachO to ELF stack pointer register contains not a 8-bytes aligned address.

Ah, that could do it. I see that LLVM does indeed make use of stack
alignment in this case. Regardless, this approach is going to go
really badly.

By default almost all ELF platforms use an ABI called AAPCS (either
hard or soft float). iOS uses an older ABI called APCS. You can't mix
code from these two worlds in any kind of non-trivial case without a
translation layer.

You've discovered one issue: AAPCS requires 8-byte alignment for sp,
APCS only requires 4. It's the first of many without a more thorough
approach to the interface between the two.

> I not yet tested some __attribute__((pcs("aapcs")))/-target-abi, maybe there is magic pcs attribute, and I could apply it for dangerous function, but I would prefer to solve that problem in general.

I don't think so; __attribute__((pcs("apcs"))) might work, if it
existed. But it doesn't. You might find it's fairly easy to add it in
Clang, but I worry about the assumptions being made in the backend.

Either way, I'd recommend against trying to hack just this one stack
alignment issue.

Tim.



More information about the llvm-dev mailing list