diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index f0fc630c46a6e2..13794035c90763 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -432,6 +432,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// values in order to be included in misexpect diagnostics. Optional DiagnosticsMisExpectTolerance = 0; + /// The name of a file to use with \c .secure_log_unique directives. + std::string AsSecureLogFile; + public: // Define accessors/mutators for code generation options of enumeration type. #define CODEGENOPT(Name, Bits, Default) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 048f28295b39b7..6d2c9998737338 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5349,6 +5349,9 @@ def fno_use_ctor_homing: Flag<["-"], "fno-use-ctor-homing">, HelpText<"Don't use constructor homing for debug info">; def fuse_ctor_homing: Flag<["-"], "fuse-ctor-homing">, HelpText<"Use constructor homing if we are using limited debug info already">; +def as_secure_log_file : Separate<["-"], "as-secure-log-file">, + HelpText<"Emit .secure_log_unique directives to this filename.">, + MarshallingInfoString>; } // let Flags = [CC1Option, CC1AsOption, NoDriverOption] diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 874fed795da9fe..f5c125da10da64 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -497,6 +497,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags, Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path); Options.MCOptions.Argv0 = CodeGenOpts.Argv0; Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs; + Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile; Options.MisExpect = CodeGenOpts.MisExpect; return true; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 6aa377c0a4309c..0786298ba918d9 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -2737,6 +2737,11 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, if (C.getDriver().embedBitcodeEnabled() || C.getDriver().embedBitcodeMarkerOnly()) Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); + + if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) { + CmdArgs.push_back("-as-secure-log-file"); + CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile)); + } } static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, diff --git a/clang/test/CodeGen/as-secure-log-file.c b/clang/test/CodeGen/as-secure-log-file.c new file mode 100644 index 00000000000000..7475a39037330f --- /dev/null +++ b/clang/test/CodeGen/as-secure-log-file.c @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-obj %s -o %t.o -as-secure-log-file %t.log +// RUN: FileCheck %s -input-file %t.log +// CHECK: "foobar" + +void test(void) { + __asm__(".secure_log_unique \"foobar\""); +} diff --git a/clang/test/Driver/AS_SECURE_LOG_FILE.s b/clang/test/Driver/AS_SECURE_LOG_FILE.s new file mode 100644 index 00000000000000..43d0841de4dffd --- /dev/null +++ b/clang/test/Driver/AS_SECURE_LOG_FILE.s @@ -0,0 +1,3 @@ +// RUN: env AS_SECURE_LOG_FILE=log_file %clang --target=x86_64-apple-darwin -c %s -### 2>&1 | FileCheck %s +// CHECK: "-cc1as" +// CHECK-SAME: "-as-secure-log-file" "log_file" diff --git a/clang/test/Misc/cc1as-as-secure-log-file.s b/clang/test/Misc/cc1as-as-secure-log-file.s new file mode 100644 index 00000000000000..c1ce6b58b422b9 --- /dev/null +++ b/clang/test/Misc/cc1as-as-secure-log-file.s @@ -0,0 +1,5 @@ +// RUN: %clang -cc1as -triple x86_64-apple-darwin %s -o %t.o -as-secure-log-file %t.log +// RUN: FileCheck %s -input-file %t.log +// CHECK: "foobar" + +.secure_log_unique "foobar" diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp index fea8934a054514..2bac511109cb4a 100644 --- a/clang/tools/driver/cc1as_main.cpp +++ b/clang/tools/driver/cc1as_main.cpp @@ -155,6 +155,9 @@ struct AssemblerInvocation { /// The version of the darwin target variant SDK which was used during the /// compilation llvm::VersionTuple DarwinTargetVariantSDKVersion; + + /// The name of a file to use with \c .secure_log_unique directives. + std::string AsSecureLogFile; /// @} public: @@ -345,6 +348,8 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, .Case("default", EmitDwarfUnwindType::Default); } + Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file); + return Success; } @@ -396,6 +401,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, MCTargetOptions MCOptions; MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind; + MCOptions.AsSecureLogFile = Opts.AsSecureLogFile; std::unique_ptr MAI( TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions)); diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h index c20ce79ee4d080..49cf6262a35f8d 100644 --- a/llvm/include/llvm/MC/MCContext.h +++ b/llvm/include/llvm/MC/MCContext.h @@ -178,7 +178,7 @@ class MCContext { /// The file name of the log file from the environment variable /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique /// directive is used or it is an error. - char *SecureLogFile; + std::string SecureLogFile; /// The stream that gets written to for the .secure_log_unique directive. std::unique_ptr SecureLog; /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to @@ -828,7 +828,7 @@ class MCContext { /// @} - char *getSecureLogFile() { return SecureLogFile; } + StringRef getSecureLogFile() { return SecureLogFile; } raw_fd_ostream *getSecureLog() { return SecureLog.get(); } void setSecureLog(std::unique_ptr Value) { diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h index 640b4aa99462b0..74fc60823a1b9f 100644 --- a/llvm/include/llvm/MC/MCTargetOptions.h +++ b/llvm/include/llvm/MC/MCTargetOptions.h @@ -76,6 +76,7 @@ class MCTargetOptions { std::string ABIName; std::string AssemblyLanguage; std::string SplitDwarfFile; + std::string AsSecureLogFile; const char *Argv0 = nullptr; ArrayRef CommandLineArgs; diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h index d51e740177f770..0213b5440e011d 100644 --- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h +++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h @@ -47,6 +47,8 @@ bool getNoTypeCheck(); std::string getABIName(); +std::string getAsSecureLogFile(); + /// Create this object with static storage to register mc-related command /// line options. struct RegisterMCTargetOptionsFlags { diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index b55ce2736ed2e8..81f13ccb2da93a 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -59,12 +59,6 @@ using namespace llvm; -static cl::opt -AsSecureLogFileName("as-secure-log-file-name", - cl::desc("As secure log file name (initialized from " - "AS_SECURE_LOG_FILE env variable)"), - cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden); - static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &, std::vector &) { SMD.print(nullptr, errs()); @@ -80,7 +74,7 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai, InlineAsmUsedLabelNames(Allocator), CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), AutoReset(DoAutoReset), TargetOptions(TargetOpts) { - SecureLogFile = AsSecureLogFileName; + SecureLogFile = TargetOptions ? TargetOptions->AsSecureLogFile : ""; if (SrcMgr && SrcMgr->getNumBuffers()) MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID()) diff --git a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp index bc59531eecb87b..604f61940930cd 100644 --- a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp +++ b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp @@ -767,8 +767,8 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) { return Error(IDLoc, ".secure_log_unique specified multiple times"); // Get the secure log path. - const char *SecureLogFile = getContext().getSecureLogFile(); - if (!SecureLogFile) + StringRef SecureLogFile = getContext().getSecureLogFile(); + if (SecureLogFile.empty()) return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE " "environment variable unset."); @@ -776,9 +776,8 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) { raw_fd_ostream *OS = getContext().getSecureLog(); if (!OS) { std::error_code EC; - auto NewOS = std::make_unique(StringRef(SecureLogFile), EC, - sys::fs::OF_Append | - sys::fs::OF_TextWithCRLF); + auto NewOS = std::make_unique( + SecureLogFile, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF); if (EC) return Error(IDLoc, Twine("can't open secure log file: ") + SecureLogFile + " (" + EC.message() + ")"); diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp index a310dc894021ff..ee4bf12d8395f9 100644 --- a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp +++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp @@ -45,6 +45,7 @@ MCOPT(bool, NoWarn) MCOPT(bool, NoDeprecatedWarn) MCOPT(bool, NoTypeCheck) MCOPT(std::string, ABIName) +MCOPT(std::string, AsSecureLogFile) llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() { #define MCBINDOPT(NAME) \ @@ -114,6 +115,10 @@ llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() { cl::init("")); MCBINDOPT(ABIName); + static cl::opt AsSecureLogFile( + "as-secure-log-file", cl::desc("As secure log file name"), cl::Hidden); + MCBINDOPT(AsSecureLogFile); + #undef MCBINDOPT } @@ -130,6 +135,7 @@ MCTargetOptions llvm::mc::InitMCTargetOptionsFromFlags() { Options.MCNoDeprecatedWarn = getNoDeprecatedWarn(); Options.MCNoTypeCheck = getNoTypeCheck(); Options.EmitDwarfUnwind = getEmitDwarfUnwind(); + Options.AsSecureLogFile = getAsSecureLogFile(); return Options; } diff --git a/llvm/test/MC/AsmParser/secure_log_unique.s b/llvm/test/MC/AsmParser/secure_log_unique.s index 8145981a31cdc7..36cda7a1773443 100644 --- a/llvm/test/MC/AsmParser/secure_log_unique.s +++ b/llvm/test/MC/AsmParser/secure_log_unique.s @@ -1,6 +1,6 @@ // RUN: rm -f %t -// RUN: env AS_SECURE_LOG_FILE=%t llvm-mc -triple x86_64-apple-darwin %s -// RUN: env AS_SECURE_LOG_FILE=%t llvm-mc -triple x86_64-apple-darwin %s +// RUN: llvm-mc -as-secure-log-file %t -triple x86_64-apple-darwin %s +// RUN: llvm-mc -as-secure-log-file %t -triple x86_64-apple-darwin %s // RUN: FileCheck --input-file=%t %s .secure_log_unique "foobar"