ARM unaligned memory access workaround

后端 未结 3 1895
Happy的楠姐
Happy的楠姐 2021-01-11 19:30

I have to port source code from to an ARM platform that runs Linux. Unfortunately I have run into unaligned memory access problems. The source uses pointer casts and access

相关标签:
3条回答
  • 2021-01-11 19:46

    There is __attribute__((__packed__)) which might help in some instances, but I really think this code should be cleaned up rather sooner than later, because it is likely that you will spend more time working around problems than it would take to fix it once and for all.

    0 讨论(0)
  • 2021-01-11 19:46

    We can assume that the problem originates from the fact that ARM is a 32 bit machine and the Linux box runs in 64 bit mode, alternatively, the code could assume that is is running on a 16 bit machine.

    One way would be to look at the underlying structure that is accessed. The members "H" and "L" might be 32 bit types that are accessed as though they are 64 bit.

    Try to modify the types of L and H to make the code behave better.

    (Admittedly, this is a stab into thin air, as the code snippet doesn't reveal the details of the application, nor of the underlying structures.)

    0 讨论(0)
  • 2021-01-11 19:48

    Wow, that's an unholy mess. Fiddling with the compiler isn't going to get you anywhere. The code is illegal on ALL architectures, but just happens to work on some (e.g x86). I would fix the code itself.

    Sadly there's no pretty way to do that. However, you might get a long way with a long list of search-and-replaces, then manually fix up the rest. I'd start by removing the declarations of those data types, so if you compile any code you missed, it'll error. Then, search-and-replace snippets such as "*(IEC_DWORD OS_SPTR *)pSP =" with "set_dword(pSP, ". Make an inline function "set_dword" to do the right thing. Carry on for as many easily replaced snippets as you can imagine. There will still be a large amount to fix up by hand.

    The only other way I can think of doing this would be a compiler plugin, as you suggest, and make every pointer in the entire compilation unit have alignment 1. The compiler will then byte load/store everything. It'll probably end up doing that for more than just the code you intended. This probably isn't as easy to achieve as it sounds.

    0 讨论(0)
提交回复
热议问题