Skip to content

Commit a8c2b7d

Browse files
dpaoliellosivadeilra
authored andcommitted
Apply 0023-dynfixup-aslr.patch
1 parent fcc8144 commit a8c2b7d

File tree

16 files changed

+104
-4
lines changed

16 files changed

+104
-4
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5255,3 +5255,10 @@ def NonString : InheritableAttr {
52555255
let Subjects = SubjectList<[Var, Field]>;
52565256
let Documentation = [NonStringDocs];
52575257
}
5258+
5259+
def DynamicFixup : InheritableAttr {
5260+
let Spellings = [Declspec<"dynfixup">];
5261+
let Subjects = SubjectList<[ExternalGlobalVar]>;
5262+
let LangOpts = [MicrosoftExt];
5263+
let Documentation = [Undocumented];
5264+
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5310,6 +5310,14 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
53105310
isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
53115311
GV->setSection(".cp.rodata");
53125312

5313+
// Propagate MSVC dynamic fixup attribute for globals for further handling
5314+
// in target(s).
5315+
if (getTriple().isWindowsMSVCEnvironment() &&
5316+
D->getLanguageLinkage() == CLanguageLinkage &&
5317+
isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
5318+
if (const DynamicFixupAttr *DA = D->getAttr<DynamicFixupAttr>())
5319+
GV->addAttribute("msvc_dynfixup");
5320+
53135321
// Handle code model attribute
53145322
if (const auto *CMA = D->getAttr<CodeModelAttr>())
53155323
GV->setCodeModel(CMA->getModel());

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6961,6 +6961,11 @@ static void handleVTablePointerAuthentication(Sema &S, Decl *D,
69616961
CustomDiscriminationValue));
69626962
}
69636963

6964+
static void handleDynamicFixup(Sema &S, Decl *D,
6965+
const ParsedAttr &AL) {
6966+
D->addAttr(::new ( S.Context) DynamicFixupAttr(S.Context, AL));
6967+
}
6968+
69646969
//===----------------------------------------------------------------------===//
69656970
// Top Level Sema Entry Points
69666971
//===----------------------------------------------------------------------===//
@@ -7885,6 +7890,11 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
78857890
case ParsedAttr::AT_VTablePointerAuthentication:
78867891
handleVTablePointerAuthentication(S, D, AL);
78877892
break;
7893+
7894+
case ParsedAttr::AT_DynamicFixup:
7895+
handleDynamicFixup(S, D, AL);
7896+
break;
7897+
78887898
}
78897899
}
78907900

llvm/include/llvm/MC/MCExpr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class MCSymbolRefExpr : public MCExpr {
196196
// a cleaner approach.
197197
enum VariantKind : uint16_t {
198198
VK_COFF_IMGREL32 = 3, // symbol@imgrel (image-relative)
199+
VK_COFF_DYNFIXUP, // Absolute relocation for external tooling
199200

200201
FirstTargetSpecifier,
201202
};

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,15 @@ const MCExpr *AArch64AsmPrinter::lowerConstant(const Constant *CV,
35633563
const Constant *BaseCV,
35643564
uint64_t Offset) {
35653565
if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
3566+
3567+
// Check for dynamic fixup in the constant pool and propagate to the symbol
3568+
// reference
3569+
if (const auto *GVar = dyn_cast<llvm::GlobalVariable>(GV)) {
3570+
if (GVar->hasAttribute("msvc_dynfixup"))
3571+
return MCSymbolRefExpr::create(MCInstLowering.GetGlobalValueSymbol(GV, 0),
3572+
MCSymbolRefExpr::VK_COFF_DYNFIXUP,
3573+
OutContext);
3574+
}
35663575
return MCSymbolRefExpr::create(MCInstLowering.GetGlobalValueSymbol(GV, 0),
35673576
OutContext);
35683577
}

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9890,6 +9890,17 @@ SDValue AArch64TargetLowering::LowerGlobalAddress(SDValue Op,
98909890
}
98919891

98929892
SDValue Result;
9893+
SDLoc DL(GN);
9894+
EVT PtrVT = getPointerTy(DAG.getDataLayout());
9895+
9896+
// Dynamic fixups require an absolute relocation, so lower to a load from the
9897+
// constant pool where this relocation can be applied.
9898+
if ((OpFlags & AArch64II::MO_DYNFIXUP) != 0) {
9899+
Result = DAG.getTargetConstantPool(GV, PtrVT, Align(8));
9900+
Result = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Result);
9901+
return Result;
9902+
}
9903+
98939904
if (getTargetMachine().getCodeModel() == CodeModel::Large &&
98949905
!getTargetMachine().isPositionIndependent()) {
98959906
Result = getAddrLarge(GN, DAG, OpFlags);
@@ -9898,8 +9909,6 @@ SDValue AArch64TargetLowering::LowerGlobalAddress(SDValue Op,
98989909
} else {
98999910
Result = getAddr(GN, DAG, OpFlags);
99009911
}
9901-
EVT PtrVT = getPointerTy(DAG.getDataLayout());
9902-
SDLoc DL(GN);
99039912
if (OpFlags & (AArch64II::MO_DLLIMPORT | AArch64II::MO_COFFSTUB))
99049913
Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
99059914
MachinePointerInfo::getGOT(DAG.getMachineFunction()));

llvm/lib/Target/AArch64/AArch64Subtarget.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/CodeGen/MachineFrameInfo.h"
2525
#include "llvm/CodeGen/MachineScheduler.h"
2626
#include "llvm/IR/GlobalValue.h"
27+
#include "llvm/IR/GlobalVariable.h"
2728
#include "llvm/Support/SipHash.h"
2829
#include "llvm/TargetParser/AArch64TargetParser.h"
2930

@@ -464,6 +465,14 @@ AArch64Subtarget::ClassifyGlobalReference(const GlobalValue *GV,
464465
if (TM.getCodeModel() == CodeModel::Large && isTargetMachO())
465466
return AArch64II::MO_GOT;
466467

468+
// MSVC Dynamic fixup requires a an absolute relocation. Load from constant
469+
// pool and apply that relocation there.
470+
if (const auto *GVar = dyn_cast<llvm::GlobalVariable>(GV)) {
471+
if (GVar->hasAttribute("msvc_dynfixup")) {
472+
return AArch64II::MO_DYNFIXUP;
473+
}
474+
}
475+
467476
// All globals dynamically protected by MTE must have their address tags
468477
// synthesized. This is done by having the loader stash the tag in the GOT
469478
// entry. Force all tagged globals (even ones with internal linkage) through

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCAsmInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static cl::opt<AsmWriterVariantTy> AsmWriterVariant(
3333

3434
const MCAsmInfo::AtSpecifier COFFAtSpecifiers[] = {
3535
{MCSymbolRefExpr::VK_COFF_IMGREL32, "IMGREL"},
36+
{MCSymbolRefExpr::VK_COFF_DYNFIXUP, "DYNFIXUP"},
3637
{AArch64::S_MACHO_PAGEOFF, "PAGEOFF"},
3738
};
3839

llvm/lib/Target/AArch64/MCTargetDesc/AArch64WinCOFFObjectWriter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AArch64WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
4747
unsigned AArch64WinCOFFObjectWriter::getRelocType(
4848
MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup,
4949
bool IsCrossSection, const MCAsmBackend &MAB) const {
50+
5051
unsigned FixupKind = Fixup.getKind();
5152
bool PCRel = Fixup.isPCRel();
5253
if (IsCrossSection) {
@@ -105,7 +106,10 @@ unsigned AArch64WinCOFFObjectWriter::getRelocType(
105106
}
106107

107108
case FK_Data_8:
108-
return COFF::IMAGE_REL_ARM64_ADDR64;
109+
if (Spec == MCSymbolRefExpr::VK_COFF_DYNFIXUP)
110+
return COFF::IMAGE_REL_ARM64_ABSOLUTE;
111+
else
112+
return COFF::IMAGE_REL_ARM64_ADDR64;
109113

110114
case FK_SecRel_2:
111115
return COFF::IMAGE_REL_ARM64_SECTION;

llvm/lib/Target/AArch64/Utils/AArch64BaseInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,13 @@ enum TOF {
883883
/// uses "__imp_aux". For other symbols, this means it uses the mangled
884884
/// ("#" prefix for C) name.
885885
MO_ARM64EC_CALLMANGLE = 0x800,
886+
887+
/// MO_DYNFIXUP - This flag indicates that this symbol will be dynamically
888+
// fixed up -- first by the linker, and then potentially at runtime.
889+
// Reference it from the literal pool with an ABS relocation
890+
// N.B. Never stored so does not exceed 12 bits associated with target flags
891+
MO_DYNFIXUP = 0x1000,
892+
886893
};
887894
} // end namespace AArch64II
888895

0 commit comments

Comments
 (0)