Hi All,
I am using EFM32GG11 cpu and have problem with malloc function. It crashes on the first call.
I have specified stack and heap areas in the scatter file.
I would like to debug through malloc function. I am using the standart C library linked to my keil MDK 5 project,
How could i build c runtime and link it to my project?
Thanks,
Define "crashes"
Where does the processor end up?
If you have a Hard Fault you're touching some memory oddly. Check that the memory described to the linker matches the capabilities of the part.
Check how memory is arranged in the .MAP file.
Make sure your SystemInit() code enables clocks, external memories, etc.
Use FromELF to disassemble the .AXF file so you can play along with the debugger. You can step into code where you lack source, it is not that hard.
Pretty sure Keil's code is solid, so issue is likely with structures, or initialization.
Also shouldn't be hard to figure the linked-list implementation, and walk the heap structures.
Check structure alignment, CM4 will fault on unaligned access with LDRD,STRD
Hello,
Thanks for reply. I have checked that HardFauld occur due to read at 0xFFFFFFFF address. Somehow this address is calculated inside malloc function. I currently far from being able to walk in disassembly and check what happens with linked list.
I have some troubles with scatter file. I found an example scatter file without heap definition.
I have defined ARM_LIB_HEAP variable.
ARM_LIB_HEAP 0x2007F000 EMPTY HeapSize {
}
There is a
RW_IRAM1 0x20000158 0x0007EEA8 { ; RW data .ANY (+RW +ZI) }
region for RAM.
I have added another region for heap
RW_IRAM2 0x2007F000 0xC00 { ; RW data .ANY (+RW +ZI) }
Still Hard fault.
I would like to find out how C runtime library initialize it's internal variables for heap base?
Maybe some init function must be called before C runtime?
My systeminit function is called and clocks are initialized, without malloc cpu is working fine.
The Heap/Stack are more usually defined in startup.s
;******************************************************************************* ; ; Amount of memory (in bytes) allocated for Stack ; Tailor this value to your application needs ; <h> Stack Configuration ; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> Stack_Size EQU 0x0800 AREA STACK, NOINIT, READWRITE, ALIGN=3 Stack_Mem SPACE Stack_Size __initial_sp ; <h> Heap Configuration ; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> Heap_Size EQU 0x2000 AREA HEAP, NOINIT, READWRITE, ALIGN=3 __heap_base Heap_Mem SPACE Heap_Size __heap_limit ;*****************************************************************************
When using MicroLib, exporting
. EXPORT __initial_sp EXPORT __heap_base EXPORT __heap_limit
What the plumbing looks like
From .MAP HEAP 0x20002c50 Section 8192 startup_stm32l432xx.o(HEAP) ... __microlib_freelist 0x20000004 Data 4 mvars.o(.data) __microlib_freelist_initialised 0x20000008 Data 4 mvars.o(.data) ... __heap_base 0x20002c50 Data 0 startup_stm32l432xx.o(HEAP) __heap_limit 0x20004c50 Data 0 startup_stm32l432xx.o(HEAP) __initial_sp 0x20005450 Data 0 startup_stm32l432xx.o(STACK) FromELF Listing i.malloc malloc 0x08006f2c: b5f0 .. PUSH {r4-r7,lr} 0x08006f2e: 300b .0 ADDS r0,r0,#0xb 0x08006f30: 4d15 .M LDR r5,[pc,#84] ; [0x8006f88] = 0x20000008 0x08006f32: f0200107 ... BIC r1,r0,#7 0x08006f36: 2400 .$ MOVS r4,#0 0x08006f38: 4a14 .J LDR r2,[pc,#80] ; [0x8006f8c] = 0x20000004 0x08006f3a: e00f .. B 0x8006f5c ; malloc + 48 0x08006f3c: 6803 .h LDR r3,[r0,#0] 0x08006f3e: 428b .B CMP r3,r1 0x08006f40: d30b .. BCC 0x8006f5a ; malloc + 46 0x08006f42: 428b .B CMP r3,r1 0x08006f44: d905 .. BLS 0x8006f52 ; malloc + 38 0x08006f46: 1a5e ^. SUBS r6,r3,r1 0x08006f48: 1843 C. ADDS r3,r0,r1 0x08006f4a: 6847 Gh LDR r7,[r0,#4] 0x08006f4c: e9c36700 ...g STRD r6,r7,[r3,#0] 0x08006f50: e000 .. B 0x8006f54 ; malloc + 40 0x08006f52: 6843 Ch LDR r3,[r0,#4] 0x08006f54: 6013 .` STR r3,[r2,#0] 0x08006f56: c002 .. STM r0!,{r1} 0x08006f58: e004 .. B 0x8006f64 ; malloc + 56 0x08006f5a: 1d02 .. ADDS r2,r0,#4 0x08006f5c: 6810 .h LDR r0,[r2,#0] 0x08006f5e: 2800 .( CMP r0,#0 0x08006f60: d1ec .. BNE 0x8006f3c ; malloc + 16 0x08006f62: 4620 F MOV r0,r4 0x08006f64: 2800 .( CMP r0,#0 0x08006f66: d102 .. BNE 0x8006f6e ; malloc + 66 0x08006f68: 6828 (h LDR r0,[r5,#0] 0x08006f6a: b108 .. CBZ r0,0x8006f70 ; malloc + 68 0x08006f6c: 2000 . MOVS r0,#0 0x08006f6e: bdf0 .. POP {r4-r7,pc} 0x08006f70: 4a06 .J LDR r2,[pc,#24] ; [0x8006f8c] = 0x20000004 0x08006f72: 4807 .H LDR r0,[pc,#28] ; [0x8006f90] = 0x20002c54 0x08006f74: 6010 .` STR r0,[r2,#0] 0x08006f76: 4a07 .J LDR r2,[pc,#28] ; [0x8006f94] = 0x20004c50 0x08006f78: 1a12 .. SUBS r2,r2,r0 0x08006f7a: f0220207 "... BIC r2,r2,#7 0x08006f7e: e9c02400 ...$ STRD r2,r4,[r0,#0] 0x08006f82: 2001 . MOVS r0,#1 0x08006f84: 6028 (` STR r0,[r5,#0] 0x08006f86: e7d7 .. B 0x8006f38 ; malloc + 12 $d 0x08006f88: 20000008 ... DCD 536870920 __microlib_freelist_initialised 0x08006f8c: 20000004 ... DCD 536870916 __microlib_freelist 0x08006f90: 20002c54 T,. DCD 536882260 __heapbase + 4 0x08006f94: 20004c50 PL. DCD 536890448 __heaplimit $t
Annotated listing
08006F2C SUB16 malloc: ; Xref 8001E6C 8002B48 08006F2C B5F0 push {r4, r5, r6, r7, lr} 08006F2E 300B adds r0, #11 08006F30 4D15 ldr r5, [pc, #84] ; ($8006F88=$20000008) __microlib_freelist_initialised 08006F32 F020 0107 bic.w r1, r0, #7 ; $7 08006F36 2400 movs r4, #0 ; NULL 08006F38 LOC loc_8006F38: ; Xref 8006F86 08006F38 4A14 ldr r2, [pc, #80] ; ($8006F8C=$20000004) __microlib_freelist 08006F3A E00F b.n loc_8006F5C 08006F3C LOC loc_8006F3C: ; Xref 8006F60 08006F3C 6803 ldr r3, [r0, #0] 08006F3E 428B cmp r3, r1 08006F40 LOC loc_8006F40: ; Xref 8006FC6 08006F40 D30B bcc.n loc_8006F5A 08006F42 428B cmp r3, r1 08006F44 D905 bls.n loc_8006F52 08006F46 1A5E subs r6, r3, r1 08006F48 1843 adds r3, r0, r1 08006F4A 6847 ldr r7, [r0, #4] 08006F4C E9C3 6700 strd r6, r7, [r3] 08006F50 E000 b.n loc_8006F54 08006F52 LOC loc_8006F52: ; Xref 8006F44 08006F52 6843 ldr r3, [r0, #4] 08006F54 LOC loc_8006F54: ; Xref 8006F50 08006F54 6013 str r3, [r2, #0] 08006F56 C002 stmia r0!, {r1} 08006F58 E004 b.n loc_8006F64 08006F5A LOC loc_8006F5A: ; Xref 8006F40 08006F5A 1D02 adds r2, r0, #4 08006F5C LOC loc_8006F5C: ; Xref 8006F3A 08006F5C 6810 ldr r0, [r2, #0] 08006F5E 2800 cmp r0, #0 08006F60 D1EC bne.n loc_8006F3C 08006F62 4620 mov r0, r4 08006F64 LOC loc_8006F64: ; Xref 8006F58 08006F64 2800 cmp r0, #0 08006F66 D102 bne.n loc_8006F6E ; allocation success, leave 08006F68 6828 ldr r0, [r5, #0] ; [__microlib_freelist_initialised] 08006F6A B108 cbz r0, loc_8006F70 ; init == 0? 08006F6C 2000 movs r0, #0 08006F6E LOC loc_8006F6E: ; Xref 8006F66 08006F6E BDF0 pop {r4, r5, r6, r7, pc} 08006F70 LOC loc_8006F70: ; Xref 8006F6A 08006F70 4A06 ldr r2, [pc, #24] ; ($8006F8C=$20000004) __microlib_freelist 08006F72 4807 ldr r0, [pc, #28] ; ($8006F90=$20002C54) __heap_base + 4 08006F74 6010 str r0, [r2, #0] 08006F76 4A07 ldr r2, [pc, #28] ; ($8006F94=$20004C50) __heap_limit 08006F78 1A12 subs r2, r2, r0 08006F7A F022 0207 bic.w r2, r2, #7 ; $7 08006F7E LOC loc_8006F7E: ; Xref 8006FC0 08006F7E E9C0 2400 strd r2, r4, [r0] ; linked list (size this, prev) 08006F82 2001 movs r0, #1 08006F84 6028 str r0, [r5, #0] ; init = 1 08006F86 LOC loc_8006F86: ; Xref 8006FC8 08006F86 E7D7 b.n loc_8006F38 08006F88 LOC loc_8006F88: ; Xref 8006FCA 08006F88 20000008 dd $20000008 08006F8C 20000004 dd $20000004 08006F90 20002C54 dd $20002C54 08006F94 20004C50 dd $20004C50
Unfortunately i am not using MicroLib. I am using default C library. (no check in Keil Project options for Use MicroLib).
Currently i can't understand one issue. I have specified the scatter file to the linker. I have
ARM_LIB_HEAP 0x2007F000 EMPTY HeapSize { }
line.
Nut in .map file i see
HEAP 0x20019cc8 Section 3072 startup_efm32gg11b.o(HEAP) Heap_Mem 0x20019cc8 Data 3072 startup_efm32gg11b.o(HEAP)
Why address is 0x20019cc8 and not 0x2007F000 ? I can't understand.