• AVR Freaks

Hot!Replacing the C runtime library?

Author
realexander
Super Member
  • Total Posts : 230
  • Reward points : 0
  • Joined: 2006/04/08 09:50:42
  • Location: 0
  • Status: offline
2020/05/27 15:26:08 (permalink)
0

Replacing the C runtime library?

Hi,
 
I'm running into strange behavior with the C runtime library, e.g. crashes in sprintf and bizarre results from gmtime. I'm not able to reproduce it in a small program, and I may well be stomping on memory somewhere. But it's really tough to debug without the library source code.
 
Has anyone had experience replacing the C runtime with an open source version? Is there any documentation on the procedure?
 
- Bob
 
#1

5 Replies Related Threads

    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11876
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Replacing the C runtime library? 2020/05/27 16:18:13 (permalink)
    +2 (2)
    The most likely cause of your sprintf crash is overflowing the target buffer.  Use snprintf instead.
     
    gmtime is not reentrant; are you calling it in an interrupt handler?
    #2
    realexander
    Super Member
    • Total Posts : 230
    • Reward points : 0
    • Joined: 2006/04/08 09:50:42
    • Location: 0
    • Status: offline
    Re: Replacing the C runtime library? 2020/05/27 16:22:44 (permalink)
    0
    The buffer I provide for sprintf is twice as big as it needs to be. And I initialize it to 0xff so I can see that, after the crash, the entire buffer was not in use. I can even see how far sprintf got before it crashed: it was processing a %02d.
     
    I'm not calling gmtime from a interrupt. I discovered, though, that other time functions use the same buffer as gmtime. Again, that's not what's happening here.
     
    #3
    RISC
    Super Member
    • Total Posts : 5731
    • Reward points : 0
    • Status: online
    Re: Replacing the C runtime library? 2020/05/27 16:34:02 (permalink)
    +2 (2)
    Hi,
    For XC compilers you should be able to find the source code of the startup files as they are provided so that users can edit them.
    Look into your compiler directories there is an assembly file  (look for crt0.S).
     https://www.microchip.com/forums/m1085270.aspx
    Regards
     
    #4
    LdB_ECM
    Super Member
    • Total Posts : 377
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Replacing the C runtime library? 2020/05/27 21:40:06 (permalink)
    +1 (1)
    You should be able to just add a user command line flag depending how much you want to replace. You take out the default lib you will have to provide a vector table and clear the BSS.
    -nodefaultlibs
    -nostdlib
     
    #5
    moser
    Super Member
    • Total Posts : 568
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: Replacing the C runtime library? 2020/06/08 03:31:44 (permalink)
    0
    If you are also printing floats or doubles, then make sure those are not nan or inf. For example check with the finite() function or with isnan() and isinf().
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5