39 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
42 StringTable.append(4,
char(0));
45 bool useBigObj()
const {
46 return static_cast<int32_t
>(Obj.Sections.size()) >
50 bool isPE()
const {
return Obj.OptionalHeader.has_value(); }
53 uint32_t getFileAlignment()
const {
54 return Obj.OptionalHeader->Header.FileAlignment;
61 unsigned getSymbolSize()
const {
65 bool parseSections() {
66 for (COFFYAML::Section &Sec : Obj.Sections) {
76 std::string str =
utostr(Index);
78 ErrHandler(
"string table got too large");
87 ErrHandler(
"section alignment is too large");
91 ErrHandler(
"section alignment is not a power of 2");
100 bool parseSymbols() {
101 for (COFFYAML::Symbol &Sym : Obj.Symbols) {
121 if (!parseSections())
129 auto [It,
Inserted] = StringTableMap.try_emplace(Str, StringTable.size());
131 StringTable.append(Str.begin(), Str.end());
132 StringTable.push_back(0);
137 COFFYAML::Object &Obj;
139 codeview::StringsAndChecksums StringsAndChecksums;
141 StringMap<unsigned> StringTableMap;
142 std::string StringTable;
143 uint32_t SectionTableStart;
144 uint32_t SectionTableSize;
149enum { DOSStubSize = 128 };
170 ExitOnError Err(
"Error occurred writing .debug$S section");
174 std::vector<DebugSubsectionRecordBuilder> Builders;
176 for (
auto &SS : CVSS) {
177 DebugSubsectionRecordBuilder
B(SS);
178 Size +=
B.calculateSerializedLength();
179 Builders.push_back(std::move(
B));
186 for (
const auto &
B : Builders) {
187 Err(
B.commit(Writer, CodeViewContainer::ObjectFile));
197 CP.SectionTableStart =
204 CP.SectionTableStart + CP.SectionTableSize;
211 CP.StringsAndChecksums);
220 if (S.
Name ==
".debug$S") {
223 "Object file does not have debug string table!");
228 }
else if (S.
Name ==
".debug$T") {
231 }
else if (S.
Name ==
".debug$P") {
234 }
else if (S.
Name ==
".debug$H") {
241 DataSize +=
E.size();
243 CurrentSectionDataOffset =
alignTo(CurrentSectionDataOffset,
244 CP.isPE() ? CP.getFileAlignment() : 4);
267 uint32_t SymbolTableStart = CurrentSectionDataOffset;
271 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.
Symbols.begin(),
275 if (i->FunctionDefinition)
276 NumberOfAuxSymbols += 1;
277 if (i->bfAndefSymbol)
278 NumberOfAuxSymbols += 1;
280 NumberOfAuxSymbols += 1;
281 if (!i->File.empty())
282 NumberOfAuxSymbols +=
283 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
284 if (i->SectionDefinition)
285 NumberOfAuxSymbols += 1;
287 NumberOfAuxSymbols += 1;
288 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
289 NumberOfSymbols += 1 + NumberOfAuxSymbols;
295 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
301 CP.StringTable.size();
309 memset(Header, 0,
sizeof(*Header));
310 Header->Magic = Magic;
311 Header->SectionAlignment = CP.Obj.
OptionalHeader->Header.SectionAlignment;
312 Header->FileAlignment = CP.Obj.
OptionalHeader->Header.FileAlignment;
313 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
314 SizeOfUninitializedData = 0;
315 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
316 Header->FileAlignment);
317 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
326 if (S.
Name ==
".text")
328 else if (S.
Name ==
".data")
333 Header->SizeOfCode = SizeOfCode;
334 Header->SizeOfInitializedData = SizeOfInitializedData;
335 Header->SizeOfUninitializedData = SizeOfUninitializedData;
336 Header->AddressOfEntryPoint =
339 Header->MajorOperatingSystemVersion =
341 Header->MinorOperatingSystemVersion =
343 Header->MajorImageVersion = CP.Obj.
OptionalHeader->Header.MajorImageVersion;
344 Header->MinorImageVersion = CP.Obj.
OptionalHeader->Header.MinorImageVersion;
345 Header->MajorSubsystemVersion =
347 Header->MinorSubsystemVersion =
349 Header->SizeOfImage = SizeOfImage;
350 Header->SizeOfHeaders = SizeOfHeaders;
352 Header->DLLCharacteristics = CP.Obj.
OptionalHeader->Header.DLLCharacteristics;
353 Header->SizeOfStackReserve = CP.Obj.
OptionalHeader->Header.SizeOfStackReserve;
354 Header->SizeOfStackCommit = CP.Obj.
OptionalHeader->Header.SizeOfStackCommit;
355 Header->SizeOfHeapReserve = CP.Obj.
OptionalHeader->Header.SizeOfHeapReserve;
356 Header->SizeOfHeapCommit = CP.Obj.
OptionalHeader->Header.SizeOfHeapCommit;
357 Header->NumberOfRvaAndSize = CP.Obj.
OptionalHeader->Header.NumberOfRvaAndSize;
365 memset(&DH, 0,
sizeof(DH));
378 CBA.
write(
reinterpret_cast<const char *
>(&DH),
sizeof(DH));
385 if (CP.useBigObj()) {
412 CBA.
write(
reinterpret_cast<const char *
>(&PEH),
sizeof(PEH));
418 CBA.
write(
reinterpret_cast<const char *
>(&PEH),
sizeof(PEH));
422 const std::optional<COFF::DataDirectory> *DataDirectories =
425 if (
I >= NumDataDir || !DataDirectories[
I]) {
428 CBA.
write(DataDirectories[
I]->RelativeVirtualAddress, LittleEndian);
429 CBA.
write(DataDirectories[
I]->
Size, LittleEndian);
450 unsigned CurSymbol = 0;
453 SymbolTableIndexMap[Sym.
Name] = CurSymbol;
464 E.writeAsBinary(CBA);
478 if (R.SymbolTableIndex) {
479 if (!R.SymbolName.empty())
481 <<
"Both SymbolName and SymbolTableIndex specified\n";
482 SymbolTableIndex = *R.SymbolTableIndex;
484 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
486 CBA.
write(R.VirtualAddress, LittleEndian);
487 CBA.
write(SymbolTableIndex, LittleEndian);
488 CBA.
write(R.Type, LittleEndian);
493 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.
Symbols.begin(),
497 CBA.
write(i->Header.Value, LittleEndian);
499 CBA.
write(i->Header.SectionNumber, LittleEndian);
501 CBA.
write(
static_cast<int16_t
>(i->Header.SectionNumber), LittleEndian);
502 CBA.
write(i->Header.Type, LittleEndian);
503 CBA.
write(i->Header.StorageClass, LittleEndian);
504 CBA.
write(i->Header.NumberOfAuxSymbols, LittleEndian);
506 if (i->FunctionDefinition) {
507 CBA.
write(i->FunctionDefinition->TagIndex, LittleEndian);
508 CBA.
write(i->FunctionDefinition->TotalSize, LittleEndian);
509 CBA.
write(i->FunctionDefinition->PointerToLinenumber, LittleEndian);
510 CBA.
write(i->FunctionDefinition->PointerToNextFunction, LittleEndian);
511 CBA.
writeZeros(
sizeof(i->FunctionDefinition->unused));
514 if (i->bfAndefSymbol) {
515 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused1));
516 CBA.
write(i->bfAndefSymbol->Linenumber, LittleEndian);
517 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused2));
518 CBA.
write(i->bfAndefSymbol->PointerToNextFunction, LittleEndian);
519 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused3));
522 if (i->WeakExternal) {
523 CBA.
write(i->WeakExternal->TagIndex, LittleEndian);
524 CBA.
write(i->WeakExternal->Characteristics, LittleEndian);
525 CBA.
writeZeros(
sizeof(i->WeakExternal->unused));
528 if (!i->File.empty()) {
529 unsigned SymbolSize = CP.getSymbolSize();
531 (i->File.size() + SymbolSize - 1) / SymbolSize;
532 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
533 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
534 CBA.
write(i->File.data(), i->File.size());
537 if (i->SectionDefinition) {
538 CBA.
write(i->SectionDefinition->Length, LittleEndian);
539 CBA.
write(i->SectionDefinition->NumberOfRelocations, LittleEndian);
540 CBA.
write(i->SectionDefinition->NumberOfLinenumbers, LittleEndian);
541 CBA.
write(i->SectionDefinition->CheckSum, LittleEndian);
542 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number),
544 CBA.
write(i->SectionDefinition->Selection, LittleEndian);
545 CBA.
writeZeros(
sizeof(i->SectionDefinition->unused));
546 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16),
551 CBA.
write(i->CLRToken->AuxType, LittleEndian);
553 CBA.
write(i->CLRToken->SymbolTableIndex, LittleEndian);
561 CBA.
write(CP.StringTable.data(), CP.StringTable.size());
568 Size +=
sizeof(*UInt32);
578 CBA.
write(
reinterpret_cast<const char *
>(&S),
579 std::min(
sizeof(S),
static_cast<size_t>(S.Size)));
580 if (
sizeof(S) < S.Size)
600 COFFParser CP(Doc, ErrHandler);
602 ErrHandler(
"failed to parse YAML file");
607 ErrHandler(
"failed to layout optional header for COFF file");
612 ErrHandler(
"failed to layout COFF file");
619 ErrHandler(
"failed to write COFF file");
625 ErrHandler(
"the desired output size is greater than permitted. Use the "
626 "--max-size option to change the limit");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool layoutCOFF(COFFParser &CP)
static bool writeCOFF(COFFParser &CP, ContiguousBlobAccumulator &CBA)
static yaml::BinaryRef toDebugS(ArrayRef< CodeViewYAML::YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator)
static void writeLoadConfig(T &S, ContiguousBlobAccumulator &CBA)
static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header)
static bool layoutOptionalHeader(COFFParser &CP)
This file defines ContiguousBlobAccumulator, the size-limited output buffer shared by the yaml2obj em...
static size_t getStringIndex(StringRef Name)
static llvm::Error parse(GsymDataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
static LLVM_PACKED_END size_t getHeaderSize(uint16_t Version)
static bool is64Bit(const char *name)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Lightweight error class with error context and mandatory checking.
Helper for check-and-exit error handling.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
static LLVM_ABI raw_ostream & error()
Convenience method for printing "error: " to stderr.
bool hasChecksums() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Specialized YAMLIO scalar type for representing a binary blob.
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
LLVM_ABI void writeAsBinary(const BinaryRef &Bin, uint64_t N=UINT64_MAX)
void writeBlobToStream(raw_ostream &Out) const
void write(const char *Ptr, size_t Size)
uint64_t getOffset() const
void writeZeros(uint64_t Num)
@ IMAGE_FILE_MACHINE_UNKNOWN
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
@ IMAGE_SCN_CNT_INITIALIZED_DATA
@ IMAGE_SCN_LNK_NRELOC_OVFL
const int32_t MaxNumberOfSections16
static const char BigObjMagic[]
static const char PEMagic[]
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
LLVM_ABI void initializeStringsAndChecksums(ArrayRef< YAMLDebugSubsection > Sections, codeview::StringsAndChecksums &SC)
LLVM_ABI Expected< std::vector< std::shared_ptr< codeview::DebugSubsection > > > toCodeViewSubsectionList(BumpPtrAllocator &Allocator, ArrayRef< YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC)
LLVM_ABI ArrayRef< uint8_t > toDebugH(const DebugHSection &DebugH, BumpPtrAllocator &Alloc)
LLVM_ABI ArrayRef< uint8_t > toDebugT(ArrayRef< LeafRecord >, BumpPtrAllocator &Alloc, StringRef SectionName)
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, aligned > aligned_ulittle32_t
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
LLVM_ABI bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH, uint64_t MaxSize)
This is an optimization pass for GlobalISel generic memory operations.
std::string utostr(uint64_t X, bool isNeg=false)
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
OutputIt copy(R &&Range, OutputIt Out)
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
void consumeError(Error Err)
Consume a Error without doing anything.
std::optional< PEHeader > OptionalHeader
std::vector< Section > Sections
std::vector< Symbol > Symbols
std::optional< object::coff_load_configuration64 > LoadConfig64
std::optional< object::coff_load_configuration32 > LoadConfig32
LLVM_ABI void writeAsBinary(yaml::ContiguousBlobAccumulator &CBA) const
std::optional< uint32_t > UInt32
LLVM_ABI size_t size() const
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
std::vector< SectionDataEntry > StructuredData
std::vector< CodeViewYAML::LeafRecord > DebugT
yaml::BinaryRef SectionData
std::optional< CodeViewYAML::DebugHSection > DebugH
std::vector< CodeViewYAML::LeafRecord > DebugP
std::vector< Relocation > Relocations
COFF::SymbolComplexType ComplexType
COFF::SymbolBaseType SimpleType
uint32_t PointerToRelocations
uint16_t NumberOfLineNumbers
uint32_t PointerToRawData
uint16_t NumberOfRelocations
uint32_t PointerToLineNumbers
uint8_t NumberOfAuxSymbols
Common declarations for yaml2obj.