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);
268 CP.StringTable.size();
276 memset(Header, 0,
sizeof(*Header));
277 Header->Magic = Magic;
278 Header->SectionAlignment = CP.Obj.
OptionalHeader->Header.SectionAlignment;
279 Header->FileAlignment = CP.Obj.
OptionalHeader->Header.FileAlignment;
280 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
281 SizeOfUninitializedData = 0;
282 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
283 Header->FileAlignment);
284 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
293 if (S.
Name ==
".text")
295 else if (S.
Name ==
".data")
300 Header->SizeOfCode = SizeOfCode;
301 Header->SizeOfInitializedData = SizeOfInitializedData;
302 Header->SizeOfUninitializedData = SizeOfUninitializedData;
303 Header->AddressOfEntryPoint =
306 Header->MajorOperatingSystemVersion =
308 Header->MinorOperatingSystemVersion =
310 Header->MajorImageVersion = CP.Obj.
OptionalHeader->Header.MajorImageVersion;
311 Header->MinorImageVersion = CP.Obj.
OptionalHeader->Header.MinorImageVersion;
312 Header->MajorSubsystemVersion =
314 Header->MinorSubsystemVersion =
316 Header->SizeOfImage = SizeOfImage;
317 Header->SizeOfHeaders = SizeOfHeaders;
319 Header->DLLCharacteristics = CP.Obj.
OptionalHeader->Header.DLLCharacteristics;
320 Header->SizeOfStackReserve = CP.Obj.
OptionalHeader->Header.SizeOfStackReserve;
321 Header->SizeOfStackCommit = CP.Obj.
OptionalHeader->Header.SizeOfStackCommit;
322 Header->SizeOfHeapReserve = CP.Obj.
OptionalHeader->Header.SizeOfHeapReserve;
323 Header->SizeOfHeapCommit = CP.Obj.
OptionalHeader->Header.SizeOfHeapCommit;
324 Header->NumberOfRvaAndSize = CP.Obj.
OptionalHeader->Header.NumberOfRvaAndSize;
334 NumberOfAuxSymbols += 1;
336 NumberOfAuxSymbols += 1;
338 NumberOfAuxSymbols += 1;
340 NumberOfAuxSymbols +=
341 (Sym.
File.
size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
343 NumberOfAuxSymbols += 1;
345 NumberOfAuxSymbols += 1;
353 uint64_t PointerToSymbolTableOffset;
358 memset(&DH, 0,
sizeof(DH));
371 CBA.
write(
reinterpret_cast<const char *
>(&DH),
sizeof(DH));
378 if (CP.useBigObj()) {
389 PointerToSymbolTableOffset = CBA.
getOffset();
398 PointerToSymbolTableOffset = CBA.
getOffset();
409 CBA.
write(
reinterpret_cast<const char *
>(&PEH),
sizeof(PEH));
415 CBA.
write(
reinterpret_cast<const char *
>(&PEH),
sizeof(PEH));
419 const std::optional<COFF::DataDirectory> *DataDirectories =
422 if (
I >= NumDataDir || !DataDirectories[
I]) {
425 CBA.
write(DataDirectories[
I]->RelativeVirtualAddress, LittleEndian);
426 CBA.
write(DataDirectories[
I]->
Size, LittleEndian);
447 unsigned CurSymbol = 0;
450 SymbolTableIndexMap[Sym.
Name] = CurSymbol;
461 E.writeAsBinary(CBA);
475 if (R.SymbolTableIndex) {
476 if (!R.SymbolName.empty())
478 <<
"Both SymbolName and SymbolTableIndex specified\n";
479 SymbolTableIndex = *R.SymbolTableIndex;
481 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
483 CBA.
write(R.VirtualAddress, LittleEndian);
484 CBA.
write(SymbolTableIndex, LittleEndian);
485 CBA.
write(R.Type, LittleEndian);
498 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.
Symbols.begin(),
502 CBA.
write(i->Header.Value, LittleEndian);
504 CBA.
write(i->Header.SectionNumber, LittleEndian);
506 CBA.
write(
static_cast<int16_t
>(i->Header.SectionNumber), LittleEndian);
507 CBA.
write(i->Header.Type, LittleEndian);
508 CBA.
write(i->Header.StorageClass, LittleEndian);
509 CBA.
write(i->Header.NumberOfAuxSymbols, LittleEndian);
511 if (i->FunctionDefinition) {
512 CBA.
write(i->FunctionDefinition->TagIndex, LittleEndian);
513 CBA.
write(i->FunctionDefinition->TotalSize, LittleEndian);
514 CBA.
write(i->FunctionDefinition->PointerToLinenumber, LittleEndian);
515 CBA.
write(i->FunctionDefinition->PointerToNextFunction, LittleEndian);
516 CBA.
writeZeros(
sizeof(i->FunctionDefinition->unused));
519 if (i->bfAndefSymbol) {
520 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused1));
521 CBA.
write(i->bfAndefSymbol->Linenumber, LittleEndian);
522 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused2));
523 CBA.
write(i->bfAndefSymbol->PointerToNextFunction, LittleEndian);
524 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused3));
527 if (i->WeakExternal) {
528 CBA.
write(i->WeakExternal->TagIndex, LittleEndian);
529 CBA.
write(i->WeakExternal->Characteristics, LittleEndian);
530 CBA.
writeZeros(
sizeof(i->WeakExternal->unused));
533 if (!i->File.empty()) {
534 unsigned SymbolSize = CP.getSymbolSize();
536 (i->File.size() + SymbolSize - 1) / SymbolSize;
537 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
538 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
539 CBA.
write(i->File.data(), i->File.size());
542 if (i->SectionDefinition) {
543 CBA.
write(i->SectionDefinition->Length, LittleEndian);
544 CBA.
write(i->SectionDefinition->NumberOfRelocations, LittleEndian);
545 CBA.
write(i->SectionDefinition->NumberOfLinenumbers, LittleEndian);
546 CBA.
write(i->SectionDefinition->CheckSum, LittleEndian);
547 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number),
549 CBA.
write(i->SectionDefinition->Selection, LittleEndian);
550 CBA.
writeZeros(
sizeof(i->SectionDefinition->unused));
551 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16),
556 CBA.
write(i->CLRToken->AuxType, LittleEndian);
558 CBA.
write(i->CLRToken->SymbolTableIndex, LittleEndian);
566 CBA.
write(CP.StringTable.data(), CP.StringTable.size());
573 Size +=
sizeof(*UInt32);
583 CBA.
write(
reinterpret_cast<const char *
>(&S),
584 std::min(
sizeof(S),
static_cast<size_t>(S.Size)));
585 if (
sizeof(S) < S.Size)
605 COFFParser CP(Doc, ErrHandler);
607 ErrHandler(
"failed to parse YAML file");
612 ErrHandler(
"failed to layout optional header for COFF file");
617 ErrHandler(
"failed to layout COFF file");
624 ErrHandler(
"failed to write COFF file");
630 ErrHandler(
"the desired output size is greater than permitted. Use the "
631 "--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",...
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
Get the string size.
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)
void updateDataAt(uint64_t Pos, T Val, llvm::endianness E)
@ 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
std::optional< COFF::AuxiliaryWeakExternal > WeakExternal
std::optional< COFF::AuxiliarybfAndefSymbol > bfAndefSymbol
COFF::SymbolComplexType ComplexType
std::optional< COFF::AuxiliarySectionDefinition > SectionDefinition
std::optional< COFF::AuxiliaryFunctionDefinition > FunctionDefinition
COFF::SymbolBaseType SimpleType
std::optional< COFF::AuxiliaryCLRToken > CLRToken
uint32_t PointerToRelocations
uint16_t NumberOfLineNumbers
uint32_t PointerToRawData
uint16_t NumberOfRelocations
uint32_t PointerToLineNumbers
uint8_t NumberOfAuxSymbols
Common declarations for yaml2obj.