Unaligned access question
Hi,
This isn't directly related to kernel development, but I am a bit stumped as to why my test program below causes an unaligned access when 'i' is 7 (that is, when it tries to access across a 64 bit boundary of 'array'). It's counter intuitive to me, I would have expected it either fault on all loads (for every value of 'i') or to have not faulted at all.
(yes, this is abstracted from some code that is chopping up data coming over the network, which seems to be a common cause of alignment problems).
Thanks for any suggestions,
-i
[email protected]
http://www.gelato.unsw.edu.au
--- little test program ---
#include <stdio.h>
typedef struct a_struct_t {
unsigned char a[2];
} a_struct;void print(const a_struct st) {
printf("%p -> %c | %p -> %c\n", &st.a[0], st.a[0], &st.a[1], st.a[1]);
}int main(void)
{
unsigned char *array = "1234567812345678";
int i;
for ( i = 1 ; i < 10 ; i+=2 ) {
printf("array[%d] %p\n", i, &array[i]);
print( *((a_struct*)&array[i]) );
}
return 0;
}--- sample output ---
array[1] 0x4000000000000a790x60000fffffffbcf0 -> 2 | 0x60000fffffffbcf1 -> 3
array[3] 0x4000000000000a7b0x60000fffffffbcf0 -> 4 | 0x60000fffffffbcf1 -> 5
array[5] 0x4000000000000a7d0x60000fffffffbcf0 -> 6 | 0x60000fffffffbcf1 -> 7
array[7] 0x4000000000000a7f
align(20189): unaligned access to 0x4000000000000a7f, ip=0x4000000000000770 0x60000fffffffbcf0 -> 8 | 0x60000fffffffbcf1 -> 1
array[9] 0x4000000000000a810x60000fffffffbcf0 -> 2 | 0x60000fffffffbcf1 -> 3
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Ian" = Ian Wienand <[email protected]> writes:
Ian> Hi,
Ian> This isn't directly related to kernel development, but I am a bit Ian> stumped as to why my test program below causes an unaligned Ian> access when 'i' is 7 (that is, when it tries to access across a Ian> 64 bit boundary of 'array'). It's counter intuitive to me, I Ian> would have expected it either fault on all loads (for every value Ian> of 'i') or to have not faulted at all.
The IA64 can handle unaligned acceses itself if they're within a small window -- eight bytes for Itanium-2, can't remember for Itanium-I -- and if PSR.ac is 0.
--
Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au
You are lost in a maze of BitKeeper repositories, all slightly different.
Quoted 8 lines · Ian Wienand
Hi,
This isn't directly related to kernel development, but I am a bit stumped as to why my test program below causes an unaligned access when 'i' is 7 (that is, when it tries to access across a 64 bit boundary of 'array'). It's counter intuitive to me, I would have expected it either fault on all loads (for every value of 'i') or to have not faulted at all.
I think adding
__attribute__ ((aligned (1)))
will make it read the structure in a way that can handle less-aligned addresses. (Or maybe 2-byte aligned.) By default gcc assumes strong alignment so that it can use faster operations (ld8 or something I suppose).
As Peter said unaligned access will not necessarily trap.
Signature · 1 line
Martin
Quoted 10 lines · Martin Pool
Quoted 8 lines · Ian Wienand
Hi,
This isn't directly related to kernel development, but I am a bit stumped as to why my test program below causes an unaligned access when 'i' is 7 (that is, when it tries to access across a 64 bit boundary of 'array'). It's counter intuitive to me, I would have expected it either fault on all loads (for every value of 'i') or to have not faulted at all.
Quoted 3 lines
I think adding
__attribute__((aligned (1)))
You cannot decrease alignment with the aligned attribute, for that you need the packed attribute. This sets alignment to 1, which you can increase again with the aligned attribute.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."