Help! Am I going mad
or is there something obviously wrong with my C programming
skills?
Here are two very
simple files to compile. What I have found is that if I compile the fn.c code
using gcc and ccc for the main code, it gets the wrong answer. Whereas it works
if I use either ccc or gcc for both. It also works if I compile the fn.c code
with ccc and main with gcc.
Anyone know what's
wrong ... is this a bug with ccc or gcc ?
cat
fn.c
unsigned
int fn()
{
return
(0xc00fc00f);
}
cat
madness.c
extern unsigned int
fn();
#define MAGIC
0xc00fc00f
int
main()
{
unsigned int baseAddr;
baseAddr = fn();
if (baseAddr == MAGIC)
printf("GOOD baseAddr %x == %x\n", baseAddr, MAGIC);
else
printf("BADNESS baseAddr %x
!= %x\n", baseAddr, MAGIC);
}
Then compile as
follows;
cc -O3 -c fn.c -o
fn_gcc.o
ccc -O3 -c fn.c -o
fn_ccc.o
ccc -O3 madness.c -o
madness_gcc fn_gcc.o
ccc -O3 madness.c -o
madness_ccc fn_ccc.o
Then
run;
./madness_ccc
GOOD baseAddr c00fc00f == c00fc00f
./madness_gcc
BADNESS baseAddr c00fc00f != c00fc00f
It starts working if
I change the MAGIC constant to 0x400fc00f so that suggests sign problems
?
Disassembling the fn
code we see the only differences ...
objdump -d fn_gcc.o
fn_gcc.o: file format
elf64-alpha
Disassembly of
section .text:
0000000000000000
<fn>:
0: 0f c0 1f 20
lda v0,-16369(zero)
4: 10 c0
00 24 ldah
v0,-16368(v0)
8: 20 f6 01 48
zapnot v0,0xf,v0
c: 01 80 fa
6b ret
zero,(ra),0x1
...
objdump -d fn_ccc.o
fn_ccc.o: file format
elf64-alpha
Disassembly of
section .text:
0000000000000000
<fn>:
0: 10 c0 1f 24
ldah v0,-16368(zero)
4: 1f 04 ff
47 nop
8: 0f c0 00
20 lda
v0,-16369(v0)
c: 01 80 fa 6b
ret zero,(ra),0x1
GCC is inserting a
ZAPNOT instruction to zero the top 4-bytes - is this correct
?
Or is the main code
compilation that is broken, I notice that ccc uses 'xor' to do the compare
whereas gcc uses 'cmpeq' ?
For the record, I am
using Alpha RH6.1 with ccc (ccc-6.2.9.504-2) and gcc
(egcs-2.91.66)
Cheers,
Addy.