1.2.3. Porting code and detecting unaligned accesses

Legacy C code for other architectures (for example, x86 CISC) might perform accesses to unaligned data using pointers that do not work on ARM processors. This is non-portable code, and such accesses must be identified and corrected to work on RISC architectures, which expect aligned data.

Identifying the unaligned accesses can be difficult, because the use of load or store operations with unaligned addresses gives incorrect behavior. It is difficult to trace which part of the C source is causing the problem.

ARM processors with full Memory Management Units (MMUs), for example, the ARM926™, support optional alignment checking, where the processor checks every access to ensure it is correctly aligned. The MMU raises a Data Abort if an incorrectly aligned access occurs.

For simple cores such as the ARM7TDMI®, it is recommended that alignment-checking be implemented within the Application Specific Integrated Circuit (ASIC) or Application Specific Standard Product (ASSP). You can do this with an additional hardware block that is external to the ARM core, and that monitors the access size and the least significant bits of the address bus for every data access. You can configure the ASIC/ASSP to raise the ABORT signal in the case of an unaligned access. ARM Limited recommends that such logic is included on ASIC/ASSP devices where code is ported from other architectures.

If the system is configured to abort on unaligned accesses, a Data Abort exception handler must be installed. When an unaligned access occurs, the Data Abort handler is entered, and this can identify the erroneous data access instruction, which is located at (r14-8).

When identified, you must fix the data access by changing the C source. These changes can be made conditional using the following:


#ifdef __arm
  #define  PACKED  __packed
#else
  #define  PACKED
#endif
...
  PACKED int *pi;
...

It is best to minimize accesses to unaligned data because of code size and performance overheads.

See the description of the --pointer_alignment and --min_array_alignment options in the section on controlling code generation of RealView Compilation Tools v3.0 Compiler and Libraries Guide.

Copyright © 2002-2006 ARM Limited. All rights reserved.ARM DUI 0203G
Non-Confidential