aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--com32/elflink/test_com32.c31
-rw-r--r--com32/include/sys/exec.h1
-rw-r--r--com32/include/sys/module.h5
-rw-r--r--com32/lib/sys/module/common.c9
-rw-r--r--com32/lib/sys/module/elf_module.c9
-rw-r--r--com32/lib/sys/module/exec.c12
6 files changed, 55 insertions, 12 deletions
diff --git a/com32/elflink/test_com32.c b/com32/elflink/test_com32.c
index 60bbbed8..ca616536 100644
--- a/com32/elflink/test_com32.c
+++ b/com32/elflink/test_com32.c
@@ -14,7 +14,7 @@
-void print_help() {
+static void print_help() {
printf("List of available commands:\n");
printf("exit - exits the program\n");
printf("help - shows this message\n");
@@ -24,11 +24,11 @@ void print_help() {
printf("list - prints the currently loaded modules\n");
}
-void print_prompt() {
+static void print_prompt() {
printf("\nelflink> ");
}
-void read_command(char *cmd, int size) {
+static void read_command(char *cmd, int size) {
char *nl = NULL;
fgets(cmd, size, stdin);
@@ -39,7 +39,7 @@ void read_command(char *cmd, int size) {
*nl = '\0';
}
-void process_spawn() {
+static void process_spawn() {
// Compose the command line
char **cmd_line = malloc((MAX_COMMAND_ARGS+1)*sizeof(char*));
int argc = 0, result;
@@ -68,7 +68,7 @@ void process_spawn() {
free(cmd_line);
}
-void process_library(int load) {
+static void process_library(int load) {
char *crt_lib;
int result;
@@ -90,7 +90,24 @@ void process_library(int load) {
}
}
-int process_command(char *cmd) {
+static void process_list() {
+ struct elf_module *module;
+ struct module_dep *crt_dep;
+
+ for_each_module(module) {
+ printf("%s (%dK, %s, %s) : ", module->name, module->module_size >> 10,
+ module->shallow ? "shallow" : "regular",
+ module->main_func == NULL ? "library" : "program");
+
+ list_for_each_entry(crt_dep, &module->required, list) {
+ printf("%s ", crt_dep->module->name);
+ }
+
+ printf("\n");
+ }
+}
+
+static int process_command(char *cmd) {
char *cmd_name;
cmd_name = strtok(cmd, COMMAND_DELIM);
@@ -107,7 +124,7 @@ int process_command(char *cmd) {
} else if (strcmp(cmd_name, "unload") == 0) {
process_library(0);
} else if (strcmp(cmd_name, "list") == 0) {
-
+ process_list();
} else {
printf("Unknown command. Type 'help' for a list of valid commands.\n");
}
diff --git a/com32/include/sys/exec.h b/com32/include/sys/exec.h
index e2be29b0..c839df02 100644
--- a/com32/include/sys/exec.h
+++ b/com32/include/sys/exec.h
@@ -19,6 +19,7 @@
*/
extern int exec_init();
+
/**
* load_library - Loads a dynamic library into the environment.
*/
diff --git a/com32/include/sys/module.h b/com32/include/sys/module.h
index 2c9ce9d4..c3c077ad 100644
--- a/com32/include/sys/module.h
+++ b/com32/include/sys/module.h
@@ -239,6 +239,11 @@ extern int module_load_shallow(struct elf_module *module);
extern int module_unload(struct elf_module *module);
/**
+ * module_unloadable -
+ */
+extern int module_unloadable(struct elf_module *module);
+
+/**
* module_find - searches for a module by its name.
* @name: the name of the module, as it was specified in module_alloc.
*
diff --git a/com32/lib/sys/module/common.c b/com32/lib/sys/module/common.c
index 9934a7a4..387dc1a3 100644
--- a/com32/lib/sys/module/common.c
+++ b/com32/lib/sys/module/common.c
@@ -316,12 +316,19 @@ int check_symbols(struct elf_module *module) {
return 0;
}
+int module_unloadable(struct elf_module *module) {
+ if (!list_empty(&module->dependants))
+ return 0;
+
+ return 1;
+}
+
// Unloads the module from the system and releases all the associated memory
int module_unload(struct elf_module *module) {
struct module_dep *crt_dep, *tmp;
// Make sure nobody needs us
- if (!list_empty(&module->dependants)) {
+ if (!module_unloadable(module)) {
DBG_PRINT("Module is required by other modules.\n");
return -1;
}
diff --git a/com32/lib/sys/module/elf_module.c b/com32/lib/sys/module/elf_module.c
index 2952e617..ba535f45 100644
--- a/com32/lib/sys/module/elf_module.c
+++ b/com32/lib/sys/module/elf_module.c
@@ -401,12 +401,21 @@ static int extract_operations(struct elf_module *module) {
module->init_func = (module_init_t*)module_get_absolute(
init_sym->st_value, module);
+ if (*(module->init_func) == NULL) {
+ module->init_func = NULL;
+ }
module->exit_func = (module_exit_t*)module_get_absolute(
exit_sym->st_value, module);
+ if (*(module->exit_func) == NULL) {
+ module->exit_func = NULL;
+ }
module->main_func = (module_main_t*)module_get_absolute(
main_sym->st_value, module);
+ if (*(module->main_func) == NULL) {
+ module->main_func = NULL;
+ }
return 0;
}
diff --git a/com32/lib/sys/module/exec.c b/com32/lib/sys/module/exec.c
index bb855efb..3cd53187 100644
--- a/com32/lib/sys/module/exec.c
+++ b/com32/lib/sys/module/exec.c
@@ -69,13 +69,13 @@ int load_library(const char *name) {
return res;
}
- if (*(module->main_func) != NULL) {
+ if (module->main_func != NULL) {
DBG_PRINT("Cannot load executable module as library.\n");
module_unload(module);
return -1;
}
- if (*(module->init_func) != NULL) {
+ if (module->init_func != NULL) {
res = (*(module->init_func))();
DBG_PRINT("Initialization function returned: %d\n", res);
} else {
@@ -97,7 +97,11 @@ int unload_library(const char *name) {
if (module == NULL)
return -1;
- if (*(module->exit_func) != NULL) {
+ if (!module_unloadable(module)) {
+ return -1;
+ }
+
+ if (module->exit_func != NULL) {
(*(module->exit_func))();
}
@@ -121,7 +125,7 @@ int spawnv(const char *name, const char **argv) {
return res;
}
- if (*(module->main_func) != NULL) {
+ if (module->main_func != NULL) {
const char **last_arg = argv;
void *old_tag;
while (*last_arg != NULL)