DynamicLib.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <sys/internals.h>
  30. char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
  31. class Global {
  32. public:
  33. Global(int i)
  34. : m_i(i)
  35. {
  36. // FIXME: Because we need to call printf, and we don't have access to the stdout
  37. // file descriptor from the main executable, we need to initialize LibC ourself.
  38. __environ_is_malloced = false;
  39. environ = __static_environ;
  40. __libc_init();
  41. }
  42. int get_i() const { return m_i; }
  43. private:
  44. int m_i = 0;
  45. };
  46. // This object exists to call __stdio_init and __malloc_init. Also to show that global vars work
  47. Global g_glob { 5 };
  48. extern "C" {
  49. // Tell the compiler that these symbols might be accessed from other places:
  50. extern int global_lib_variable;
  51. void global_lib_function();
  52. const char* other_lib_function(int my_argument);
  53. int global_lib_variable = 1234;
  54. void global_lib_function()
  55. {
  56. outln("Hello from Dynamic Lib! g_glob::m_i == {}", g_glob.get_i());
  57. }
  58. const char* other_lib_function(int my_argument)
  59. {
  60. dbgln("Hello from Dynamic Lib, now from the debug port! g_glob::m_i == {}", g_glob.get_i());
  61. int sum = my_argument + global_lib_variable;
  62. // FIXME: We can't just return AK::String::format across the lib boundary here.
  63. // It will use malloc from our DSO's copy of LibC, and then probably be free'd into
  64. // the malloc of the main program which would be what they call 'very crash'.
  65. // Feels very Windows :)
  66. static String s_string;
  67. s_string = String::formatted("Here's your string! Sum of argument and global_lib_variable: {}", sum);
  68. return s_string.characters();
  69. }
  70. }