-
Notifications
You must be signed in to change notification settings - Fork 5
/
SymbolReader.ahk
executable file
·147 lines (112 loc) · 4.45 KB
/
SymbolReader.ahk
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class SymbolReaderBase {
__New(pFileData, Length) {
this.pData := pFileData
this.Size := Length
}
ReadString(Offset, Length := -1) {
if (Length > 0) {
return StrGet(this.pData + Offset, Length, "UTF-8")
}
else {
return StrGet(this.pData + Offset, "UTF-8")
}
}
__Call(MethodName, Params*) {
if (RegexMatch(MethodName, "O)Read(\w+)", Read) && Read[1] != "String") {
return NumGet(this.pData + 0, Params[1], Read[1])
}
}
}
class PESymbolReader extends SymbolReaderBase {
ReadSectionHeader(HeaderOffset) {
Result := {}
Result.Name := this.ReadString(HeaderOffset, 8)
Result.VirtualSize := this.ReadUInt(HeaderOffset + 8)
Result.VirtualAddress := this.ReadUInt(HeaderOffset + 12)
Result.FileSize := this.ReadUInt(HeaderOffset + 16)
Result.FileOffset := this.ReadUInt(HeaderOffset + 20)
Result.Characteristics := this.ReadUInt(HeaderOffset + 36)
return Result
}
ReadSymbolHeader(SymbolNamesOffset, HeaderOffset) {
Result := {}
if (this.ReadUInt(HeaderOffset) != 0) {
Result.Name := this.ReadString(HeaderOffset, 8)
}
else {
Result.Name := this.ReadString(SymbolNamesOffset + this.ReadUInt(HeaderOffset + 4))
}
Result.Value := this.ReadUInt(HeaderOffset + 8)
Result.SectionIndex := this.ReadUShort(HeaderOffset + 12)
Result.Type := this.ReadUShort(HeaderOffset + 14)
Result.StorageClass := this.ReadUChar(HeaderOffset + 16)
Result.AuxSymbolCount := this.ReadUChar(HeaderOffset + 17)
return Result
}
Read() {
static SIZEOF_COFF_HEADER := 20
static SIZEOF_SECTION_HEADER := 40
static SIZEOF_SYMBOL := 18
if (this.ReadUShort(0) != 0x8664) {
throw Exception("Not a valid 64 bit PE object file")
}
SectionHeaderCount := this.ReadUShort(2)
SizeOfOptionalHeader := this.ReadUShort(16)
SectionHeaderTableOffset := SIZEOF_COFF_HEADER + SizeOfOptionalHeader
Sections := []
SectionsByName := {}
loop, % SectionHeaderCount {
Sections.Push(NextSection := this.ReadSectionHeader(SectionHeaderTableOffset + ((A_Index - 1) * SIZEOF_SECTION_HEADER)))
SectionsByName[NextSection.Name] := NextSection
}
SymbolTableOffset := this.ReadUInt(8)
SymbolCount := this.ReadUInt(12)
SymbolNamesOffset := SymbolTableOffset + (SymbolCount * SIZEOF_SYMBOL)
Symbols := []
SymbolsByName := {}
SymbolIndex := 0
while (SymbolIndex < SymbolCount) {
Symbols.Push(NextSymbol := this.ReadSymbolHeader(SymbolNamesOffset, SymbolTableOffset + (SymbolIndex * SIZEOF_SYMBOL)))
SymbolsByName[NextSymbol.Name] := NextSymbol
SymbolIndex += 1 + NextSymbol.AuxSymbolCount
}
TextSection := SectionsByName[".text"]
RelocationSecton := SectionsByName[".reloc"]
Relocations := []
if (RelocationSecton && SymbolsByName[".text_offset"]) {
TextOffset := SymbolsByName[".text_offset"].Value
ImageBase := TextSection.VirtualAddress - TextOffset
RelocationsOffset := 0
while (RelocationsOffset < RelocationSecton.FileSize) {
RelocationPage := this.ReadUInt(RelocationSecton.FileOffset + RelocationsOffset)
RelocationsOffset += 4
PageRelocationCount := (this.ReadUInt(RelocationSecton.FileOffset + RelocationsOffset) - 8) / 2
RelocationsOffset += 4
loop, % PageRelocationCount {
Relocation := this.ReadUShort(RelocationSecton.FileOffset + RelocationsOffset)
RelocationsOffset += 2
RelocationType := (Relocation >> 12) & 0xF
RelocationAddress := (RelocationPage + (Relocation & 0xFFF))
RelocationOffset := RelocationAddress - TextSection.VirtualAddress
if (RelocationOffset < 0 || RelocationOffset > TextSection.FileSize) {
continue
}
else if (RelocationType = 0) {
continue
}
else if (RelocationType != 10) {
throw Exception("Unknown relocation type '" RelocationType "'")
}
Relocations.Push(RelocationOffset)
; This relocation currently has an RVA in place of an actual address, which makes it easy to relocate in an actual
; image. But since this code is loaded as if `.text` goes at RVA 0, we need to translate the RVA into a `.text`
; based offset.
OldValue := this.ReadPtr(TextSection.FileOffset + RelocationOffset)
OldValue -= TextOffset
NumPut(OldValue, this.pData + 0, TextSection.FileOffset + RelocationOffset, "Ptr")
}
}
}
return {"AbsoluteSymbols": SymbolsByName, "Symbols": Symbols, "Sections": Sections, "SectionsByName": SectionsByName, "Relocations": Relocations}
}
}