Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions contrib/juliac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ allflags = Base.shell_split(allflags)
rpath = get_rpath(; relative = relative_rpath)
rpath = Base.shell_split(rpath)
tmpdir = mktempdir(cleanup=false)
initsrc_path = joinpath(tmpdir, "init.c")
init_path = joinpath(tmpdir, "init.a")
img_path = joinpath(tmpdir, "img.a")
bc_path = joinpath(tmpdir, "img-bc.a")

Expand Down Expand Up @@ -122,19 +120,6 @@ function compile_products(enable_trim::Bool)
exit(1)
end

# Compile the initialization code
open(initsrc_path, "w") do io
print(io, """
#include <julia.h>
__attribute__((constructor)) void static_init(void) {
if (jl_is_initialized())
return;
julia_init(JL_IMAGE_IN_MEMORY);
jl_exception_clear();
}
""")
end
run(`cc $(cflags) -g -c -o $init_path $initsrc_path`)
end

function link_products()
Expand All @@ -150,11 +135,11 @@ function link_products()
julia_libs = Base.shell_split(Base.isdebugbuild() ? "-ljulia-debug -ljulia-internal-debug" : "-ljulia -ljulia-internal")
try
if output_type == "--output-lib"
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
elseif output_type == "--output-sysimage"
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
else
cmd2 = `cc $(allflags) $(rpath) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`
cmd2 = `cc $(allflags) $(rpath) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
end
verbose && println("Running: $cmd2")
run(cmd2)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ linking against `libjulia`.
The first thing that must be done before calling any other Julia C function is to
initialize Julia. This is done by calling `jl_init`, which tries to automatically determine
Julia's install location. If you need to specify a custom location, or specify which system
image to load, use `jl_init_with_image` instead.
image to load, use `jl_init_with_image_file` or `jl_init_with_image_handle` instead.

The second statement in the test program evaluates a Julia statement using a call to `jl_eval_string`.

Expand Down
19 changes: 14 additions & 5 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,18 +968,26 @@ static GlobalVariable *emit_shard_table(Module &M, Type *T_size, Type *T_psize,
return tables_gv;
}

static Function *emit_pgcstack_default_func(Module &M, Type *T_ptr) {
auto FT = FunctionType::get(T_ptr, false);
auto F = Function::Create(FT, GlobalValue::InternalLinkage, "pgcstack_default_func", &M);
llvm::IRBuilder<> builder(BasicBlock::Create(M.getContext(), "top", F));
builder.CreateRet(Constant::getNullValue(T_ptr));
return F;
}

// See src/processor.h for documentation about this table. Corresponds to jl_image_ptls_t.
static GlobalVariable *emit_ptls_table(Module &M, Type *T_size, Type *T_psize) {
static GlobalVariable *emit_ptls_table(Module &M, Type *T_size, Type *T_ptr) {
std::array<Constant *, 3> ptls_table{
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_pgcstack_func_slot"),
new GlobalVariable(M, T_ptr, false, GlobalValue::ExternalLinkage, emit_pgcstack_default_func(M, T_ptr), "jl_pgcstack_func_slot"),
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_pgcstack_key_slot"),
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_tls_offset"),
};
for (auto &gv : ptls_table) {
cast<GlobalVariable>(gv)->setVisibility(GlobalValue::HiddenVisibility);
cast<GlobalVariable>(gv)->setDSOLocal(true);
}
auto ptls_table_arr = ConstantArray::get(ArrayType::get(T_psize, ptls_table.size()), ptls_table);
auto ptls_table_arr = ConstantArray::get(ArrayType::get(T_ptr, ptls_table.size()), ptls_table);
auto ptls_table_gv = new GlobalVariable(M, ptls_table_arr->getType(), false,
GlobalValue::ExternalLinkage, ptls_table_arr, "jl_ptls_table");
ptls_table_gv->setVisibility(GlobalValue::HiddenVisibility);
Expand Down Expand Up @@ -2178,6 +2186,7 @@ void jl_dump_native_impl(void *native_code,

Type *T_size = DL.getIntPtrType(Context);
Type *T_psize = T_size->getPointerTo();
Type *T_ptr = PointerType::get(Context, 0);

auto FT = FunctionType::get(Type::getInt8Ty(Context)->getPointerTo()->getPointerTo(), {}, false);
auto F = Function::Create(FT, Function::ExternalLinkage, "get_jl_RTLD_DEFAULT_handle_addr", metadataM);
Expand All @@ -2201,7 +2210,7 @@ void jl_dump_native_impl(void *native_code,
builder.CreateRet(ConstantInt::get(T_int32, 1));
}
if (imaging_mode) {
auto specs = jl_get_llvm_clone_targets();
auto specs = jl_get_llvm_clone_targets(jl_options.cpu_target);
const uint32_t base_flags = has_veccall ? JL_TARGET_VEC_CALL : 0;
SmallVector<uint8_t, 0> data;
auto push_i32 = [&] (uint32_t v) {
Expand All @@ -2220,7 +2229,7 @@ void jl_dump_native_impl(void *native_code,
GlobalVariable::InternalLinkage,
value, "jl_dispatch_target_ids");
auto shards = emit_shard_table(metadataM, T_size, T_psize, threads);
auto ptls = emit_ptls_table(metadataM, T_size, T_psize);
auto ptls = emit_ptls_table(metadataM, T_size, T_ptr);
auto header = emit_image_header(metadataM, threads, nfvars, ngvars);
auto AT = ArrayType::get(T_size, sizeof(jl_small_typeof) / sizeof(void*));
auto jl_small_typeof_copy = new GlobalVariable(metadataM, AT, false,
Expand Down
21 changes: 16 additions & 5 deletions src/dlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,32 @@ JL_DLLEXPORT int jl_dlclose(void *handle) JL_NOTSAFEPOINT
#endif
}

void *jl_find_dynamic_library_by_addr(void *symbol) {
void *jl_find_dynamic_library_by_addr(void *symbol, int throw_err) {
void *handle;
#ifdef _OS_WINDOWS_
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)symbol,
(HMODULE*)&handle)) {
jl_error("could not load base module");
if (throw_err)
jl_error("could not load base module");
return NULL;
}
#else
Dl_info info;
if (!dladdr(symbol, &info) || !info.dli_fname) {
jl_error("could not load base module");
if (throw_err)
jl_error("could not load base module");
return NULL;
}
handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
dlclose(handle); // Undo ref count increment from `dlopen`
#if !defined(__APPLE__)
if (handle == RTLD_DEFAULT && (RTLD_DEFAULT != NULL || dlerror() == NULL)) {
// We loaded the executable but got RTLD_DEFAULT back, ask for a real handle instead
handle = dlopen("", RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
}
#endif
if (handle != NULL)
dlclose(handle); // Undo ref count increment from `dlopen`
#endif
return handle;
}
Expand All @@ -277,7 +288,7 @@ JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags,

// modname == NULL is a sentinel value requesting the handle of libjulia-internal
if (modname == NULL)
return jl_find_dynamic_library_by_addr(&jl_load_dynamic_library);
return jl_find_dynamic_library_by_addr(&jl_load_dynamic_library, throw_err);

abspath = jl_isabspath(modname);
is_atpath = 0;
Expand Down
Loading