This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Set the IMAGE_DLL_CHARACTERISTICS_NO_SEH flag automatically
ClosedPublic

Authored by mstorsjo on Dec 14 2017, 11:36 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

mstorsjo created this revision.Dec 14 2017, 11:36 AM
rnk added a comment.Dec 14 2017, 11:59 AM

Does link.exe set this flag if there are no handlers in .sxdata? This flag seems like it was added as a way to mark DLLs that don't have _load_config_used as not having any exception handlers for /safeseh. Maybe we should just set this flag implicitly if there were no handlers in .sxdata and _load_config_used never resolved.

In D41252#955735, @rnk wrote:

Does link.exe set this flag if there are no handlers in .sxdata?

It doesn't seem so - a plain C hello world exe won't have it set.

This flag seems like it was added as a way to mark DLLs that don't have _load_config_used as not having any exception handlers for /safeseh. Maybe we should just set this flag implicitly if there were no handlers in .sxdata and _load_config_used never resolved.

If we choose to do that, we'd need to make this a no-op flag in the mingw driver instead - I've just run across some code that seems to set this flag (for unknown reasons).

rnk added a comment.Dec 14 2017, 12:19 PM
In D41252#955735, @rnk wrote:

Does link.exe set this flag if there are no handlers in .sxdata?

It doesn't seem so - a plain C hello world exe won't have it set.

Well, it will link the CRT, which has _except_handler4. You would need to make a no CRT test case and check that. It's probably simplest with a DLL with a custom entry point. You can also use llvm-readobj -coff-load-config to see that it doesn't have a load config directory.

This flag seems like it was added as a way to mark DLLs that don't have _load_config_used as not having any exception handlers for /safeseh. Maybe we should just set this flag implicitly if there were no handlers in .sxdata and _load_config_used never resolved.

If we choose to do that, we'd need to make this a no-op flag in the mingw driver instead - I've just run across some code that seems to set this flag (for unknown reasons).

Makes sense.

In D41252#955754, @rnk wrote:
In D41252#955735, @rnk wrote:

Does link.exe set this flag if there are no handlers in .sxdata?

It doesn't seem so - a plain C hello world exe won't have it set.

Well, it will link the CRT, which has _except_handler4. You would need to make a no CRT test case and check that. It's probably simplest with a DLL with a custom entry point. You can also use llvm-readobj -coff-load-config to see that it doesn't have a load config directory.

Oh, indeed. Yes, with such a DLL, I get the flag set automatically.

In D41252#955735, @rnk wrote:

Does link.exe set this flag if there are no handlers in .sxdata? This flag seems like it was added as a way to mark DLLs that don't have _load_config_used as not having any exception handlers for /safeseh. Maybe we should just set this flag implicitly if there were no handlers in .sxdata and _load_config_used never resolved.

So I guess this would make sense and match link.exe. Do you happen to know how this relates to SEH based exception handling in (GCC based) MinGW btw? When I build an executable (or DLL) with mingw-gcc that has SEH exceptions, llvm-readobj -coff-load-config doesn't show anything at all for such a binary. So if we were to link such binaries with LLD (which I don't do yet because libcxxabi lacks support for SEH), wouldn't we end up setting this flag for them as well?

mstorsjo updated this revision to Diff 127073.Dec 15 2017, 12:05 AM
mstorsjo retitled this revision from [MinGW] Implement the --no-seh flag to [COFF] Set the IMAGE_DLL_CHARACTERISTICS_NO_SEH flag automatically.
mstorsjo edited the summary of this revision. (Show Details)

Split out the MinGW part, made it set the flag automatically based on Reid's suggestion.

rnk added a comment.Dec 15 2017, 11:25 AM
In D41252#955735, @rnk wrote:

Does link.exe set this flag if there are no handlers in .sxdata? This flag seems like it was added as a way to mark DLLs that don't have _load_config_used as not having any exception handlers for /safeseh. Maybe we should just set this flag implicitly if there were no handlers in .sxdata and _load_config_used never resolved.

So I guess this would make sense and match link.exe. Do you happen to know how this relates to SEH based exception handling in (GCC based) MinGW btw? When I build an executable (or DLL) with mingw-gcc that has SEH exceptions, llvm-readobj -coff-load-config doesn't show anything at all for such a binary. So if we were to link such binaries with LLD (which I don't do yet because libcxxabi lacks support for SEH), wouldn't we end up setting this flag for them as well?

I suspect that this flag is only meaningful for x86 images, and we should only ever set it for x86 images that have no exception handlers and no load config. GCC's SEH support is x64-only, so this should never interfere with that.

mstorsjo updated this revision to Diff 127166.Dec 15 2017, 11:35 AM

Only set the flag on x86.

rnk accepted this revision.Dec 15 2017, 11:44 AM

Only set the flag on x86.

Can you confirm that this is consistent with MSVC? If so, looks good!

COFF/Writer.cpp
692 ↗(On Diff #127166)

nit: I'd test the Machine first to short-circuit out of the symbol lookup for x64.

This revision is now accepted and ready to land.Dec 15 2017, 11:44 AM
In D41252#957049, @rnk wrote:

Only set the flag on x86.

Can you confirm that this is consistent with MSVC? If so, looks good!

Yes, confirmed that MSVC only sets it on x86.

COFF/Writer.cpp
692 ↗(On Diff #127166)

Will do before pushing.

This revision was automatically updated to reflect the committed changes.