2
2
import cllvm
3
3
#endif
4
4
5
- /// An in-memory representation of a platform object file.
5
+ /// An in-memory representation of a format-independent object file.
6
6
public class ObjectFile {
7
7
let llvm : LLVMObjectFileRef
8
8
@@ -20,6 +20,7 @@ public class ObjectFile {
20
20
/// the provided path.
21
21
/// - parameter path: The absolute file path on your filesystem.
22
22
public convenience init ? ( path: String ) {
23
+
23
24
guard let memoryBuffer = try ? MemoryBuffer ( contentsOf: path) else {
24
25
return nil
25
26
}
@@ -36,10 +37,9 @@ public class ObjectFile {
36
37
return SymbolSequence ( llvm: LLVMGetSymbols ( llvm) , object: self )
37
38
}
38
39
39
- // FIXME: Re-introduce this when disposal becomes safe.
40
- // deinit {
41
- // LLVMDisposeObjectFile(llvm)
42
- // }
40
+ deinit {
41
+ LLVMDisposeObjectFile ( llvm)
42
+ }
43
43
}
44
44
45
45
/// A Section represents one of the binary sections in an object file.
@@ -53,11 +53,24 @@ public struct Section {
53
53
/// The address of the section in the object file.
54
54
public let address : Int
55
55
56
+ /// The parent sequence of this section.
57
+ private let sectionIterator : LLVMSectionIteratorRef
58
+
59
+
56
60
internal init ( fromIterator si: LLVMSectionIteratorRef ) {
57
- name = String ( cString: LLVMGetSectionName ( si) )
58
- size = Int ( LLVMGetSectionSize ( si) )
59
- contents = String ( cString: LLVMGetSectionContents ( si) )
60
- address = Int ( LLVMGetSectionAddress ( si) )
61
+ self . sectionIterator = si
62
+ self . name = String ( cString: LLVMGetSectionName ( si) )
63
+ self . size = Int ( LLVMGetSectionSize ( si) )
64
+ self . contents = String ( cString: LLVMGetSectionContents ( si) )
65
+ self . address = Int ( LLVMGetSectionAddress ( si) )
66
+ }
67
+
68
+ /// Returns a sequence of all the relocations in this object file.
69
+ public var relocations : RelocationSequence {
70
+ return RelocationSequence (
71
+ llvm: LLVMGetRelocations ( self . sectionIterator) ,
72
+ sectionIterator: self . sectionIterator
73
+ )
61
74
}
62
75
}
63
76
@@ -82,10 +95,9 @@ public class SectionSequence: Sequence {
82
95
}
83
96
}
84
97
85
- // FIXME: Re-introduce this when disposal becomes safe.
86
- // deinit {
87
- // LLVMDisposeSectionIterator(llvm)
88
- // }
98
+ deinit {
99
+ LLVMDisposeSectionIterator ( llvm)
100
+ }
89
101
}
90
102
91
103
/// A symbol is a top-level addressable entity in an object file.
@@ -107,9 +119,17 @@ public struct Symbol {
107
119
/// A Relocation represents the contents of a relocated symbol in the dynamic
108
120
/// linker.
109
121
public struct Relocation {
122
+ /// Retrieves the type of this relocation.
123
+ ///
124
+ /// The value of this integer is dependent upon the type of object file
125
+ /// it was retrieved from.
110
126
public let type : Int
127
+ /// The offset the relocated symbol resides at.
111
128
public let offset : Int
129
+ /// The symbol that is the subject of the relocation.
112
130
public let symbol : Symbol
131
+ /// Get a string that represents the type of this relocation for display
132
+ /// purposes.
113
133
public let typeName : String
114
134
115
135
internal init ( fromIterator ri: LLVMRelocationIteratorRef ) {
@@ -142,10 +162,9 @@ public class RelocationSequence: Sequence {
142
162
}
143
163
}
144
164
145
- // FIXME: Re-introduce this when disposal becomes safe.
146
- // deinit {
147
- // LLVMDisposeSectionIterator(llvm)
148
- // }
165
+ deinit {
166
+ LLVMDisposeSectionIterator ( llvm)
167
+ }
149
168
}
150
169
151
170
/// A sequence for iterating over the symbols in an object file.
@@ -169,9 +188,10 @@ public class SymbolSequence: Sequence {
169
188
return Symbol ( fromIterator: self . llvm)
170
189
}
171
190
}
191
+
172
192
173
- // FIXME: Re-introduce this when disposal becomes safe.
174
- // deinit {
175
- // LLVMDisposeSymbolIterator(llvm)
176
- // }
193
+
194
+ deinit {
195
+ LLVMDisposeSymbolIterator ( llvm)
196
+ }
177
197
}
0 commit comments