Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用libclang从内存中的C代码生成程序集_C++_Clang_Llvm_Libclang - Fatal编程技术网

C++ 使用libclang从内存中的C代码生成程序集

C++ 使用libclang从内存中的C代码生成程序集,c++,clang,llvm,libclang,C++,Clang,Llvm,Libclang,我需要实现一个库,使用LLVM/Clang作为后端将C代码编译成eBPF字节码。代码将从内存中读取,我也需要在内存中获取生成的汇编代码 到目前为止,我已经能够使用以下代码编译到LLVM IR: #include <string> #include <vector> #include <clang/Frontend/CompilerInstance.h> #include <clang/Basic/DiagnosticOptions.h> #inc

我需要实现一个库,使用LLVM/Clang作为后端将C代码编译成eBPF字节码。代码将从内存中读取,我也需要在内存中获取生成的汇编代码

到目前为止,我已经能够使用以下代码编译到LLVM IR:

#include <string>
#include <vector>

#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Basic/TargetInfo.h>
#include <llvm/Support/TargetSelect.h>

using namespace std;
using namespace clang;
using namespace llvm;

int main() {

    constexpr auto testCodeFileName = "test.cpp";
    constexpr auto testCode = "int test() { return 2+2; }";

    // Prepare compilation arguments
    vector<const char *> args;
    args.push_back(testCodeFileName);

    // Prepare DiagnosticEngine 
    DiagnosticOptions DiagOpts;
    TextDiagnosticPrinter *textDiagPrinter =
            new clang::TextDiagnosticPrinter(errs(),
                                         &DiagOpts);
    IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    DiagnosticsEngine *pDiagnosticsEngine =
            new DiagnosticsEngine(pDiagIDs,
                                         &DiagOpts,
                                         textDiagPrinter);

    // Initialize CompilerInvocation
    CompilerInvocation *CI = new CompilerInvocation();
    CompilerInvocation::CreateFromArgs(*CI, &args[0], &args[0] +     args.size(), *pDiagnosticsEngine);

    // Map code filename to a memoryBuffer
    StringRef testCodeData(testCode);
    unique_ptr<MemoryBuffer> buffer = MemoryBuffer::getMemBufferCopy(testCodeData);
    CI->getPreprocessorOpts().addRemappedFile(testCodeFileName, buffer.get());


    // Create and initialize CompilerInstance
    CompilerInstance Clang;
    Clang.setInvocation(CI);
    Clang.createDiagnostics();

    // Set target (I guess I can initialize only the BPF target, but I don't know how)
    InitializeAllTargets();
    const std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>();
    targetOptions->Triple = string("bpf");
    TargetInfo *pTargetInfo = TargetInfo::CreateTargetInfo(*pDiagnosticsEngine,targetOptions);
    Clang.setTarget(pTargetInfo);

    // Create and execute action
    // CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
    CodeGenAction *compilerAction = new EmitAssemblyAction();
    Clang.ExecuteAction(*compilerAction);

    buffer.release();
}
如果我理解正确,如果我将编译器操作更改为EmitAssemblyAction(),我应该能够生成汇编代码,但我可能没有初始化某些东西,因为我在llvm::TargetPassConfig::AddPasseToHandleExceptions(此=this@entry=0x6d8d30)at/tmp/llvm-3.7.1.src/lib/CodeGen/passs.cpp:419

这一行的代码是:

switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {

有人举过一个例子,或者知道我遗漏了什么吗?

因此,如果您在启用asserts的情况下编译LLVM,错误会更清楚,它实际上会告诉您需要做什么:

x: .../src/llvm/lib/CodeGen/LLVMTargetMachine.cpp:63: 
void llvm::LLVMTargetMachine::initAsmInfo(): 
Assertion `TmpAsmInfo && "MCAsmInfo not initialized. " 
"Make sure you include the correct TargetSelect.h" 
"and that InitializeAllTargetMCs() is being invoked!"' failed.
(我在上面添加了一些换行符,因为它是作为一条长线打印的)

main
开头添加所需的
InitializeAllTargetMCs()
后,我遇到了另一个错误。查看编译器的对象文件生成,我“猜测”这是另一个
InitializeAll*
调用的问题。经过一点测试,您还需要
initializeAlasmPrinters()-考虑到您想要生成汇编代码,这很有意义

我不完全确定如何“查看”代码的结果,但将这两个结果添加到
main
的开头会使代码运行到完成,而不是断言,退出时出现错误或崩溃,这通常是朝着正确方向迈出的一步

这就是“我的”代码中的
main
的样子:


[我使用了最新的3.8预发行版来测试这一点,但我非常怀疑它在这方面与3.7.1有很大不同]

如果有人面临类似的问题,我已经能够从/到内存进行编译,通过stdin发送代码并从stdout获得输出

我不知道是否有其他方法可以实现这一点,可能是使用clang::Driver,但在阅读clang/LLVM源代码时,我发现获取对象所需执行的操作是EmitObjAction(),如果没有从stdin接收到输入,该操作似乎总是生成一个.o文件

因此,我在执行操作之前为管道替换了stdin/stdout,这样就避免了生成文件

#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <cstdio>

#include <unistd.h>
#include <fcntl.h>

#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Basic/TargetInfo.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/IR/Module.h>

using namespace std;
using namespace clang;
using namespace llvm;

int main(int argc, char *argv[])
{ 
    // code to compile for the eBPF virtual machine
    constexpr auto testCode = "int main() { return get_nbs(); }";

    // Send code through a pipe to stdin
    int codeInPipe[2];
    pipe2(codeInPipe, O_NONBLOCK);
    write(codeInPipe[1], (void *) testCode, strlen(testCode));
    close(codeInPipe[1]); // We need to close the pipe to send an EOF
    dup2(codeInPipe[0], STDIN_FILENO);

    // Prepare reception of code through stdout
    int codeOutPipe[2];
    pipe(codeOutPipe);
    dup2(codeOutPipe[1], STDOUT_FILENO);

    // Initialize various LLVM/Clang components
    InitializeAllTargetMCs();   
    InitializeAllAsmPrinters();
    InitializeAllTargets();

    // Prepare compilation arguments
    vector<const char *> args;
    args.push_back("--target=bpf"); // Target is bpf assembly
    args.push_back("-xc"); // Code is in c language
    args.push_back("-"); // Read code from stdin

    CompilerInvocation *CI = createInvocationFromCommandLine(makeArrayRef(args) , NULL);

    // Create CompilerInstance
    CompilerInstance Clang;
    Clang.setInvocation(CI);

    // Initialize CompilerInstace
    Clang.createDiagnostics();

    // Create and execute action
    CodeGenAction *compilerAction; 
    compilerAction = new EmitObjAction();
    Clang.ExecuteAction(*compilerAction);

    // Get compiled object (be carefull with buffer size)
    close(codeInPipe[0]);
    char objBuffer[2048];
    read(codeOutPipe[0], objBuffer, 2048); 

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间铿锵;
使用名称空间llvm;
int main(int argc,char*argv[])
{ 
//要为eBPF虚拟机编译的代码
constexpr auto testCode=“int main(){return get_nbs();}”;
//通过管道将代码发送到stdin
int-codeInPipe[2];
管道2(代码管道,O_非阻塞);
编写(codeInPipe[1],(void*)测试代码,strlen(testCode));
close(codeInPipe[1]);//我们需要关闭管道以发送EOF
dup2(代码管道[0],标准文件号);
//准备通过标准输出接收代码
int代码输出管道[2];
管道(代码输出管道);
dup2(代码输出管道[1],标准输出文件编号);
//初始化各种LLVM/Clang组件
初始化AllTargetMCS();
初始化打印机();
初始化所有目标();
//准备编译参数
向量args;
args.push_back(“--target=bpf”);//目标是bpf程序集
args.push_back(“-xc”);//代码是用c语言编写的
args.push_back(“-”)//从stdin读取代码
CompilerInvocation*CI=createInvocationFromCommandLine(makeArrayRef(args),NULL);
//创建编译器状态
编者的声音;
铿锵.setInvocation(CI);
//初始化编译器状态
Clang.createDiagnostics();
//创建并执行操作
CodeGenAction*编译器操作;
compilerAction=newemitObjAction();
命令执行(*编译器操作);
//获取编译对象(小心缓冲区大小)
关闭(代码管道[0]);
char objBuffer[2048];
read(codeOutPipe[0],objBuffer,2048);
返回0;
}

我想你需要拿出一个可以编译和测试的完整示例…添加了可编译的示例。在修复打字错误后,我得到一个关于
致命错误的错误:无法将文件“test.cpp”重新映射到文件“int test(){return 2+2;}”的内容。
Ops,复制时似乎丢失了一些代码,抱歉。你能再试一次吗?谢谢你,这解决了我的问题。我使用调试符号编译了clang/llvm,但没有断言,启用它们有很大帮助。我将附上我得到的最终代码作为回复,因为我必须更改一些东西以从/到内存进行编译。
int main() {

    constexpr auto testCodeFileName = "test.cpp";
    constexpr auto testCode = "int test() { return 2+2; }";

    InitializeAllTargetMCs();
    InitializeAllAsmPrinters();

    // Prepare compilation arguments
    vector<const char *> args;
    args.push_back(testCodeFileName);

    // Prepare DiagnosticEngine 
    DiagnosticOptions DiagOpts;
    TextDiagnosticPrinter *textDiagPrinter =
            new clang::TextDiagnosticPrinter(errs(),
                                         &DiagOpts);
    IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    DiagnosticsEngine *pDiagnosticsEngine =
            new DiagnosticsEngine(pDiagIDs,
                                         &DiagOpts,
                                         textDiagPrinter);

    // Initialize CompilerInvocation
    CompilerInvocation *CI = new CompilerInvocation();
    CompilerInvocation::CreateFromArgs(*CI, &args[0], &args[0] +     args.size(), *pDiagnosticsEngine);

    // Map code filename to a memoryBuffer
    StringRef testCodeData(testCode);
    unique_ptr<MemoryBuffer> buffer = MemoryBuffer::getMemBufferCopy(testCodeData);
    CI->getPreprocessorOpts().addRemappedFile(testCodeFileName, buffer.get());


    // Create and initialize CompilerInstance
    CompilerInstance Clang;
    Clang.setInvocation(CI);
    Clang.createDiagnostics();

    // Set target (I guess I can initialize only the BPF target, but I don't know how)
    InitializeAllTargets();
    const std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>();
    targetOptions->Triple = string("bpf");
    TargetInfo *pTargetInfo = TargetInfo::CreateTargetInfo(*pDiagnosticsEngine,targetOptions);
    Clang.setTarget(pTargetInfo);

    // Create and execute action
    // CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
    CodeGenAction *compilerAction = new EmitAssemblyAction();
    Clang.ExecuteAction(*compilerAction);

    buffer.release();
}
export CC=clang
export CXX=clang++ 
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local/llvm-debug -DLLVM_TAR
GETS_TO_BUILD=X86 ../llvm
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <cstdio>

#include <unistd.h>
#include <fcntl.h>

#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Basic/TargetInfo.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/IR/Module.h>

using namespace std;
using namespace clang;
using namespace llvm;

int main(int argc, char *argv[])
{ 
    // code to compile for the eBPF virtual machine
    constexpr auto testCode = "int main() { return get_nbs(); }";

    // Send code through a pipe to stdin
    int codeInPipe[2];
    pipe2(codeInPipe, O_NONBLOCK);
    write(codeInPipe[1], (void *) testCode, strlen(testCode));
    close(codeInPipe[1]); // We need to close the pipe to send an EOF
    dup2(codeInPipe[0], STDIN_FILENO);

    // Prepare reception of code through stdout
    int codeOutPipe[2];
    pipe(codeOutPipe);
    dup2(codeOutPipe[1], STDOUT_FILENO);

    // Initialize various LLVM/Clang components
    InitializeAllTargetMCs();   
    InitializeAllAsmPrinters();
    InitializeAllTargets();

    // Prepare compilation arguments
    vector<const char *> args;
    args.push_back("--target=bpf"); // Target is bpf assembly
    args.push_back("-xc"); // Code is in c language
    args.push_back("-"); // Read code from stdin

    CompilerInvocation *CI = createInvocationFromCommandLine(makeArrayRef(args) , NULL);

    // Create CompilerInstance
    CompilerInstance Clang;
    Clang.setInvocation(CI);

    // Initialize CompilerInstace
    Clang.createDiagnostics();

    // Create and execute action
    CodeGenAction *compilerAction; 
    compilerAction = new EmitObjAction();
    Clang.ExecuteAction(*compilerAction);

    // Get compiled object (be carefull with buffer size)
    close(codeInPipe[0]);
    char objBuffer[2048];
    read(codeOutPipe[0], objBuffer, 2048); 

    return 0;
}