Skip to content

Commit da177ce

Browse files
committed
Small obj2yaml cleanups.
* using namespace llvm. * whitespace. * early return. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179920 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f59a2a8 commit da177ce

File tree

1 file changed

+76
-69
lines changed

1 file changed

+76
-69
lines changed

tools/obj2yaml/coff2yaml.cpp

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
#include "obj2yaml.h"
1111
#include "llvm/Object/COFF.h"
1212

13+
using namespace llvm;
1314

1415
template <typename One, typename Two>
1516
struct pod_pair { // I'd much rather use std::pair, but it's not a POD
1617
One first;
1718
Two second;
1819
};
1920

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 *>
2223
MachineTypePairs [] = {
2324
STRING_PAIR(IMAGE_FILE_MACHINE_UNKNOWN),
2425
STRING_PAIR(IMAGE_FILE_MACHINE_AM33),
@@ -43,7 +44,7 @@ MachineTypePairs [] = {
4344
STRING_PAIR(IMAGE_FILE_MACHINE_WCEMIPSV2)
4445
};
4546

46-
static const pod_pair<llvm::COFF::SectionCharacteristics, const char *>
47+
static const pod_pair<COFF::SectionCharacteristics, const char *>
4748
SectionCharacteristicsPairs1 [] = {
4849
STRING_PAIR(IMAGE_SCN_TYPE_NO_PAD),
4950
STRING_PAIR(IMAGE_SCN_CNT_CODE),
@@ -60,7 +61,7 @@ SectionCharacteristicsPairs1 [] = {
6061
STRING_PAIR(IMAGE_SCN_MEM_PRELOAD)
6162
};
6263

63-
static const pod_pair<llvm::COFF::SectionCharacteristics, const char *>
64+
static const pod_pair<COFF::SectionCharacteristics, const char *>
6465
SectionCharacteristicsPairsAlignment [] = {
6566
STRING_PAIR(IMAGE_SCN_ALIGN_1BYTES),
6667
STRING_PAIR(IMAGE_SCN_ALIGN_2BYTES),
@@ -78,7 +79,7 @@ SectionCharacteristicsPairsAlignment [] = {
7879
STRING_PAIR(IMAGE_SCN_ALIGN_8192BYTES)
7980
};
8081

81-
static const pod_pair<llvm::COFF::SectionCharacteristics, const char *>
82+
static const pod_pair<COFF::SectionCharacteristics, const char *>
8283
SectionCharacteristicsPairs2 [] = {
8384
STRING_PAIR(IMAGE_SCN_LNK_NRELOC_OVFL),
8485
STRING_PAIR(IMAGE_SCN_MEM_DISCARDABLE),
@@ -89,8 +90,8 @@ SectionCharacteristicsPairs2 [] = {
8990
STRING_PAIR(IMAGE_SCN_MEM_READ),
9091
STRING_PAIR(IMAGE_SCN_MEM_WRITE)
9192
};
92-
93-
static const pod_pair<llvm::COFF::SymbolBaseType, const char *>
93+
94+
static const pod_pair<COFF::SymbolBaseType, const char *>
9495
SymbolBaseTypePairs [] = {
9596
STRING_PAIR(IMAGE_SYM_TYPE_NULL),
9697
STRING_PAIR(IMAGE_SYM_TYPE_VOID),
@@ -110,15 +111,15 @@ SymbolBaseTypePairs [] = {
110111
STRING_PAIR(IMAGE_SYM_TYPE_DWORD)
111112
};
112113

113-
static const pod_pair<llvm::COFF::SymbolComplexType, const char *>
114+
static const pod_pair<COFF::SymbolComplexType, const char *>
114115
SymbolComplexTypePairs [] = {
115116
STRING_PAIR(IMAGE_SYM_DTYPE_NULL),
116117
STRING_PAIR(IMAGE_SYM_DTYPE_POINTER),
117118
STRING_PAIR(IMAGE_SYM_DTYPE_FUNCTION),
118119
STRING_PAIR(IMAGE_SYM_DTYPE_ARRAY),
119120
};
120-
121-
static const pod_pair<llvm::COFF::SymbolStorageClass, const char *>
121+
122+
static const pod_pair<COFF::SymbolStorageClass, const char *>
122123
SymbolStorageClassPairs [] = {
123124
STRING_PAIR(IMAGE_SYM_CLASS_END_OF_FUNCTION),
124125
STRING_PAIR(IMAGE_SYM_CLASS_NULL),
@@ -149,7 +150,7 @@ SymbolStorageClassPairs [] = {
149150
STRING_PAIR(IMAGE_SYM_CLASS_CLR_TOKEN),
150151
};
151152

152-
static const pod_pair<llvm::COFF::RelocationTypeX86, const char *>
153+
static const pod_pair<COFF::RelocationTypeX86, const char *>
153154
RelocationTypeX86Pairs [] = {
154155
STRING_PAIR(IMAGE_REL_I386_ABSOLUTE),
155156
STRING_PAIR(IMAGE_REL_I386_DIR16),
@@ -181,7 +182,7 @@ RelocationTypeX86Pairs [] = {
181182
STRING_PAIR(IMAGE_REL_AMD64_SSPAN32)
182183
};
183184

184-
static const pod_pair<llvm::COFF::RelocationTypesARM, const char *>
185+
static const pod_pair<COFF::RelocationTypesARM, const char *>
185186
RelocationTypesARMPairs [] = {
186187
STRING_PAIR(IMAGE_REL_ARM_ABSOLUTE),
187188
STRING_PAIR(IMAGE_REL_ARM_ADDR32),
@@ -203,8 +204,8 @@ RelocationTypesARMPairs [] = {
203204

204205
namespace yaml { // COFF-specific yaml-writing specific routines
205206

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) {
208209
for (std::size_t i = 0; i < NameSize; ++i) {
209210
if (!Name[i]) break;
210211
Out << Name[i];
@@ -214,8 +215,9 @@ static llvm::raw_ostream &writeName(llvm::raw_ostream &Out,
214215

215216
// Given an array of pod_pair<enum, const char *>, write all enums that match
216217
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) {
219221
for (std::size_t i = 0; i < N; ++i)
220222
if (Val & Arr[i].first)
221223
Out << Arr[i].second << ", ";
@@ -226,8 +228,8 @@ static llvm::raw_ostream &writeBitMask(llvm::raw_ostream &Out,
226228

227229
// Given an array of pod_pair<enum, const char *>, look up a value
228230
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) {
231233
T n = static_cast<T>(Val);
232234
for (std::size_t i = 0; i < N; ++i)
233235
if (n == Arr[i].first)
@@ -236,8 +238,8 @@ const char *nameLookup(const pod_pair<T, const char *> (&Arr)[N],
236238
}
237239

238240

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) {
241243

242244
Out << "header: !Header\n";
243245
Out << " Machine: ";
@@ -247,112 +249,117 @@ static llvm::raw_ostream &yamlCOFFHeader(
247249
}
248250

249251

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;
253256
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+
258261
Out << " - !Section\n";
259262
Out << " Name: ";
260263
yaml::writeName(Out, sect->Name, sizeof(sect->Name)) << '\n';
261264

262265
Out << " Characteristics: [";
263266
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")
266269
<< ", ";
267270
yaml::writeBitMask(Out, SectionCharacteristicsPairs2, sect->Characteristics);
268271
Out << "] # ";
269272
yaml::writeHexNumber(Out, sect->Characteristics) << '\n';
270273

271-
llvm::ArrayRef<uint8_t> sectionData;
272-
Obj.getSectionContents(sect, sectionData);
274+
ArrayRef<uint8_t> sectionData;
275+
Obj.getSectionContents(sect, sectionData);
273276
Out << " SectionData: ";
274277
yaml::writeHexStream(Out, sectionData) << '\n';
275278
if (iter->begin_relocations() != iter->end_relocations())
276279
Out << " Relocations:\n";
277-
for (llvm::object::relocation_iterator rIter = iter->begin_relocations();
280+
for (object::relocation_iterator rIter = iter->begin_relocations();
278281
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);
280283

281284
Out << " - !Relocation\n";
282285
Out << " VirtualAddress: " ;
283286
yaml::writeHexNumber(Out, reloc->VirtualAddress) << '\n';
284287
Out << " SymbolTableIndex: " << reloc->SymbolTableIndex << '\n';
285-
Out << " Type: "
288+
Out << " Type: "
286289
<< nameLookup(RelocationTypeX86Pairs, reloc->Type) << '\n';
287290
// TODO: Use the correct reloc type for the machine.
288291
Out << '\n';
289292
}
290293

291-
}
294+
}
292295
return Out;
293296
}
294297

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;
298302
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)) {
301305
// 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);
304308
Obj.getSymbolName(symbol, str);
305309
std::size_t simpleType = symbol->getBaseType();
306310
std::size_t complexType = symbol->getComplexType();
307311
std::size_t storageClass = symbol->StorageClass;
308-
312+
309313
Out << " - !Symbol\n";
310-
Out << " Name: " << str << '\n';
314+
Out << " Name: " << str << '\n';
311315

312316
Out << " Value: " << symbol->Value << '\n';
313317
Out << " SectionNumber: " << symbol->SectionNumber << '\n';
314318

315-
Out << " SimpleType: "
316-
<< nameLookup(SymbolBaseTypePairs, simpleType,
317-
"# Unknown_SymbolBaseType")
319+
Out << " SimpleType: "
320+
<< nameLookup(SymbolBaseTypePairs, simpleType,
321+
"# Unknown_SymbolBaseType")
318322
<< " # (" << simpleType << ")\n";
319-
320-
Out << " ComplexType: "
321-
<< nameLookup(SymbolComplexTypePairs, complexType,
322-
"# Unknown_SymbolComplexType")
323+
324+
Out << " ComplexType: "
325+
<< nameLookup(SymbolComplexTypePairs, complexType,
326+
"# Unknown_SymbolComplexType")
323327
<< " # (" << complexType << ")\n";
324-
325-
Out << " StorageClass: "
328+
329+
Out << " StorageClass: "
326330
<< nameLookup(SymbolStorageClassPairs, storageClass,
327-
"# Unknown_StorageClass")
331+
"# Unknown_StorageClass")
328332
<< " # (" << (int) storageClass << ")\n";
329333

330334
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: "
333337
<< (int) symbol->NumberOfAuxSymbols << '\n';
334338
Out << " AuxillaryData: ";
335339
yaml::writeHexStream(Out, aux);
336340
}
337-
341+
338342
Out << '\n';
339343
}
340344

341345
return Out;
342346
}
343347

344348

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+
357364
return ec;
358365
}

0 commit comments

Comments
 (0)