Skip to content

Commit c8db90c

Browse files
committed
Give Sections a Relocation iterator
1 parent a202616 commit c8db90c

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

Sources/LLVM/ObjectFile.swift

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import cllvm
33
#endif
44

5-
/// An in-memory representation of a platform object file.
5+
/// An in-memory representation of a format-independent object file.
66
public class ObjectFile {
77
let llvm: LLVMObjectFileRef
88

@@ -20,6 +20,7 @@ public class ObjectFile {
2020
/// the provided path.
2121
/// - parameter path: The absolute file path on your filesystem.
2222
public convenience init?(path: String) {
23+
2324
guard let memoryBuffer = try? MemoryBuffer(contentsOf: path) else {
2425
return nil
2526
}
@@ -36,10 +37,9 @@ public class ObjectFile {
3637
return SymbolSequence(llvm: LLVMGetSymbols(llvm), object: self)
3738
}
3839

39-
// FIXME: Re-introduce this when disposal becomes safe.
40-
// deinit {
41-
// LLVMDisposeObjectFile(llvm)
42-
// }
40+
deinit {
41+
LLVMDisposeObjectFile(llvm)
42+
}
4343
}
4444

4545
/// A Section represents one of the binary sections in an object file.
@@ -53,11 +53,24 @@ public struct Section {
5353
/// The address of the section in the object file.
5454
public let address: Int
5555

56+
/// The parent sequence of this section.
57+
private let sectionIterator: LLVMSectionIteratorRef
58+
59+
5660
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+
)
6174
}
6275
}
6376

@@ -82,10 +95,9 @@ public class SectionSequence: Sequence {
8295
}
8396
}
8497

85-
// FIXME: Re-introduce this when disposal becomes safe.
86-
// deinit {
87-
// LLVMDisposeSectionIterator(llvm)
88-
// }
98+
deinit {
99+
LLVMDisposeSectionIterator(llvm)
100+
}
89101
}
90102

91103
/// A symbol is a top-level addressable entity in an object file.
@@ -107,9 +119,17 @@ public struct Symbol {
107119
/// A Relocation represents the contents of a relocated symbol in the dynamic
108120
/// linker.
109121
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.
110126
public let type: Int
127+
/// The offset the relocated symbol resides at.
111128
public let offset: Int
129+
/// The symbol that is the subject of the relocation.
112130
public let symbol: Symbol
131+
/// Get a string that represents the type of this relocation for display
132+
/// purposes.
113133
public let typeName: String
114134

115135
internal init(fromIterator ri: LLVMRelocationIteratorRef) {
@@ -142,10 +162,9 @@ public class RelocationSequence: Sequence {
142162
}
143163
}
144164

145-
// FIXME: Re-introduce this when disposal becomes safe.
146-
// deinit {
147-
// LLVMDisposeSectionIterator(llvm)
148-
// }
165+
deinit {
166+
LLVMDisposeSectionIterator(llvm)
167+
}
149168
}
150169

151170
/// A sequence for iterating over the symbols in an object file.
@@ -169,9 +188,10 @@ public class SymbolSequence: Sequence {
169188
return Symbol(fromIterator: self.llvm)
170189
}
171190
}
191+
172192

173-
// FIXME: Re-introduce this when disposal becomes safe.
174-
// deinit {
175-
// LLVMDisposeSymbolIterator(llvm)
176-
// }
193+
194+
deinit {
195+
LLVMDisposeSymbolIterator(llvm)
196+
}
177197
}

0 commit comments

Comments
 (0)