Skip to content

Commit 282da83

Browse files
[XCOFF][AIX] Issue an error when specifying an alias for a common symbol
Summary: There is no support in XCOFF for labels on common symbols. Therefore, an alias for a common symbol is not supported. Issue an error in the front end when an aliasee is a common symbol. Issue a similar error in the back end in case an IR specifies an alias for a common symbol. Reviewed by: hubert.reinterpretcast, DiggerLin Differential Revision: https://reviews.llvm.org/D158739
1 parent b6ce860 commit 282da83

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ def err_avx_calling_convention : Error<warn_avx_calling_convention.Summary>;
283283
def err_alias_to_undefined : Error<
284284
"%select{alias|ifunc}0 must point to a defined "
285285
"%select{variable or |}1function">;
286+
def err_alias_to_common : Error<
287+
"alias to a variable in a common section is not allowed">;
286288
def note_alias_requires_mangled_name : Note<
287289
"the %select{function or variable|function}0 specified in an %select{alias|ifunc}1 must refer to its mangled name">;
288290
def note_alias_mangled_name_alternative: Note<

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) {
563563
}
564564

565565
static bool checkAliasedGlobal(
566-
DiagnosticsEngine &Diags, SourceLocation Location, bool IsIFunc,
567-
const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV,
566+
const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location,
567+
bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV,
568568
const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames,
569569
SourceRange AliasRange) {
570570
GV = getAliasedGlobal(Alias);
@@ -573,6 +573,14 @@ static bool checkAliasedGlobal(
573573
return false;
574574
}
575575

576+
if (GV->hasCommonLinkage()) {
577+
const llvm::Triple &Triple = Context.getTargetInfo().getTriple();
578+
if (Triple.getObjectFormat() == llvm::Triple::XCOFF) {
579+
Diags.Report(Location, diag::err_alias_to_common);
580+
return false;
581+
}
582+
}
583+
576584
if (GV->isDeclaration()) {
577585
Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
578586
Diags.Report(Location, diag::note_alias_requires_mangled_name)
@@ -633,7 +641,7 @@ void CodeGenModule::checkAliases() {
633641
StringRef MangledName = getMangledName(GD);
634642
llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
635643
const llvm::GlobalValue *GV = nullptr;
636-
if (!checkAliasedGlobal(Diags, Location, IsIFunc, Alias, GV,
644+
if (!checkAliasedGlobal(getContext(), Diags, Location, IsIFunc, Alias, GV,
637645
MangledDeclNames, Range)) {
638646
Error = true;
639647
continue;

clang/test/CodeGen/aix-common.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -triple powerpc-ibm-aix -S -fcommon %s -verify -o -
2+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -S -fcommon %s -verify -o -
3+
int xxxxxx;
4+
extern int yyyyyy __attribute__((__alias__("xxxxxx") )); //expected-error {{alias to a variable in a common section is not allowed}}
5+
6+
void *gggggg() { return &yyyyyy; }

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,11 +2762,21 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
27622762

27632763
// Construct an aliasing list for each GlobalObject.
27642764
for (const auto &Alias : M.aliases()) {
2765-
const GlobalObject *Base = Alias.getAliaseeObject();
2766-
if (!Base)
2765+
const GlobalObject *Aliasee = Alias.getAliaseeObject();
2766+
if (!Aliasee)
27672767
report_fatal_error(
27682768
"alias without a base object is not yet supported on AIX");
2769-
GOAliasMap[Base].push_back(&Alias);
2769+
2770+
if (Aliasee->hasCommonLinkage()) {
2771+
report_fatal_error("Aliases to common variables are not allowed on AIX:"
2772+
"\n\tAlias attribute for " +
2773+
Alias.getGlobalIdentifier() +
2774+
" is invalid because " + Aliasee->getName() +
2775+
" is common.",
2776+
false);
2777+
}
2778+
2779+
GOAliasMap[Aliasee].push_back(&Alias);
27702780
}
27712781

27722782
return Result;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: not llc -filetype=obj -mtriple powerpc-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s
2+
; RUN: not llc -filetype=asm -mtriple powerpc-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s
3+
; RUN: not llc -filetype=obj -mtriple powerpc64-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s
4+
; RUN: not llc -filetype=asm -mtriple powerpc64-ibm-aix-xcoff -o %t.o < %s 2>&1 | FileCheck %s
5+
@x= common global i32 0, align 4
6+
7+
@y= alias i32, ptr @x
8+
9+
; Function Attrs: noinline nounwind optnone
10+
define ptr @g() #0 {
11+
entry:
12+
ret ptr @y
13+
}
14+
; CHECK: LLVM ERROR: Aliases to common variables are not allowed on AIX:
15+
; CHECK-NEXT: Alias attribute for y is invalid because x is common.

0 commit comments

Comments
 (0)