11
11
// /
12
12
// ===----------------------------------------------------------------------===//
13
13
14
- #include " yaml2obj.h"
15
14
#include " llvm/ADT/STLExtras.h"
16
15
#include " llvm/ADT/StringExtras.h"
17
16
#include " llvm/ADT/StringMap.h"
20
19
#include " llvm/DebugInfo/CodeView/StringsAndChecksums.h"
21
20
#include " llvm/Object/COFF.h"
22
21
#include " llvm/ObjectYAML/ObjectYAML.h"
22
+ #include " llvm/ObjectYAML/yaml2obj.h"
23
23
#include " llvm/Support/Endian.h"
24
24
#include " llvm/Support/MemoryBuffer.h"
25
25
#include " llvm/Support/SourceMgr.h"
29
29
30
30
using namespace llvm ;
31
31
32
+ namespace {
33
+
32
34
// / This parses a yaml stream that represents a COFF object file.
33
35
// / See docs/yaml2obj for the yaml scheema.
34
36
struct COFFParser {
@@ -64,7 +66,8 @@ struct COFFParser {
64
66
65
67
bool parseSections () {
66
68
for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections .begin (),
67
- e = Obj.Sections .end (); i != e; ++i) {
69
+ e = Obj.Sections .end ();
70
+ i != e; ++i) {
68
71
COFFYAML::Section &Sec = *i;
69
72
70
73
// If the name is less than 8 bytes, store it in place, otherwise
@@ -102,7 +105,8 @@ struct COFFParser {
102
105
103
106
bool parseSymbols () {
104
107
for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols .begin (),
105
- e = Obj.Symbols .end (); i != e; ++i) {
108
+ e = Obj.Symbols .end ();
109
+ i != e; ++i) {
106
110
COFFYAML::Symbol &Sym = *i;
107
111
108
112
// If the name is less than 8 bytes, store it in place, otherwise
@@ -113,8 +117,8 @@ struct COFFParser {
113
117
} else {
114
118
// Add string to the string table and format the index for output.
115
119
unsigned Index = getStringIndex (Name);
116
- *reinterpret_cast <support::aligned_ulittle32_t *>(
117
- Sym. Header . Name + 4 ) = Index;
120
+ *reinterpret_cast <support::aligned_ulittle32_t *>(Sym. Header . Name + 4 ) =
121
+ Index;
118
122
}
119
123
120
124
Sym.Header .Type = Sym.SimpleType ;
@@ -153,6 +157,10 @@ struct COFFParser {
153
157
uint32_t SectionTableSize;
154
158
};
155
159
160
+ enum { DOSStubSize = 128 };
161
+
162
+ } // end anonymous namespace
163
+
156
164
// Take a CP and assign addresses and sizes to everything. Returns false if the
157
165
// layout is not valid to do.
158
166
static bool layoutOptionalHeader (COFFParser &CP) {
@@ -166,10 +174,6 @@ static bool layoutOptionalHeader(COFFParser &CP) {
166
174
return true ;
167
175
}
168
176
169
- namespace {
170
- enum { DOSStubSize = 128 };
171
- }
172
-
173
177
static yaml::BinaryRef
174
178
toDebugS (ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
175
179
const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) {
@@ -271,7 +275,7 @@ static bool layoutCOFF(COFFParser &CP) {
271
275
uint32_t NumberOfSymbols = 0 ;
272
276
for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj .Symbols .begin (),
273
277
e = CP.Obj .Symbols .end ();
274
- i != e; ++i) {
278
+ i != e; ++i) {
275
279
uint32_t NumberOfAuxSymbols = 0 ;
276
280
if (i->FunctionDefinition )
277
281
NumberOfAuxSymbols += 1 ;
@@ -298,24 +302,23 @@ static bool layoutCOFF(COFFParser &CP) {
298
302
else
299
303
CP.Obj .Header .PointerToSymbolTable = 0 ;
300
304
301
- *reinterpret_cast <support::ulittle32_t *>(&CP.StringTable [0 ])
302
- = CP.StringTable .size ();
305
+ *reinterpret_cast <support::ulittle32_t *>(&CP.StringTable [0 ]) =
306
+ CP.StringTable .size ();
303
307
304
308
return true ;
305
309
}
306
310
307
- template <typename value_type>
308
- struct binary_le_impl {
311
+ template <typename value_type> struct binary_le_impl {
309
312
value_type Value;
310
313
binary_le_impl (value_type V) : Value(V) {}
311
314
};
312
315
313
316
template <typename value_type>
314
- raw_ostream &operator <<( raw_ostream &OS
315
- , const binary_le_impl<value_type> &BLE) {
317
+ raw_ostream &operator <<(raw_ostream &OS,
318
+ const binary_le_impl<value_type> &BLE) {
316
319
char Buffer[sizeof (BLE.Value )];
317
320
support::endian::write<value_type, support::little, support::unaligned>(
318
- Buffer, BLE.Value );
321
+ Buffer, BLE.Value );
319
322
OS.write (Buffer, sizeof (BLE.Value ));
320
323
return OS;
321
324
}
@@ -335,13 +338,13 @@ raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
335
338
return OS;
336
339
}
337
340
338
- template <typename T>
339
- zeros_impl<sizeof (T)> zeros (const T &) {
341
+ template <typename T> zeros_impl<sizeof (T)> zeros (const T &) {
340
342
return zeros_impl<sizeof (T)>();
341
343
}
342
344
343
345
template <typename T>
344
- static uint32_t initializeOptionalHeader (COFFParser &CP, uint16_t Magic, T Header) {
346
+ static uint32_t initializeOptionalHeader (COFFParser &CP, uint16_t Magic,
347
+ T Header) {
345
348
memset (Header, 0 , sizeof (*Header));
346
349
Header->Magic = Magic;
347
350
Header->SectionAlignment = CP.Obj .OptionalHeader ->Header .SectionAlignment ;
@@ -376,10 +379,8 @@ static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Heade
376
379
CP.Obj .OptionalHeader ->Header .MajorOperatingSystemVersion ;
377
380
Header->MinorOperatingSystemVersion =
378
381
CP.Obj .OptionalHeader ->Header .MinorOperatingSystemVersion ;
379
- Header->MajorImageVersion =
380
- CP.Obj .OptionalHeader ->Header .MajorImageVersion ;
381
- Header->MinorImageVersion =
382
- CP.Obj .OptionalHeader ->Header .MinorImageVersion ;
382
+ Header->MajorImageVersion = CP.Obj .OptionalHeader ->Header .MajorImageVersion ;
383
+ Header->MinorImageVersion = CP.Obj .OptionalHeader ->Header .MinorImageVersion ;
383
384
Header->MajorSubsystemVersion =
384
385
CP.Obj .OptionalHeader ->Header .MajorSubsystemVersion ;
385
386
Header->MinorSubsystemVersion =
@@ -423,15 +424,13 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
423
424
if (CP.useBigObj ()) {
424
425
OS << binary_le (static_cast <uint16_t >(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
425
426
<< binary_le (static_cast <uint16_t >(0xffff ))
426
- << binary_le (static_cast <uint16_t >(COFF::BigObjHeader::MinBigObjectVersion))
427
+ << binary_le (
428
+ static_cast <uint16_t >(COFF::BigObjHeader::MinBigObjectVersion))
427
429
<< binary_le (CP.Obj .Header .Machine )
428
430
<< binary_le (CP.Obj .Header .TimeDateStamp );
429
431
OS.write (COFF::BigObjMagic, sizeof (COFF::BigObjMagic));
430
- OS << zeros (uint32_t (0 ))
431
- << zeros (uint32_t (0 ))
432
- << zeros (uint32_t (0 ))
433
- << zeros (uint32_t (0 ))
434
- << binary_le (CP.Obj .Header .NumberOfSections )
432
+ OS << zeros (uint32_t (0 )) << zeros (uint32_t (0 )) << zeros (uint32_t (0 ))
433
+ << zeros (uint32_t (0 )) << binary_le (CP.Obj .Header .NumberOfSections )
435
434
<< binary_le (CP.Obj .Header .PointerToSymbolTable )
436
435
<< binary_le (CP.Obj .Header .NumberOfSymbols );
437
436
} else {
@@ -450,7 +449,8 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
450
449
OS.write (reinterpret_cast <char *>(&PEH), sizeof (PEH));
451
450
} else {
452
451
object::pe32_header PEH;
453
- uint32_t BaseOfData = initializeOptionalHeader (CP, COFF::PE32Header::PE32, &PEH);
452
+ uint32_t BaseOfData =
453
+ initializeOptionalHeader (CP, COFF::PE32Header::PE32, &PEH);
454
454
PEH.BaseOfData = BaseOfData;
455
455
OS.write (reinterpret_cast <char *>(&PEH), sizeof (PEH));
456
456
}
@@ -472,7 +472,7 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
472
472
// Output section table.
473
473
for (std::vector<COFFYAML::Section>::iterator i = CP.Obj .Sections .begin (),
474
474
e = CP.Obj .Sections .end ();
475
- i != e; ++i) {
475
+ i != e; ++i) {
476
476
OS.write (i->Header .Name , COFF::NameSize);
477
477
OS << binary_le (i->Header .VirtualSize )
478
478
<< binary_le (i->Header .VirtualAddress )
@@ -514,8 +514,7 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
514
514
} else {
515
515
SymbolTableIndex = SymbolTableIndexMap[R.SymbolName ];
516
516
}
517
- OS << binary_le (R.VirtualAddress )
518
- << binary_le (SymbolTableIndex)
517
+ OS << binary_le (R.VirtualAddress ) << binary_le (SymbolTableIndex)
519
518
<< binary_le (R.Type );
520
519
}
521
520
}
@@ -524,15 +523,14 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
524
523
525
524
for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj .Symbols .begin (),
526
525
e = CP.Obj .Symbols .end ();
527
- i != e; ++i) {
526
+ i != e; ++i) {
528
527
OS.write (i->Header .Name , COFF::NameSize);
529
528
OS << binary_le (i->Header .Value );
530
529
if (CP.useBigObj ())
531
- OS << binary_le (i->Header .SectionNumber );
530
+ OS << binary_le (i->Header .SectionNumber );
532
531
else
533
- OS << binary_le (static_cast <int16_t >(i->Header .SectionNumber ));
534
- OS << binary_le (i->Header .Type )
535
- << binary_le (i->Header .StorageClass )
532
+ OS << binary_le (static_cast <int16_t >(i->Header .SectionNumber ));
533
+ OS << binary_le (i->Header .Type ) << binary_le (i->Header .StorageClass )
536
534
<< binary_le (i->Header .NumberOfAuxSymbols );
537
535
538
536
if (i->FunctionDefinition ) {
@@ -578,8 +576,7 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
578
576
OS.write_zeros (CP.getSymbolSize () - COFF::Symbol16Size);
579
577
}
580
578
if (i->CLRToken ) {
581
- OS << binary_le (i->CLRToken ->AuxType )
582
- << zeros (i->CLRToken ->unused1 )
579
+ OS << binary_le (i->CLRToken ->AuxType ) << zeros (i->CLRToken ->unused1 )
583
580
<< binary_le (i->CLRToken ->SymbolTableIndex )
584
581
<< zeros (i->CLRToken ->unused2 );
585
582
OS.write_zeros (CP.getSymbolSize () - COFF::Symbol16Size);
@@ -592,6 +589,9 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
592
589
return true ;
593
590
}
594
591
592
+ namespace llvm {
593
+ namespace yaml {
594
+
595
595
int yaml2coff (llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
596
596
COFFParser CP (Doc);
597
597
if (!CP.parse ()) {
@@ -614,3 +614,6 @@ int yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
614
614
}
615
615
return 0 ;
616
616
}
617
+
618
+ } // namespace yaml
619
+ } // namespace llvm
0 commit comments