diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td index 9ed9a88fa3d62..c950a2d95f641 100644 --- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td +++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td @@ -283,6 +283,8 @@ def err_avx_calling_convention : Error; def err_alias_to_undefined : Error< "%select{alias|ifunc}0 must point to a defined " "%select{variable or |}1function">; +def err_alias_to_common : Error< + "alias to a variable in a common section is not allowed">; def note_alias_requires_mangled_name : Note< "the %select{function or variable|function}0 specified in an %select{alias|ifunc}1 must refer to its mangled name">; def note_alias_mangled_name_alternative: Note< diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index f27fc019ccc53..07bce12d7e668 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -563,8 +563,8 @@ static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) { } static bool checkAliasedGlobal( - DiagnosticsEngine &Diags, SourceLocation Location, bool IsIFunc, - const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV, + const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location, + bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV, const llvm::MapVector &MangledDeclNames, SourceRange AliasRange) { GV = getAliasedGlobal(Alias); @@ -573,6 +573,14 @@ static bool checkAliasedGlobal( return false; } + if (GV->hasCommonLinkage()) { + const llvm::Triple &Triple = Context.getTargetInfo().getTriple(); + if (Triple.getObjectFormat() == llvm::Triple::XCOFF) { + Diags.Report(Location, diag::err_alias_to_common); + return false; + } + } + if (GV->isDeclaration()) { Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc; Diags.Report(Location, diag::note_alias_requires_mangled_name) @@ -633,7 +641,7 @@ void CodeGenModule::checkAliases() { StringRef MangledName = getMangledName(GD); llvm::GlobalValue *Alias = GetGlobalValue(MangledName); const llvm::GlobalValue *GV = nullptr; - if (!checkAliasedGlobal(Diags, Location, IsIFunc, Alias, GV, + if (!checkAliasedGlobal(getContext(), Diags, Location, IsIFunc, Alias, GV, MangledDeclNames, Range)) { Error = true; continue; diff --git a/clang/test/CodeGen/aix-common.c b/clang/test/CodeGen/aix-common.c new file mode 100644 index 0000000000000..c18eb8f30623f --- /dev/null +++ b/clang/test/CodeGen/aix-common.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -triple powerpc-ibm-aix -S -fcommon %s -verify -o - +// RUN: %clang_cc1 -triple powerpc64-ibm-aix -S -fcommon %s -verify -o - +int xxxxxx; +extern int yyyyyy __attribute__((__alias__("xxxxxx") )); //expected-error {{alias to a variable in a common section is not allowed}} + +void *gggggg() { return &yyyyyy; } diff --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp index 377bd7af9f191..1ecccdebe036f 100644 --- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -2762,11 +2762,21 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) { // Construct an aliasing list for each GlobalObject. for (const auto &Alias : M.aliases()) { - const GlobalObject *Base = Alias.getAliaseeObject(); - if (!Base) + const GlobalObject *Aliasee = Alias.getAliaseeObject(); + if (!Aliasee) report_fatal_error( "alias without a base object is not yet supported on AIX"); - GOAliasMap[Base].push_back(&Alias); + + if (Aliasee->hasCommonLinkage()) { + report_fatal_error("Aliases to common variables are not allowed on AIX:" + "\n\tAlias attribute for " + + Alias.getGlobalIdentifier() + + " is invalid because " + Aliasee->getName() + + " is common.", + false); + } + + GOAliasMap[Aliasee].push_back(&Alias); } return Result; diff --git a/llvm/test/CodeGen/PowerPC/aix-common.ll b/llvm/test/CodeGen/PowerPC/aix-common.ll new file mode 100644 index 0000000000000..f02c68607f5b0 --- /dev/null +++ b/llvm/test/CodeGen/PowerPC/aix-common.ll @@ -0,0 +1,15 @@ +; RUN: not llc -filetype=obj -mtriple powerpc-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s +; RUN: not llc -filetype=asm -mtriple powerpc-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s +; RUN: not llc -filetype=obj -mtriple powerpc64-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s +; RUN: not llc -filetype=asm -mtriple powerpc64-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s +@x= common global i32 0, align 4 + +@y= alias i32, ptr @x + +; Function Attrs: noinline nounwind optnone +define ptr @g() #0 { +entry: + ret ptr @y +} +; CHECK: LLVM ERROR: Aliases to common variables are not allowed on AIX: +; CHECK-NEXT: Alias attribute for y is invalid because x is common.