Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 6861d89

Browse files
committed
Handle undefined weak hidden symbols on all architectures.
We were handling the non-hidden case in lib/Target/TargetMachine.cpp, but the hidden case was handled in architecture dependent code and only X86_64 and AArch64 were covered. While it is true that some code sequences in some ABIs might be able to produce the correct value at runtime, that doesn't seem to be the common case. I left the AArch64 code in place since it also forces a got access for non-pic code. It is not clear if that is needed, but it is probably better to change that in another commit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316799 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 67384e3 commit 6861d89

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

lib/Target/TargetMachine.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
128128
if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
129129
return true;
130130

131+
// Most PIC code sequences that assume that a symbol is local cannot
132+
// produce a 0 if it turns out the symbol is undefined. While this
133+
// is ABI and relocation depended, it seems worth it to handle it
134+
// here.
135+
// FIXME: this is probably not ELF specific.
136+
if (GV && isPositionIndependent() && TT.isOSBinFormatELF() &&
137+
GV->hasExternalWeakLinkage())
138+
return false;
139+
131140
if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility() ||
132141
GV->isDSOLocal()))
133142
return true;
@@ -149,9 +158,8 @@ bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
149158
return true;
150159

151160
bool IsTLS = GV && GV->isThreadLocal();
152-
bool IsAccessViaCopyRelocs = Options.MCOptions.MCPIECopyRelocations && GV &&
153-
isa<GlobalVariable>(GV) &&
154-
!GV->hasExternalWeakLinkage();
161+
bool IsAccessViaCopyRelocs =
162+
Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV);
155163
Triple::ArchType Arch = TT.getArch();
156164
bool IsPPC =
157165
Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le;

lib/Target/X86/X86Subtarget.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,6 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
9999
return X86II::MO_GOTOFF;
100100
}
101101

102-
static bool shouldAssumeGlobalReferenceLocal(const X86Subtarget *ST,
103-
const TargetMachine &TM,
104-
const Module &M,
105-
const GlobalValue *GV) {
106-
if (!TM.shouldAssumeDSOLocal(M, GV))
107-
return false;
108-
// A weak reference can end up being 0. If the code can be more that 4g away
109-
// from zero and we are using the small code model we have to treat it as non
110-
// local.
111-
if (GV && GV->hasExternalWeakLinkage() &&
112-
TM.getCodeModel() == CodeModel::Small && TM.isPositionIndependent() &&
113-
ST->is64Bit() && ST->isTargetELF())
114-
return false;
115-
return true;
116-
}
117-
118102
unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV,
119103
const Module &M) const {
120104
// Large model never uses stubs.
@@ -134,7 +118,7 @@ unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV,
134118
}
135119
}
136120

137-
if (shouldAssumeGlobalReferenceLocal(this, TM, M, GV))
121+
if (TM.shouldAssumeDSOLocal(M, GV))
138122
return classifyLocalReference(GV);
139123

140124
if (isTargetCOFF())

test/CodeGen/X86/global-access-pie-copyrelocs.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ entry:
7777
ret i32* @e
7878
}
7979

80+
; ExternalWeak hidden Linkage
81+
@he = extern_weak hidden global i32, align 4
82+
83+
define i32* @my_access_global_he() #0 {
84+
; X32-LABEL: my_access_global_he:
85+
; X32: addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
86+
; X32: movl he@GOT(%eax), %eax
87+
; X64-LABEL: my_access_global_he:
88+
; X64: movq he@GOTPCREL(%rip), %rax
89+
ret i32* @he
90+
}
91+
92+
8093
; External Linkage, only declaration, store a value.
8194

8295
define i32 @my_access_global_store_d() #0 {

test/CodeGen/X86/weak-undef.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define i32* @bar1() {
88
; CHECK: bar1:
99
; CHECK: movq foo1@GOTPCREL(%rip), %rax
1010
; I386: bar1:
11-
; I386: leal foo1@GOTOFF(%eax), %eax
11+
; I386: movl foo1@GOT(%eax), %eax
1212

1313
@foo2 = external hidden global i32, align 4
1414
define i32* @bar2() {
@@ -46,7 +46,7 @@ define i32()* @bar5() {
4646
; CHECK: bar5:
4747
; CHECK: movq foo5@GOTPCREL(%rip), %rax
4848
; I386: bar5:
49-
; I386: leal foo5@GOTOFF(%eax), %eax
49+
; I386: movl foo5@GOT(%eax), %eax
5050

5151
declare external hidden i32 @foo6()
5252
define i32()* @bar6() {

0 commit comments

Comments
 (0)