crt1.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* startup code for Cortex-M3*/
  2. #define USE_LIBC 1
  3. #pragma GCC optimize("Os")
  4. extern unsigned long _etext, _sdata, _edata, _end;
  5. extern void main(void);
  6. extern void __libc_init_array(void);
  7. /* come here on power up, copy the .data and clear the .bss */
  8. __attribute__ ((noreturn)) void _start(void){
  9. unsigned long *pulSrc, *pulDest;
  10. /* Copy the data segment initializers from flash to SRAM */
  11. pulSrc = &_etext;
  12. pulDest = &_sdata;
  13. for(; pulDest < &_edata; )
  14. *(pulDest++) = *(pulSrc++);
  15. /* Zero fill the bss segment. */
  16. for(; pulDest < &_end; )
  17. *(pulDest++) = 0;
  18. #ifdef USE_LIBC
  19. /* initialize the libc */
  20. __libc_init_array();
  21. #endif
  22. /* jump to user program */
  23. while (1)
  24. main();
  25. }
  26. #ifdef USE_LIBC
  27. #include <sys/errno.h>
  28. extern unsigned int _stack_bottom;
  29. #define RAM_END (void*)&_stack_bottom
  30. /*
  31. * sbrk -- changes heap size size. Get nbytes more
  32. * RAM. We just increment a pointer in what's
  33. * left of memory on the board.
  34. */
  35. void * _sbrk(nbytes)
  36. int nbytes;
  37. {
  38. static void * heap_ptr = (void*) &_end;
  39. void * base;
  40. if (heap_ptr + nbytes < RAM_END) {
  41. base = heap_ptr;
  42. heap_ptr += nbytes;
  43. return (base);
  44. }
  45. errno = ENOMEM;
  46. return ((void*)-1);
  47. }
  48. #endif