Skip to content

Commit

Permalink
retire dyld_object_name
Browse files Browse the repository at this point in the history
make obj->name always non-null instead.
  • Loading branch information
yamt committed Jul 26, 2023
1 parent 92014c6 commit bac7281
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
71 changes: 24 additions & 47 deletions libdyld/dyld.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ static const struct name name_heap_base =
NAME_FROM_CSTR_LITERAL("__heap_base");
static const struct name name_heap_end = NAME_FROM_CSTR_LITERAL("__heap_end");

static const struct name name_main_object = NAME_FROM_CSTR_LITERAL("<main>");

/*
* __wasm_apply_data_relocs is generated by wasm-ld.
* it applies relocations to data segments.
Expand Down Expand Up @@ -199,14 +201,11 @@ is_func_export(const struct module *m, const struct export *ex)
return ex->desc.type == EXTERNTYPE_FUNC;
}

const struct name *
dyld_object_name(struct dyld_object *obj)
static bool
is_main_object(struct dyld_object *obj)
{
const struct name *objname = obj->name;
if (objname == NULL) {
objname = &name_main_obj;
}
return objname;
struct dyld *dyld = obj->dyld;
return LIST_FIRST(&dyld->objs) == obj;
}

void
Expand Down Expand Up @@ -235,7 +234,7 @@ dyld_find_object_by_name(struct dyld *d, const struct name *name)
{
struct dyld_object *obj;
LIST_FOREACH(obj, &d->objs, q) {
if (obj->name == NULL) { /* main module */
if (is_main_object(obj)) {
continue;
}
if (!compare_name(obj->name, name)) {
Expand Down Expand Up @@ -276,14 +275,11 @@ dyld_load_needed_objects(struct dyld *d)
int ret = 0;
struct dyld_object *obj;
LIST_FOREACH(obj, &d->objs, q) {
#if defined(TOYWASM_ENABLE_TRACING)
const struct name *objname = dyld_object_name(obj);
#endif
const struct dylink_needs *needs = &obj->module->dylink->needs;
uint32_t i;
for (i = 0; i < needs->count; i++) {
const struct name *name = &needs->names[i];
xlog_trace("dyld: %.*s requires %.*s", CSTR(objname),
xlog_trace("dyld: %.*s requires %.*s", CSTR(obj->name),
CSTR(name));
if (dyld_find_object_by_name(d, name)) {
continue;
Expand Down Expand Up @@ -464,12 +460,9 @@ dyld_allocate_memory_for_obj(struct dyld *d, struct dyld_object *obj)
if (ret != 0) {
return ret;
}
#if defined(TOYWASM_ENABLE_TRACING)
const struct name *objname = dyld_object_name(obj);
#endif
xlog_trace("dyld: memory allocated for obj %.*s: %08" PRIx32
" - %08" PRIx32,
CSTR(objname), obj->memory_base,
CSTR(obj->name), obj->memory_base,
obj->memory_base + minfo->memorysize);
return 0;
}
Expand Down Expand Up @@ -533,12 +526,9 @@ dyld_allocate_table_for_obj(struct dyld *d, struct dyld_object *obj)
if (ret != 0) {
return ret;
}
#if defined(TOYWASM_ENABLE_TRACING)
const struct name *objname = dyld_object_name(obj);
#endif
xlog_trace("dyld: table elem allocated for %.*s (mem_info): %08" PRIx32
" - %08" PRIx32,
CSTR(objname), obj->table_base,
CSTR(obj->name), obj->table_base,
obj->table_base + minfo->tablesize);

/*
Expand All @@ -564,7 +554,7 @@ dyld_allocate_table_for_obj(struct dyld *d, struct dyld_object *obj)
obj->nexports = nexports;
xlog_trace("dyld: table elem allocated for %.*s (export): %08" PRIx32
" - %08" PRIx32,
CSTR(objname), obj->table_export_base,
CSTR(obj->name), obj->table_export_base,
obj->table_export_base + nexports);
return ret;
}
Expand All @@ -577,8 +567,7 @@ dyld_object_destroy(struct dyld_object *obj)
* obj->name might have been freed given the order dyld_clear()
* frees objects
*/
const struct name *objname = dyld_object_name(obj);
xlog_trace("dyld: destroying %.*s", CSTR(objname));
xlog_trace("dyld: destroying %.*s", CSTR(obj->name));
#endif
if (obj->local_import_obj != NULL) {
import_object_destroy(obj->local_import_obj);
Expand Down Expand Up @@ -624,22 +613,21 @@ dyld_execute_init_func(struct dyld_object *obj, const struct name *name)
static int
dyld_execute_init_funcs(struct dyld_object *obj)
{
const struct name *objname = dyld_object_name(obj);
unsigned int i;
for (i = 0; i < ARRAYCOUNT(init_funcs); i++) {
const struct name *funcname = &init_funcs[i];
int ret = dyld_execute_init_func(obj, funcname);
if (ret == ENOENT) {
xlog_trace("dyld: %.*s doesn't have %.*s",
CSTR(objname), CSTR(funcname));
CSTR(obj->name), CSTR(funcname));
continue;
}
if (ret != 0) {
xlog_error("dyld: %.*s %.*s failed with %d",
CSTR(objname), CSTR(funcname), ret);
CSTR(obj->name), CSTR(funcname), ret);
return ret;
}
xlog_trace("dyld: %.*s %.*s succeeded", CSTR(objname),
xlog_trace("dyld: %.*s %.*s succeeded", CSTR(obj->name),
CSTR(funcname));
}
return 0;
Expand Down Expand Up @@ -670,7 +658,6 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name,
goto fail;
}
obj->name = name;
const struct name *objname = dyld_object_name(obj);
ret = map_file(filename, (void *)&obj->bin, &obj->binsz);
if (ret != 0) {
goto fail;
Expand All @@ -687,7 +674,7 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name,
}
load_context_clear(&lctx);
if (obj->module->dylink == NULL) {
xlog_error("module %.*s doesn't have dylink.0", CSTR(objname));
xlog_error("module %.*s doesn't have dylink.0", CSTR(name));
ret = EINVAL;
goto fail;
}
Expand All @@ -705,7 +692,7 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name,
}
xlog_trace("dyld: obj %.*s __memory_base %08" PRIx32
" __table_base %08" PRIx32,
CSTR(objname), obj->memory_base, obj->table_base);
CSTR(name), obj->memory_base, obj->table_base);
global_set_i32(&obj->memory_base_global, obj->memory_base);
global_set_i32(&obj->table_base_global, obj->table_base);
assert(d->shared_import_obj != NULL);
Expand All @@ -723,7 +710,7 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name,
report_clear(&report);
LIST_INSERT_TAIL(&d->objs, obj, q);
obj->dyld = d;
xlog_trace("dyld: %.*s loaded", CSTR(objname));
xlog_trace("dyld: %.*s loaded", CSTR(name));
return 0;
fail:
if (obj != NULL) {
Expand Down Expand Up @@ -808,10 +795,7 @@ dyld_register_funcinst(struct dyld *d, struct dyld_object *obj,
const struct funcinst *fi)
{
struct tableinst *ti = d->tableinst;
#if defined(TOYWASM_ENABLE_TRACING)
const struct name *objname = dyld_object_name(obj);
#endif
xlog_trace("dyld: registering obj %.*s finst %p", CSTR(objname),
xlog_trace("dyld: registering obj %.*s finst %p", CSTR(obj->name),
(void *)fi);
uint32_t end = obj->table_export_base + obj->nexports;
/*
Expand Down Expand Up @@ -896,15 +880,10 @@ dyld_resolve_symbol(struct dyld_object *refobj, enum symtype symtype,
*/
addr = global_get_i32(gi) + obj->memory_base;
}
#if defined(TOYWASM_ENABLE_TRACING)
const struct name *refobjname =
dyld_object_name(refobj);
const struct name *objname = dyld_object_name(obj);
#endif
xlog_trace("dyld: resolved %s %.*s %.*s -> %.*s idx "
"%" PRIu32 " addr %08" PRIx32,
symtype_str(symtype), CSTR(refobjname),
CSTR(sym), CSTR(objname), ed->idx, addr);
symtype_str(symtype), CSTR(refobj->name),
CSTR(sym), CSTR(obj->name), ed->idx, addr);
*resultp = addr;
return 0;
}
Expand All @@ -921,9 +900,8 @@ dyld_resolve_got_symbols(struct dyld_object *refobj)
{
const struct module *m = refobj->module;
struct globalinst *got = refobj->gots;
const struct name *objname = dyld_object_name(refobj);
int ret;
xlog_trace("dyld: relocating %.*s", CSTR(objname));
xlog_trace("dyld: relocating %.*s", CSTR(refobj->name));
uint32_t i;
for (i = 0; i < m->nimports; i++) {
const struct import *im = &m->imports[i];
Expand All @@ -939,7 +917,7 @@ dyld_resolve_got_symbols(struct dyld_object *refobj)
ret = dyld_resolve_symbol(refobj, symtype, &im->name, &addr);
if (ret != 0) {
xlog_error("dyld: failed to resolve %.*s %.*s %.*s",
CSTR(objname), CSTR(&im->module_name),
CSTR(refobj->name), CSTR(&im->module_name),
CSTR(&im->name));
goto fail;
}
Expand Down Expand Up @@ -973,8 +951,7 @@ dyld_load(struct dyld *d, const char *filename)
if (ret != 0) {
goto fail;
}
/* REVISIT: name of main module */
ret = dyld_load_object_from_file(d, NULL, filename);
ret = dyld_load_object_from_file(d, &name_main_object, filename);
if (ret != 0) {
goto fail;
}
Expand Down
1 change: 0 additions & 1 deletion libdyld/dyld.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,5 @@ int dyld_load(struct dyld *d, const char *filename);
struct instance *dyld_main_object_instance(struct dyld *d);
void dyld_options_set_defaults(struct dyld_options *opts);

const struct name *dyld_object_name(struct dyld_object *obj);
int dyld_resolve_symbol(struct dyld_object *refobj, enum symtype symtype,
const struct name *sym, uint32_t *resultp);
6 changes: 3 additions & 3 deletions libdyld/dyld_plt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ dyld_plt(struct exec_context *ctx, struct host_instance *hi,
if (plt->finst == NULL) {
struct dyld_object *refobj = plt->refobj;
struct dyld *d = refobj->dyld;
const struct name *objname = dyld_object_name(refobj);
const struct name *sym = plt->sym;
int ret;
uint32_t addr;
ret = dyld_resolve_symbol(plt->refobj, SYM_TYPE_FUNC, sym,
&addr);
if (ret != 0) {
xlog_error("dyld: PLT failed to resolve %.*s %.*s",
CSTR(objname), CSTR(sym));
CSTR(refobj->name), CSTR(sym));
return ret;
}
ret = table_get_func(ctx, d->tableinst, addr, ft, &plt->finst);
Expand All @@ -32,7 +31,8 @@ dyld_plt(struct exec_context *ctx, struct host_instance *hi,
}
xlog_trace("dyld: PLT resolved %.*s %.*s to addr %08" PRIx32
" finst %p",
CSTR(objname), CSTR(sym), addr, (void *)plt->finst);
CSTR(refobj->name), CSTR(sym), addr,
(void *)plt->finst);
}

#if 0 /* a bit dirty */
Expand Down

0 comments on commit bac7281

Please sign in to comment.