Skip to content

Commit

Permalink
Safer function registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Oct 12, 2024
1 parent 76dec0c commit db887bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/compiler/llvm_codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ void llvm_set_comdat(GenContext *c, LLVMValueRef global)
void llvm_set_selector_linkage(GenContext *c, LLVMValueRef selector)
{
LLVMSetVisibility(selector, LLVMDefaultVisibility);
LLVMSetLinkage(selector, LLVMLinkOnceODRLinkage);
LLVMSetLinkage(selector, LLVMWeakAnyLinkage);
llvm_set_comdat(c, selector);
}

Expand Down
3 changes: 1 addition & 2 deletions src/compiler/llvm_codegen_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,7 @@ void llvm_emit_dynamic_functions(GenContext *c, Decl **funcs)
}

LLVMValueRef initializer = LLVMAddFunction(c->module, ".c3_dynamic_register", c->xtor_func_type);
LLVMSetLinkage(initializer, LLVMPrivateLinkage);
LLVMSetVisibility(initializer, LLVMDefaultVisibility);
LLVMSetLinkage(initializer, LLVMInternalLinkage);
LLVMSetAlignment(initializer, 8);
LLVMValueRef vals_fn[3] = { llvm_const_int(c, type_int, 1), initializer, llvm_get_zero(c, type_voidptr) };
vec_add(c->constructors, LLVMConstNamedStruct(c->xtor_entry_type, vals_fn, 3));
Expand Down
15 changes: 11 additions & 4 deletions src/compiler/llvm_codegen_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,18 @@ static inline LLVMValueRef llvm_generate_introspection_global(GenContext *c, LLV
LLVMSetGlobalConstant(global_name, 0);
if (is_external)
{
LLVMSetLinkage(global_name, LLVMExternalLinkage);
if (compiler.platform.os == OS_TYPE_WIN32)
{
LLVMSetLinkage(global_name, LLVMExternalLinkage);
}
else
{
LLVMSetLinkage(global_name, LLVMExternalWeakLinkage);
}
}
else
{
llvm_set_linkonce(c, global_name);
llvm_set_selector_linkage(c, global_name);
}
if (original_global)
{
Expand Down Expand Up @@ -558,7 +565,7 @@ static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type)
scratch_buffer_to_string(),
LLVMTypeOf(associated_value_arr),
0);
llvm_set_linkonce(c, global_ref);
llvm_set_selector_linkage(c, global_ref);
LLVMSetInitializer(global_ref, associated_value_arr);
LLVMSetGlobalConstant(global_ref, true);
associated_value->backend_ref = global_ref;
Expand Down Expand Up @@ -605,7 +612,7 @@ static LLVMValueRef llvm_get_introspection_for_fault(GenContext *c, Type *type)
llvm_const_int(c, type_usz, val->enum_constant.ordinal + 1 )};

LLVMSetInitializer(global_name, llvm_get_struct_named(c->fault_type, vals, 3));
llvm_set_linkonce(c, global_name);
llvm_set_selector_linkage(c, global_name);
val->backend_ref = LLVMBuildPtrToInt(c->builder, global_name, c->typeid_type, "");
}
LLVMValueRef* values = elements ? MALLOC(sizeof(LLVMValueRef) * elements) : NULL;
Expand Down

0 comments on commit db887bb

Please sign in to comment.