Skip to content

Commit

Permalink
wip dyld
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jul 16, 2023
1 parent 149a350 commit aff651e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/dyld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <errno.h>
#include <string.h>

#include "dyld.h"
#include "module.h"
#include "load_context.h"
#include "util.h"
#include "fileio.h"
#include "xlog.h"

void
dyld_init(struct dyld *d)
{
memset(d, 0, sizeof(*d));
}

int
dyld_load_needed_objects(struct dyld *d, struct dyld_object *obj)
{
struct dylink_needs *needs = &obj->module->dylink->needs;
uint32_t i;
int ret = 0;
for (i = 0; i < needs->count; i++) {
const struct name *name = &needs->names[i];
char filename[MAX_PATH];
snprintf(filename, sizeof(filename), "%.*s", CSTR(name));
ret = dyld_load_object_from_file(d, filename);
if (ret != 0) {
goto fail;
}
}
fail:
return ret;
}

int
dyld_load_object_from_file(struct dyld *d, const char *name)
{
struct dyld_object *obj;
int ret;
obj = zalloc(sizeof(*obj));
if (obj == NULL) {
ret = ENOMEM;
goto fail;
}
ret = map_file(name, &obj->bin, &obj->binsz);
if (ret != 0) {
goto fail;
}
struct load_context lctx;
load_context_init(&lctx);
ret = module_create(&obj->module, obj->bin, obj->bin + obj->binsz, &lctx);
load_context_clear(&lctx);
if (ret != 0) {
goto fail;
}
if (obj->module->dylink == NULL) {
xlog_error("module %s doesn't have dylink.0", name);
ret = EINVAL;
goto fail;
}
ret = dyld_load_needed_objects(d, obj);
fail:
return ret;
}
28 changes: 28 additions & 0 deletions lib/dyld.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct dyld_plt {
struct finst *finst;
struct tableinst *tableinst;
uint32_t idx_in_table;
};

struct dyld_object {
uint32_t memory_base;
uint32_t table_base;

const uint8_t *bin;
size_t binsz;
struct module *module;
struct instance *instance;

struct dyld_object *next;
};

struct dyld {
uint32_t memory_base;
uint32_t table_base;
struct meminst *meminst;
struct tableinst *tableinst;
struct dyld_object *objs;
};

int dyld_resolve_symbol(dyld_object *refobj, enum importtype type,
const struct name *name, void **resultp);

0 comments on commit aff651e

Please sign in to comment.