Serenity Operating System
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
27#include <AK/String.h>
28#include <assert.h>
29#include <stdio.h>
30
31char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
32
33// FIXME: Because we need to call printf, and we don't have access to the stout file descriptor
34// from the main executable. We need to call __libc_init....
35extern "C" void __libc_init();
36extern "C" bool __environ_is_malloced;
37
38class Global {
39public:
40 Global(int i)
41 : m_i(i)
42 {
43 __environ_is_malloced = false;
44 environ = __static_environ;
45 __libc_init();
46 }
47
48 int get_i() const { return m_i; }
49
50private:
51 int m_i = 0;
52};
53
54// This object exists to call __stdio_init and __malloc_init. Also to show that global vars work
55Global g_glob { 5 };
56
57extern "C" {
58int global_lib_variable = 1234;
59
60void global_lib_function()
61{
62 printf("Hello from Dynamic Lib! g_glob::m_i == %d\n", g_glob.get_i());
63}
64
65const char* other_lib_function(int my_argument)
66{
67 dbgprintf("Hello from Dynamic Lib, now from the debug port! g_glob::m_i == %d\n", g_glob.get_i());
68
69 int sum = my_argument + global_lib_variable;
70
71 // FIXME: We can't just return AK::String::format across the lib boundary here.
72 // It will use malloc from our DSO's copy of LibC, and then probably be free'd into
73 // the malloc of the main program which would be what they call 'very crash'.
74 // Feels very Windows :)
75 static String s_string;
76 s_string = String::format("Here's your string! Sum of argument and global_lib_variable: %d", sum);
77 return s_string.characters();
78}
79}