crt_init.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * 2016-06-28 Bernard Add _init/_fini routines for GCC.
  25. * 2016-10-02 Bernard Add WEAK for cplusplus_system_init routine.
  26. */
  27. #include <rtthread.h>
  28. #ifdef __CC_ARM
  29. extern void $Super$$__cpp_initialize__aeabi_(void);
  30. /* we need to change the cpp_initialize order */
  31. void $Sub$$__cpp_initialize__aeabi_(void)
  32. {
  33. /* empty */
  34. }
  35. #elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__)
  36. /* The _init()/_fini() routines has been defined in codesourcery g++ lite */
  37. void _init()
  38. {
  39. }
  40. void _fini()
  41. {
  42. }
  43. RT_WEAK void *__dso_handle = 0;
  44. #endif
  45. RT_WEAK
  46. int cplusplus_system_init(void)
  47. {
  48. #if defined(__GNUC__) && !defined(__CC_ARM)
  49. typedef void (*pfunc) ();
  50. extern pfunc __ctors_start__[];
  51. extern pfunc __ctors_end__[];
  52. pfunc *p;
  53. for (p = __ctors_start__; p < __ctors_end__; p++)
  54. (*p)();
  55. #elif defined(__CC_ARM)
  56. /* If there is no SHT$$INIT_ARRAY, calling
  57. * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
  58. * the problem still exists. So we have to initialize the C++ runtime by ourself.
  59. */
  60. typedef void PROC();
  61. extern const unsigned long SHT$$INIT_ARRAY$$Base[];
  62. extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
  63. const unsigned long *base = SHT$$INIT_ARRAY$$Base;
  64. const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
  65. for (; base != lim; base++)
  66. {
  67. PROC *proc = (PROC*)((const char*)base + *base);
  68. (*proc)();
  69. }
  70. #endif
  71. return 0;
  72. }
  73. INIT_COMPONENT_EXPORT(cplusplus_system_init);