This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Dealing with non-aligned data

Hello,

A colleague of mine, while processing some binary data, transfered a RAM memory address to a function. That address was not even (ARM7 architecture) so the LDR instruction behaved exactly as the assembly manual specifies it should in case of a misaligned address - round in the address + some more shift magic, which created a memory access off by 1 byte. My question is: Is there a way to deal with this runtime error in advance?

  • What exactly was this supposed to do anyway? Copy the function code to some place?

    First of all, the reason the function address is odd, is that the LSB is used to distinguish ARM from Thumb functions.

    The function in question was most likely a Thumb function. You could just clear the LSB of the function address to get the real function address. If it has to be assembler, use LDRH/STRH to copy instructions, since the worst case is half word aligned addresses.

    Without more input, this is all I can say.

    Regards
    Marcus
    http://www.doulos.com/arm/

  • Marcus,

    I thought it was clear that the address in question was the address of the data to process, not the instruction to execute...

  • Is there a way to deal with this runtime error in advance?<p>

    Depending on which chip you use, the memory controller may be able to detect misaligned accesses and signal a data abort.

  • Christoph,

    Thanks for your reply. Hmm, that was not what I was hoping to hear. But still it makes perfect sense - the compiler cannot know in advance whether a pointer will contain an unaligned address or not. I guess that you just have to be prepared for it. What is rather worrying (maybe it is a question of taste or style...) is that some ARM cores (non Cortex) do not complain at all if an unaligned access is done. So one can write/import broken code, without ever knowing _why_ it does not work as expected. The C166 resets when an unaligned access is executed, as far as I recall.

  • is that some ARM cores (non Cortex) do not complain at all if an unaligned access is done.

    It's not the fault of the core, really. If I remember correctly, the ARM7 core was originally designed to be used in ASICs, and hence does not come with any fancy peripherals (no memory controller). It's up to the chip designer to include (or omit, if minimal gate count/power consumption is required) these.

    Then again, producing a misaligned access in C requires playing fast and loose with pointers.

  • My question is: Is there a way to deal with this runtime error in advance?

    Sure there is. You can use __packed pointers (feature of Realview) to access unaligned data. You can detect unaligned pointers using some arithmetics:

    if ((uintptr_t)ptr & 3 != 0)
    {
        /* Error: bad pointer alignment */
    }
    

  • What difference two letters can make. I was reading this as
    "[...]transfered a RAM memory address of a function. That
    address was not even [and caused grief, etc]". Sorry about this.

    Back to the question -- it is still not clear what the situation
    is. Are you using assembler or C? Why use an LDR in the first place if
    the address may be unaligned? Or were you just observing the compiler
    generate an LDR? If C, why not just use memcpy() and properly declared
    pointers?

    I would not use a run-time check for this. If the pointer has been
    properly declared, as e.g. (char *) or (__packed int *) (as others
    have pointed out), memcpy() is likely your best option.

    > What is rather worrying (maybe it is a question of taste or
    > style...) is that some ARM cores (non Cortex) do not complain at all
    > if an unaligned access is done.

    In fact none of them complains by default. Some return rotated data,
    some others force the two LSB to 0. Some allow to trap unaligned
    access, others don't.

    > So one can write/import broken code

    As usual with low level languages.

    Best regards
    Marcus
    http://www.doulos.com/arm/

  • Marcus,

    If C, why not just use memcpy() and properly declared
    pointers?

    because we didn't feel like using __packed due to its toll...
    I think the question has been answered to my satisfaction. thank you all.