@@ -15,6 +15,9 @@ public enum BinaryFileError: Error {
1515public 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.
119124public 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
0 commit comments