crt.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * C Run Time support for NOLIBC
  4. * Copyright (C) 2023 Zhangjin Wu <falcon@tinylab.org>
  5. */
  6. #ifndef _NOLIBC_CRT_H
  7. #define _NOLIBC_CRT_H
  8. #ifndef NOLIBC_NO_RUNTIME
  9. #include "compiler.h"
  10. char **environ __attribute__((weak));
  11. const unsigned long *_auxv __attribute__((weak));
  12. void _start(void);
  13. static void __stack_chk_init(void);
  14. static void exit(int);
  15. extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
  16. extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
  17. extern void (*const __init_array_start[])(int, char **, char**) __attribute__((weak));
  18. extern void (*const __init_array_end[])(int, char **, char**) __attribute__((weak));
  19. extern void (*const __fini_array_start[])(void) __attribute__((weak));
  20. extern void (*const __fini_array_end[])(void) __attribute__((weak));
  21. void _start_c(long *sp);
  22. __attribute__((weak,used))
  23. #if __nolibc_has_feature(undefined_behavior_sanitizer)
  24. __attribute__((no_sanitize("function")))
  25. #endif
  26. void _start_c(long *sp)
  27. {
  28. long argc;
  29. char **argv;
  30. char **envp;
  31. int exitcode;
  32. void (* const *ctor_func)(int, char **, char **);
  33. void (* const *dtor_func)(void);
  34. const unsigned long *auxv;
  35. /* silence potential warning: conflicting types for 'main' */
  36. int _nolibc_main(int, char **, char **) __asm__ ("main");
  37. /* initialize stack protector */
  38. __stack_chk_init();
  39. /*
  40. * sp : argc <-- argument count, required by main()
  41. * argv: argv[0] <-- argument vector, required by main()
  42. * argv[1]
  43. * ...
  44. * argv[argc-1]
  45. * null
  46. * environ: environ[0] <-- environment variables, required by main() and getenv()
  47. * environ[1]
  48. * ...
  49. * null
  50. * _auxv: _auxv[0] <-- auxiliary vector, required by getauxval()
  51. * _auxv[1]
  52. * ...
  53. * null
  54. */
  55. /* assign argc and argv */
  56. argc = *sp;
  57. argv = (void *)(sp + 1);
  58. /* find environ */
  59. environ = envp = argv + argc + 1;
  60. /* find _auxv */
  61. for (auxv = (void *)envp; *auxv++;)
  62. ;
  63. _auxv = auxv;
  64. for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
  65. (*ctor_func)(argc, argv, envp);
  66. for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
  67. (*ctor_func)(argc, argv, envp);
  68. /* go to application */
  69. exitcode = _nolibc_main(argc, argv, envp);
  70. for (dtor_func = __fini_array_end; dtor_func > __fini_array_start;)
  71. (*--dtor_func)();
  72. exit(exitcode);
  73. }
  74. #endif /* NOLIBC_NO_RUNTIME */
  75. #endif /* _NOLIBC_CRT_H */