-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LLD][COFF] Support CF guards on ARM64X #128440
Open
cjacek
wants to merge
1
commit into
llvm:main
Choose a base branch
from
cjacek:arm64x-guardcf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Both native and EC views share table chunks. Ensure relevant symbols are set in both symbol tables.
@llvm/pr-subscribers-platform-windows @llvm/pr-subscribers-lld-coff Author: Jacek Caban (cjacek) ChangesBoth native and EC views share table chunks. Ensure relevant symbols are set in both symbol tables. Full diff: https://github.com/llvm/llvm-project/pull/128440.diff 2 Files Affected:
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 58727c1615769..c746ea8e1d7b8 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1995,10 +1995,17 @@ static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
// Common is always data, so it is ignored.
break;
case Symbol::DefinedAbsoluteKind:
- case Symbol::DefinedSyntheticKind:
// Absolute is never code, synthetic generally isn't and usually isn't
// determinable.
break;
+ case Symbol::DefinedSyntheticKind:
+ // For EC export thunks, mark both the thunk itself and its target.
+ if (auto expChunk = dyn_cast_or_null<ECExportThunkChunk>(
+ cast<Defined>(s)->getChunk())) {
+ addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
+ addSymbolToRVASet(addressTakenSyms, expChunk->target);
+ }
+ break;
case Symbol::LazyArchiveKind:
case Symbol::LazyObjectKind:
case Symbol::LazyDLLSymbolKind:
@@ -2063,9 +2070,11 @@ void Writer::createGuardCFTables() {
// with /guard:cf.
for (ObjFile *file : ctx.objFileInstances) {
if (file->hasGuardCF()) {
- Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
- cast<DefinedAbsolute>(flagSym)->setVA(
- uint32_t(GuardFlags::CF_INSTRUMENTED));
+ ctx.forEachSymtab([&](SymbolTable &symtab) {
+ Symbol *flagSym = symtab.findUnderscore("__guard_flags");
+ cast<DefinedAbsolute>(flagSym)->setVA(
+ uint32_t(GuardFlags::CF_INSTRUMENTED));
+ });
break;
}
}
@@ -2147,8 +2156,10 @@ void Writer::createGuardCFTables() {
guardFlags |= uint32_t(GuardFlags::CF_LONGJUMP_TABLE_PRESENT);
if (config->guardCF & GuardCFLevel::EHCont)
guardFlags |= uint32_t(GuardFlags::EH_CONTINUATION_TABLE_PRESENT);
- Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
- cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
+ ctx.forEachSymtab([guardFlags](SymbolTable &symtab) {
+ Symbol *flagSym = symtab.findUnderscore("__guard_flags");
+ cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
+ });
}
// Take a list of input sections containing symbol table indices and add those
@@ -2219,10 +2230,12 @@ void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
tableChunk = make<RVATableChunk>(std::move(tableSymbols));
rdataSec->addChunk(tableChunk);
- Symbol *t = ctx.symtab.findUnderscore(tableSym);
- Symbol *c = ctx.symtab.findUnderscore(countSym);
- replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
- cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4));
+ ctx.forEachSymtab([&](SymbolTable &symtab) {
+ Symbol *t = symtab.findUnderscore(tableSym);
+ Symbol *c = symtab.findUnderscore(countSym);
+ replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
+ cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4));
+ });
}
// Create CHPE metadata chunks.
diff --git a/lld/test/COFF/arm64x-guardcf.s b/lld/test/COFF/arm64x-guardcf.s
new file mode 100644
index 0000000000000..750bf0b3862c5
--- /dev/null
+++ b/lld/test/COFF/arm64x-guardcf.s
@@ -0,0 +1,122 @@
+// REQUIRES: aarch64, x86
+// RUN: split-file %s %t.dir && cd %t.dir
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows func-gfids.s -o func-gfids-arm64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows func-gfids.s -o func-gfids-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows func-exp.s -o func-exp-arm64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows func-exp.s -o func-exp-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows dllmain.s -o dllmain-arm64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows dllmain.s -o dllmain-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=x86_64-windows func-amd64.s -o func-amd64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o loadconfig-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows %S/Inputs/loadconfig-arm64.s -o loadconfig-arm64.obj
+
+
+// Check that CF guard tables contain both native and EC symbols and are referenced from both load configs.
+
+// RUN: lld-link -dll -noentry -machine:arm64x func-gfids-arm64.obj func-gfids-arm64ec.obj func-amd64.obj -guard:cf -out:out.dll \
+// RUN: loadconfig-arm64ec.obj loadconfig-arm64.obj
+// RUN: llvm-readobj --coff-load-config out.dll | FileCheck --check-prefix=LOADCFG %s
+
+// LOADCFG: LoadConfig [
+// LOADCFG: GuardCFFunctionCount: 3
+// LOADCFG-NEXT: GuardFlags [ (0x10500)
+// LOADCFG-NEXT: CF_FUNCTION_TABLE_PRESENT (0x400)
+// LOADCFG-NEXT: CF_INSTRUMENTED (0x100)
+// LOADCFG-NEXT: CF_LONGJUMP_TABLE_PRESENT (0x10000)
+// LOADCFG-NEXT: ]
+// LOADCFG: ]
+// LOADCFG: GuardFidTable [
+// LOADCFG-NEXT: 0x180001000
+// LOADCFG-NEXT: 0x180002000
+// LOADCFG-NEXT: 0x180003000
+// LOADCFG-NEXT: ]
+// LOADCFG: HybridObject {
+// LOADCFG: LoadConfig [
+// LOADCFG: GuardCFFunctionCount: 3
+// LOADCFG-NEXT: GuardFlags [ (0x10500)
+// LOADCFG-NEXT: CF_FUNCTION_TABLE_PRESENT (0x400)
+// LOADCFG-NEXT: CF_INSTRUMENTED (0x100)
+// LOADCFG-NEXT: CF_LONGJUMP_TABLE_PRESENT (0x10000)
+// LOADCFG-NEXT: ]
+// LOADCFG: ]
+// LOADCFG: GuardFidTable [
+// LOADCFG-NEXT: 0x180001000
+// LOADCFG-NEXT: 0x180002000
+// LOADCFG-NEXT: 0x180003000
+// LOADCFG-NEXT: ]
+// LOADCFG: ]
+
+
+// Check that exports from both views are present in CF guard tables.
+
+// RUN: lld-link -dll -noentry -machine:arm64x func-exp-arm64.obj func-exp-arm64ec.obj -guard:cf -out:out-exp.dll \
+// RUN: loadconfig-arm64ec.obj loadconfig-arm64.obj
+// RUN: llvm-readobj --coff-load-config out-exp.dll | FileCheck --check-prefix=LOADCFG %s
+
+
+// Check that entry points from both views are present in CF guard tables.
+
+// RUN: lld-link -dll -machine:arm64x dllmain-arm64.obj dllmain-arm64ec.obj -guard:cf -out:out-entry.dll \
+// RUN: loadconfig-arm64ec.obj loadconfig-arm64.obj
+// RUN: llvm-readobj --coff-load-config out-entry.dll | FileCheck --check-prefix=LOADCFG %s
+
+
+// Check that both load configs are marked as instrumented if any input object was built with /guard:cf.
+
+// RUN: lld-link -dll -noentry -machine:arm64x func-gfids-arm64ec.obj -out:out-nocfg.dll \
+// RUN: loadconfig-arm64ec.obj loadconfig-arm64.obj
+
+// RUN: llvm-readobj --coff-load-config out-nocfg.dll | FileCheck --check-prefix=LOADCFG-INST %s
+
+// LOADCFG-INST: LoadConfig [
+// LOADCFG-INST: GuardFlags [ (0x100)
+// LOADCFG-INST-NEXT: CF_INSTRUMENTED (0x100)
+// LOADCFG-INST-NEXT: ]
+// LOADCFG-INST: ]
+// LOADCFG-INST: HybridObject {
+// LOADCFG-INST: LoadConfig [
+// LOADCFG-INST: GuardFlags [ (0x100)
+// LOADCFG-INST-NEXT: CF_INSTRUMENTED (0x100)
+// LOADCFG-INST-NEXT: ]
+// LOADCFG-INST: ]
+// LOADCFG-INST: ]
+
+#--- func-gfids.s
+ .def @feat.00; .scl 3; .type 0; .endef
+ .globl @feat.00
+@feat.00 = 0x800
+
+ .globl func
+func:
+ ret
+
+ .section .gfids$y,"dr"
+ .symidx func
+
+#--- func-amd64.s
+ .def @feat.00; .scl 3; .type 0; .endef
+ .globl @feat.00
+@feat.00 = 0x800
+
+ .globl func_amd64
+func_amd64:
+ ret
+
+ .section .gfids$y,"dr"
+ .symidx func_amd64
+
+#--- func-exp.s
+ .def func; .scl 2; .type 32; .endef
+ .globl func
+func:
+ ret
+
+ .section .drectve
+ .ascii "-export:func"
+
+#--- dllmain.s
+ .def _DllMainCRTStartup; .scl 2; .type 32; .endef
+ .globl _DllMainCRTStartup
+_DllMainCRTStartup:
+ ret
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Both native and EC views share table chunks. Ensure relevant symbols are set in both symbol tables.