Skip to content

Commit 4348e89

Browse files
authored
Apply some of the trimming patches to 1.12 (#58321)
2 parents d433021 + 66b7bd0 commit 4348e89

22 files changed

+569
-493
lines changed

contrib/juliac.jl

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ allflags = Base.shell_split(allflags)
8888
rpath = get_rpath(; relative = relative_rpath)
8989
rpath = Base.shell_split(rpath)
9090
tmpdir = mktempdir(cleanup=false)
91-
initsrc_path = joinpath(tmpdir, "init.c")
92-
init_path = joinpath(tmpdir, "init.a")
9391
img_path = joinpath(tmpdir, "img.a")
9492
bc_path = joinpath(tmpdir, "img-bc.a")
9593

@@ -122,19 +120,6 @@ function compile_products(enable_trim::Bool)
122120
exit(1)
123121
end
124122

125-
# Compile the initialization code
126-
open(initsrc_path, "w") do io
127-
print(io, """
128-
#include <julia.h>
129-
__attribute__((constructor)) void static_init(void) {
130-
if (jl_is_initialized())
131-
return;
132-
julia_init(JL_IMAGE_IN_MEMORY);
133-
jl_exception_clear();
134-
}
135-
""")
136-
end
137-
run(`cc $(cflags) -g -c -o $init_path $initsrc_path`)
138123
end
139124

140125
function link_products()
@@ -150,11 +135,11 @@ function link_products()
150135
julia_libs = Base.shell_split(Base.isdebugbuild() ? "-ljulia-debug -ljulia-internal-debug" : "-ljulia -ljulia-internal")
151136
try
152137
if output_type == "--output-lib"
153-
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`
138+
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
154139
elseif output_type == "--output-sysimage"
155140
cmd2 = `cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
156141
else
157-
cmd2 = `cc $(allflags) $(rpath) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`
142+
cmd2 = `cc $(allflags) $(rpath) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
158143
end
159144
verbose && println("Running: $cmd2")
160145
run(cmd2)

doc/src/manual/embedding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ linking against `libjulia`.
5454
The first thing that must be done before calling any other Julia C function is to
5555
initialize Julia. This is done by calling `jl_init`, which tries to automatically determine
5656
Julia's install location. If you need to specify a custom location, or specify which system
57-
image to load, use `jl_init_with_image` instead.
57+
image to load, use `jl_init_with_image_file` or `jl_init_with_image_handle` instead.
5858
5959
The second statement in the test program evaluates a Julia statement using a call to `jl_eval_string`.
6060

src/aotcompile.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -971,18 +971,26 @@ static GlobalVariable *emit_shard_table(Module &M, Type *T_size, Type *T_psize,
971971
return tables_gv;
972972
}
973973

974+
static Function *emit_pgcstack_default_func(Module &M, Type *T_ptr) {
975+
auto FT = FunctionType::get(T_ptr, false);
976+
auto F = Function::Create(FT, GlobalValue::InternalLinkage, "pgcstack_default_func", &M);
977+
llvm::IRBuilder<> builder(BasicBlock::Create(M.getContext(), "top", F));
978+
builder.CreateRet(Constant::getNullValue(T_ptr));
979+
return F;
980+
}
981+
974982
// See src/processor.h for documentation about this table. Corresponds to jl_image_ptls_t.
975-
static GlobalVariable *emit_ptls_table(Module &M, Type *T_size, Type *T_psize) {
983+
static GlobalVariable *emit_ptls_table(Module &M, Type *T_size, Type *T_ptr) {
976984
std::array<Constant *, 3> ptls_table{
977-
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_pgcstack_func_slot"),
985+
new GlobalVariable(M, T_ptr, false, GlobalValue::ExternalLinkage, emit_pgcstack_default_func(M, T_ptr), "jl_pgcstack_func_slot"),
978986
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_pgcstack_key_slot"),
979987
new GlobalVariable(M, T_size, false, GlobalValue::ExternalLinkage, Constant::getNullValue(T_size), "jl_tls_offset"),
980988
};
981989
for (auto &gv : ptls_table) {
982990
cast<GlobalVariable>(gv)->setVisibility(GlobalValue::HiddenVisibility);
983991
cast<GlobalVariable>(gv)->setDSOLocal(true);
984992
}
985-
auto ptls_table_arr = ConstantArray::get(ArrayType::get(T_psize, ptls_table.size()), ptls_table);
993+
auto ptls_table_arr = ConstantArray::get(ArrayType::get(T_ptr, ptls_table.size()), ptls_table);
986994
auto ptls_table_gv = new GlobalVariable(M, ptls_table_arr->getType(), false,
987995
GlobalValue::ExternalLinkage, ptls_table_arr, "jl_ptls_table");
988996
ptls_table_gv->setVisibility(GlobalValue::HiddenVisibility);
@@ -2181,6 +2189,7 @@ void jl_dump_native_impl(void *native_code,
21812189

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

21852194
auto FT = FunctionType::get(Type::getInt8Ty(Context)->getPointerTo()->getPointerTo(), {}, false);
21862195
auto F = Function::Create(FT, Function::ExternalLinkage, "get_jl_RTLD_DEFAULT_handle_addr", metadataM);
@@ -2204,7 +2213,7 @@ void jl_dump_native_impl(void *native_code,
22042213
builder.CreateRet(ConstantInt::get(T_int32, 1));
22052214
}
22062215
if (imaging_mode) {
2207-
auto specs = jl_get_llvm_clone_targets();
2216+
auto specs = jl_get_llvm_clone_targets(jl_options.cpu_target);
22082217
const uint32_t base_flags = has_veccall ? JL_TARGET_VEC_CALL : 0;
22092218
SmallVector<uint8_t, 0> data;
22102219
auto push_i32 = [&] (uint32_t v) {
@@ -2223,7 +2232,7 @@ void jl_dump_native_impl(void *native_code,
22232232
GlobalVariable::InternalLinkage,
22242233
value, "jl_dispatch_target_ids");
22252234
auto shards = emit_shard_table(metadataM, T_size, T_psize, threads);
2226-
auto ptls = emit_ptls_table(metadataM, T_size, T_psize);
2235+
auto ptls = emit_ptls_table(metadataM, T_size, T_ptr);
22272236
auto header = emit_image_header(metadataM, threads, nfvars, ngvars);
22282237
auto AT = ArrayType::get(T_size, sizeof(jl_small_typeof) / sizeof(void*));
22292238
auto jl_small_typeof_copy = new GlobalVariable(metadataM, AT, false,

src/dlload.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,32 @@ JL_DLLEXPORT int jl_dlclose(void *handle) JL_NOTSAFEPOINT
240240
#endif
241241
}
242242

243-
void *jl_find_dynamic_library_by_addr(void *symbol) {
243+
void *jl_find_dynamic_library_by_addr(void *symbol, int throw_err) {
244244
void *handle;
245245
#ifdef _OS_WINDOWS_
246246
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
247247
(LPCWSTR)symbol,
248248
(HMODULE*)&handle)) {
249-
jl_error("could not load base module");
249+
if (throw_err)
250+
jl_error("could not load base module");
251+
return NULL;
250252
}
251253
#else
252254
Dl_info info;
253255
if (!dladdr(symbol, &info) || !info.dli_fname) {
254-
jl_error("could not load base module");
256+
if (throw_err)
257+
jl_error("could not load base module");
258+
return NULL;
255259
}
256260
handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
257-
dlclose(handle); // Undo ref count increment from `dlopen`
261+
#if !defined(__APPLE__)
262+
if (handle == RTLD_DEFAULT && (RTLD_DEFAULT != NULL || dlerror() == NULL)) {
263+
// We loaded the executable but got RTLD_DEFAULT back, ask for a real handle instead
264+
handle = dlopen("", RTLD_NOW | RTLD_NOLOAD | RTLD_LOCAL);
265+
}
266+
#endif
267+
if (handle != NULL)
268+
dlclose(handle); // Undo ref count increment from `dlopen`
258269
#endif
259270
return handle;
260271
}
@@ -277,7 +288,7 @@ JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags,
277288

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

282293
abspath = jl_isabspath(modname);
283294
is_atpath = 0;

0 commit comments

Comments
 (0)