-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOFFFileTests.swift
More file actions
120 lines (108 loc) · 4.13 KB
/
Copy pathCOFFFileTests.swift
File metadata and controls
120 lines (108 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import XCTest
@testable import PEKit
final class COFFFileTests: XCTestCase {
var coff: COFFFile!
override func setUp() {
let path = ""
let url = URL(fileURLWithPath: path)
coff = try! COFFFile(url: url)
}
}
extension COFFFileTests {
func testHeader() throws {
let header = coff.header
print(
"""
COFFHeader {
Machine: \(header.machine!) (0x\(String(header.machine!.rawValue, radix: 16)))
SectionCount: \(header.numberOfSections)
TimeDateStamp: \(Date(timeIntervalSince1970: header.timeDateStamp)) (0x\(String(header.layout.TimeDateStamp, radix: 16)))
PointerToSymbolTable: 0x\(String(header.symbolTableOffset, radix: 16))
SymbolCount: \(header.numberOfSymbols)
StringTableSize: \(header.stringTableSize(in: coff))
OptionalHeaderSize: \(header.sizeOfOptionalHeader)
Characteristics [ (0x\(String(header.characteristics.rawValue, radix: 16)))
\(header.characteristics.bits.map({ " \($0) (0x\(String($0.rawValue, radix: 16)))" }).joined(separator: "\n"))
]
}
"""
)
}
}
extension COFFFileTests {
func testSections() {
for (i, section) in coff.sections.enumerated() {
print(
"""
Section {
Number: \(i + 1)
Name: \(section.name)
PhysicalAddress: 0x\(String(section.physicalAddress, radix: 16))
VirtualSize: 0x\(String(section.virtualSize, radix: 16))
VirtualAddress: 0x\(String(section.virtualAddress, radix: 16))
RawDataSize: \(section.sizeOfRawData)
PointerToRawData: 0x\(String(section.pointerToRawData, radix: 16))
PointerToRelocations: 0x\(String(section.pointerToRelocations, radix: 16))
PointerToLineNumbers: 0x\(String(section.pointerToLinenumbers, radix: 16))
RelocationCount: \(section.numberOfRelocations)
LineNumberCount: \(section.numberOfLinenumbers)
Characteristics [ (0x\(String(section.characteristics.rawValue, radix: 16)))
\(section.characteristics.bits.map({ " \($0) (0x\(String($0.rawValue, radix: 16)))" }).joined(separator: "\n"))
]
}
"""
)
let relocations = coff.relocations(in: section)
if !relocations.isEmpty {
print(" Relocations [")
for relocation in relocations {
print(
"""
Relocation {
VirtualAddress: 0x\(String(relocation.virtualAddress, radix: 16))
SymbolTableIndex: \(relocation.symbolTableIndex)
Type: 0x\(String(relocation.type, radix: 16))
}
"""
)
}
print(" ]")
}
}
}
}
extension COFFFileTests {
func testSymbolStrings() throws {
if let strings = coff.symbolStrings {
print(
"""
StringTable {
FirstString: \(strings.string(at: 0)?.string ?? "nil")
}
"""
)
}
}
func testSymbols() {
print(
"""
SymbolTable [
"""
)
for (i, symbol) in coff.symbols.enumerated() {
print(
"""
Symbol {
Number: \(i + 1)
Name: \(symbol.name(in: coff) ?? "nil")
Value: 0x\(String(symbol.value, radix: 16))
SectionNumber: \(symbol.sectionNumber)
StorageClass: \(symbol.storageClass?.description ?? "unknown") (0x\(String(symbol.storageClass?.rawValue ?? 0, radix: 16)))
NumberOfAuxSymbols: \(symbol.numberOfAuxSymbols)
}
"""
)
}
print("]")
}
}