Index: clang/include/clang/Basic/CodeGenOptions.h =================================================================== --- clang/include/clang/Basic/CodeGenOptions.h +++ clang/include/clang/Basic/CodeGenOptions.h @@ -432,6 +432,9 @@ /// 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) Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -5345,6 +5345,9 @@ 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] Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -497,6 +497,7 @@ 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; Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -2737,6 +2737,11 @@ 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, Index: clang/test/CodeGen/as-secure-log-file.c =================================================================== --- /dev/null +++ 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\""); +} Index: clang/test/Driver/AS_SECURE_LOG_FILE.s =================================================================== --- /dev/null +++ clang/test/Driver/AS_SECURE_LOG_FILE.s @@ -0,0 +1,3 @@ +// RUN: env AS_SECURE_LOG_FILE=%t %clang -target x86_64-apple-darwin -c %s -o %t.o -### 2>&1 | FileCheck %s -DLOG_FILE=%t +// CHECK: "-cc1as" +// CHECK-SAME: "-as-secure-log-file" "[[LOG_FILE]]" Index: clang/test/Misc/cc1as-as-secure-log-file.s =================================================================== --- /dev/null +++ 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" Index: clang/tools/driver/cc1as_main.cpp =================================================================== --- clang/tools/driver/cc1as_main.cpp +++ clang/tools/driver/cc1as_main.cpp @@ -155,6 +155,9 @@ /// 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 @@ .Case("default", EmitDwarfUnwindType::Default); } + Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file); + return Success; } @@ -396,6 +401,7 @@ MCTargetOptions MCOptions; MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind; + MCOptions.AsSecureLogFile = Opts.AsSecureLogFile; std::unique_ptr MAI( TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions)); Index: llvm/include/llvm/MC/MCContext.h =================================================================== --- llvm/include/llvm/MC/MCContext.h +++ llvm/include/llvm/MC/MCContext.h @@ -178,7 +178,7 @@ /// 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 @@ /// @} - char *getSecureLogFile() { return SecureLogFile; } + StringRef getSecureLogFile() { return SecureLogFile; } raw_fd_ostream *getSecureLog() { return SecureLog.get(); } void setSecureLog(std::unique_ptr Value) { Index: llvm/include/llvm/MC/MCTargetOptions.h =================================================================== --- llvm/include/llvm/MC/MCTargetOptions.h +++ llvm/include/llvm/MC/MCTargetOptions.h @@ -76,6 +76,7 @@ std::string ABIName; std::string AssemblyLanguage; std::string SplitDwarfFile; + std::string AsSecureLogFile; const char *Argv0 = nullptr; ArrayRef CommandLineArgs; Index: llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h =================================================================== --- llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h +++ llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h @@ -47,6 +47,8 @@ std::string getABIName(); +std::string getAsSecureLogFile(); + /// Create this object with static storage to register mc-related command /// line options. struct RegisterMCTargetOptionsFlags { Index: llvm/lib/MC/MCContext.cpp =================================================================== --- llvm/lib/MC/MCContext.cpp +++ 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 @@ 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()) Index: llvm/lib/MC/MCParser/DarwinAsmParser.cpp =================================================================== --- llvm/lib/MC/MCParser/DarwinAsmParser.cpp +++ llvm/lib/MC/MCParser/DarwinAsmParser.cpp @@ -767,8 +767,8 @@ 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 @@ 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() + ")"); Index: llvm/lib/MC/MCTargetOptionsCommandFlags.cpp =================================================================== --- llvm/lib/MC/MCTargetOptionsCommandFlags.cpp +++ llvm/lib/MC/MCTargetOptionsCommandFlags.cpp @@ -45,6 +45,7 @@ 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 @@ 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 @@ Options.MCNoDeprecatedWarn = getNoDeprecatedWarn(); Options.MCNoTypeCheck = getNoTypeCheck(); Options.EmitDwarfUnwind = getEmitDwarfUnwind(); + Options.AsSecureLogFile = getAsSecureLogFile(); return Options; } Index: llvm/test/MC/AsmParser/secure_log_unique.s =================================================================== --- llvm/test/MC/AsmParser/secure_log_unique.s +++ 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"