crt_init.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #endif
  44. WEAK
  45. int cplusplus_system_init(void)
  46. {
  47. #if defined(__GNUC__) && !defined(__CC_ARM)
  48. typedef void (*pfunc) ();
  49. extern pfunc __ctors_start__[];
  50. extern pfunc __ctors_end__[];
  51. pfunc *p;
  52. for (p = __ctors_start__; p < __ctors_end__; p++)
  53. (*p)();
  54. #elif defined(__CC_ARM)
  55. /* If there is no SHT$$INIT_ARRAY, calling
  56. * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
  57. * the problem still exists. So we have to initialize the C++ runtime by ourself.
  58. */
  59. typedef void PROC();
  60. extern const unsigned long SHT$$INIT_ARRAY$$Base[];
  61. extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
  62. const unsigned long *base = SHT$$INIT_ARRAY$$Base;
  63. const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
  64. for (; base != lim; base++)
  65. {
  66. PROC *proc = (PROC*)((const char*)base + *base);
  67. (*proc)();
  68. }
  69. #endif
  70. return 0;
  71. }
  72. INIT_COMPONENT_EXPORT(cplusplus_system_init);