2008-08-18 10:13 AM
object code generation issue with codesourcery toolchain arm-2008q1
2011-05-17 03:42 AM
Hello !
I encountered to following problem with object code for STM32 generated by codesourcery toolchain arm-2008q1, while trying to compile given C file. Here it is its contents: //strncpy.c char *strncpy(char *pDest, const char *pSrc, size_t Num) { char *pS; for (pS = pDest;0 *pS++ = *pSrc++; /* Kopiert maximal Num Zeichen von pSrc */ for (; 0 *pS++ = '\0'; return (pDest); } //End******************************************************************** Object code generated by codesourcery toolchain is: 080039fc : 80039fc: b500 push {lr} 80039fe: 4696 mov lr, r2 8003a00: b1aa cbz r2, 8003a2e 8003a02: 780b ldrb r3, [r1, #0] 8003a04: b90b cbnz r3, 8003a0a 8003a06: 4602 mov r2, r0 8003a08: e00a b.n 8003a20 8003a0a: 4602 mov r2, r0 8003a0c: 7013 strb r3, [r2, #0] 8003a0e: f11e 3eff adds.w lr, lr, #4294967295 ; 0xffffffff8003a12: d00c beq.n 8003a2e 8003a14: f102 0201 add.w r2, r2, #1 ; 0x1 8003a18: f811 3f01 ldrb.w r3, [r1, #1]! 8003a1c: 2b00 cmp r3, #0 8003a1e: d1f5 bne.n 8003a0c 8003a20: f04f 0300 mov.w r3, #0 ; 0x0 8003a24: f802 3b01 strb.w r3, [r2], #1 8003a28: f11e 3eff adds.w lr, lr, #4294967295 ; 0xffffffff
8003a2c: d1f8 bne.n 8003a20 8003a2e: bd00 pop {pc} //********************************************************************** Used gcc options to compile are : CFLAGS_DEFAULT = -c -pipe -mthumb -mcpu=cortex-m3 -Wcomment -Wimplicit -Wreturn-type -Wunused -Wuninitialized -ffunction-sections -fdata-sections -fomit-frame-pointer -nostdinc -std=gnu89 -ffreestanding //************************************************************************* The problem i get in the code snippet above is fact, that link register(LR) is used as counter variable in loop implementation, which could be seen more precisely in these 2 instructions(marked with bold in in disassembly): '' adds.w lr, lr, #4294967295 ; 0xffffffff bne.n 8003a20 '' and '' 8003a0e: f11e 3eff adds.w lr, lr, #4294967295 ; 0xffffffff 8003a12: d00c beq.n 8003a2e '' When I debug my application, LR value never reaches zero, so I get an hard fault exception(possibly when I reach out beyond onboard memory borders). Although ''-mthumb -mcpu=cortex-m3'' is used, code generated for loop implementation is very strange to me. I 've looked through object code, generated for loop implementation, and I've never seen such a thing. Is codesourcery toolchain is responsible for emitting ''uncorrect'' code or gcc options I used are responsible. I hope somebody can tell me where I get wrong.
2011-05-17 03:42 AM
Hi ,
When you debug you code , I'm wondering how you are calling your function char *strncpy(char *pDest, const char *pSrc, size_t Num) in your main code? What is the value of Num ? I see in the assembly that lr = r2 at the beginning which should contain Num variable and tests if Num=0 then exits the function in that case. Cheers, STOne-32.2011-05-17 03:42 AM
Thanks for the reply and I'm sorry for the missing info.
I didn't mention it, but of course strncpy() is not calling itself. It is called in main application, where strncpy() is called with Num=4. Yes, lr is set to r2(lr=r2). When debug my application Num never reaches zero. The point is : I know, that LR can be used as general purpose register, when it's not used for return from function. But is there any reason, preventing LR value to not be changed?