Skip to content

Commit 91e69d8

Browse files
committedDec 13, 2018
[MachO][TLOF] Add support for local symbols in the indirect symbol table
On 32-bit archs, before, we would assume that an indirect symbol will never have local linkage. This can lead to miscompiles where the symbol's value would be 0 and the linker would use that value, because the indirect symbol table would contain the value `INDIRECT_SYMBOL_LOCAL` for that specific symbol. Differential Revision: https://reviews.llvm.org/D55573 llvm-svn: 349060
1 parent 0250e29 commit 91e69d8

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed
 

‎llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,22 @@ const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
11001100
// .indirect_symbol _extfoo
11011101
// .long 0
11021102
//
1103+
// The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1104+
// may point to both local (same translation unit) and global (other
1105+
// translation units) symbols. Example:
1106+
//
1107+
// .section __DATA,__pointers,non_lazy_symbol_pointers
1108+
// L1:
1109+
// .indirect_symbol _myGlobal
1110+
// .long 0
1111+
// L2:
1112+
// .indirect_symbol _myLocal
1113+
// .long _myLocal
1114+
//
1115+
// If the symbol is local, instead of the symbol's index, the assembler
1116+
// places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1117+
// Then the linker will notice the constant in the table and will look at the
1118+
// content of the symbol.
11031119
MachineModuleInfoMachO &MachOMMI =
11041120
MMI->getObjFileInfo<MachineModuleInfoMachO>();
11051121
MCContext &Ctx = getContext();
@@ -1119,9 +1135,12 @@ const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
11191135
MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
11201136

11211137
MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
1122-
if (!StubSym.getPointer())
1123-
StubSym = MachineModuleInfoImpl::
1124-
StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
1138+
if (!StubSym.getPointer()) {
1139+
bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal();
1140+
// With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
1141+
StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
1142+
!IsIndirectLocal);
1143+
}
11251144

11261145
const MCExpr *BSymExpr =
11271146
MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);

‎llvm/test/MC/MachO/cstexpr-gotpcrel-32.ll

+21
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,24 @@ define i32 @t0(i32 %a) {
7272
i32 ptrtoint (i32 (i32)* @t0 to i32)), %a
7373
ret i32 %x
7474
}
75+
76+
; Text indirect local symbols.
77+
; CHECK-LABEL: _localindirect
78+
; CHECK: .long 65603
79+
@localindirect = internal constant i32 65603
80+
@got.localindirect = private unnamed_addr constant i32* @localindirect
81+
82+
; CHECK-LABEL: _localindirectuser:
83+
; CHECK: .long L_localindirect$non_lazy_ptr-_localindirectuser
84+
@localindirectuser = internal constant
85+
i32 sub (i32 ptrtoint (i32** @got.localindirect to i32),
86+
i32 ptrtoint (i32* @localindirectuser to i32))
87+
88+
; CHECK-LABEL: L_localindirect$non_lazy_ptr:
89+
; CHECK: .indirect_symbol _localindirect
90+
; CHECK-NOT: .long 0
91+
; CHECK-NEXT: .long _localindirect
92+
define i8* @testRelativeIndirectSymbol() {
93+
%1 = bitcast i32* @localindirectuser to i8*
94+
ret i8* %1
95+
}

0 commit comments

Comments
 (0)