Skip to content

Commit 425198b

Browse files
mspangzmodem
authored andcommitted
[GlobalMerge] Preserve symbol visibility when merging globals
Symbols created for merged external global variables have default visibility. This can break programs when compiling with -Oz -fvisibility=hidden as symbols that should be hidden will be exported at link time. Differential Revision: https://reviews.llvm.org/D73235 (cherry picked from commit a2fb2c0)
1 parent 81d73c6 commit 425198b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
524524
for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k), ++idx) {
525525
GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
526526
std::string Name = Globals[k]->getName();
527+
GlobalValue::VisibilityTypes Visibility = Globals[k]->getVisibility();
527528
GlobalValue::DLLStorageClassTypes DLLStorage =
528529
Globals[k]->getDLLStorageClass();
529530

@@ -549,6 +550,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
549550
if (Linkage != GlobalValue::InternalLinkage || !IsMachO) {
550551
GlobalAlias *GA = GlobalAlias::create(Tys[StructIdxs[idx]], AddrSpace,
551552
Linkage, Name, GEP, &M);
553+
GA->setVisibility(Visibility);
552554
GA->setDLLStorageClass(DLLStorage);
553555
}
554556

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: llc %s -mtriple=arm-none-linux-gnu -o - | FileCheck %s
2+
; RUN: llc %s -mtriple=aarch64-none-linux-gnu -o - | FileCheck %s
3+
4+
@x = hidden global i32 0, align 4
5+
@y = hidden global i32 0, align 4
6+
7+
define hidden void @f() #0 {
8+
store i32 0, i32* @x, align 4
9+
store i32 0, i32* @y, align 4
10+
ret void
11+
}
12+
13+
attributes #0 = { minsize optsize }
14+
15+
; CHECK: .local .L_MergedGlobals
16+
; CHECK: .comm .L_MergedGlobals,8,4
17+
18+
; CHECK: .globl x
19+
; CHECK: .hidden x
20+
; CHECK: .set x, .L_MergedGlobals
21+
; CHECK: .size x, 4
22+
23+
; CHECK: .globl y
24+
; CHECK: .hidden y
25+
; CHECK: .set y, .L_MergedGlobals+4
26+
; CHECK: .size y, 4

0 commit comments

Comments
 (0)