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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
|
//===- yaml2xcoff - Convert YAML to a xcoff object file -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// The xcoff component of yaml2obj.
///
//===----------------------------------------------------------------------===//
#include "llvm/ADT/DenseMap.h"
#include "llvm/BinaryFormat/XCOFF.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/XCOFFObjectFile.h"
#include "llvm/ObjectYAML/ObjectYAML.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::object;
namespace {
constexpr unsigned DefaultSectionAlign = 4;
constexpr int16_t MaxSectionIndex = INT16_MAX;
constexpr uint32_t MaxRawDataSize = UINT32_MAX;
class XCOFFWriter {
public:
XCOFFWriter(XCOFFYAML::Object &Obj, raw_ostream &OS, yaml::ErrorHandler EH)
: Obj(Obj), W(OS, llvm::endianness::big), ErrHandler(EH),
StrTblBuilder(StringTableBuilder::XCOFF) {
Is64Bit = Obj.Header.Magic == (llvm::yaml::Hex16)XCOFF::XCOFF64;
}
bool writeXCOFF();
private:
void reportOverwrite(uint64_t currentOffset, uint64_t specifiedOffset,
const Twine &fieldName);
bool nameShouldBeInStringTable(StringRef SymbolName);
bool initFileHeader(uint64_t CurrentOffset);
void initAuxFileHeader();
bool initSectionHeaders(uint64_t &CurrentOffset);
bool initRelocations(uint64_t &CurrentOffset);
bool initStringTable();
bool assignAddressesAndIndices();
void writeFileHeader();
void writeAuxFileHeader();
void writeSectionHeaders();
bool writeSectionData();
bool writeRelocations();
bool writeSymbols();
void writeStringTable();
bool writeAuxSymbol(const XCOFFYAML::CsectAuxEnt &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::FileAuxEnt &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::FunctionAuxEnt &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::ExcpetionAuxEnt &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::BlockAuxEnt &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::SectAuxEntForDWARF &AuxSym);
bool writeAuxSymbol(const XCOFFYAML::SectAuxEntForStat &AuxSym);
bool writeAuxSymbol(const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym);
XCOFFYAML::Object &Obj;
bool Is64Bit = false;
support::endian::Writer W;
yaml::ErrorHandler ErrHandler;
StringTableBuilder StrTblBuilder;
uint64_t StartOffset = 0u;
// Map the section name to its corrresponding section index.
DenseMap<StringRef, int16_t> SectionIndexMap = {
{StringRef("N_DEBUG"), XCOFF::N_DEBUG},
{StringRef("N_ABS"), XCOFF::N_ABS},
{StringRef("N_UNDEF"), XCOFF::N_UNDEF}};
XCOFFYAML::FileHeader InitFileHdr = Obj.Header;
XCOFFYAML::AuxiliaryHeader InitAuxFileHdr;
std::vector<XCOFFYAML::Section> InitSections = Obj.Sections;
};
static void writeName(StringRef StrName, support::endian::Writer W) {
char Name[XCOFF::NameSize];
memset(Name, 0, XCOFF::NameSize);
char SrcName[] = "";
memcpy(Name, StrName.size() ? StrName.data() : SrcName, StrName.size());
ArrayRef<char> NameRef(Name, XCOFF::NameSize);
W.write(NameRef);
}
void XCOFFWriter::reportOverwrite(uint64_t CurrentOffset,
uint64_t specifiedOffset,
const Twine &fieldName) {
ErrHandler("current file offset (" + Twine(CurrentOffset) +
") is bigger than the specified " + fieldName + " (" +
Twine(specifiedOffset) + ") ");
}
bool XCOFFWriter::nameShouldBeInStringTable(StringRef SymbolName) {
// For XCOFF64: The symbol name is always in the string table.
return (SymbolName.size() > XCOFF::NameSize) || Is64Bit;
}
bool XCOFFWriter::initRelocations(uint64_t &CurrentOffset) {
for (XCOFFYAML::Section &InitSection : InitSections) {
if (!InitSection.Relocations.empty()) {
uint64_t RelSize = Is64Bit ? XCOFF::RelocationSerializationSize64
: XCOFF::RelocationSerializationSize32;
uint64_t UsedSize = RelSize * InitSection.Relocations.size();
// If NumberOfRelocations was specified, we use it, even if it's
// not consistent with the number of provided relocations.
if (!InitSection.NumberOfRelocations)
InitSection.NumberOfRelocations = InitSection.Relocations.size();
// If the YAML file specified an offset to relocations, we use it.
if (InitSection.FileOffsetToRelocations) {
if (CurrentOffset > InitSection.FileOffsetToRelocations) {
reportOverwrite(CurrentOffset, InitSection.FileOffsetToRelocations,
"FileOffsetToRelocations for the " +
InitSection.SectionName + " section");
return false;
}
CurrentOffset = InitSection.FileOffsetToRelocations;
} else
InitSection.FileOffsetToRelocations = CurrentOffset;
CurrentOffset += UsedSize;
if (CurrentOffset > MaxRawDataSize) {
ErrHandler("maximum object size (" + Twine(MaxRawDataSize) +
") exceeded when writing relocation data for section " +
Twine(InitSection.SectionName));
return false;
}
}
}
return true;
}
bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) {
uint64_t CurrentEndDataAddr = 0;
uint64_t CurrentEndTDataAddr = 0;
for (uint16_t I = 0, E = InitSections.size(); I < E; ++I) {
// Assign indices for sections.
if (InitSections[I].SectionName.size()) {
int16_t &SectionIndex = SectionIndexMap[InitSections[I].SectionName];
if (!SectionIndex) {
// The section index starts from 1.
SectionIndex = I + 1;
if ((I + 1) > MaxSectionIndex) {
ErrHandler("exceeded the maximum permitted section index of " +
Twine(MaxSectionIndex));
return false;
}
}
}
if (!InitSections[I].Size)
InitSections[I].Size = InitSections[I].SectionData.binary_size();
// Section data addresses (physical/virtual) are related to symbol
// addresses and alignments. Furthermore, it is possible to specify the
// same starting addresses for the .text, .data, and .tdata sections.
// Without examining all the symbols and their addreses and alignments,
// it is not possible to compute valid section addresses. The only
// condition required by XCOFF is that the .bss section immediately
// follows the .data section, and the .tbss section immediately follows
// the .tdata section. Therefore, we only assign addresses to the .bss
// and .tbss sections if they do not already have non-zero addresses.
// (If the YAML file is being used to generate a valid object file, we
// expect all section addresses to be specified explicitly.)
switch (InitSections[I].Flags) {
case XCOFF::STYP_DATA:
CurrentEndDataAddr = InitSections[I].Address + InitSections[I].Size;
break;
case XCOFF::STYP_BSS:
if (!InitSections[I].Address)
InitSections[I].Address = CurrentEndDataAddr;
break;
case XCOFF::STYP_TDATA:
CurrentEndTDataAddr = InitSections[I].Address + InitSections[I].Size;
break;
case XCOFF::STYP_TBSS:
if (!InitSections[I].Address)
InitSections[I].Address = CurrentEndTDataAddr;
break;
}
if (InitSections[I].SectionData.binary_size()) {
if (InitSections[I].FileOffsetToData) {
// Use the providedFileOffsetToData.
if (CurrentOffset > InitSections[I].FileOffsetToData) {
reportOverwrite(CurrentOffset, InitSections[I].FileOffsetToData,
"FileOffsetToData for the " +
InitSections[I].SectionName + " section");
return false;
}
CurrentOffset = InitSections[I].FileOffsetToData;
} else {
CurrentOffset = alignTo(CurrentOffset, DefaultSectionAlign);
InitSections[I].FileOffsetToData = CurrentOffset;
}
CurrentOffset += InitSections[I].SectionData.binary_size();
if (CurrentOffset > MaxRawDataSize) {
ErrHandler("maximum object size (" + Twine(MaxRawDataSize) +
") exceeded when writing data for section " + Twine(I + 1) +
" (" + Twine(InitSections[I].SectionName) + ")");
return false;
}
}
if (InitSections[I].SectionSubtype) {
uint32_t DWARFSubtype =
static_cast<uint32_t>(*InitSections[I].SectionSubtype);
if (InitSections[I].Flags != XCOFF::STYP_DWARF) {
ErrHandler("a DWARFSectionSubtype is only allowed for a DWARF section");
return false;
}
unsigned Mask = Is64Bit ? XCOFFSectionHeader64::SectionFlagsTypeMask
: XCOFFSectionHeader32::SectionFlagsTypeMask;
if (DWARFSubtype & Mask) {
ErrHandler("the low-order bits of DWARFSectionSubtype must be 0");
return false;
}
InitSections[I].Flags |= DWARFSubtype;
}
}
return initRelocations(CurrentOffset);
}
bool XCOFFWriter::initStringTable() {
if (Obj.StrTbl.RawContent) {
size_t RawSize = Obj.StrTbl.RawContent->binary_size();
if (Obj.StrTbl.Strings || Obj.StrTbl.Length) {
ErrHandler(
"can't specify Strings or Length when RawContent is specified");
return false;
}
if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize < RawSize) {
ErrHandler("specified ContentSize (" + Twine(*Obj.StrTbl.ContentSize) +
") is less than the RawContent data size (" + Twine(RawSize) +
")");
return false;
}
return true;
}
if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize <= 3) {
ErrHandler("ContentSize shouldn't be less than 4 without RawContent");
return false;
}
// Build the string table.
StrTblBuilder.clear();
if (Obj.StrTbl.Strings) {
// Add all specified strings to the string table.
for (StringRef StringEnt : *Obj.StrTbl.Strings)
StrTblBuilder.add(StringEnt);
size_t StrTblIdx = 0;
size_t NumOfStrings = Obj.StrTbl.Strings->size();
for (XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
if (nameShouldBeInStringTable(YamlSym.SymbolName)) {
if (StrTblIdx < NumOfStrings) {
// Overwrite the symbol name with the specified string.
YamlSym.SymbolName = (*Obj.StrTbl.Strings)[StrTblIdx];
++StrTblIdx;
} else
// Names that are not overwritten are still stored in the string
// table.
StrTblBuilder.add(YamlSym.SymbolName);
}
}
} else {
for (const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
if (nameShouldBeInStringTable(YamlSym.SymbolName))
StrTblBuilder.add(YamlSym.SymbolName);
}
}
// Check if the file name in the File Auxiliary Entry should be added to the
// string table.
for (const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
for (const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym :
YamlSym.AuxEntries) {
if (auto AS = dyn_cast<XCOFFYAML::FileAuxEnt>(AuxSym.get()))
if (nameShouldBeInStringTable(AS->FileNameOrString.value_or("")))
StrTblBuilder.add(AS->FileNameOrString.value_or(""));
}
}
StrTblBuilder.finalize();
size_t StrTblSize = StrTblBuilder.getSize();
if (Obj.StrTbl.ContentSize && *Obj.StrTbl.ContentSize < StrTblSize) {
ErrHandler("specified ContentSize (" + Twine(*Obj.StrTbl.ContentSize) +
") is less than the size of the data that would otherwise be "
"written (" +
Twine(StrTblSize) + ")");
return false;
}
return true;
}
bool XCOFFWriter::initFileHeader(uint64_t CurrentOffset) {
// The default format of the object file is XCOFF32.
InitFileHdr.Magic = XCOFF::XCOFF32;
InitFileHdr.NumberOfSections = Obj.Sections.size();
InitFileHdr.NumberOfSymTableEntries = Obj.Symbols.size();
for (XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
uint32_t AuxCount = YamlSym.AuxEntries.size();
if (YamlSym.NumberOfAuxEntries && *YamlSym.NumberOfAuxEntries < AuxCount) {
ErrHandler("specified NumberOfAuxEntries " +
Twine(static_cast<uint32_t>(*YamlSym.NumberOfAuxEntries)) +
" is less than the actual number "
"of auxiliary entries " +
Twine(AuxCount));
return false;
}
YamlSym.NumberOfAuxEntries = YamlSym.NumberOfAuxEntries.value_or(AuxCount);
// Add the number of auxiliary symbols to the total number.
InitFileHdr.NumberOfSymTableEntries += *YamlSym.NumberOfAuxEntries;
}
// Calculate SymbolTableOffset for the file header.
if (InitFileHdr.NumberOfSymTableEntries) {
if (Obj.Header.SymbolTableOffset) {
if (CurrentOffset > Obj.Header.SymbolTableOffset) {
reportOverwrite(CurrentOffset, Obj.Header.SymbolTableOffset,
"SymbolTableOffset");
return false;
}
CurrentOffset = Obj.Header.SymbolTableOffset;
}
InitFileHdr.SymbolTableOffset = CurrentOffset;
CurrentOffset +=
InitFileHdr.NumberOfSymTableEntries * XCOFF::SymbolTableEntrySize;
if (CurrentOffset > MaxRawDataSize) {
ErrHandler("maximum object size of " + Twine(MaxRawDataSize) +
" exceeded when writing symbols");
return false;
}
}
// TODO: Calculate FileOffsetToLineNumbers when line number supported.
return true;
}
void XCOFFWriter::initAuxFileHeader() {
if (Obj.AuxHeader)
InitAuxFileHdr = *Obj.AuxHeader;
// In general, an object file might contain multiple sections of a given type,
// but in a loadable module, there must be exactly one .text, .data, .bss, and
// .loader section. A loadable object might also have one .tdata section and
// one .tbss section.
// Set these section-related values if not set explicitly. We assume that the
// input YAML matches the format of the loadable object, but if multiple input
// sections still have the same type, the first section with that type
// prevails.
for (uint16_t I = 0, E = InitSections.size(); I < E; ++I) {
switch (InitSections[I].Flags) {
case XCOFF::STYP_TEXT:
if (!InitAuxFileHdr.TextSize)
InitAuxFileHdr.TextSize = InitSections[I].Size;
if (!InitAuxFileHdr.TextStartAddr)
InitAuxFileHdr.TextStartAddr = InitSections[I].Address;
if (!InitAuxFileHdr.SecNumOfText)
InitAuxFileHdr.SecNumOfText = I + 1;
break;
case XCOFF::STYP_DATA:
if (!InitAuxFileHdr.InitDataSize)
InitAuxFileHdr.InitDataSize = InitSections[I].Size;
if (!InitAuxFileHdr.DataStartAddr)
InitAuxFileHdr.DataStartAddr = InitSections[I].Address;
if (!InitAuxFileHdr.SecNumOfData)
InitAuxFileHdr.SecNumOfData = I + 1;
break;
case XCOFF::STYP_BSS:
if (!InitAuxFileHdr.BssDataSize)
InitAuxFileHdr.BssDataSize = InitSections[I].Size;
if (!InitAuxFileHdr.SecNumOfBSS)
InitAuxFileHdr.SecNumOfBSS = I + 1;
break;
case XCOFF::STYP_TDATA:
if (!InitAuxFileHdr.SecNumOfTData)
InitAuxFileHdr.SecNumOfTData = I + 1;
break;
case XCOFF::STYP_TBSS:
if (!InitAuxFileHdr.SecNumOfTBSS)
InitAuxFileHdr.SecNumOfTBSS = I + 1;
break;
case XCOFF::STYP_LOADER:
if (!InitAuxFileHdr.SecNumOfLoader)
InitAuxFileHdr.SecNumOfLoader = I + 1;
break;
default:
break;
}
}
}
bool XCOFFWriter::assignAddressesAndIndices() {
uint64_t FileHdrSize =
Is64Bit ? XCOFF::FileHeaderSize64 : XCOFF::FileHeaderSize32;
// If AuxHeaderSize is specified in the YAML file, we construct
// an auxiliary header.
uint64_t AuxFileHdrSize = 0;
if (Obj.Header.AuxHeaderSize)
AuxFileHdrSize = Obj.Header.AuxHeaderSize;
else if (Obj.AuxHeader)
AuxFileHdrSize =
(Is64Bit ? XCOFF::AuxFileHeaderSize64 : XCOFF::AuxFileHeaderSize32);
uint64_t SecHdrSize =
Is64Bit ? XCOFF::SectionHeaderSize64 : XCOFF::SectionHeaderSize32;
uint64_t CurrentOffset =
FileHdrSize + AuxFileHdrSize + InitSections.size() * SecHdrSize;
// Calculate section header info.
if (!initSectionHeaders(CurrentOffset))
return false;
// Calculate file header info.
if (!initFileHeader(CurrentOffset))
return false;
InitFileHdr.AuxHeaderSize = AuxFileHdrSize;
// Initialize the auxiliary file header.
if (AuxFileHdrSize)
initAuxFileHeader();
// Initialize the string table.
return initStringTable();
}
void XCOFFWriter::writeFileHeader() {
W.write<uint16_t>(Obj.Header.Magic ? Obj.Header.Magic : InitFileHdr.Magic);
W.write<uint16_t>(Obj.Header.NumberOfSections ? Obj.Header.NumberOfSections
: InitFileHdr.NumberOfSections);
W.write<int32_t>(Obj.Header.TimeStamp);
if (Is64Bit) {
W.write<uint64_t>(InitFileHdr.SymbolTableOffset);
W.write<uint16_t>(InitFileHdr.AuxHeaderSize);
W.write<uint16_t>(Obj.Header.Flags);
W.write<int32_t>(Obj.Header.NumberOfSymTableEntries
? Obj.Header.NumberOfSymTableEntries
: InitFileHdr.NumberOfSymTableEntries);
} else {
W.write<uint32_t>(InitFileHdr.SymbolTableOffset);
W.write<int32_t>(Obj.Header.NumberOfSymTableEntries
? Obj.Header.NumberOfSymTableEntries
: InitFileHdr.NumberOfSymTableEntries);
W.write<uint16_t>(InitFileHdr.AuxHeaderSize);
W.write<uint16_t>(Obj.Header.Flags);
}
}
void XCOFFWriter::writeAuxFileHeader() {
W.write<uint16_t>(InitAuxFileHdr.Magic.value_or(yaml::Hex16(1)));
W.write<uint16_t>(InitAuxFileHdr.Version.value_or(yaml::Hex16(1)));
if (Is64Bit) {
W.OS.write_zeros(4); // Reserved for debugger.
W.write<uint64_t>(InitAuxFileHdr.TextStartAddr.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.DataStartAddr.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.TOCAnchorAddr.value_or(yaml::Hex64(0)));
} else {
W.write<uint32_t>(InitAuxFileHdr.TextSize.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.InitDataSize.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.BssDataSize.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.EntryPointAddr.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.TextStartAddr.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.DataStartAddr.value_or(yaml::Hex64(0)));
// A short 32-bit auxiliary header ends here.
if (InitFileHdr.AuxHeaderSize == XCOFF::AuxFileHeaderSizeShort)
return;
W.write<uint32_t>(InitAuxFileHdr.TOCAnchorAddr.value_or(yaml::Hex64(0)));
}
W.write<uint16_t>(InitAuxFileHdr.SecNumOfEntryPoint.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfText.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfData.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfTOC.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfLoader.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfBSS.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.MaxAlignOfText.value_or(yaml::Hex16(0)));
W.write<uint16_t>(InitAuxFileHdr.MaxAlignOfData.value_or(yaml::Hex16(0)));
W.write<uint16_t>(InitAuxFileHdr.ModuleType.value_or(yaml::Hex16(0)));
W.write<uint8_t>(InitAuxFileHdr.CpuFlag.value_or(yaml::Hex8(0)));
W.write<uint8_t>(0); // Reserved for CPU type.
if (Is64Bit) {
W.write<uint8_t>(InitAuxFileHdr.TextPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(InitAuxFileHdr.DataPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(InitAuxFileHdr.StackPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(
InitAuxFileHdr.FlagAndTDataAlignment.value_or(yaml::Hex8(0x80)));
W.write<uint64_t>(InitAuxFileHdr.TextSize.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.InitDataSize.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.BssDataSize.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.EntryPointAddr.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.MaxStackSize.value_or(yaml::Hex64(0)));
W.write<uint64_t>(InitAuxFileHdr.MaxDataSize.value_or(yaml::Hex64(0)));
} else {
W.write<uint32_t>(InitAuxFileHdr.MaxStackSize.value_or(yaml::Hex64(0)));
W.write<uint32_t>(InitAuxFileHdr.MaxDataSize.value_or(yaml::Hex64(0)));
W.OS.write_zeros(4); // Reserved for debugger.
W.write<uint8_t>(InitAuxFileHdr.TextPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(InitAuxFileHdr.DataPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(InitAuxFileHdr.StackPageSize.value_or(yaml::Hex8(0)));
W.write<uint8_t>(
InitAuxFileHdr.FlagAndTDataAlignment.value_or(yaml::Hex8(0)));
}
W.write<uint16_t>(InitAuxFileHdr.SecNumOfTData.value_or(0));
W.write<uint16_t>(InitAuxFileHdr.SecNumOfTBSS.value_or(0));
if (Is64Bit) {
W.write<uint16_t>(
InitAuxFileHdr.Flag.value_or(yaml::Hex16(XCOFF::SHR_SYMTAB)));
if (InitFileHdr.AuxHeaderSize > XCOFF::AuxFileHeaderSize64)
W.OS.write_zeros(InitFileHdr.AuxHeaderSize - XCOFF::AuxFileHeaderSize64);
} else {
if (InitFileHdr.AuxHeaderSize > XCOFF::AuxFileHeaderSize32)
W.OS.write_zeros(InitFileHdr.AuxHeaderSize - XCOFF::AuxFileHeaderSize32);
}
}
void XCOFFWriter::writeSectionHeaders() {
for (uint16_t I = 0, E = Obj.Sections.size(); I < E; ++I) {
XCOFFYAML::Section DerivedSec = InitSections[I];
writeName(DerivedSec.SectionName, W);
if (Is64Bit) {
// Virtual address is the same as physical address.
W.write<uint64_t>(DerivedSec.Address); // Physical address
W.write<uint64_t>(DerivedSec.Address); // Virtual address
W.write<uint64_t>(DerivedSec.Size);
W.write<uint64_t>(DerivedSec.FileOffsetToData);
W.write<uint64_t>(DerivedSec.FileOffsetToRelocations);
W.write<uint64_t>(DerivedSec.FileOffsetToLineNumbers);
W.write<uint32_t>(DerivedSec.NumberOfRelocations);
W.write<uint32_t>(DerivedSec.NumberOfLineNumbers);
W.write<int32_t>(DerivedSec.Flags);
W.OS.write_zeros(4);
} else {
// Virtual address is the same as physical address.
W.write<uint32_t>(DerivedSec.Address); // Physical address
W.write<uint32_t>(DerivedSec.Address); // Virtual address
W.write<uint32_t>(DerivedSec.Size);
W.write<uint32_t>(DerivedSec.FileOffsetToData);
W.write<uint32_t>(DerivedSec.FileOffsetToRelocations);
W.write<uint32_t>(DerivedSec.FileOffsetToLineNumbers);
W.write<uint16_t>(DerivedSec.NumberOfRelocations);
W.write<uint16_t>(DerivedSec.NumberOfLineNumbers);
W.write<int32_t>(DerivedSec.Flags);
}
}
}
bool XCOFFWriter::writeSectionData() {
for (uint16_t I = 0, E = Obj.Sections.size(); I < E; ++I) {
XCOFFYAML::Section YamlSec = Obj.Sections[I];
if (YamlSec.SectionData.binary_size()) {
// Fill the padding size with zeros.
int64_t PaddingSize = (uint64_t)InitSections[I].FileOffsetToData -
(W.OS.tell() - StartOffset);
if (PaddingSize < 0) {
ErrHandler("redundant data was written before section data");
return false;
}
W.OS.write_zeros(PaddingSize);
YamlSec.SectionData.writeAsBinary(W.OS);
}
}
return true;
}
bool XCOFFWriter::writeRelocations() {
for (uint16_t I = 0, E = Obj.Sections.size(); I < E; ++I) {
XCOFFYAML::Section YamlSec = Obj.Sections[I];
if (!YamlSec.Relocations.empty()) {
int64_t PaddingSize =
InitSections[I].FileOffsetToRelocations - (W.OS.tell() - StartOffset);
if (PaddingSize < 0) {
ErrHandler("redundant data was written before relocations");
return false;
}
W.OS.write_zeros(PaddingSize);
for (const XCOFFYAML::Relocation &YamlRel : YamlSec.Relocations) {
if (Is64Bit)
W.write<uint64_t>(YamlRel.VirtualAddress);
else
W.write<uint32_t>(YamlRel.VirtualAddress);
W.write<uint32_t>(YamlRel.SymbolIndex);
W.write<uint8_t>(YamlRel.Info);
W.write<uint8_t>(YamlRel.Type);
}
}
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::CsectAuxEnt &AuxSym) {
uint8_t SymAlignAndType = 0;
if (AuxSym.SymbolAlignmentAndType) {
if (AuxSym.SymbolType || AuxSym.SymbolAlignment) {
ErrHandler("cannot specify SymbolType or SymbolAlignment if "
"SymbolAlignmentAndType is specified");
return false;
}
SymAlignAndType = *AuxSym.SymbolAlignmentAndType;
} else {
if (AuxSym.SymbolType) {
uint8_t SymbolType = *AuxSym.SymbolType;
if (SymbolType & ~XCOFFCsectAuxRef::SymbolTypeMask) {
ErrHandler("symbol type must be less than " +
Twine(1 + XCOFFCsectAuxRef::SymbolTypeMask));
return false;
}
SymAlignAndType = SymbolType;
}
if (AuxSym.SymbolAlignment) {
const uint8_t ShiftedSymbolAlignmentMask =
XCOFFCsectAuxRef::SymbolAlignmentMask >>
XCOFFCsectAuxRef::SymbolAlignmentBitOffset;
if (*AuxSym.SymbolAlignment & ~ShiftedSymbolAlignmentMask) {
ErrHandler("symbol alignment must be less than " +
Twine(1 + ShiftedSymbolAlignmentMask));
return false;
}
SymAlignAndType |= (*AuxSym.SymbolAlignment
<< XCOFFCsectAuxRef::SymbolAlignmentBitOffset);
}
}
if (Is64Bit) {
W.write<uint32_t>(AuxSym.SectionOrLengthLo.value_or(0));
W.write<uint32_t>(AuxSym.ParameterHashIndex.value_or(0));
W.write<uint16_t>(AuxSym.TypeChkSectNum.value_or(0));
W.write<uint8_t>(SymAlignAndType);
W.write<uint8_t>(AuxSym.StorageMappingClass.value_or(XCOFF::XMC_PR));
W.write<uint32_t>(AuxSym.SectionOrLengthHi.value_or(0));
W.write<uint8_t>(0);
W.write<uint8_t>(XCOFF::AUX_CSECT);
} else {
W.write<uint32_t>(AuxSym.SectionOrLength.value_or(0));
W.write<uint32_t>(AuxSym.ParameterHashIndex.value_or(0));
W.write<uint16_t>(AuxSym.TypeChkSectNum.value_or(0));
W.write<uint8_t>(SymAlignAndType);
W.write<uint8_t>(AuxSym.StorageMappingClass.value_or(XCOFF::XMC_PR));
W.write<uint32_t>(AuxSym.StabInfoIndex.value_or(0));
W.write<uint16_t>(AuxSym.StabSectNum.value_or(0));
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::ExcpetionAuxEnt &AuxSym) {
assert(Is64Bit && "can't write the exception auxiliary symbol for XCOFF32");
W.write<uint64_t>(AuxSym.OffsetToExceptionTbl.value_or(0));
W.write<uint32_t>(AuxSym.SizeOfFunction.value_or(0));
W.write<uint32_t>(AuxSym.SymIdxOfNextBeyond.value_or(0));
W.write<uint8_t>(0);
W.write<uint8_t>(XCOFF::AUX_EXCEPT);
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::FunctionAuxEnt &AuxSym) {
if (Is64Bit) {
W.write<uint64_t>(AuxSym.PtrToLineNum.value_or(0));
W.write<uint32_t>(AuxSym.SizeOfFunction.value_or(0));
W.write<uint32_t>(AuxSym.SymIdxOfNextBeyond.value_or(0));
W.write<uint8_t>(0);
W.write<uint8_t>(XCOFF::AUX_FCN);
} else {
W.write<uint32_t>(AuxSym.OffsetToExceptionTbl.value_or(0));
W.write<uint32_t>(AuxSym.SizeOfFunction.value_or(0));
W.write<uint32_t>(AuxSym.PtrToLineNum.value_or(0));
W.write<uint32_t>(AuxSym.SymIdxOfNextBeyond.value_or(0));
W.OS.write_zeros(2);
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::FileAuxEnt &AuxSym) {
StringRef FileName = AuxSym.FileNameOrString.value_or("");
if (nameShouldBeInStringTable(FileName)) {
W.write<int32_t>(0);
W.write<uint32_t>(StrTblBuilder.getOffset(FileName));
} else {
writeName(FileName, W);
}
W.OS.write_zeros(XCOFF::FileNamePadSize);
W.write<uint8_t>(AuxSym.FileStringType.value_or(XCOFF::XFT_FN));
if (Is64Bit) {
W.OS.write_zeros(2);
W.write<uint8_t>(XCOFF::AUX_FILE);
} else {
W.OS.write_zeros(3);
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::BlockAuxEnt &AuxSym) {
if (Is64Bit) {
W.write<uint32_t>(AuxSym.LineNum.value_or(0));
W.OS.write_zeros(13);
W.write<uint8_t>(XCOFF::AUX_SYM);
} else {
W.OS.write_zeros(2);
W.write<uint16_t>(AuxSym.LineNumHi.value_or(0));
W.write<uint16_t>(AuxSym.LineNumLo.value_or(0));
W.OS.write_zeros(12);
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::SectAuxEntForDWARF &AuxSym) {
if (Is64Bit) {
W.write<uint64_t>(AuxSym.LengthOfSectionPortion.value_or(0));
W.write<uint64_t>(AuxSym.NumberOfRelocEnt.value_or(0));
W.write<uint8_t>(0);
W.write<uint8_t>(XCOFF::AUX_SECT);
} else {
W.write<uint32_t>(AuxSym.LengthOfSectionPortion.value_or(0));
W.OS.write_zeros(4);
W.write<uint32_t>(AuxSym.NumberOfRelocEnt.value_or(0));
W.OS.write_zeros(6);
}
return true;
}
bool XCOFFWriter::writeAuxSymbol(const XCOFFYAML::SectAuxEntForStat &AuxSym) {
assert(!Is64Bit && "can't write the stat auxiliary symbol for XCOFF64");
W.write<uint32_t>(AuxSym.SectionLength.value_or(0));
W.write<uint16_t>(AuxSym.NumberOfRelocEnt.value_or(0));
W.write<uint16_t>(AuxSym.NumberOfLineNum.value_or(0));
W.OS.write_zeros(10);
return true;
}
bool XCOFFWriter::writeAuxSymbol(
const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
if (auto AS = dyn_cast<XCOFFYAML::CsectAuxEnt>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::FunctionAuxEnt>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::ExcpetionAuxEnt>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::FileAuxEnt>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::BlockAuxEnt>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::SectAuxEntForDWARF>(AuxSym.get()))
return writeAuxSymbol(*AS);
else if (auto AS = dyn_cast<XCOFFYAML::SectAuxEntForStat>(AuxSym.get()))
return writeAuxSymbol(*AS);
llvm_unreachable("unknown auxiliary symbol type");
return false;
}
bool XCOFFWriter::writeSymbols() {
int64_t PaddingSize =
InitFileHdr.SymbolTableOffset - (W.OS.tell() - StartOffset);
if (PaddingSize < 0) {
ErrHandler("redundant data was written before symbols");
return false;
}
W.OS.write_zeros(PaddingSize);
for (const XCOFFYAML::Symbol &YamlSym : Obj.Symbols) {
if (Is64Bit) {
W.write<uint64_t>(YamlSym.Value);
W.write<uint32_t>(StrTblBuilder.getOffset(YamlSym.SymbolName));
} else {
if (nameShouldBeInStringTable(YamlSym.SymbolName)) {
// For XCOFF32: A value of 0 indicates that the symbol name is in the
// string table.
W.write<int32_t>(0);
W.write<uint32_t>(StrTblBuilder.getOffset(YamlSym.SymbolName));
} else {
writeName(YamlSym.SymbolName, W);
}
W.write<uint32_t>(YamlSym.Value);
}
if (YamlSym.SectionName) {
auto It = SectionIndexMap.find(*YamlSym.SectionName);
if (It == SectionIndexMap.end()) {
ErrHandler("the SectionName " + *YamlSym.SectionName +
" specified in the symbol does not exist");
return false;
}
if (YamlSym.SectionIndex && It->second != *YamlSym.SectionIndex) {
ErrHandler("the SectionName " + *YamlSym.SectionName +
" and the SectionIndex (" + Twine(*YamlSym.SectionIndex) +
") refer to different sections");
return false;
}
W.write<int16_t>(It->second);
} else {
W.write<int16_t>(YamlSym.SectionIndex.value_or(0));
}
W.write<uint16_t>(YamlSym.Type);
W.write<uint8_t>(YamlSym.StorageClass);
uint8_t NumOfAuxSym = YamlSym.NumberOfAuxEntries.value_or(0);
W.write<uint8_t>(NumOfAuxSym);
if (!NumOfAuxSym && !YamlSym.AuxEntries.size())
continue;
// Now write auxiliary entries.
if (!YamlSym.AuxEntries.size()) {
W.OS.write_zeros(XCOFF::SymbolTableEntrySize * NumOfAuxSym);
} else {
for (const std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym :
YamlSym.AuxEntries) {
if (!writeAuxSymbol(AuxSym))
return false;
}
// Pad with zeros.
if (NumOfAuxSym > YamlSym.AuxEntries.size())
W.OS.write_zeros(XCOFF::SymbolTableEntrySize *
(NumOfAuxSym - YamlSym.AuxEntries.size()));
}
}
return true;
}
void XCOFFWriter::writeStringTable() {
if (Obj.StrTbl.RawContent) {
Obj.StrTbl.RawContent->writeAsBinary(W.OS);
if (Obj.StrTbl.ContentSize) {
assert(*Obj.StrTbl.ContentSize >= Obj.StrTbl.RawContent->binary_size() &&
"Specified ContentSize is less than the RawContent size.");
W.OS.write_zeros(*Obj.StrTbl.ContentSize -
Obj.StrTbl.RawContent->binary_size());
}
return;
}
size_t StrTblBuilderSize = StrTblBuilder.getSize();
// If neither Length nor ContentSize is specified, write the StrTblBuilder
// directly, which contains the auto-generated Length value.
if (!Obj.StrTbl.Length && !Obj.StrTbl.ContentSize) {
if (StrTblBuilderSize <= 4)
return;
StrTblBuilder.write(W.OS);
return;
}
// Serialize the string table's content to a temporary buffer.
std::unique_ptr<WritableMemoryBuffer> Buf =
WritableMemoryBuffer::getNewMemBuffer(StrTblBuilderSize);
uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart());
StrTblBuilder.write(Ptr);
// Replace the first 4 bytes, which contain the auto-generated Length value,
// with the specified value.
memset(Ptr, 0, 4);
support::endian::write32be(Ptr, Obj.StrTbl.Length ? *Obj.StrTbl.Length
: *Obj.StrTbl.ContentSize);
// Copy the buffer content to the actual output stream.
W.OS.write(Buf->getBufferStart(), Buf->getBufferSize());
// Add zeros as padding after strings.
if (Obj.StrTbl.ContentSize) {
assert(*Obj.StrTbl.ContentSize >= StrTblBuilderSize &&
"Specified ContentSize is less than the StringTableBuilder size.");
W.OS.write_zeros(*Obj.StrTbl.ContentSize - StrTblBuilderSize);
}
}
bool XCOFFWriter::writeXCOFF() {
if (!assignAddressesAndIndices())
return false;
StartOffset = W.OS.tell();
writeFileHeader();
if (InitFileHdr.AuxHeaderSize)
writeAuxFileHeader();
if (!Obj.Sections.empty()) {
writeSectionHeaders();
if (!writeSectionData())
return false;
if (!writeRelocations())
return false;
}
if (!Obj.Symbols.empty() && !writeSymbols())
return false;
writeStringTable();
return true;
}
} // end anonymous namespace
namespace llvm {
namespace yaml {
bool yaml2xcoff(XCOFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
XCOFFWriter Writer(Doc, Out, EH);
return Writer.writeXCOFF();
}
} // namespace yaml
} // namespace llvm
|