From 3c81dabd76accd809295f94f6d6e2eb9ad45dce0 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sat, 21 Feb 2009 00:24:10 +0000 Subject: [PATCH] Emit extern_weak when needed. - PR3629. llvm-svn: 65203 --- clang/lib/CodeGen/CodeGenModule.cpp | 15 ++++++++++----- clang/test/CodeGen/global-decls.c | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 clang/test/CodeGen/global-decls.c diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 7432b01e255c..ccba324aa024 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -264,7 +264,8 @@ void CodeGenModule::SetGlobalValueAttributes(const Decl *D, } else GV->setLinkage(llvm::Function::DLLImportLinkage); } - } + } else if (D->getAttr()) + GV->setLinkage(llvm::Function::ExternalWeakLinkage); } else { if (IsInternal) { GV->setLinkage(llvm::Function::InternalLinkage); @@ -287,11 +288,10 @@ void CodeGenModule::SetGlobalValueAttributes(const Decl *D, setGlobalVisibility(GV, attr->getVisibility()); // FIXME: else handle -fvisibility - if (const AsmLabelAttr *ALA = D->getAttr()) { - // Prefaced with special LLVM marker to indicate that the name - // should not be munged. + // Prefaced with special LLVM marker to indicate that the name + // should not be munged. + if (const AsmLabelAttr *ALA = D->getAttr()) GV->setName("\01" + ALA->getLabel()); - } if (const SectionAttr *SA = D->getAttr()) GV->setSection(SA->getName()); @@ -598,8 +598,13 @@ void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { GV->setConstant(D->getType().isConstant(Context)); + // FIXME: Merge with other attribute handling code. + if (D->getStorageClass() == VarDecl::PrivateExtern) setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); + + if (D->getAttr()) + GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); } // Make sure the result is of the correct type. diff --git a/clang/test/CodeGen/global-decls.c b/clang/test/CodeGen/global-decls.c new file mode 100644 index 000000000000..b0e35bd7a6da --- /dev/null +++ b/clang/test/CodeGen/global-decls.c @@ -0,0 +1,22 @@ +// RUN: clang -arch i386 -emit-llvm -o %t %s && + +// RUN: grep '@g0_ext = extern_weak global i32' %t && +extern int g0_ext __attribute__((weak)); +// RUN: grep 'declare extern_weak i32 @g1_ext()' %t && +extern int __attribute__((weak)) g1_ext (void); + +// RUN: grep '@g0_common = weak global i32' %t && +int g0_common __attribute__((weak)); + +// RUN: grep '@g0_def = weak global i32' %t && +int g0_def __attribute__((weak)) = 52; +// RUN: grep 'define weak i32 @g1_def()' %t && +int __attribute__((weak)) g1_def (void) {} + +// Force _ext references +void f0() { + int a = g0_ext; + int b = g1_ext(); +} + +// RUN: true