crt_init.c 2.5 KB

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