-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathPatchInfoReader.cpp
397 lines (335 loc) · 12.3 KB
/
PatchInfoReader.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 2019, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// PatchInfo reader.
//
#include <cassert>
#include <cstddef>
#include <map>
#include "PatchInfo.h"
#include "PatchInfoRecord.h"
#include "PatchInfoReader.h"
namespace {
class PatchInfoReader {
const char *Data;
std::size_t Size;
unsigned ShEntries;
const cm::patch::PInfoSectionHdr *Sh;
// All symbol tables are merged into a single one.
std::map<unsigned, cm::patch::Symbol *> SymbolTable;
typedef std::map<unsigned, cm::patch::Binary *> BinarySectionMapTy;
typedef std::map<unsigned, bool> SymbolTableSectionMapTy;
BinarySectionMapTy BinarySectionMap;
SymbolTableSectionMapTy SymbolTableSectionMap;
public:
PatchInfoReader(const char *B, std::size_t S) : Data(B), Size(S), ShEntries(0){Sh = nullptr;}
bool read(cm::patch::Collection &C);
protected:
bool readHeader(cm::patch::Collection &C);
bool readSections(cm::patch::Collection &C);
bool readDummySection(cm::patch::Collection &C, unsigned n);
bool readUnknownSection(cm::patch::Collection &C, unsigned n);
bool readBinarySection(cm::patch::Collection &C, unsigned n);
bool readRelocationSection(cm::patch::Collection &C, unsigned n);
bool readSymbolTableSection(cm::patch::Collection &C, unsigned n);
bool readStringTableSection(cm::patch::Collection &C, unsigned n);
bool readInitRegAccessTableSection(cm::patch::Collection &C, unsigned n);
bool readFiniRegAccessTableSection(cm::patch::Collection &C, unsigned n);
bool readTokenTableSection(cm::patch::Collection &C, unsigned n);
bool readRegisterAccessTableSection(cm::patch::Collection &C, unsigned n,
cm::patch::PInfo_U16 ShType);
bool isValidSection(unsigned n) {
if (n >= ShEntries)
return false;
if (!Sh ||
Sh[n].ShOffset >= Size ||
Sh[n].ShOffset + Sh[n].ShSize > Size)
return false;
return true;
}
bool isValidSectionOfType(unsigned n, cm::patch::PInfo_U16 ShType) {
if (!isValidSection(n))
return false;
return Sh[n].ShType == ShType;
}
const char *getString(unsigned ShIdx, unsigned Idx) {
const char *StringTable = Data + Sh[ShIdx].ShOffset;
return StringTable + Idx;
}
std::pair<BinarySectionMapTy::iterator, bool>
getOrReadBinarySection(cm::patch::Collection &C, unsigned n);
std::pair<SymbolTableSectionMapTy::iterator, bool>
getOrReadSymbolTableSection(cm::patch::Collection &C, unsigned n);
};
} // End anonymous namespace
bool readPatchInfo(const char *Buf, std::size_t Sz, cm::patch::Collection &C) {
PatchInfoReader R(Buf, Sz);
return R.read(C);
}
bool PatchInfoReader::read(cm::patch::Collection &C) {
return readHeader(C) || readSections(C);
}
bool PatchInfoReader::readHeader(cm::patch::Collection &C) {
if (Size < sizeof(cm::patch::PInfoHdr))
return true;
const cm::patch::PInfoHdr *H =
reinterpret_cast<const cm::patch::PInfoHdr *>(Data);
if (!H->checkMagic())
return true;
if (H->Version != cm::patch::PV_0)
return true;
if (!H->isValidPlatform())
return true;
if (H->ShOffset >= Size)
return true;
if (H->ShOffset + H->ShNum * sizeof(cm::patch::PInfoSectionHdr) > Size)
return true;
if (C.getPlatform() != cm::patch::PP_NONE && C.getPlatform() != H->Platform)
return true;
C.setPlatform(H->Platform);
Sh = reinterpret_cast<const cm::patch::PInfoSectionHdr *>(Data + H->ShOffset);
ShEntries = H->ShNum;
return false;
}
std::pair<PatchInfoReader::BinarySectionMapTy::iterator, bool>
PatchInfoReader::getOrReadBinarySection(cm::patch::Collection &C, unsigned n) {
auto BI = BinarySectionMap.end();
if (!readBinarySection(C, n)) {
BI = BinarySectionMap.find(n);
assert(BI != BinarySectionMap.end());
}
return std::make_pair(BI, BI == BinarySectionMap.end());
}
std::pair<PatchInfoReader::SymbolTableSectionMapTy::iterator, bool>
PatchInfoReader::getOrReadSymbolTableSection(cm::patch::Collection &C,
unsigned n) {
auto SI = SymbolTableSectionMap.end();
if (!readSymbolTableSection(C, n)) {
SI = SymbolTableSectionMap.find(n);
assert(SI != SymbolTableSectionMap.end());
}
return std::make_pair(SI, SI == SymbolTableSectionMap.end());
}
bool PatchInfoReader::readSections(cm::patch::Collection &C) {
if (!Sh)
return true;
for (unsigned n = 0; n != ShEntries; ++n) {
switch (Sh[n].ShType) {
case cm::patch::PSHT_NONE:
if (readDummySection(C, n)) return true;
break;
case cm::patch::PSHT_BINARY:
if (readBinarySection(C, n)) return true;
break;
case cm::patch::PSHT_REL:
if (readRelocationSection(C, n)) return true;
break;
case cm::patch::PSHT_SYMTAB:
if (readSymbolTableSection(C, n)) return true;
break;
case cm::patch::PSHT_STRTAB:
if (readStringTableSection(C, n)) return true;
break;
case cm::patch::PSHT_INITREGTAB:
if (readInitRegAccessTableSection(C, n)) return true;
break;
case cm::patch::PSHT_FINIREGTAB:
if (readFiniRegAccessTableSection(C, n)) return true;
break;
case cm::patch::PSHT_TOKTAB:
if (readTokenTableSection(C, n)) return true;
break;
default:
if (readUnknownSection(C, n)) return true;
break;
}
}
return false;
}
bool PatchInfoReader::readDummySection(cm::patch::Collection &C, unsigned n) {
if (!isValidSectionOfType(n, cm::patch::PSHT_NONE)) return true;
return false;
}
bool PatchInfoReader::readBinarySection(cm::patch::Collection &C, unsigned n) {
// Skip if this binary section is ready read.
if (BinarySectionMap.count(n))
return false;
// Bail out if it's not an valid binary section.
if (!isValidSectionOfType(n, cm::patch::PSHT_BINARY))
return true;
const char *Buf = nullptr;
std::size_t Sz = Sh[n].ShSize;
if (Sz)
Buf = Data + Sh[n].ShOffset;
cm::patch::Binary *Bin = C.addBinary(Buf, Sz);
BinarySectionMap.insert(std::make_pair(n, Bin));
return false;
}
bool PatchInfoReader::readRelocationSection(cm::patch::Collection &C,
unsigned n) {
// Skip if this relocation section is already read.
if (!isValidSectionOfType(n, cm::patch::PSHT_REL))
return true;
bool Ret;
BinarySectionMapTy::iterator BI;
std::tie(BI, Ret) = getOrReadBinarySection(C, Sh[n].ShLink2);
if (Ret)
return true;
cm::patch::Binary *Bin = BI->second;
SymbolTableSectionMapTy::iterator SI;
std::tie(SI, Ret) = getOrReadSymbolTableSection(C, Sh[n].ShLink);
if (Ret)
return true;
// Scan through relocations.
std::size_t Sz = Sh[n].ShSize;
const cm::patch::PInfoRelocation *Rel =
reinterpret_cast<const cm::patch::PInfoRelocation *>(Data + Sh[n].ShOffset);
for (unsigned i = 0; Sz > 0; ++i, Sz -= sizeof(cm::patch::PInfoRelocation)) {
auto I = SymbolTable.find(Rel[i].RelSym);
if (I == SymbolTable.end())
return true;
cm::patch::Symbol *S = I->second;
Bin->addReloc(Rel[i].RelAddr, S);
}
return false;
}
bool PatchInfoReader::readSymbolTableSection(cm::patch::Collection &C,
unsigned n) {
// Skip if this section is ready read.
if (SymbolTableSectionMap.count(n))
return false;
// Bail out if it's an invalid section.
if (!isValidSectionOfType(n, cm::patch::PSHT_SYMTAB))
return true;
// Read string table.
unsigned ShIdx = Sh[n].ShLink;
if (readStringTableSection(C, ShIdx))
return true;
// Scan through the symbol table.
const cm::patch::PInfoSymbol *Sym =
reinterpret_cast<const cm::patch::PInfoSymbol *>(Data + Sh[n].ShOffset);
std::size_t Sz = Sh[n].ShSize;
for (unsigned i = 0; Sz > 0; ++i, Sz -= sizeof(cm::patch::PInfoSymbol)) {
// Skip unamed symbol.
unsigned StrIdx = Sym[i].SymName;
if (!StrIdx)
continue;
const char *Name = getString(ShIdx, StrIdx);
cm::patch::Binary *Bin = nullptr;
unsigned Ndx = Sym[i].SymShndx;
if (Ndx) {
// Only support binary section so far.
bool Ret;
BinarySectionMapTy::iterator BI;
std::tie(BI, Ret) = getOrReadBinarySection(C, Ndx);
if (Ret)
return true;
Bin = BI->second;
}
cm::patch::Symbol *S = C.getSymbol(Name);
if (Bin)
while (S && !S->isUnresolved()) {
// In case a symbol has multiple definitions (due to combining a single
// kernel multiple times), rename the conflicting one.
Name = C.getUniqueName(Name);
S = C.getSymbol(Name);
}
S = C.addSymbol(Name);
if (Bin && S->isUnresolved()) {
S->setBinary(Bin);
S->setAddr(Sym[i].SymValue);
S->setExtra(Sym[i].SymExtra);
}
// Assume there's just one symbol table section per patch info.
SymbolTable.emplace(i, S);
}
SymbolTableSectionMap.emplace(n, true);
return false;
}
bool PatchInfoReader::readStringTableSection(cm::patch::Collection &C,
unsigned n) {
if (!isValidSectionOfType(n, cm::patch::PSHT_STRTAB))
return true;
return false;
}
bool
PatchInfoReader::readRegisterAccessTableSection(cm::patch::Collection &C,
unsigned n,
cm::patch::PInfo_U16 ShType) {
if (!isValidSectionOfType(n, ShType))
return true;
BinarySectionMapTy::iterator BI;
bool Ret;
std::tie(BI, Ret) = getOrReadBinarySection(C, Sh[n].ShLink2);
if (Ret)
return true;
cm::patch::Binary *Bin = BI->second;
// Scan through register accesses.
std::size_t Sz = Sh[n].ShSize;
const cm::patch::PInfoRegAccess *Acc =
reinterpret_cast<const cm::patch::PInfoRegAccess *>(Data + Sh[n].ShOffset);
switch (ShType) {
default:
return true;
case cm::patch::PSHT_INITREGTAB:
for (unsigned i = 0; Sz > 0; ++i, Sz -= sizeof(cm::patch::PInfoRegAccess))
Bin->addInitRegAccess(Acc[i].RegAccAddr, Acc[i].RegAccRegNo,
Acc[i].RegAccDUT);
break;
case cm::patch::PSHT_FINIREGTAB:
for (unsigned i = 0; Sz > 0; ++i, Sz -= sizeof(cm::patch::PInfoRegAccess))
Bin->addFiniRegAccess(Acc[i].RegAccAddr, Acc[i].RegAccRegNo,
Acc[i].RegAccDUT);
break;
}
return false;
}
bool PatchInfoReader::readInitRegAccessTableSection(cm::patch::Collection &C,
unsigned n) {
return readRegisterAccessTableSection(C, n, cm::patch::PSHT_INITREGTAB);
}
bool PatchInfoReader::readFiniRegAccessTableSection(cm::patch::Collection &C,
unsigned n) {
return readRegisterAccessTableSection(C, n, cm::patch::PSHT_FINIREGTAB);
}
bool PatchInfoReader::readTokenTableSection(cm::patch::Collection &C,
unsigned n) {
if (!isValidSectionOfType(n, cm::patch::PSHT_TOKTAB))
return true;
BinarySectionMapTy::iterator BI;
bool Ret;
std::tie(BI, Ret) = getOrReadBinarySection(C, Sh[n].ShLink2);
if (Ret)
return true;
cm::patch::Binary *Bin = BI->second;
// Scan through tokens.
std::size_t Sz = Sh[n].ShSize;
const cm::patch::PInfoToken *Tok =
reinterpret_cast<const cm::patch::PInfoToken *>(Data + Sh[n].ShOffset);
for (unsigned i = 0; Sz > 0; ++i, Sz -= sizeof(cm::patch::PInfoToken))
Bin->addToken(Tok[i].TokenNo);
return false;
}
bool PatchInfoReader::readUnknownSection(cm::patch::Collection &C, unsigned n) {
if (!isValidSection(n))
return true;
return false;
}