crt0.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Types.h>
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/internals.h>
  11. #include <unistd.h>
  12. #ifndef _DYNAMIC_LOADER
  13. extern "C" {
  14. extern size_t __stack_chk_guard;
  15. int main(int, char**, char**);
  16. // Tell the compiler that this may be called from somewhere else.
  17. int _entry(int argc, char** argv, char** env);
  18. void _start(int, char**, char**);
  19. NAKED void _start(int, char**, char**)
  20. {
  21. asm(
  22. "push $0\n"
  23. "jmp _entry@plt\n");
  24. }
  25. int _entry(int argc, char** argv, char** env)
  26. {
  27. size_t original_stack_chk = __stack_chk_guard;
  28. arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard));
  29. if (__stack_chk_guard == 0)
  30. __stack_chk_guard = original_stack_chk;
  31. environ = env;
  32. __environ_is_malloced = false;
  33. _init();
  34. int status = main(argc, argv, environ);
  35. exit(status);
  36. // We should never get here, but if we ever do, make sure to
  37. // restore the stack guard to the value we entered _start with.
  38. // Then we won't trigger the stack canary check on the way out.
  39. __stack_chk_guard = original_stack_chk;
  40. return 20150614;
  41. }
  42. }
  43. #endif