From 28fb69e00a4d3f3e59ccd63cf5c56c4af66a475c Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Mon, 12 Jul 2021 03:37:06 +0000 Subject: [PATCH] [AIX] Emit version string in .file directive AIX .file directive support including compiler version string. https://www.ibm.com/docs/en/aix/7.2?topic=ops-file-pseudo-op This patch adds the support so that it will be easier to identify build compiler in objects. Reviewed By: #powerpc, shchenz Differential Revision: https://reviews.llvm.org/D105743 --- llvm/include/llvm/MC/MCAsmInfo.h | 5 ++++ llvm/include/llvm/MC/MCObjectStreamer.h | 2 ++ llvm/include/llvm/MC/MCStreamer.h | 4 ++++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 20 +++++++++++++--- llvm/lib/MC/MCAsmInfoXCOFF.cpp | 1 + llvm/lib/MC/MCAsmStreamer.cpp | 24 +++++++++++++++++++ llvm/lib/MC/MCObjectStreamer.cpp | 8 +++++++ llvm/lib/MC/MCStreamer.cpp | 3 +++ .../PowerPC/aix-filename-absolute-path.ll | 1 + .../PowerPC/aix-filename-relative-path.ll | 1 + ...name-special-character-double-quotation.ll | 1 + ...name-special-character-single-quotation.ll | 1 + 12 files changed, 68 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h index b1d6b7fb7fd3..355f569861d8 100644 --- a/llvm/include/llvm/MC/MCAsmInfo.h +++ b/llvm/include/llvm/MC/MCAsmInfo.h @@ -390,6 +390,10 @@ protected: /// for ELF targets. Defaults to true. bool HasSingleParameterDotFile = true; + /// True if the target has a four strings .file directive, strings seperated + /// by comma. Defaults to false. + bool HasFourStringsDotFile = false; + /// True if the target has a .ident directive, this is true for ELF targets. /// Defaults to false. bool HasIdentDirective = false; @@ -729,6 +733,7 @@ public: bool hasFunctionAlignment() const { return HasFunctionAlignment; } bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; } bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; } + bool hasFourStringsDotFile() const { return HasFourStringsDotFile; } bool hasIdentDirective() const { return HasIdentDirective; } bool hasNoDeadStrip() const { return HasNoDeadStrip; } bool hasAltEntry() const { return HasAltEntry; } diff --git a/llvm/include/llvm/MC/MCObjectStreamer.h b/llvm/include/llvm/MC/MCObjectStreamer.h index a0169c4c8228..dcdee2b5774b 100644 --- a/llvm/include/llvm/MC/MCObjectStreamer.h +++ b/llvm/include/llvm/MC/MCObjectStreamer.h @@ -184,6 +184,8 @@ public: void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc) override; void emitFileDirective(StringRef Filename) override; + void emitFileDirective(StringRef Filename, StringRef CompilerVerion, + StringRef TimeStamp, StringRef Description) override; void emitAddrsig() override; void emitAddrsigSym(const MCSymbol *Sym) override; diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h index d6ed2111ce8b..fd326ff18712 100644 --- a/llvm/include/llvm/MC/MCStreamer.h +++ b/llvm/include/llvm/MC/MCStreamer.h @@ -854,6 +854,10 @@ public: /// "foo.c"' assembler directive. virtual void emitFileDirective(StringRef Filename); + /// Emit ".file assembler diretive with additioal info. + virtual void emitFileDirective(StringRef Filename, StringRef CompilerVerion, + StringRef TimeStamp, StringRef Description); + /// Emit the "identifiers" directive. This implements the /// '.ident "version foo"' assembler directive. virtual void emitIdent(StringRef IdentString) {} diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index a1507a349d43..90797c6d571a 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -59,6 +59,7 @@ #include "llvm/CodeGen/TargetLowering.h" #include "llvm/CodeGen/TargetOpcodes.h" #include "llvm/CodeGen/TargetRegisterInfo.h" +#include "llvm/Config/config.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Comdat.h" #include "llvm/IR/Constant.h" @@ -297,11 +298,24 @@ bool AsmPrinter::doInitialization(Module &M) { // don't, this at least helps the user find where a global came from. if (MAI->hasSingleParameterDotFile()) { // .file "foo.c" + + SmallString<128> FileName; if (MAI->hasBasenameOnlyForFileDirective()) - OutStreamer->emitFileDirective( - llvm::sys::path::filename(M.getSourceFileName())); + FileName = llvm::sys::path::filename(M.getSourceFileName()); else - OutStreamer->emitFileDirective(M.getSourceFileName()); + FileName = M.getSourceFileName(); + if (MAI->hasFourStringsDotFile()) { +#ifdef PACKAGE_VENDOR + const char VerStr[] = + PACKAGE_VENDOR " " PACKAGE_NAME " version " PACKAGE_VERSION; +#else + const char VerStr[] = PACKAGE_NAME " version " PACKAGE_VERSION; +#endif + // TODO: Add timestamp and description. + OutStreamer->emitFileDirective(FileName, VerStr, "", ""); + } else { + OutStreamer->emitFileDirective(FileName); + } } GCModuleInfo *MI = getAnalysisIfAvailable(); diff --git a/llvm/lib/MC/MCAsmInfoXCOFF.cpp b/llvm/lib/MC/MCAsmInfoXCOFF.cpp index 0006754acb86..ae7afeb30099 100644 --- a/llvm/lib/MC/MCAsmInfoXCOFF.cpp +++ b/llvm/lib/MC/MCAsmInfoXCOFF.cpp @@ -22,6 +22,7 @@ MCAsmInfoXCOFF::MCAsmInfoXCOFF() { IsLittleEndian = false; HasVisibilityOnlyWithLinkage = true; HasBasenameOnlyForFileDirective = false; + HasFourStringsDotFile = true; // For XCOFF, string constant consists of any number of characters enclosed in // "" (double quotation marks). diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index f6d877c519f8..72f4ee3f33be 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -253,6 +253,8 @@ public: SMLoc Loc) override; void emitFileDirective(StringRef Filename) override; + void emitFileDirective(StringRef Filename, StringRef CompilerVerion, + StringRef TimeStamp, StringRef Description) override; Expected tryEmitDwarfFileDirective(unsigned FileNo, StringRef Directory, StringRef Filename, @@ -1450,6 +1452,28 @@ void MCAsmStreamer::emitFileDirective(StringRef Filename) { EmitEOL(); } +void MCAsmStreamer::emitFileDirective(StringRef Filename, + StringRef CompilerVerion, + StringRef TimeStamp, + StringRef Description) { + assert(MAI->hasFourStringsDotFile()); + OS << "\t.file\t"; + PrintQuotedString(Filename, OS); + OS << ","; + if (!CompilerVerion.empty()) { + PrintQuotedString(CompilerVerion, OS); + } + if (!TimeStamp.empty()) { + OS << ","; + PrintQuotedString(TimeStamp, OS); + } + if (!Description.empty()) { + OS << ","; + PrintQuotedString(Description, OS); + } + EmitEOL(); +} + void MCAsmStreamer::printDwarfFileDirective( unsigned FileNo, StringRef Directory, StringRef Filename, Optional Checksum, Optional Source, diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 4dae2f41fc45..2865a2ad80a9 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -848,6 +848,14 @@ void MCObjectStreamer::emitFileDirective(StringRef Filename) { getAssembler().addFileName(Filename); } +void MCObjectStreamer::emitFileDirective(StringRef Filename, + StringRef CompilerVerion, + StringRef TimeStamp, + StringRef Description) { + getAssembler().addFileName(Filename); + // TODO: add additional info to integrated assembler. +} + void MCObjectStreamer::emitAddrsig() { getAssembler().getWriter().emitAddrsigSection(); } diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp index 2a1998fd9c4c..fc7fb555f0b9 100644 --- a/llvm/lib/MC/MCStreamer.cpp +++ b/llvm/lib/MC/MCStreamer.cpp @@ -1147,6 +1147,9 @@ void MCStreamer::EndCOFFSymbolDef() { llvm_unreachable("this directive only supported on COFF targets"); } void MCStreamer::emitFileDirective(StringRef Filename) {} +void MCStreamer::emitFileDirective(StringRef Filename, StringRef CompilerVerion, + StringRef TimeStamp, StringRef Description) { +} void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { llvm_unreachable("this directive only supported on COFF targets"); } diff --git a/llvm/test/CodeGen/PowerPC/aix-filename-absolute-path.ll b/llvm/test/CodeGen/PowerPC/aix-filename-absolute-path.ll index d5b6886ebcef..be9f70d27e5c 100644 --- a/llvm/test/CodeGen/PowerPC/aix-filename-absolute-path.ll +++ b/llvm/test/CodeGen/PowerPC/aix-filename-absolute-path.ll @@ -4,5 +4,6 @@ ; RUN: | FileCheck %s ; CHECK: .file "/absolute/path/to/file" +; CHECK-SAME: ,{{.*version}} source_filename = "/absolute/path/to/file" diff --git a/llvm/test/CodeGen/PowerPC/aix-filename-relative-path.ll b/llvm/test/CodeGen/PowerPC/aix-filename-relative-path.ll index 6df85c84d267..f4cb6d3d7963 100644 --- a/llvm/test/CodeGen/PowerPC/aix-filename-relative-path.ll +++ b/llvm/test/CodeGen/PowerPC/aix-filename-relative-path.ll @@ -4,5 +4,6 @@ ; RUN: | FileCheck %s ; CHECK: .file "../relative/path/to/file" +; CHECK-SAME: ,{{.*version}} source_filename = "../relative/path/to/file" diff --git a/llvm/test/CodeGen/PowerPC/aix-filename-special-character-double-quotation.ll b/llvm/test/CodeGen/PowerPC/aix-filename-special-character-double-quotation.ll index 05ef566bcf09..9735110024f5 100644 --- a/llvm/test/CodeGen/PowerPC/aix-filename-special-character-double-quotation.ll +++ b/llvm/test/CodeGen/PowerPC/aix-filename-special-character-double-quotation.ll @@ -4,5 +4,6 @@ ; RUN: | FileCheck %s ; CHECK: .file "1""2.c" +; CHECK-SAME: ,{{.*version}} source_filename = "1\222.c" diff --git a/llvm/test/CodeGen/PowerPC/aix-filename-special-character-single-quotation.ll b/llvm/test/CodeGen/PowerPC/aix-filename-special-character-single-quotation.ll index a600494e151b..6f75a3c455d8 100644 --- a/llvm/test/CodeGen/PowerPC/aix-filename-special-character-single-quotation.ll +++ b/llvm/test/CodeGen/PowerPC/aix-filename-special-character-single-quotation.ll @@ -4,5 +4,6 @@ ; RUN: | FileCheck %s ; CHECK: .file "1'2.c" +; CHECK-SAME: ,{{.*version}} source_filename = "1'2.c"