crt_init.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2014-12-03 Bernard Add copyright header.
  9. * 2014-12-29 Bernard Add cplusplus initialization for ARMCC.
  10. * 2016-06-28 Bernard Add _init/_fini routines for GCC.
  11. * 2016-10-02 Bernard Add WEAK for cplusplus_system_init routine.
  12. */
  13. #include <rtthread.h>
  14. #ifdef __CC_ARM
  15. extern void $Super$$__cpp_initialize__aeabi_(void);
  16. /* we need to change the cpp_initialize order */
  17. void $Sub$$__cpp_initialize__aeabi_(void)
  18. {
  19. /* empty */
  20. }
  21. #elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__)
  22. /* The _init()/_fini() routines has been defined in codesourcery g++ lite */
  23. RT_WEAK void _init()
  24. {
  25. }
  26. RT_WEAK void _fini()
  27. {
  28. }
  29. RT_WEAK void *__dso_handle = 0;
  30. #endif
  31. RT_WEAK
  32. int cplusplus_system_init(void)
  33. {
  34. #if defined(__GNUC__) && !defined(__CC_ARM)
  35. typedef void (*pfunc) ();
  36. extern pfunc __ctors_start__[];
  37. extern pfunc __ctors_end__[];
  38. pfunc *p;
  39. for (p = __ctors_start__; p < __ctors_end__; p++)
  40. (*p)();
  41. #elif defined(__CC_ARM)
  42. /* If there is no SHT$$INIT_ARRAY, calling
  43. * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
  44. * the problem still exists. So we have to initialize the C++ runtime by ourself.
  45. */
  46. typedef void PROC();
  47. extern const unsigned long SHT$$INIT_ARRAY$$Base[];
  48. extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
  49. const unsigned long *base = SHT$$INIT_ARRAY$$Base;
  50. const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
  51. for (; base != lim; base++)
  52. {
  53. PROC *proc = (PROC*)((const char*)base + *base);
  54. (*proc)();
  55. }
  56. #endif
  57. return 0;
  58. }
  59. INIT_COMPONENT_EXPORT(cplusplus_system_init);