Index: lld/COFF/Writer.cpp =================================================================== --- lld/COFF/Writer.cpp +++ lld/COFF/Writer.cpp @@ -185,8 +185,7 @@ IdataContents Idata; DelayLoadContents DelayIdata; EdataContents Edata; - RVATableChunk *GuardFidsTable = nullptr; - RVATableChunk *SEHTable = nullptr; + bool HasSafeExceptionHandlers = false; DebugDirectoryChunk *DebugDirectory = nullptr; std::vector DebugRecords; @@ -828,8 +827,7 @@ PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION; if (Config->GuardCF != GuardCFLevel::Off) PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF; - if (Config->Machine == I386 && !SEHTable && - !Symtab->findUnderscore("_load_config_used")) + if (Config->Machine == I386 && !HasSafeExceptionHandlers) PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH; if (Config->TerminalServerAware) PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE; @@ -944,6 +942,8 @@ markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers); } + HasSafeExceptionHandlers = !Handlers.empty(); + maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table", "__safe_se_handler_count"); } Index: lld/test/COFF/safeseh.s =================================================================== --- lld/test/COFF/safeseh.s +++ lld/test/COFF/safeseh.s @@ -6,6 +6,7 @@ # __safe_se_handler_table needs to be relocated against ImageBase. # check that the relocation is present. +# # CHECK-NOGC-NOT: IMAGE_DLL_CHARACTERISTICS_NO_SEH # CHECK-NOGC: BaseReloc [ # CHECK-NOGC: Entry { @@ -19,9 +20,14 @@ # CHECK-NOGC-NEXT: 0x401006 # CHECK-NOGC-NEXT: ] -# Without the SEH table, the address is absolute, so check that we do -# not have a relocation for it. -# CHECK-GC-NOT: IMAGE_DLL_CHARACTERISTICS_NO_SEH +# If we enable GC, the exception handler should be removed, and we should add +# the DLL characteristic flag that indicates that there are no exception +# handlers in this DLL. The exception handler table in the load config should +# be empty and there should be no relocations for it. +# +# CHECK-GC: Characteristics [ +# CHECK-GC: IMAGE_DLL_CHARACTERISTICS_NO_SEH +# CHECK-GC: ] # CHECK-GC: BaseReloc [ # CHECK-GC-NEXT: ] # CHECK-GC: LoadConfig [