Skip to content

Commit f14a9a2

Browse files
committed
Add Section retrieval
1 parent 6d83474 commit f14a9a2

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Sources/LLVM/IRGlobal.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,18 @@ extension IRGlobal {
3131
get { return StorageClass(llvm: LLVMGetDLLStorageClass(asLLVM())) }
3232
set { LLVMSetDLLStorageClass(asLLVM(), newValue.llvm) }
3333
}
34+
35+
/// Retrieves the section associated with the symbol that will eventually be
36+
/// emitted for this global value.
37+
///
38+
/// - Note: Global `Alias` values may or may not be resolvable to any
39+
/// particular section given the state of the IR in an arbitrary module. A
40+
/// return value of the empty string indicates a failed section lookup.
41+
public var section: String {
42+
get {
43+
guard let sname = LLVMGetSection(asLLVM()) else { return "" }
44+
return String(cString: sname)
45+
}
46+
set { LLVMSetSection(asLLVM(), newValue) }
47+
}
3448
}

Sources/LLVM/ObjectFile.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public struct Section {
5656
/// The parent sequence of this section.
5757
private let sectionIterator: LLVMSectionIteratorRef
5858

59-
6059
internal init(fromIterator si: LLVMSectionIteratorRef) {
6160
self.sectionIterator = si
6261
self.name = String(cString: LLVMGetSectionName(si))
@@ -72,6 +71,12 @@ public struct Section {
7271
sectionIterator: self.sectionIterator
7372
)
7473
}
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+
}
7580
}
7681

7782
/// A sequence for iterating over the sections in an object file.
@@ -109,10 +114,14 @@ public struct Symbol {
109114
/// The address of the symbol in the object file.
110115
public let address: Int
111116

117+
/// The parent sequence of this symbol.
118+
fileprivate let symbolIterator: LLVMSymbolIteratorRef
119+
112120
internal init(fromIterator si: LLVMSymbolIteratorRef) {
113121
self.name = String(cString: LLVMGetSymbolName(si))
114122
self.size = Int(LLVMGetSymbolSize(si))
115123
self.address = Int(LLVMGetSymbolAddress(si))
124+
self.symbolIterator = si
116125
}
117126
}
118127

@@ -188,8 +197,6 @@ public class SymbolSequence: Sequence {
188197
return Symbol(fromIterator: self.llvm)
189198
}
190199
}
191-
192-
193200

194201
deinit {
195202
LLVMDisposeSymbolIterator(llvm)

0 commit comments

Comments
 (0)