commit 02f5a9fa62cc54835467e36b7015a6bcb642583d (tree)
parent b95ff12f2fb4aa5c20aecb789a91396557662287
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sat, 25 Aug 2018 03:55:32 -0400
fix handling multiple extern vars with the same name
Diffstat:
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/codegen.cpp b/src/codegen.cpp
@@ -5812,12 +5812,16 @@ static void do_code_gen(CodeGen *g) {
LLVMValueRef global_value;
if (var->linkage == VarLinkageExternal) {
- global_value = LLVMAddGlobal(g->module, var->value->type->type_ref, buf_ptr(&var->name));
-
- // TODO debug info for the extern variable
+ LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
+ if (existing_llvm_var) {
+ global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(var->value->type->type_ref, 0));
+ } else {
+ global_value = LLVMAddGlobal(g->module, var->value->type->type_ref, buf_ptr(&var->name));
+ // TODO debug info for the extern variable
- LLVMSetLinkage(global_value, LLVMExternalLinkage);
- LLVMSetAlignment(global_value, var->align_bytes);
+ LLVMSetLinkage(global_value, LLVMExternalLinkage);
+ LLVMSetAlignment(global_value, var->align_bytes);
+ }
} else {
bool exported = (var->linkage == VarLinkageExport);
const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported));