-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVpuShaderBinary.cpp
More file actions
270 lines (211 loc) · 5.82 KB
/
VpuShaderBinary.cpp
File metadata and controls
270 lines (211 loc) · 5.82 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "VpuShaderBinary.h"
#include <Windows.h>
#ifdef _M_IX86
#define TLS_SYMBOL "_g_tls"
#define MAIN_SYMBOL "_main"
#else
#define TLS_SYMBOL "g_tls"
#define MAIN_SYMBOL "main"
#endif
using namespace llvm;
using namespace object;
bool VpuShaderBinary::FindExport(const char * name, uint32_t & value)
{
#if 1
//#ifdef _M_IX86
for(uint32_t i = 0; i < m_coff->getNumberOfSymbols(); i++)
{
Expected<COFFSymbolRef> symbolOrErr = m_coff->getSymbol(i);
if (!symbolOrErr)
return false;
auto symbol = *symbolOrErr;
StringRef testName;
if (m_coff->getSymbolName(symbol, testName))
return false;
if (testName == name) {
value = symbol.getValue();
return true;
}
i += symbol.getNumberOfAuxSymbols();
}
#else
for (const SymbolRef &Symbol : m_obj->symbols()) {
Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
if (!TypeOrError)
return false;
SymbolRef::Type Type = *TypeOrError;
if (Type == SymbolRef::ST_Debug)
continue;
Expected<StringRef> NameOrErr = Symbol.getName();
if (!NameOrErr)
return false;
StringRef testName;
testName = *NameOrErr;
if (testName == name) {
Expected<uint64_t> AddressOrError = Symbol.getAddress();
if (!AddressOrError)
return false;
uint64_t Address = *AddressOrError;
value = (uint32_t)Address;
return true;
}
}
#endif
return false;
}
bool VpuShaderBinary::LoadCode(uint8_t * base, uint32_t size)
{
StringRef contents;
if (m_code.getContents(contents))
return false;
if (contents.size() > m_codeSize)
return false;
memset(base, 0, m_codeSize);
memcpy(base, contents.data(), contents.size());
for (auto r : m_relocations)
if (!ApplyRelocation(base, r))
return false;
return true;
}
bool VpuShaderBinary::Load(uint8_t * base, uint32_t size)
{
return LoadCode(base, size);
}
bool VpuShaderBinary::ApplyRelocation(uint8_t * base, VpuRelocation & relocation)
{
uint32_t * loc = (uint32_t *)(base + relocation.m_fixupOffset);
if (relocation.m_type == IMAGE_REL_I386_REL32 || relocation.m_type == IMAGE_REL_AMD64_REL32)
*loc = relocation.m_referenceOffset - relocation.m_fixupOffset - 4;
#ifdef _M_IX86
else if (relocation.m_type == IMAGE_REL_I386_DIR32)
*loc = relocation.m_referenceOffset + (uint32_t)base;
#endif
else
return false;
return true;
}
bool VpuShaderBinary::BuildRelocations(void)
{
StringRef tlsName = StringRef(TLS_SYMBOL);
//#ifdef _M_IX86
#if 1
for (auto relocation : m_code.relocations()) {
#else
for (auto relocation : m_reloc.relocations()) {
#endif
uint32_t relocationOffset = (uint32_t)relocation.getOffset();
uint32_t relocationType = relocation.getType();
auto symbol = relocation.getSymbol();
Expected<SymbolRef::Type> typeOrErr = symbol->getType();
if (!typeOrErr) return false;
SymbolRef::Type type = *typeOrErr;
Expected<StringRef> nameOrErr = symbol->getName();
if (!nameOrErr) return false;
StringRef name = *nameOrErr;
#if 0
auto sectionOrErr = symbol->getSection();
if (!sectionOrErr) return false;
auto section = *sectionOrErr;
#endif
uint32_t symbolOffset = symbol->getValue();
if (type == SymbolRef::Type::ST_Unknown) {
if (name.contains(".")) {
std::string newName(name);
std::replace(newName.begin(), newName.end(), '.', '_');
if (!FindExport(newName.c_str(), symbolOffset))
return false;
}
else
return false;
}
else if (type == SymbolRef::Type::ST_Data) {
if (name == tlsName)
symbolOffset = m_tlsOffset;
else
return false;
}
else if (type == SymbolRef::Type::ST_Function) {
; // do nothing
}
else
return false;
//#ifdef _M_IX86
#if 1
if (relocationType != IMAGE_REL_I386_REL32 &&
relocationType != IMAGE_REL_AMD64_REL32 &&
relocationType != IMAGE_REL_I386_DIR32)
return false;
VpuRelocation relocation;
relocation.m_type = relocationType;
relocation.m_fixupOffset = relocationOffset;
relocation.m_referenceOffset = symbolOffset;
m_relocations.push_back(relocation);
#else
if (relocationType == ELF::R_X86_64_PLT32) {
uint32_t * loc = (uint32_t *)(base + relocationOffset);
*loc = symbolOffset - relocationOffset - 4;
}
else if (relocationType == ELF::R_X86_64_64) {
uint64_t * loc = (uint64_t *)(base + relocationOffset);
*loc = symbolOffset + (uint64_t)base;
}
else if (relocationType == ELF::R_X86_64_32S) {
uint32_t * loc = (uint64_t *)(base + relocationOffset);
*loc = symbolOffset + (uint64_t)base;
}
#endif
}
return true;
}
bool VpuShaderBinary::Open(const char * path)
{
StringRef file(path);
Expected<OwningBinary<Binary>> binaryOrErr = createBinary(file);
if (!binaryOrErr)
return false;
m_binary = std::move(*binaryOrErr);
Binary * binary = m_binary.getBinary();
m_obj = dyn_cast<ObjectFile>(binary);
if (m_obj == nullptr)
return false;
//#ifdef _M_IX86
#if 1
m_coff = dyn_cast<const COFFObjectFile>(m_obj);
if (m_coff == nullptr)
return false;
#endif
if (!FindExport(TLS_SYMBOL, m_tlsSize))
return false;
if (!FindExport(MAIN_SYMBOL, m_mainOffset))
return false;
for (const SectionRef §ion : m_obj->sections())
{
StringRef name;
if (section.getName(name))
return false;
if (name == ".text") {
m_code = section;
m_codeSize = AlignSectionSize(section.getSize());
}
#if 0
// #ifndef _M_IX86
if (name == ".rela.text") {
m_reloc = section;
}
#endif
}
if (m_code == SectionRef())
return false;
#if 0
//#ifndef _M_IX86
if (m_reloc == SectionRef())
return false;
#endif
m_tlsOffset = m_codeSize;
m_dataSize = AlignSectionSize(m_tlsSize);
m_imageSize = m_codeSize + m_dataSize;
if (!BuildRelocations())
return false;
m_loaded = true;
return true;
}