Skip to content

Commit 310c254

Browse files
committed
Store the backing buffer in Binary
1 parent ff16d03 commit 310c254

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Sources/LLVM/ObjectFile.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public enum BinaryFileError: Error {
1515
public class BinaryFile {
1616
let llvm: LLVMBinaryRef
1717

18+
/// The backing buffer for this binary file.
19+
public let buffer: MemoryBuffer
20+
1821
/// The kind of this binary file.
1922
public let kind: Kind
2023

@@ -77,9 +80,10 @@ public class BinaryFile {
7780
}
7881
}
7982

80-
init(llvm: LLVMBinaryRef) {
83+
init(llvm: LLVMBinaryRef, buffer: MemoryBuffer) {
8184
self.llvm = llvm
8285
self.kind = Kind(llvm: LLVMBinaryGetType(llvm))
86+
self.buffer = buffer
8387
}
8488

8589
/// Creates a Binary File with the contents of a provided memory buffer.
@@ -96,6 +100,7 @@ public class BinaryFile {
96100
throw BinaryFileError.couldNotCreate(String(cString: error))
97101
}
98102
self.kind = Kind(llvm: LLVMBinaryGetType(self.llvm))
103+
self.buffer = memoryBuffer
99104
}
100105

101106
/// Creates an `ObjectFile` with the contents of the object file at
@@ -117,8 +122,8 @@ public class BinaryFile {
117122

118123
/// An in-memory representation of a format-independent object file.
119124
public final class ObjectFile: BinaryFile {
120-
override init(llvm: LLVMBinaryRef) {
121-
super.init(llvm: llvm)
125+
override init(llvm: LLVMBinaryRef, buffer: MemoryBuffer) {
126+
super.init(llvm: llvm, buffer: buffer)
122127
precondition(self.kind != .machOUniversalBinary,
123128
"File format is not an object file; use MachOUniversalBinaryFile instead")
124129
}
@@ -175,7 +180,8 @@ public final class MachOUniversalBinaryFile: BinaryFile {
175180
defer { LLVMDisposeMessage(error) }
176181
throw BinaryFileError.couldNotCreate(String(cString: error))
177182
}
178-
return ObjectFile(llvm: archFile)
183+
let buffer = MemoryBuffer(llvm: LLVMBinaryGetMemoryBuffer(archFile))
184+
return ObjectFile(llvm: archFile, buffer: buffer)
179185
}
180186
}
181187

Sources/llvmshims/include/shim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct LLVMOpaqueBinary *LLVMBinaryRef;
5858

5959
LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
6060
LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf, LLVMContextRef Context, char **ErrorMessage);
61+
LLVMMemoryBufferRef LLVMBinaryGetMemoryBuffer(LLVMBinaryRef BR);
6162
void LLVMDisposeBinary(LLVMBinaryRef BR);
6263

6364
LLVMBinaryRef LLVMUniversalBinaryCopyObjectForArchitecture(LLVMBinaryRef BR, const char *Arch, size_t ArchLen, char **ErrorMessage);

Sources/llvmshims/src/shim.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern "C" {
6161

6262
LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
6363
LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf, LLVMContextRef Context, char **ErrorMessage);
64+
LLVMMemoryBufferRef LLVMBinaryGetMemoryBuffer(LLVMBinaryRef BR);
6465
void LLVMDisposeBinary(LLVMBinaryRef BR);
6566

6667
LLVMBinaryRef LLVMUniversalBinaryCopyObjectForArchitecture(LLVMBinaryRef BR, const char *Arch, size_t ArchLen, char **ErrorMessage);
@@ -156,6 +157,13 @@ LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf, LLVMContextRef Contex
156157
return wrap(ObjOrErr.get().release());
157158
}
158159

160+
LLVMMemoryBufferRef LLVMBinaryGetMemoryBuffer(LLVMBinaryRef BR) {
161+
auto Buf = unwrap(BR)->getMemoryBufferRef();
162+
return wrap(llvm::MemoryBuffer::getMemBuffer(
163+
Buf.getBuffer(), Buf.getBufferIdentifier(),
164+
/*RequiresNullTerminator*/false).release());
165+
}
166+
159167
void LLVMDisposeBinary(LLVMBinaryRef BR) {
160168
delete unwrap(BR);
161169
}

0 commit comments

Comments
 (0)