From 551b1774524428aae5692ed3d41f969288ecd5a2 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Fri, 3 Dec 2021 15:48:36 -0500 Subject: [OpenMP] Add a flag for embedding a file into the module This patch adds support for a flag `-fembed-offload-binary` to embed a file as an ELF section in the output by placing it in a global variable. This can be used to bundle offloading files with the host binary so it can be accessed by the linker. The section is named using the `-fembed-offload-section` option. Depends on D116541 Reviewed By: JonChesterfield Differential Revision: https://reviews.llvm.org/D116542 --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'llvm/lib/Bitcode/Writer/BitcodeWriter.cpp') diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index eb4e09e..fafccb4d 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -69,6 +69,7 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" #include #include #include @@ -4973,3 +4974,19 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); NewUsed->setSection("llvm.metadata"); } + +void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, + StringRef SectionName) { + ArrayRef ModuleData = + ArrayRef(Buf.getBufferStart(), Buf.getBufferSize()); + + // Embed the buffer into the module. + llvm::Constant *ModuleConstant = + llvm::ConstantDataArray::get(M.getContext(), ModuleData); + llvm::GlobalVariable *GV = new llvm::GlobalVariable( + M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, + ModuleConstant, "llvm.embedded.object"); + GV->setSection(SectionName); + + appendToCompilerUsed(M, GV); +} -- cgit v1.1