cxx_crt_init.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2006-2021, 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. #if defined(__ARMCC_VERSION)
  15. extern void $Super$$__cpp_initialize__aeabi_(void);
  16. /* we need to change the cpp_initialize order */
  17. rt_weak 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. /**
  32. * @brief This function initializes the C++ runtime environment for ARM and GCC compilers.
  33. *
  34. * @note If there is no SHT$$INIT_ARRAY section, calling $Super$$__cpp_initialize__aeabi_() will cause an error
  35. * in ARMCC compiler. Therefore, this function manually iterates through the base addresses of the
  36. * SHT$$INIT_ARRAY section to call the constructor functions of each object. In GCC compiler, this function
  37. * uses the __ctors_start__ and __ctors_end__ global variables to determine the range of constructor function
  38. * pointers and calls each constructor function of every object in that range.
  39. *
  40. * @return Returns 0 if the initialization of the C++ runtime environment is successful. Otherwise, it returns
  41. * an error code indicating the failure of the operation.
  42. */
  43. rt_weak int cplusplus_system_init(void)
  44. {
  45. #if defined(__ARMCC_VERSION)
  46. /* If there is no SHT$$INIT_ARRAY, calling
  47. * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
  48. * the problem still exists. So we have to initialize the C++ runtime by ourself.
  49. */
  50. typedef void PROC();
  51. extern const unsigned long SHT$$INIT_ARRAY$$Base[];
  52. extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
  53. const unsigned long *base = SHT$$INIT_ARRAY$$Base;
  54. const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
  55. for (; base != lim; base++)
  56. {
  57. PROC *proc = (PROC *)((const char *)base + *base);
  58. (*proc)();
  59. }
  60. #elif defined(__GNUC__)
  61. typedef void(*pfunc)();
  62. extern pfunc __ctors_start__[];
  63. extern pfunc __ctors_end__[];
  64. pfunc *p;
  65. for (p = __ctors_start__; p < __ctors_end__; p++)
  66. (*p)();
  67. #endif
  68. return 0;
  69. }
  70. INIT_COMPONENT_EXPORT(cplusplus_system_init);