10
10
#include " obj2yaml.h"
11
11
#include " llvm/Object/COFF.h"
12
12
13
+ using namespace llvm ;
13
14
14
15
template <typename One, typename Two>
15
16
struct pod_pair { // I'd much rather use std::pair, but it's not a POD
16
17
One first;
17
18
Two second;
18
19
};
19
20
20
- #define STRING_PAIR (x ) {llvm:: COFF::x, #x}
21
- static const pod_pair<llvm:: COFF::MachineTypes, const char *>
21
+ #define STRING_PAIR (x ) {COFF::x, #x}
22
+ static const pod_pair<COFF::MachineTypes, const char *>
22
23
MachineTypePairs [] = {
23
24
STRING_PAIR (IMAGE_FILE_MACHINE_UNKNOWN),
24
25
STRING_PAIR (IMAGE_FILE_MACHINE_AM33),
@@ -43,7 +44,7 @@ MachineTypePairs [] = {
43
44
STRING_PAIR (IMAGE_FILE_MACHINE_WCEMIPSV2)
44
45
};
45
46
46
- static const pod_pair<llvm:: COFF::SectionCharacteristics, const char *>
47
+ static const pod_pair<COFF::SectionCharacteristics, const char *>
47
48
SectionCharacteristicsPairs1 [] = {
48
49
STRING_PAIR (IMAGE_SCN_TYPE_NO_PAD),
49
50
STRING_PAIR (IMAGE_SCN_CNT_CODE),
@@ -60,7 +61,7 @@ SectionCharacteristicsPairs1 [] = {
60
61
STRING_PAIR (IMAGE_SCN_MEM_PRELOAD)
61
62
};
62
63
63
- static const pod_pair<llvm:: COFF::SectionCharacteristics, const char *>
64
+ static const pod_pair<COFF::SectionCharacteristics, const char *>
64
65
SectionCharacteristicsPairsAlignment [] = {
65
66
STRING_PAIR (IMAGE_SCN_ALIGN_1BYTES),
66
67
STRING_PAIR (IMAGE_SCN_ALIGN_2BYTES),
@@ -78,7 +79,7 @@ SectionCharacteristicsPairsAlignment [] = {
78
79
STRING_PAIR (IMAGE_SCN_ALIGN_8192BYTES)
79
80
};
80
81
81
- static const pod_pair<llvm:: COFF::SectionCharacteristics, const char *>
82
+ static const pod_pair<COFF::SectionCharacteristics, const char *>
82
83
SectionCharacteristicsPairs2 [] = {
83
84
STRING_PAIR (IMAGE_SCN_LNK_NRELOC_OVFL),
84
85
STRING_PAIR (IMAGE_SCN_MEM_DISCARDABLE),
@@ -89,8 +90,8 @@ SectionCharacteristicsPairs2 [] = {
89
90
STRING_PAIR (IMAGE_SCN_MEM_READ),
90
91
STRING_PAIR (IMAGE_SCN_MEM_WRITE)
91
92
};
92
-
93
- static const pod_pair<llvm:: COFF::SymbolBaseType, const char *>
93
+
94
+ static const pod_pair<COFF::SymbolBaseType, const char *>
94
95
SymbolBaseTypePairs [] = {
95
96
STRING_PAIR (IMAGE_SYM_TYPE_NULL),
96
97
STRING_PAIR (IMAGE_SYM_TYPE_VOID),
@@ -110,15 +111,15 @@ SymbolBaseTypePairs [] = {
110
111
STRING_PAIR (IMAGE_SYM_TYPE_DWORD)
111
112
};
112
113
113
- static const pod_pair<llvm:: COFF::SymbolComplexType, const char *>
114
+ static const pod_pair<COFF::SymbolComplexType, const char *>
114
115
SymbolComplexTypePairs [] = {
115
116
STRING_PAIR (IMAGE_SYM_DTYPE_NULL),
116
117
STRING_PAIR (IMAGE_SYM_DTYPE_POINTER),
117
118
STRING_PAIR (IMAGE_SYM_DTYPE_FUNCTION),
118
119
STRING_PAIR (IMAGE_SYM_DTYPE_ARRAY),
119
120
};
120
-
121
- static const pod_pair<llvm:: COFF::SymbolStorageClass, const char *>
121
+
122
+ static const pod_pair<COFF::SymbolStorageClass, const char *>
122
123
SymbolStorageClassPairs [] = {
123
124
STRING_PAIR (IMAGE_SYM_CLASS_END_OF_FUNCTION),
124
125
STRING_PAIR (IMAGE_SYM_CLASS_NULL),
@@ -149,7 +150,7 @@ SymbolStorageClassPairs [] = {
149
150
STRING_PAIR (IMAGE_SYM_CLASS_CLR_TOKEN),
150
151
};
151
152
152
- static const pod_pair<llvm:: COFF::RelocationTypeX86, const char *>
153
+ static const pod_pair<COFF::RelocationTypeX86, const char *>
153
154
RelocationTypeX86Pairs [] = {
154
155
STRING_PAIR (IMAGE_REL_I386_ABSOLUTE),
155
156
STRING_PAIR (IMAGE_REL_I386_DIR16),
@@ -181,7 +182,7 @@ RelocationTypeX86Pairs [] = {
181
182
STRING_PAIR (IMAGE_REL_AMD64_SSPAN32)
182
183
};
183
184
184
- static const pod_pair<llvm:: COFF::RelocationTypesARM, const char *>
185
+ static const pod_pair<COFF::RelocationTypesARM, const char *>
185
186
RelocationTypesARMPairs [] = {
186
187
STRING_PAIR (IMAGE_REL_ARM_ABSOLUTE),
187
188
STRING_PAIR (IMAGE_REL_ARM_ADDR32),
@@ -203,8 +204,8 @@ RelocationTypesARMPairs [] = {
203
204
204
205
namespace yaml { // COFF-specific yaml-writing specific routines
205
206
206
- static llvm:: raw_ostream &writeName (llvm:: raw_ostream &Out,
207
- const char *Name, std::size_t NameSize) {
207
+ static raw_ostream &writeName (raw_ostream &Out,
208
+ const char *Name, std::size_t NameSize) {
208
209
for (std::size_t i = 0 ; i < NameSize; ++i) {
209
210
if (!Name[i]) break ;
210
211
Out << Name[i];
@@ -214,8 +215,9 @@ static llvm::raw_ostream &writeName(llvm::raw_ostream &Out,
214
215
215
216
// Given an array of pod_pair<enum, const char *>, write all enums that match
216
217
template <typename T, std::size_t N>
217
- static llvm::raw_ostream &writeBitMask (llvm::raw_ostream &Out,
218
- const pod_pair<T, const char *> (&Arr)[N], unsigned long Val) {
218
+ static raw_ostream &writeBitMask (raw_ostream &Out,
219
+ const pod_pair<T, const char *> (&Arr)[N],
220
+ unsigned long Val) {
219
221
for (std::size_t i = 0 ; i < N; ++i)
220
222
if (Val & Arr[i].first )
221
223
Out << Arr[i].second << " , " ;
@@ -226,8 +228,8 @@ static llvm::raw_ostream &writeBitMask(llvm::raw_ostream &Out,
226
228
227
229
// Given an array of pod_pair<enum, const char *>, look up a value
228
230
template <typename T, std::size_t N>
229
- const char *nameLookup (const pod_pair<T, const char *> (&Arr)[N],
230
- unsigned long Val, const char *NotFound = NULL) {
231
+ const char *nameLookup (const pod_pair<T, const char *> (&Arr)[N],
232
+ unsigned long Val, const char *NotFound = NULL) {
231
233
T n = static_cast <T>(Val);
232
234
for (std::size_t i = 0 ; i < N; ++i)
233
235
if (n == Arr[i].first )
@@ -236,8 +238,8 @@ const char *nameLookup(const pod_pair<T, const char *> (&Arr)[N],
236
238
}
237
239
238
240
239
- static llvm:: raw_ostream &yamlCOFFHeader (
240
- const llvm::object::coff_file_header *Header,llvm:: raw_ostream &Out) {
241
+ static raw_ostream &yamlCOFFHeader (const object::coff_file_header *Header,
242
+ raw_ostream &Out) {
241
243
242
244
Out << " header: !Header\n " ;
243
245
Out << " Machine: " ;
@@ -247,112 +249,117 @@ static llvm::raw_ostream &yamlCOFFHeader(
247
249
}
248
250
249
251
250
- static llvm::raw_ostream &yamlCOFFSections (llvm::object::COFFObjectFile &Obj,
251
- std::size_t NumSections, llvm::raw_ostream &Out) {
252
- llvm::error_code ec;
252
+ static raw_ostream &yamlCOFFSections (object::COFFObjectFile &Obj,
253
+ std::size_t NumSections,
254
+ raw_ostream &Out) {
255
+ error_code ec;
253
256
Out << " sections:\n " ;
254
- for (llvm:: object::section_iterator iter = Obj.begin_sections ();
255
- iter != Obj.end_sections (); iter.increment (ec)) {
256
- const llvm:: object::coff_section *sect = Obj.getCOFFSection (iter);
257
-
257
+ for (object::section_iterator iter = Obj.begin_sections ();
258
+ iter != Obj.end_sections (); iter.increment (ec)) {
259
+ const object::coff_section *sect = Obj.getCOFFSection (iter);
260
+
258
261
Out << " - !Section\n " ;
259
262
Out << " Name: " ;
260
263
yaml::writeName (Out, sect->Name , sizeof (sect->Name )) << ' \n ' ;
261
264
262
265
Out << " Characteristics: [" ;
263
266
yaml::writeBitMask (Out, SectionCharacteristicsPairs1, sect->Characteristics );
264
- Out << nameLookup (SectionCharacteristicsPairsAlignment,
265
- sect->Characteristics & 0x00F00000 , " # Unrecognized_IMAGE_SCN_ALIGN" )
267
+ Out << nameLookup (SectionCharacteristicsPairsAlignment,
268
+ sect->Characteristics & 0x00F00000 , " # Unrecognized_IMAGE_SCN_ALIGN" )
266
269
<< " , " ;
267
270
yaml::writeBitMask (Out, SectionCharacteristicsPairs2, sect->Characteristics );
268
271
Out << " ] # " ;
269
272
yaml::writeHexNumber (Out, sect->Characteristics ) << ' \n ' ;
270
273
271
- llvm:: ArrayRef<uint8_t > sectionData;
272
- Obj.getSectionContents (sect, sectionData);
274
+ ArrayRef<uint8_t > sectionData;
275
+ Obj.getSectionContents (sect, sectionData);
273
276
Out << " SectionData: " ;
274
277
yaml::writeHexStream (Out, sectionData) << ' \n ' ;
275
278
if (iter->begin_relocations () != iter->end_relocations ())
276
279
Out << " Relocations:\n " ;
277
- for (llvm:: object::relocation_iterator rIter = iter->begin_relocations ();
280
+ for (object::relocation_iterator rIter = iter->begin_relocations ();
278
281
rIter != iter->end_relocations (); rIter.increment (ec)) {
279
- const llvm:: object::coff_relocation *reloc = Obj.getCOFFRelocation (rIter);
282
+ const object::coff_relocation *reloc = Obj.getCOFFRelocation (rIter);
280
283
281
284
Out << " - !Relocation\n " ;
282
285
Out << " VirtualAddress: " ;
283
286
yaml::writeHexNumber (Out, reloc->VirtualAddress ) << ' \n ' ;
284
287
Out << " SymbolTableIndex: " << reloc->SymbolTableIndex << ' \n ' ;
285
- Out << " Type: "
288
+ Out << " Type: "
286
289
<< nameLookup (RelocationTypeX86Pairs, reloc->Type ) << ' \n ' ;
287
290
// TODO: Use the correct reloc type for the machine.
288
291
Out << ' \n ' ;
289
292
}
290
293
291
- }
294
+ }
292
295
return Out;
293
296
}
294
297
295
- static llvm::raw_ostream& yamlCOFFSymbols (llvm::object::COFFObjectFile &Obj,
296
- std::size_t NumSymbols, llvm::raw_ostream &Out) {
297
- llvm::error_code ec;
298
+ static raw_ostream& yamlCOFFSymbols (object::COFFObjectFile &Obj,
299
+ std::size_t NumSymbols,
300
+ raw_ostream &Out) {
301
+ error_code ec;
298
302
Out << " symbols:\n " ;
299
- for (llvm:: object::symbol_iterator iter = Obj.begin_symbols ();
300
- iter != Obj.end_symbols (); iter.increment (ec)) {
303
+ for (object::symbol_iterator iter = Obj.begin_symbols ();
304
+ iter != Obj.end_symbols (); iter.increment (ec)) {
301
305
// Gather all the info that we need
302
- llvm:: StringRef str;
303
- const llvm:: object::coff_symbol *symbol = Obj.getCOFFSymbol (iter);
306
+ StringRef str;
307
+ const object::coff_symbol *symbol = Obj.getCOFFSymbol (iter);
304
308
Obj.getSymbolName (symbol, str);
305
309
std::size_t simpleType = symbol->getBaseType ();
306
310
std::size_t complexType = symbol->getComplexType ();
307
311
std::size_t storageClass = symbol->StorageClass ;
308
-
312
+
309
313
Out << " - !Symbol\n " ;
310
- Out << " Name: " << str << ' \n ' ;
314
+ Out << " Name: " << str << ' \n ' ;
311
315
312
316
Out << " Value: " << symbol->Value << ' \n ' ;
313
317
Out << " SectionNumber: " << symbol->SectionNumber << ' \n ' ;
314
318
315
- Out << " SimpleType: "
316
- << nameLookup (SymbolBaseTypePairs, simpleType,
317
- " # Unknown_SymbolBaseType" )
319
+ Out << " SimpleType: "
320
+ << nameLookup (SymbolBaseTypePairs, simpleType,
321
+ " # Unknown_SymbolBaseType" )
318
322
<< " # (" << simpleType << " )\n " ;
319
-
320
- Out << " ComplexType: "
321
- << nameLookup (SymbolComplexTypePairs, complexType,
322
- " # Unknown_SymbolComplexType" )
323
+
324
+ Out << " ComplexType: "
325
+ << nameLookup (SymbolComplexTypePairs, complexType,
326
+ " # Unknown_SymbolComplexType" )
323
327
<< " # (" << complexType << " )\n " ;
324
-
325
- Out << " StorageClass: "
328
+
329
+ Out << " StorageClass: "
326
330
<< nameLookup (SymbolStorageClassPairs, storageClass,
327
- " # Unknown_StorageClass" )
331
+ " # Unknown_StorageClass" )
328
332
<< " # (" << (int ) storageClass << " )\n " ;
329
333
330
334
if (symbol->NumberOfAuxSymbols > 0 ) {
331
- llvm:: ArrayRef<uint8_t > aux = Obj.getSymbolAuxData (symbol);
332
- Out << " NumberOfAuxSymbols: "
335
+ ArrayRef<uint8_t > aux = Obj.getSymbolAuxData (symbol);
336
+ Out << " NumberOfAuxSymbols: "
333
337
<< (int ) symbol->NumberOfAuxSymbols << ' \n ' ;
334
338
Out << " AuxillaryData: " ;
335
339
yaml::writeHexStream (Out, aux);
336
340
}
337
-
341
+
338
342
Out << ' \n ' ;
339
343
}
340
344
341
345
return Out;
342
346
}
343
347
344
348
345
- llvm::error_code coff2yaml (llvm::raw_ostream &Out, llvm::MemoryBuffer *TheObj) {
346
- llvm::error_code ec;
347
- llvm::object::COFFObjectFile obj (TheObj, ec);
348
- if (!ec) {
349
- const llvm::object::coff_file_header *hd;
350
- ec = obj.getHeader (hd);
351
- if (!ec) {
352
- yamlCOFFHeader (hd, Out);
353
- yamlCOFFSections (obj, hd->NumberOfSections , Out);
354
- yamlCOFFSymbols (obj, hd->NumberOfSymbols , Out);
355
- }
356
- }
349
+ error_code coff2yaml (raw_ostream &Out, MemoryBuffer *TheObj) {
350
+ error_code ec;
351
+ object::COFFObjectFile obj (TheObj, ec);
352
+ if (ec)
353
+ return ec;
354
+
355
+ const object::coff_file_header *hd;
356
+ ec = obj.getHeader (hd);
357
+ if (ec)
358
+ return ec;
359
+
360
+ yamlCOFFHeader (hd, Out);
361
+ yamlCOFFSections (obj, hd->NumberOfSections , Out);
362
+ yamlCOFFSymbols (obj, hd->NumberOfSymbols , Out);
363
+
357
364
return ec;
358
365
}
0 commit comments