Skip to content

Commit 7296482

Browse files
committed
rustllvm: Add bindings to the LLVM linker
1 parent c47a075 commit 7296482

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Diff for: src/comp/lib/llvm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,9 @@ native mod llvm = llvm_lib {
848848
call. */
849849
fn LLVMRustGetLastError() -> sbuf;
850850

851-
851+
/** Links LLVM modules together. `Src` is destroyed by this call and
852+
must never be referenced again. */
853+
fn LLVMLinkModules(ModuleRef Dest, ModuleRef Src) -> Bool;
852854
}
853855

854856
native mod rustllvm = llvm_lib {

Diff for: src/rustllvm/RustWrapper.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "llvm/Linker.h"
1516
#include "llvm/PassManager.h"
1617
#include "llvm/ADT/Triple.h"
1718
#include "llvm/Support/FormattedStream.h"
@@ -25,12 +26,13 @@
2526

2627
using namespace llvm;
2728

28-
static char *LLVMRustError;
29+
static const char *LLVMRustError;
2930

3031
extern "C" LLVMMemoryBufferRef
3132
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
3233
LLVMMemoryBufferRef MemBuf = NULL;
33-
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf, &LLVMRustError);
34+
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf,
35+
const_cast<char **>(&LLVMRustError));
3436
return MemBuf;
3537
}
3638

@@ -49,6 +51,20 @@ enum LLVMCodeGenFileType {
4951
LLVMNullFile // Do not emit any output.
5052
};
5153

54+
extern "C" bool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src) {
55+
static std::string err;
56+
57+
// For some strange reason, unwrap() doesn't work here. "No matching
58+
// function" error.
59+
Module *DM = reinterpret_cast<Module *>(Dest);
60+
Module *SM = reinterpret_cast<Module *>(Src);
61+
if (Linker::LinkModules(DM, SM, &err)) {
62+
LLVMRustError = err.c_str();
63+
return false;
64+
}
65+
return true;
66+
}
67+
5268
extern "C" void LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
5369
LLVMModuleRef M,
5470
const char *triple,

Diff for: src/rustllvm/rustllvm.def.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
LLVMRustCreateMemoryBufferWithContentsOfFile
22
LLVMRustWriteOutputFile
33
LLVMRustGetLastError
4+
LLVMLinkModules
45
LLVMCreateObjectFile
56
LLVMDisposeObjectFile
67
LLVMGetSections

0 commit comments

Comments
 (0)