|
1 | 1 | #if SWIFT_PACKAGE |
2 | | -import cllvm |
| 2 | + import cllvm |
3 | 3 | #endif |
4 | 4 |
|
5 | 5 | /// An in-memory representation of a format-independent object file. |
6 | 6 | public class ObjectFile { |
7 | | - let llvm: LLVMObjectFileRef |
8 | | - |
9 | | - /// Creates an `ObjectFile` with the contents of a provided memory buffer. |
10 | | - /// - parameter memoryBuffer: A memory buffer containing a valid binary |
11 | | - /// object file. |
12 | | - public init?(memoryBuffer: MemoryBuffer) { |
13 | | - guard let file = LLVMCreateObjectFile(memoryBuffer.llvm) else { |
14 | | - return nil |
15 | | - } |
16 | | - self.llvm = file |
17 | | - } |
18 | | - |
19 | | - /// Creates an `ObjectFile` with the contents of the object file at |
20 | | - /// the provided path. |
21 | | - /// - parameter path: The absolute file path on your filesystem. |
22 | | - public convenience init?(path: String) { |
23 | | - |
24 | | - guard let memoryBuffer = try? MemoryBuffer(contentsOf: path) else { |
25 | | - return nil |
26 | | - } |
27 | | - self.init(memoryBuffer: memoryBuffer) |
28 | | - } |
29 | | - |
30 | | - /// Returns a sequence of all the sections in this object file. |
31 | | - public var sections: SectionSequence { |
32 | | - return SectionSequence(llvm: LLVMGetSections(llvm), object: self) |
33 | | - } |
34 | | - |
35 | | - /// Returns a sequence of all the symbols in this object file. |
36 | | - public var symbols: SymbolSequence { |
37 | | - return SymbolSequence(llvm: LLVMGetSymbols(llvm), object: self) |
38 | | - } |
39 | | - |
40 | | - deinit { |
41 | | - LLVMDisposeObjectFile(llvm) |
42 | | - } |
| 7 | + let llvm: LLVMObjectFileRef |
| 8 | + |
| 9 | + /// Creates an `ObjectFile` with the contents of a provided memory buffer. |
| 10 | + /// - parameter memoryBuffer: A memory buffer containing a valid binary |
| 11 | + /// object file. |
| 12 | + public init?(memoryBuffer: MemoryBuffer) { |
| 13 | + guard let file = LLVMCreateObjectFile(memoryBuffer.llvm) else { |
| 14 | + return nil |
| 15 | + } |
| 16 | + self.llvm = file |
| 17 | + } |
| 18 | + |
| 19 | + /// Creates an `ObjectFile` with the contents of the object file at |
| 20 | + /// the provided path. |
| 21 | + /// - parameter path: The absolute file path on your filesystem. |
| 22 | + public convenience init?(path: String) { |
| 23 | + |
| 24 | + guard let memoryBuffer = try? MemoryBuffer(contentsOf: path) else { |
| 25 | + return nil |
| 26 | + } |
| 27 | + self.init(memoryBuffer: memoryBuffer) |
| 28 | + } |
| 29 | + |
| 30 | + /// Returns a sequence of all the sections in this object file. |
| 31 | + public var sections: SectionSequence { |
| 32 | + return SectionSequence(llvm: LLVMGetSections(llvm), object: self) |
| 33 | + } |
| 34 | + |
| 35 | + /// Returns a sequence of all the symbols in this object file. |
| 36 | + public var symbols: SymbolSequence { |
| 37 | + return SymbolSequence(llvm: LLVMGetSymbols(llvm), object: self) |
| 38 | + } |
| 39 | + |
| 40 | + deinit { |
| 41 | + LLVMDisposeObjectFile(llvm) |
| 42 | + } |
43 | 43 | } |
44 | 44 |
|
45 | 45 | /// A Section represents one of the binary sections in an object file. |
46 | 46 | public struct Section { |
47 | | - /// The section's declared name. |
48 | | - public let name: String |
49 | | - /// The size of the contents of the section. |
50 | | - public let size: Int |
51 | | - /// The raw contents of the section. |
52 | | - public let contents: String |
53 | | - /// The address of the section in the object file. |
54 | | - public let address: Int |
55 | | - |
56 | | - /// The parent sequence of this section. |
57 | | - private let sectionIterator: LLVMSectionIteratorRef |
58 | | - |
59 | | - internal init(fromIterator si: LLVMSectionIteratorRef) { |
60 | | - self.sectionIterator = si |
61 | | - self.name = String(cString: LLVMGetSectionName(si)) |
62 | | - self.size = Int(LLVMGetSectionSize(si)) |
63 | | - self.contents = String(cString: LLVMGetSectionContents(si)) |
64 | | - self.address = Int(LLVMGetSectionAddress(si)) |
65 | | - } |
66 | | - |
67 | | - /// Returns a sequence of all the relocations in this object file. |
68 | | - public var relocations: RelocationSequence { |
69 | | - return RelocationSequence( |
70 | | - llvm: LLVMGetRelocations(self.sectionIterator), |
71 | | - sectionIterator: self.sectionIterator |
72 | | - ) |
73 | | - } |
74 | | - |
75 | | - /// Returns whether a symbol matching the given `Symbol` can be found in |
76 | | - /// this section. |
77 | | - public func contains(symbol: Symbol) -> Bool { |
78 | | - return LLVMGetSectionContainsSymbol(self.sectionIterator, symbol.symbolIterator) != 0 |
79 | | - } |
| 47 | + /// The section's declared name. |
| 48 | + public let name: String |
| 49 | + /// The size of the contents of the section. |
| 50 | + public let size: Int |
| 51 | + /// The raw contents of the section. |
| 52 | + public let contents: String |
| 53 | + /// The address of the section in the object file. |
| 54 | + public let address: Int |
| 55 | + |
| 56 | + /// The parent sequence of this section. |
| 57 | + private let sectionIterator: LLVMSectionIteratorRef |
| 58 | + |
| 59 | + internal init(fromIterator si: LLVMSectionIteratorRef) { |
| 60 | + self.sectionIterator = si |
| 61 | + self.name = String(cString: LLVMGetSectionName(si)) |
| 62 | + self.size = Int(LLVMGetSectionSize(si)) |
| 63 | + self.contents = String(cString: LLVMGetSectionContents(si)) |
| 64 | + self.address = Int(LLVMGetSectionAddress(si)) |
| 65 | + } |
| 66 | + |
| 67 | + /// Returns a sequence of all the relocations in this object file. |
| 68 | + public var relocations: RelocationSequence { |
| 69 | + return RelocationSequence( |
| 70 | + llvm: LLVMGetRelocations(self.sectionIterator), |
| 71 | + sectionIterator: self.sectionIterator |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns whether a symbol matching the given `Symbol` can be found in |
| 76 | + /// this section. |
| 77 | + public func contains(symbol: Symbol) -> Bool { |
| 78 | + return LLVMGetSectionContainsSymbol(self.sectionIterator, symbol.symbolIterator) != 0 |
| 79 | + } |
80 | 80 | } |
81 | 81 |
|
82 | 82 | /// A sequence for iterating over the sections in an object file. |
83 | 83 | public class SectionSequence: Sequence { |
84 | | - let llvm: LLVMSectionIteratorRef |
85 | | - let objectFile: ObjectFile |
86 | | - |
87 | | - init(llvm: LLVMSectionIteratorRef, object: ObjectFile) { |
88 | | - self.llvm = llvm |
89 | | - self.objectFile = object |
90 | | - } |
91 | | - |
92 | | - /// Makes an iterator that iterates over the sections in an object file. |
93 | | - public func makeIterator() -> AnyIterator<Section> { |
94 | | - return AnyIterator { |
95 | | - if LLVMIsSectionIteratorAtEnd(self.objectFile.llvm, self.llvm) != 0 { |
96 | | - return nil |
97 | | - } |
98 | | - defer { LLVMMoveToNextSection(self.llvm) } |
99 | | - return Section(fromIterator: self.llvm) |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - deinit { |
104 | | - LLVMDisposeSectionIterator(llvm) |
105 | | - } |
| 84 | + let llvm: LLVMSectionIteratorRef |
| 85 | + let objectFile: ObjectFile |
| 86 | + |
| 87 | + init(llvm: LLVMSectionIteratorRef, object: ObjectFile) { |
| 88 | + self.llvm = llvm |
| 89 | + self.objectFile = object |
| 90 | + } |
| 91 | + |
| 92 | + /// Makes an iterator that iterates over the sections in an object file. |
| 93 | + public func makeIterator() -> AnyIterator<Section> { |
| 94 | + return AnyIterator { |
| 95 | + if LLVMIsSectionIteratorAtEnd(self.objectFile.llvm, self.llvm) != 0 { |
| 96 | + return nil |
| 97 | + } |
| 98 | + defer { LLVMMoveToNextSection(self.llvm) } |
| 99 | + return Section(fromIterator: self.llvm) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + deinit { |
| 104 | + LLVMDisposeSectionIterator(llvm) |
| 105 | + } |
106 | 106 | } |
107 | 107 |
|
108 | 108 | /// A symbol is a top-level addressable entity in an object file. |
109 | 109 | public struct Symbol { |
110 | | - /// The symbol name. |
111 | | - public let name: String |
112 | | - /// The size of the data in the symbol. |
113 | | - public let size: Int |
114 | | - /// The address of the symbol in the object file. |
115 | | - public let address: Int |
116 | | - |
117 | | - /// The parent sequence of this symbol. |
118 | | - fileprivate let symbolIterator: LLVMSymbolIteratorRef |
119 | | - |
120 | | - internal init(fromIterator si: LLVMSymbolIteratorRef) { |
121 | | - self.name = String(cString: LLVMGetSymbolName(si)) |
122 | | - self.size = Int(LLVMGetSymbolSize(si)) |
123 | | - self.address = Int(LLVMGetSymbolAddress(si)) |
124 | | - self.symbolIterator = si |
125 | | - } |
| 110 | + /// The symbol name. |
| 111 | + public let name: String |
| 112 | + /// The size of the data in the symbol. |
| 113 | + public let size: Int |
| 114 | + /// The address of the symbol in the object file. |
| 115 | + public let address: Int |
| 116 | + |
| 117 | + /// The parent sequence of this symbol. |
| 118 | + fileprivate let symbolIterator: LLVMSymbolIteratorRef |
| 119 | + |
| 120 | + internal init(fromIterator si: LLVMSymbolIteratorRef) { |
| 121 | + self.name = String(cString: LLVMGetSymbolName(si)) |
| 122 | + self.size = Int(LLVMGetSymbolSize(si)) |
| 123 | + self.address = Int(LLVMGetSymbolAddress(si)) |
| 124 | + self.symbolIterator = si |
| 125 | + } |
126 | 126 | } |
127 | 127 |
|
128 | 128 | /// A Relocation represents the contents of a relocated symbol in the dynamic |
129 | 129 | /// linker. |
130 | 130 | public struct Relocation { |
131 | | - /// Retrieves the type of this relocation. |
132 | | - /// |
133 | | - /// The value of this integer is dependent upon the type of object file |
134 | | - /// it was retrieved from. |
135 | | - public let type: Int |
136 | | - /// The offset the relocated symbol resides at. |
137 | | - public let offset: Int |
138 | | - /// The symbol that is the subject of the relocation. |
139 | | - public let symbol: Symbol |
140 | | - /// Get a string that represents the type of this relocation for display |
141 | | - /// purposes. |
142 | | - public let typeName: String |
143 | | - |
144 | | - internal init(fromIterator ri: LLVMRelocationIteratorRef) { |
145 | | - self.type = Int(LLVMGetRelocationType(ri)) |
146 | | - self.offset = Int(LLVMGetRelocationOffset(ri)) |
147 | | - self.symbol = Symbol(fromIterator: LLVMGetRelocationSymbol(ri)) |
148 | | - self.typeName = String(cString: LLVMGetRelocationTypeName(ri)) |
149 | | - } |
| 131 | + /// Retrieves the type of this relocation. |
| 132 | + /// |
| 133 | + /// The value of this integer is dependent upon the type of object file |
| 134 | + /// it was retrieved from. |
| 135 | + public let type: Int |
| 136 | + /// The offset the relocated symbol resides at. |
| 137 | + public let offset: Int |
| 138 | + /// The symbol that is the subject of the relocation. |
| 139 | + public let symbol: Symbol |
| 140 | + /// Get a string that represents the type of this relocation for display |
| 141 | + /// purposes. |
| 142 | + public let typeName: String |
| 143 | + |
| 144 | + internal init(fromIterator ri: LLVMRelocationIteratorRef) { |
| 145 | + self.type = Int(LLVMGetRelocationType(ri)) |
| 146 | + self.offset = Int(LLVMGetRelocationOffset(ri)) |
| 147 | + self.symbol = Symbol(fromIterator: LLVMGetRelocationSymbol(ri)) |
| 148 | + self.typeName = String(cString: LLVMGetRelocationTypeName(ri)) |
| 149 | + } |
150 | 150 | } |
151 | 151 |
|
152 | 152 | /// A sequence for iterating over the relocations in an object file. |
153 | 153 | public class RelocationSequence: Sequence { |
154 | | - let llvm: LLVMRelocationIteratorRef |
155 | | - let sectionIterator: LLVMSectionIteratorRef |
156 | | - |
157 | | - init(llvm: LLVMRelocationIteratorRef, sectionIterator: LLVMSectionIteratorRef) { |
158 | | - self.llvm = llvm |
159 | | - self.sectionIterator = sectionIterator |
160 | | - } |
161 | | - |
162 | | - /// Creates an iterator that will iterate over all relocations in an object |
163 | | - /// file. |
164 | | - public func makeIterator() -> AnyIterator<Relocation> { |
165 | | - return AnyIterator { |
166 | | - if LLVMIsRelocationIteratorAtEnd(self.sectionIterator, self.llvm) != 0 { |
167 | | - return nil |
168 | | - } |
169 | | - defer { LLVMMoveToNextRelocation(self.llvm) } |
170 | | - return Relocation(fromIterator: self.llvm) |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - deinit { |
175 | | - LLVMDisposeSectionIterator(llvm) |
176 | | - } |
| 154 | + let llvm: LLVMRelocationIteratorRef |
| 155 | + let sectionIterator: LLVMSectionIteratorRef |
| 156 | + |
| 157 | + init(llvm: LLVMRelocationIteratorRef, sectionIterator: LLVMSectionIteratorRef) { |
| 158 | + self.llvm = llvm |
| 159 | + self.sectionIterator = sectionIterator |
| 160 | + } |
| 161 | + |
| 162 | + /// Creates an iterator that will iterate over all relocations in an object |
| 163 | + /// file. |
| 164 | + public func makeIterator() -> AnyIterator<Relocation> { |
| 165 | + return AnyIterator { |
| 166 | + if LLVMIsRelocationIteratorAtEnd(self.sectionIterator, self.llvm) != 0 { |
| 167 | + return nil |
| 168 | + } |
| 169 | + defer { LLVMMoveToNextRelocation(self.llvm) } |
| 170 | + return Relocation(fromIterator: self.llvm) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + deinit { |
| 175 | + LLVMDisposeSectionIterator(llvm) |
| 176 | + } |
177 | 177 | } |
178 | 178 |
|
179 | 179 | /// A sequence for iterating over the symbols in an object file. |
180 | 180 | public class SymbolSequence: Sequence { |
181 | | - let llvm: LLVMSymbolIteratorRef |
182 | | - let object: ObjectFile |
183 | | - |
184 | | - init(llvm: LLVMSymbolIteratorRef, object: ObjectFile) { |
185 | | - self.llvm = llvm |
186 | | - self.object = object |
187 | | - } |
188 | | - |
189 | | - /// Creates an iterator that will iterate over all symbols in an object |
190 | | - /// file. |
191 | | - public func makeIterator() -> AnyIterator<Symbol> { |
192 | | - return AnyIterator { |
193 | | - if LLVMIsSymbolIteratorAtEnd(self.object.llvm, self.llvm) != 0 { |
194 | | - return nil |
195 | | - } |
196 | | - defer { LLVMMoveToNextSymbol(self.llvm) } |
197 | | - return Symbol(fromIterator: self.llvm) |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - deinit { |
202 | | - LLVMDisposeSymbolIterator(llvm) |
203 | | - } |
| 181 | + let llvm: LLVMSymbolIteratorRef |
| 182 | + let object: ObjectFile |
| 183 | + |
| 184 | + init(llvm: LLVMSymbolIteratorRef, object: ObjectFile) { |
| 185 | + self.llvm = llvm |
| 186 | + self.object = object |
| 187 | + } |
| 188 | + |
| 189 | + /// Creates an iterator that will iterate over all symbols in an object |
| 190 | + /// file. |
| 191 | + public func makeIterator() -> AnyIterator<Symbol> { |
| 192 | + return AnyIterator { |
| 193 | + if LLVMIsSymbolIteratorAtEnd(self.object.llvm, self.llvm) != 0 { |
| 194 | + return nil |
| 195 | + } |
| 196 | + defer { LLVMMoveToNextSymbol(self.llvm) } |
| 197 | + return Symbol(fromIterator: self.llvm) |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + deinit { |
| 202 | + LLVMDisposeSymbolIterator(llvm) |
| 203 | + } |
204 | 204 | } |
0 commit comments