Skip to content

Commit

Permalink
Use weak on dyn-symbols on Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Dec 27, 2024
1 parent 6251524 commit 08e8c9b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Prevent DString from being initialized with "".
- Fix bug in OnStackAllocator when freeing overallocated data. #1720
- Use `weak_odr` rather than `weak` on Windows which seems to prevent issues such as #1704.
- Use `weak` on dyn-symbols on Linux.

### Stdlib changes
- Increase BitWriter.write_bits limit up to 32 bits.
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/llvm_codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ void llvm_set_global_tls(Decl *decl)
}
}

static void llvm_set_weak(GenContext *c, LLVMValueRef global)
void llvm_set_weak(GenContext *c, LLVMValueRef global)
{
LLVMSetLinkage(global, compiler.platform.os == OS_TYPE_WIN32 ? LLVMWeakODRLinkage : LLVMWeakAnyLinkage);
LLVMSetVisibility(global, LLVMDefaultVisibility);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/llvm_codegen_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ void llvm_emit_function_body(GenContext *c, Decl *decl)
scratch_buffer_append(".__c3_atexit_");
scratch_buffer_set_extern_decl_name(decl, false);
LLVMValueRef func = LLVMAddFunction(c->module, scratch_buffer_to_string(), c->xtor_func_type);

llvm_set_weak(c, func);
LLVMBuilderRef builder = llvm_create_function_entry(c, func, NULL);
LLVMValueRef args[1] = { decl->backend_ref };
LLVMBuildCall2(builder, c->atexit_type, atexit, args, 1, "");
Expand Down Expand Up @@ -598,6 +598,7 @@ void llvm_emit_dynamic_functions(GenContext *c, Decl **funcs)
scratch_buffer_append("$ct.dyn.");
scratch_buffer_set_extern_decl_name(decl, false);
LLVMValueRef global = llvm_add_global_raw(c, scratch_buffer_copy(), c->dtable_type, 0);
llvm_set_weak(c, global);
Decl *proto = declptrzero(decl->func_decl.interface_method);
LLVMValueRef proto_ref = proto ? llvm_get_ref(c, proto) : llvm_get_selector(c, decl->name);

Expand Down
1 change: 1 addition & 0 deletions src/compiler/llvm_codegen_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ void llvm_set_linkonce(GenContext *c, LLVMValueRef global);
void llvm_set_comdat(GenContext *c, LLVMValueRef global);
void llvm_set_private_declaration(LLVMValueRef alloc);
void llvm_set_decl_linkage(GenContext *c, Decl *decl);
void llvm_set_weak(GenContext *c, LLVMValueRef global);

void llvm_set_internal_linkage(LLVMValueRef alloc);
void llvm_set_global_tls(Decl *decl);
Expand Down
4 changes: 2 additions & 2 deletions test/test_suite/dynamic/inherit_linux.c3t
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ $"$sel.hello" = comdat any
@.panic_msg = internal constant [42 x i8] c"No method 'tesT' could be found on target\00", align 1
@.func = internal constant [5 x i8] c"main\00", align 1
@std.core.builtin.panic = extern_weak global ptr, align 8
@"$ct.dyn.inherit.Test.tesT" = global { ptr, ptr, ptr } { ptr @inherit.Test.tesT, ptr @"$sel.tesT", ptr inttoptr (i64 -1 to ptr) }, align 8
@"$ct.dyn.inherit.Test.hello" = global { ptr, ptr, ptr } { ptr @inherit.Test.hello, ptr @"$sel.hello", ptr inttoptr (i64 -1 to ptr) }, align 8
@"$ct.dyn.inherit.Test.tesT" = weak global { ptr, ptr, ptr } { ptr @inherit.Test.tesT, ptr @"$sel.tesT", ptr inttoptr (i64 -1 to ptr) }, comdat, align 8
@"$ct.dyn.inherit.Test.hello" = weak global { ptr, ptr, ptr } { ptr @inherit.Test.hello, ptr @"$sel.hello", ptr inttoptr (i64 -1 to ptr) }, comdat, align 8
@"$sel.hello" = linkonce_odr constant [6 x i8] c"hello\00", comdat, align 1
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @.c3_dynamic_register, ptr null }]
define void @inherit.Test.tesT(ptr %0) #0 {
Expand Down
6 changes: 2 additions & 4 deletions test/test_suite/dynamic/overlapping_function_linux.c3t
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ fn void main()
@.file = internal constant [30 x i8] c"overlapping_function_linux.c3\00", align 1
@.func = internal constant [5 x i8] c"main\00", align 1
@std.core.builtin.panic = extern_weak global ptr, align 8
@"$ct.dyn.overlap.Test.tesT" = global { ptr, ptr, ptr } { ptr @overlap.Test.tesT, ptr @"$sel.tesT", ptr inttoptr (i64 -1 to ptr) }, align 8
@"$ct.dyn.overlap.Test.foo" = global { ptr, ptr, ptr } { ptr @overlap.Test.foo, ptr @"$sel.foo", ptr inttoptr (i64 -1 to ptr) }, align 8
@"$ct.dyn.overlap.Test.tesT" = weak global { ptr, ptr, ptr } { ptr @overlap.Test.tesT, ptr @"$sel.tesT", ptr inttoptr (i64 -1 to ptr) }, comdat, align 8
@"$ct.dyn.overlap.Test.foo" = weak global { ptr, ptr, ptr } { ptr @overlap.Test.foo, ptr @"$sel.foo", ptr inttoptr (i64 -1 to ptr) }, comdat, align 8
@"$sel.foo" = linkonce_odr constant [4 x i8] c"foo\00", comdat, align 1
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @.c3_dynamic_register, ptr null }]
; Function Attrs: nounwind uwtable
Expand Down Expand Up @@ -134,8 +134,6 @@ entry:
call void @overlap.main()
ret i32 0
}


define weak ptr @.dyn_search(ptr %0, ptr %1) unnamed_addr comdat {
entry:
br label %check
Expand Down

0 comments on commit 08e8c9b

Please sign in to comment.