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