Skip to content

Commit 36c1d22

Browse files
committed
Reformat
1 parent 6ebb062 commit 36c1d22

File tree

3 files changed

+236
-173
lines changed

3 files changed

+236
-173
lines changed

Sources/LLVM/ObjectFile.swift

Lines changed: 173 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,204 +1,204 @@
11
#if SWIFT_PACKAGE
2-
import cllvm
2+
import cllvm
33
#endif
44

55
/// An in-memory representation of a format-independent object file.
66
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+
}
4343
}
4444

4545
/// A Section represents one of the binary sections in an object file.
4646
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+
}
8080
}
8181

8282
/// A sequence for iterating over the sections in an object file.
8383
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+
}
106106
}
107107

108108
/// A symbol is a top-level addressable entity in an object file.
109109
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+
}
126126
}
127127

128128
/// A Relocation represents the contents of a relocated symbol in the dynamic
129129
/// linker.
130130
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+
}
150150
}
151151

152152
/// A sequence for iterating over the relocations in an object file.
153153
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+
}
177177
}
178178

179179
/// A sequence for iterating over the symbols in an object file.
180180
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+
}
204204
}

0 commit comments

Comments
 (0)