Skip to content

Commit 7b189a6

Browse files
committed
Teach the MSVC toolchain driver to link the correct asan libraries.
Now that the static runtime is gone the required librares are different. Note that /MD or -D_DLL causes the dynamic runtime to get linked, this is a little gross but emulates the behavior of MSVC. "Real" msvc will link the debug build of asan if you specify /DEBUG or _DEBUG, but that's not really necessary and requires building multiple configurations from the same tree.
1 parent f99a63e commit 7b189a6

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
196196
if (TC.getSanitizerArgs(Args).needsAsanRt()) {
197197
CmdArgs.push_back(Args.MakeArgString("-debug"));
198198
CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
199-
if (TC.getSanitizerArgs(Args).needsSharedRt() ||
200-
Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
201-
for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
202-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
199+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic"));
200+
auto defines = Args.getAllArgValues(options::OPT_D);
201+
if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd) ||
202+
find(begin(defines), end(defines), "_DLL") != end(defines)) {
203203
// Make sure the dynamic runtime thunk is not optimized out at link time
204204
// to ensure proper SEH handling.
205205
CmdArgs.push_back(Args.MakeArgString(
@@ -208,19 +208,15 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
208208
: "-include:__asan_seh_interceptor"));
209209
// Make sure the linker consider all object files from the dynamic runtime
210210
// thunk.
211-
CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
211+
CmdArgs.push_back(Args.MakeArgString(
212+
std::string("-wholearchive:") +
212213
TC.getCompilerRT(Args, "asan_dynamic_runtime_thunk")));
213-
} else if (DLL) {
214-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
215214
} else {
216-
for (const auto &Lib : {"asan", "asan_cxx"}) {
217-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
218-
// Make sure the linker consider all object files from the static lib.
219-
// This is necessary because instrumented dlls need access to all the
220-
// interface exported by the static lib in the main executable.
221-
CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
222-
TC.getCompilerRT(Args, Lib)));
223-
}
215+
// Make sure the linker consider all object files from the static runtime
216+
// thunk.
217+
CmdArgs.push_back(Args.MakeArgString(
218+
std::string("-wholearchive:") +
219+
TC.getCompilerRT(Args, "asan_static_runtime_thunk")));
224220
}
225221
}
226222

0 commit comments

Comments
 (0)