crt_init.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * File : crt_init.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2008-2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2014-12-03 Bernard Add copyright header.
  23. * 2014-12-29 Bernard Add cplusplus initialization for ARMCC.
  24. */
  25. #include <rtthread.h>
  26. #ifdef __CC_ARM
  27. extern void $Super$$__cpp_initialize__aeabi_(void);
  28. /* we need to change the cpp_initialize order */
  29. void $Sub$$__cpp_initialize__aeabi_(void)
  30. {
  31. /* empty */
  32. }
  33. #endif
  34. int cplusplus_system_init(void)
  35. {
  36. #if defined(__GNUC__) && !defined(__CC_ARM)
  37. extern unsigned char __ctors_start__;
  38. extern unsigned char __ctors_end__;
  39. typedef void (*func)(void);
  40. /* .ctors initalization */
  41. func *ctors_func;
  42. for (ctors_func = (func *)&__ctors_start__;
  43. ctors_func < (func *)&__ctors_end__;
  44. ctors_func ++)
  45. {
  46. (*ctors_func)();
  47. }
  48. #elif defined(__CC_ARM)
  49. /* If there is no SHT$$INIT_ARRAY, calling
  50. * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
  51. * the problem still exists. So we have to initialize the C++ runtime by ourself.
  52. */
  53. typedef void PROC();
  54. extern const unsigned long SHT$$INIT_ARRAY$$Base[];
  55. extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
  56. const unsigned long *base = SHT$$INIT_ARRAY$$Base;
  57. const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
  58. for (; base != lim; base++)
  59. {
  60. PROC *proc = (PROC*)((const char*)base + *base);
  61. (*proc)();
  62. }
  63. #endif
  64. return 0;
  65. }
  66. INIT_COMPONENT_EXPORT(cplusplus_system_init);