Coldfire Toolchain packed structure bug version 4.1.30 and newer
Malnar, Tom
Tom.Malnar at christiedigital.com
Tue Apr 17 18:06:55 UTC 2007
We recently ugraded our tools to version 4.1.30 for the coldfire from
3.4.
Our processor is a MCF5475VR266.
We noticed a new bug in the assembler code produced when dereferencing a
packed structure.
1. The bug occurs in gcc and g++.
2. The bug occurs when using: #pramga pack(1) or __attribute__
((__packed__,aligned(1))) directives
3. The code was built using the following command line options:
gcc -g -Wall -o obj/main.o -c main.c
4. The assembly code was dumped using:
m68k-linux-gnu-objdump -lS src/testCode/obj/main.o
5. The code used in our example
#include <stdio.h>
struct TestStruct {
unsigned short var1;
};
int main() {
struct TestStruct varStruct;
struct TestStruct *pVarStruct = &varStruct;
varStruct.var1 = 1;
pVarStruct->var1 = 2;
return 0;
}
6. The good assembly code produced:
src/testCode/obj/main.o: file format elf32-m68k
Disassembly of section .text:
00000000 <main>:
main():
main.c:7
struct TestStruct {
unsigned short var1;
};
int main() {
0: 4e56 fff8 linkw %fp,#-8
main.c:9
volatile TestStruct varStruct;
volatile TestStruct *pVarStruct = &varStruct;
4: 41ee fffa lea %fp@(-6),%a0
8: 2d48 fffc movel %a0,%fp@(-4)
main.c:10
varStruct.var1 = 1;
c: 7001 moveq #1,%d0
e: 3d40 fffa movew %d0,%fp@(-6)
main.c:11
pVarStruct->var1 = 2;
12: 206e fffc moveal %fp@(-4),%a0
16: 30bc 0002 movew #2,%a0@
main.c:12
return 0;
1a: 4280 clrl %d0
main.c:13
}
1c: 4e5e unlk %fp
1e: 4e75 rts
7. Code that exhibits the issue:
#include <stdio.h>
struct __attribute__ ((__packed__,aligned(1))) TestStruct {
unsigned short var1;
};
int main() {
struct TestStruct varStruct;
struct TestStruct *pVarStruct = &varStruct;
varStruct.var1 = 1;
pVarStruct->var1 = 2;
return 0;
}
8. The assembly created by the source above. Problem: the deference of
the unsigned short variable and move is done in two 1 byte moves, plus
there also seems to be a lot of extra assembly instructions.
src/testCode/obj/main.o: file format elf32-m68k
Disassembly of section .text:
00000000 <main>:
main():
main.c:7
struct __attribute__ ((__packed__,aligned(1))) TestStruct {
unsigned short var1;
};
int main() {
0: 4e56 fff8 linkw %fp,#-8
main.c:9
TestStruct varStruct;
TestStruct *pVarStruct = &varStruct;
4: 41ee fffa lea %fp@(-6),%a0
8: 2d48 fffc movel %a0,%fp@(-4)
main.c:10
varStruct.var1 = 1;
c: 7001 moveq #1,%d0
e: 3d40 fffa movew %d0,%fp@(-6)
main.c:11
pVarStruct->var1 = 2;
12: 206e fffc moveal %fp@(-4),%a0
16: 1010 moveb %a0@,%d0
18: 4281 clrl %d1
1a: c081 andl %d1,%d0
1c: 1000 moveb %d0,%d0
1e: 1080 moveb %d0,%a0@
20: 1028 0001 moveb %a0@(1),%d0
24: 4281 clrl %d1
26: c081 andl %d1,%d0
28: 1000 moveb %d0,%d0
2a: a541 mov3ql #2,%d1
2c: 8081 orl %d1,%d0
2e: 1000 moveb %d0,%d0
30: 1140 0001 moveb %d0,%a0@(1)
main.c:12
return 0;
34: 4280 clrl %d0
main.c:13
}
36: 4e5e unlk %fp
38: 4e75 rts
9. One point to add. If we create an unsigned short pointer and assign
it to pVarStruct->var1 the assembly code produced when dereferencing the
unsigned short pointer is correct.
Does anyone have any suggestions on some things we could try? Will this
issue be addressed in a future tool chain release?
Thanks.
- Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://sourcerytools.com/pipermail/coldfire-gnu-discuss/attachments/20070417/c9048b08/attachment.html>
More information about the coldfire-gnu-discuss
mailing list