Malloc and assignment in h8300-elf
D.Venkatasubramanian, Noida
dvenkat@noida.hcltech.com
Fri Nov 15 07:30:00 GMT 2002
Hi,
I was compiling a small program with malloc and memcpy
using h8300-elf-gcc -mh.
(I am not using -mint32 flag.)
When I run this program in the simulator, the simulator hangs.
Also, there is a compilation warning generated.
On looking at the disassembly, I found
d = ((char *)malloc(6));
1d8: 79 00 00 06 79 00 00 06 mov.w #0x6,r0
1dc: 5e 00 03 8a 5e 00 03 8a jsr @0x38a:0
1e0: 0d 02 0d 02 mov.w r0,r2
1e2: 17 f2 17 f2 exts.l er2
1e4: 01 00 6f e2 01 00 6f e2 ff f8 mov.l er2,@(0xfff8:16,er6)
1e8: ff f8
Why is this instruction generated?
exts.l er2
At this point r0 has some value 0000xxxx which is moved into r2, but as
soon as this sign extension takes place, er2 becomes ffffxxxx. er2 is passed
into d.
The simulator seems to have a memory of 256 KB only, so won't ffffxxxx be
beyond that?
The simulator goes into an infinite loop in byteloop in the h8300 specific
memcpy.S
memcpy (d, s, 4);
00000d40 <byteloop>:
d40: 1b 01 1b 01 subs #0x1,er1
d42: 68 1a 68 1a mov.b @er1,r2l
d44: 6c 8a 6c 8a mov.b r2l,@-er0 <---
This instruction
d46: 1f 83 1f 83 cmp.l er0,er3
d48: 46 f6 46 f6 bne .-10 (d40)
er3 has the beginning of dst and er0 has end of destination address
at the start of this loop. er1 points to last byte of src.
At the instruction
mov.b r2l,@-er0
er0 had say ffff8273, then it becomes 007f8272, whereas it should have been
ffff8272.
er3 has ffff8271 (say). Naturally the comparison fails and this code gets
into an
infinite loop.
I was trying to find, where the problem exists, in the d = malloc code or
here in
byteloop?
Am I misinterpreting something? Please advice. The problem should be
reproducible from
the code given below.
Thanks and Regards,
Venky
________________________________________________
Program Listing
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
char *s = "Help\n";
char *d;
d = ((char *)malloc(6));
memcpy (d, s, 4);
d[5] = '\0';
printf ("String d = %s\n", d);
return 0;
}
More information about the Gcc
mailing list