Cthulhu  0.2.10
Cthulhu compiler collection
load_shared.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "common.h"
4 
5 #include "driver/driver.h"
6 #include "os/os.h"
7 
8 #include "base/panic.h"
9 #include <stdio.h>
10 
12 loaded_module_t load_shared_module(loader_t *loader, module_type_t mask, const char *name)
13 {
14  CTASSERT(loader != NULL);
15  CTASSERT(name != NULL);
16 
17  os_library_t library = { 0 };
18  os_error_t error = os_library_open(name, &library);
19 
20  if (error)
21  {
22  return load_error(eLoadErrorLibrary, error);
23  }
24 
25  loaded_module_t mod = { .type = eModNone, .library = library, .error = eLoadErrorNoEntry };
26 
27  if (mask & eModLanguage)
28  {
29  lang_main_t fn_lang;
30  error = os_library_symbol(&library, (void**)&fn_lang, CT_LANG_ENTRY);
31  if (error == eOsSuccess)
32  {
33  mod.type |= eModLanguage;
34  mod.lang = fn_lang();
35  mod.error = eLoadErrorNone;
36  }
37  }
38 
39  if (mask & eModTarget)
40  {
41  target_main_t fn_target;
42  error = os_library_symbol(&library, (void**)&fn_target, CT_TARGET_ENTRY);
43  if (error == eOsSuccess)
44  {
45  mod.type |= eModTarget;
46  mod.target = fn_target();
47  mod.error = eLoadErrorNone;
48  }
49  }
50 
51  if (mask & eModPlugin)
52  {
53  plugin_main_t fn_plugin;
54  error = os_library_symbol(&library, (void**)&fn_plugin, CT_PLUGIN_ENTRY);
55  if (error == eOsSuccess)
56  {
57  mod.type |= eModPlugin;
58  mod.plugin = fn_plugin();
59  mod.error = eLoadErrorNone;
60  }
61  }
62 
63  return mod;
64 }
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
const language_t *(* lang_main_t)(void)
Definition: driver.h:14
const target_t *(* target_main_t)(void)
Definition: driver.h:16
#define CT_LANG_ENTRY
Definition: driver.h:18
const plugin_t *(* plugin_main_t)(void)
Definition: driver.h:15
#define CT_PLUGIN_ENTRY
Definition: driver.h:19
#define CT_TARGET_ENTRY
Definition: driver.h:20
CT_NODISCARD CT_OS_API os_error_t os_library_symbol(os_library_t *library, OUT_NOTNULL void **symbol, const char *name)
get a symbol from a shared library
CT_OS_API os_error_t os_library_open(const char *path, OUT_NOTNULL os_library_t *library)
open a shared library from disk
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
STA_DECL loaded_module_t load_shared_module(loader_t *loader, module_type_t mask, const char *name)
Definition: load_shared.c:12
loaded_module_t load_error(load_error_t error, os_error_t os)
Definition: loader.c:25
module_type_t
Definition: loader.h:32
@ eOsSuccess
Definition: posix.h:24
load_error_t error
Definition: loader.h:55
const plugin_t * plugin
Definition: loader.h:52
module_type_t type
Definition: loader.h:49
const target_t * target
Definition: loader.h:53
const language_t * lang
Definition: loader.h:51
Definition: common.h:8
a shared library handle
Definition: os.h:28