[lld] 360d0cd - [LLD] Do not assume /guard:cf always set together with /guard:ehcont

Phoebe Wang via llvm-commits llvm-commits at lists.llvm.org
Tue May 16 08:12:43 PDT 2023


Author: Phoebe Wang
Date: 2023-05-16T23:12:03+08:00
New Revision: 360d0cd0a238b9b069a83e7aaff534e5712dbc90

URL: https://github.com/llvm/llvm-project/commit/360d0cd0a238b9b069a83e7aaff534e5712dbc90
DIFF: https://github.com/llvm/llvm-project/commit/360d0cd0a238b9b069a83e7aaff534e5712dbc90.diff

LOG: [LLD] Do not assume /guard:cf always set together with /guard:ehcont

MS link accepts *.obj with ehcont bit set only. LLD should match this
behavoir too.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D150508

Added: 
    

Modified: 
    lld/COFF/InputFiles.h
    lld/COFF/Writer.cpp
    lld/test/COFF/guard-ehcont.s

Removed: 
    


################################################################################
diff  --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index 3acd74fb52f62..3b55cd791bfda 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -177,7 +177,10 @@ class ObjFile : public InputFile {
   bool hasSafeSEH() { return feat00Flags & 0x1; }
 
   // True if this file was compiled with /guard:cf.
-  bool hasGuardCF() { return feat00Flags & 0x4800; }
+  bool hasGuardCF() { return feat00Flags & 0x800; }
+
+  // True if this file was compiled with /guard:ehcont.
+  bool hasGuardEHCont() { return feat00Flags & 0x4000; }
 
   // Pointer to the PDB module descriptor builder. Various debug info records
   // will reference object files by "module index", which is here. Things like

diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 645c63f690409..256e914ef84b8 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1733,20 +1733,22 @@ void Writer::createGuardCFTables() {
   SymbolRVASet ehContTargets;
   for (ObjFile *file : ctx.objFileInstances) {
     // If the object was compiled with /guard:cf, the address taken symbols
-    // are in .gfids$y sections, the longjmp targets are in .gljmp$y sections,
-    // and ehcont targets are in .gehcont$y sections. If the object was not
-    // compiled with /guard:cf, we assume there were no setjmp and ehcont
-    // targets, and that all code symbols with relocations are possibly
-    // address-taken.
+    // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
+    // sections. If the object was not compiled with /guard:cf, we assume there
+    // were no setjmp targets, and that all code symbols with relocations are
+    // possibly address-taken.
     if (file->hasGuardCF()) {
       markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
       markSymbolsForRVATable(file, file->getGuardIATChunks(), giatsRVASet);
       getSymbolsFromSections(file, file->getGuardIATChunks(), giatsSymbols);
       markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
-      markSymbolsForRVATable(file, file->getGuardEHContChunks(), ehContTargets);
     } else {
       markSymbolsWithRelocations(file, addressTakenSyms);
     }
+    // If the object was compiled with /guard:ehcont, the ehcont targets are in
+    // .gehcont$y sections.
+    if (file->hasGuardEHCont())
+      markSymbolsForRVATable(file, file->getGuardEHContChunks(), ehContTargets);
   }
 
   // Mark the image entry as address-taken.

diff  --git a/lld/test/COFF/guard-ehcont.s b/lld/test/COFF/guard-ehcont.s
index 9b80a6ac42865..f040a62f7e3dd 100644
--- a/lld/test/COFF/guard-ehcont.s
+++ b/lld/test/COFF/guard-ehcont.s
@@ -10,7 +10,7 @@
 # CHECK:   GuardCFCheckFunction: 0x0
 # CHECK:   GuardCFCheckDispatch: 0x0
 # CHECK:   GuardCFFunctionTable: 0x14000{{.*}}
-# CHECK:   GuardCFFunctionCount: 1
+# CHECK:   GuardCFFunctionCount: 4
 # CHECK:   GuardFlags [ (0x400500)
 # CHECK:     CF_FUNCTION_TABLE_PRESENT (0x400)
 # CHECK:     CF_INSTRUMENTED (0x100)
@@ -225,4 +225,4 @@ _load_config_used:
         .fill 72, 1, 0
         .quad __guard_eh_cont_table
         .quad __guard_eh_cont_count
-        .fill 32, 1, 0
\ No newline at end of file
+        .fill 32, 1, 0


        


More information about the llvm-commits mailing list