39 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
45 bool useBigObj()
const {
46 return static_cast<int32_t
>(Obj.
Sections.size()) >
61 unsigned getHeaderSize()
const {
65 unsigned getSymbolSize()
const {
69 bool parseSections() {
80 std::string str = utostr(
Index);
82 ErrHandler(
"string table got too large");
86 std::copy(str.begin(), str.end(), Sec.
Header.
Name + 1);
91 ErrHandler(
"section alignment is too large");
95 ErrHandler(
"section alignment is not a power of 2");
104 bool parseSymbols() {
125 if (!parseSections())
134 if (i == StringTableMap.end()) {
138 StringTableMap[Str] =
Index;
156enum { DOSStubSize = 128 };
167 CP.Obj.Header.SizeOfOptionalHeader =
169 CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
176 using namespace codeview;
177 ExitOnError Err(
"Error occurred writing .debug$S section");
181 std::vector<DebugSubsectionRecordBuilder> Builders;
183 for (
auto &SS : CVSS) {
184 DebugSubsectionRecordBuilder
B(SS);
185 Size +=
B.calculateSerializedLength();
186 Builders.push_back(std::move(
B));
193 for (
const auto &
B : Builders) {
194 Err(
B.commit(Writer, CodeViewContainer::ObjectFile));
204 CP.SectionTableStart =
205 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
211 CP.SectionTableStart + CP.SectionTableSize;
218 CP.StringsAndChecksums);
219 if (CP.StringsAndChecksums.hasChecksums() &&
220 CP.StringsAndChecksums.hasStrings())
227 if (S.
Name ==
".debug$S") {
229 assert(CP.StringsAndChecksums.hasStrings() &&
230 "Object file does not have debug string table!");
235 }
else if (S.
Name ==
".debug$T") {
238 }
else if (S.
Name ==
".debug$P") {
241 }
else if (S.
Name ==
".debug$H") {
247 CurrentSectionDataOffset =
alignTo(CurrentSectionDataOffset,
248 CP.isPE() ? CP.getFileAlignment() : 4);
271 uint32_t SymbolTableStart = CurrentSectionDataOffset;
275 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
276 e = CP.Obj.Symbols.end();
279 if (i->FunctionDefinition)
280 NumberOfAuxSymbols += 1;
281 if (i->bfAndefSymbol)
282 NumberOfAuxSymbols += 1;
284 NumberOfAuxSymbols += 1;
285 if (!i->File.empty())
286 NumberOfAuxSymbols +=
287 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
288 if (i->SectionDefinition)
289 NumberOfAuxSymbols += 1;
291 NumberOfAuxSymbols += 1;
292 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
293 NumberOfSymbols += 1 + NumberOfAuxSymbols;
297 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
298 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
299 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
300 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
302 CP.Obj.Header.PointerToSymbolTable = 0;
305 CP.StringTable.size();
315template <
typename value_type>
318 char Buffer[
sizeof(BLE.
Value)];
319 support::endian::write<value_type, support::little, support::unaligned>(
325template <
typename value_type>
332template <
size_t NumBytes>
334 char Buffer[NumBytes];
335 memset(Buffer, 0,
sizeof(Buffer));
336 OS.
write(Buffer,
sizeof(Buffer));
347 memset(Header, 0,
sizeof(*Header));
348 Header->Magic = Magic;
349 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
350 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
351 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
352 SizeOfUninitializedData = 0;
353 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
354 Header->FileAlignment);
355 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
371 Header->SizeOfCode = SizeOfCode;
372 Header->SizeOfInitializedData = SizeOfInitializedData;
373 Header->SizeOfUninitializedData = SizeOfUninitializedData;
374 Header->AddressOfEntryPoint =
375 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint;
376 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
377 Header->MajorOperatingSystemVersion =
378 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
379 Header->MinorOperatingSystemVersion =
380 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
381 Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion;
382 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion;
383 Header->MajorSubsystemVersion =
384 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
385 Header->MinorSubsystemVersion =
386 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
387 Header->SizeOfImage = SizeOfImage;
388 Header->SizeOfHeaders = SizeOfHeaders;
389 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
390 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
391 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
392 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
393 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
394 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
395 Header->NumberOfRvaAndSize = CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
403 memset(&DH, 0,
sizeof(DH));
416 OS.
write(
reinterpret_cast<char *
>(&DH),
sizeof(DH));
423 if (CP.useBigObj()) {
429 <<
binary_le(CP.Obj.Header.TimeDateStamp);
433 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
434 <<
binary_le(CP.Obj.Header.NumberOfSymbols);
437 <<
binary_le(
static_cast<int16_t
>(CP.Obj.Header.NumberOfSections))
438 <<
binary_le(CP.Obj.Header.TimeDateStamp)
439 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
440 <<
binary_le(CP.Obj.Header.NumberOfSymbols)
441 <<
binary_le(CP.Obj.Header.SizeOfOptionalHeader)
442 <<
binary_le(CP.Obj.Header.Characteristics);
448 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
454 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
456 for (
uint32_t I = 0;
I < CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
458 const std::optional<COFF::DataDirectory> *DataDirectories =
459 CP.Obj.OptionalHeader->DataDirectories;
460 uint32_t NumDataDir = std::size(CP.Obj.OptionalHeader->DataDirectories);
461 if (
I >= NumDataDir || !DataDirectories[
I]) {
465 OS <<
binary_le(DataDirectories[
I]->RelativeVirtualAddress);
485 assert(
OS.
tell() == CP.SectionTableStart + CP.SectionTableSize);
487 unsigned CurSymbol = 0;
490 SymbolTableIndexMap[Sym.
Name] = CurSymbol;
504 OS << binary_le<uint32_t>( S.
Relocations.size() + 1)
505 << binary_le<uint32_t>( 0)
506 << binary_le<uint16_t>( 0);
509 if (R.SymbolTableIndex) {
510 if (!R.SymbolName.empty())
512 <<
"Both SymbolName and SymbolTableIndex specified\n";
513 SymbolTableIndex = *R.SymbolTableIndex;
515 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
524 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
525 e = CP.Obj.Symbols.end();
532 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
534 <<
binary_le(i->Header.NumberOfAuxSymbols);
536 if (i->FunctionDefinition) {
538 <<
binary_le(i->FunctionDefinition->TotalSize)
539 <<
binary_le(i->FunctionDefinition->PointerToLinenumber)
540 <<
binary_le(i->FunctionDefinition->PointerToNextFunction)
541 <<
zeros(i->FunctionDefinition->unused);
544 if (i->bfAndefSymbol) {
545 OS <<
zeros(i->bfAndefSymbol->unused1)
546 <<
binary_le(i->bfAndefSymbol->Linenumber)
547 <<
zeros(i->bfAndefSymbol->unused2)
548 <<
binary_le(i->bfAndefSymbol->PointerToNextFunction)
549 <<
zeros(i->bfAndefSymbol->unused3);
552 if (i->WeakExternal) {
554 <<
binary_le(i->WeakExternal->Characteristics)
555 <<
zeros(i->WeakExternal->unused);
558 if (!i->File.empty()) {
559 unsigned SymbolSize = CP.getSymbolSize();
561 (i->File.size() + SymbolSize - 1) / SymbolSize;
562 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
563 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
564 OS.
write(i->File.data(), i->File.size());
567 if (i->SectionDefinition) {
569 <<
binary_le(i->SectionDefinition->NumberOfRelocations)
570 <<
binary_le(i->SectionDefinition->NumberOfLinenumbers)
571 <<
binary_le(i->SectionDefinition->CheckSum)
572 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number))
573 <<
binary_le(i->SectionDefinition->Selection)
574 <<
zeros(i->SectionDefinition->unused)
575 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16));
580 <<
binary_le(i->CLRToken->SymbolTableIndex)
581 <<
zeros(i->CLRToken->unused2);
587 if (CP.Obj.Header.PointerToSymbolTable)
588 OS.
write(&CP.StringTable[0], CP.StringTable.size());
597 COFFParser CP(Doc, ErrHandler);
599 ErrHandler(
"failed to parse YAML file");
604 ErrHandler(
"failed to layout optional header for COFF file");
609 ErrHandler(
"failed to layout COFF file");
613 ErrHandler(
"failed to write COFF file");
This file defines the StringMap class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool layoutCOFF(COFFParser &CP)
binary_le_impl< value_type > binary_le(value_type V)
static yaml::BinaryRef toDebugS(ArrayRef< CodeViewYAML::YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator)
zeros_impl< sizeof(T)> zeros(const T &)
static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header)
raw_ostream & operator<<(raw_ostream &OS, const binary_le_impl< value_type > &BLE)
static bool writeCOFF(COFFParser &CP, raw_ostream &OS)
static bool layoutOptionalHeader(COFFParser &CP)
static size_t getStringIndex(StringRef Name)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool is64Bit(const char *name)
ArrayRef - 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.
Allocate memory in an ever growing pool, as if by bump-pointer.
Helper for check-and-exit error handling.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
LLVM Value Representation.
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
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.
void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
@ IMAGE_FILE_MACHINE_ARM64
@ IMAGE_FILE_MACHINE_UNKNOWN
@ IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_ARM64EC
@ 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))
void initializeStringsAndChecksums(ArrayRef< YAMLDebugSubsection > Sections, codeview::StringsAndChecksums &SC)
Expected< std::vector< std::shared_ptr< codeview::DebugSubsection > > > toCodeViewSubsectionList(BumpPtrAllocator &Allocator, ArrayRef< YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC)
ArrayRef< uint8_t > toDebugH(const DebugHSection &DebugH, BumpPtrAllocator &Alloc)
ArrayRef< uint8_t > toDebugT(ArrayRef< LeafRecord >, BumpPtrAllocator &Alloc, StringRef SectionName)
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
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.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
binary_le_impl(value_type V)
std::optional< PEHeader > OptionalHeader
std::vector< Section > Sections
std::vector< Symbol > Symbols
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
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.