38 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
44 bool useBigObj()
const {
45 return static_cast<int32_t
>(Obj.
Sections.size()) >
56 unsigned getHeaderSize()
const {
60 unsigned getSymbolSize()
const {
64 bool parseSections() {
75 std::string str = utostr(
Index);
77 ErrHandler(
"string table got too large");
81 std::copy(str.begin(), str.end(), Sec.
Header.
Name + 1);
86 ErrHandler(
"section alignment is too large");
90 ErrHandler(
"section alignment is not a power of 2");
105 std::copy(
Name.begin(),
Name.end(),
Sym.Header.Name);
113 Sym.Header.Type =
Sym.SimpleType;
120 if (!parseSections())
129 if (i == StringTableMap.end()) {
133 StringTableMap[Str] =
Index;
151enum { DOSStubSize = 128 };
162 CP.Obj.Header.SizeOfOptionalHeader =
164 CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
171 using namespace codeview;
172 ExitOnError Err(
"Error occurred writing .debug$S section");
176 std::vector<DebugSubsectionRecordBuilder> Builders;
178 for (
auto &SS : CVSS) {
179 DebugSubsectionRecordBuilder
B(SS);
180 Size +=
B.calculateSerializedLength();
181 Builders.push_back(std::move(
B));
188 for (
const auto &
B : Builders) {
189 Err(
B.commit(Writer, CodeViewContainer::ObjectFile));
199 CP.SectionTableStart =
200 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
206 CP.SectionTableStart + CP.SectionTableSize;
213 CP.StringsAndChecksums);
214 if (CP.StringsAndChecksums.hasChecksums() &&
215 CP.StringsAndChecksums.hasStrings())
222 if (S.
Name ==
".debug$S") {
224 assert(CP.StringsAndChecksums.hasStrings() &&
225 "Object file does not have debug string table!");
230 }
else if (S.
Name ==
".debug$T") {
233 }
else if (S.
Name ==
".debug$P") {
236 }
else if (S.
Name ==
".debug$H") {
243 DataSize += E.
size();
245 CurrentSectionDataOffset =
alignTo(CurrentSectionDataOffset,
246 CP.isPE() ? CP.getFileAlignment() : 4);
269 uint32_t SymbolTableStart = CurrentSectionDataOffset;
273 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
274 e = CP.Obj.Symbols.end();
277 if (i->FunctionDefinition)
278 NumberOfAuxSymbols += 1;
279 if (i->bfAndefSymbol)
280 NumberOfAuxSymbols += 1;
282 NumberOfAuxSymbols += 1;
283 if (!i->File.empty())
284 NumberOfAuxSymbols +=
285 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
286 if (i->SectionDefinition)
287 NumberOfAuxSymbols += 1;
289 NumberOfAuxSymbols += 1;
290 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
291 NumberOfSymbols += 1 + NumberOfAuxSymbols;
295 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
296 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
297 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
298 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
300 CP.Obj.Header.PointerToSymbolTable = 0;
303 CP.StringTable.size();
313template <
typename value_type>
316 char Buffer[
sizeof(BLE.
Value)];
317 support::endian::write<value_type, llvm::endianness::little>(Buffer,
323template <
typename value_type>
330template <
size_t NumBytes>
332 char Buffer[NumBytes];
333 memset(Buffer, 0,
sizeof(Buffer));
334 OS.
write(Buffer,
sizeof(Buffer));
345 memset(Header, 0,
sizeof(*Header));
346 Header->Magic = Magic;
347 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
348 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
349 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
350 SizeOfUninitializedData = 0;
351 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
352 Header->FileAlignment);
353 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
362 if (S.
Name ==
".text")
364 else if (S.
Name ==
".data")
369 Header->SizeOfCode = SizeOfCode;
370 Header->SizeOfInitializedData = SizeOfInitializedData;
371 Header->SizeOfUninitializedData = SizeOfUninitializedData;
372 Header->AddressOfEntryPoint =
373 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint;
374 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
375 Header->MajorOperatingSystemVersion =
376 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
377 Header->MinorOperatingSystemVersion =
378 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
379 Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion;
380 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion;
381 Header->MajorSubsystemVersion =
382 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
383 Header->MinorSubsystemVersion =
384 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
385 Header->SizeOfImage = SizeOfImage;
386 Header->SizeOfHeaders = SizeOfHeaders;
387 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
388 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
389 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
390 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
391 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
392 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
393 Header->NumberOfRvaAndSize = CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
401 memset(&DH, 0,
sizeof(DH));
414 OS.
write(
reinterpret_cast<char *
>(&DH),
sizeof(DH));
421 if (CP.useBigObj()) {
427 <<
binary_le(CP.Obj.Header.TimeDateStamp);
431 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
432 <<
binary_le(CP.Obj.Header.NumberOfSymbols);
435 <<
binary_le(
static_cast<int16_t
>(CP.Obj.Header.NumberOfSections))
436 <<
binary_le(CP.Obj.Header.TimeDateStamp)
437 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
438 <<
binary_le(CP.Obj.Header.NumberOfSymbols)
439 <<
binary_le(CP.Obj.Header.SizeOfOptionalHeader)
440 <<
binary_le(CP.Obj.Header.Characteristics);
446 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
452 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
454 for (
uint32_t I = 0;
I < CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
456 const std::optional<COFF::DataDirectory> *DataDirectories =
457 CP.Obj.OptionalHeader->DataDirectories;
458 uint32_t NumDataDir = std::size(CP.Obj.OptionalHeader->DataDirectories);
459 if (
I >= NumDataDir || !DataDirectories[
I]) {
463 OS <<
binary_le(DataDirectories[
I]->RelativeVirtualAddress);
483 assert(
OS.
tell() == CP.SectionTableStart + CP.SectionTableSize);
485 unsigned CurSymbol = 0;
488 SymbolTableIndexMap[
Sym.Name] = CurSymbol;
489 CurSymbol += 1 +
Sym.Header.NumberOfAuxSymbols;
505 OS << binary_le<uint32_t>( S.
Relocations.size() + 1)
506 << binary_le<uint32_t>( 0)
507 << binary_le<uint16_t>( 0);
510 if (R.SymbolTableIndex) {
511 if (!R.SymbolName.empty())
513 <<
"Both SymbolName and SymbolTableIndex specified\n";
514 SymbolTableIndex = *R.SymbolTableIndex;
516 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
525 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
526 e = CP.Obj.Symbols.end();
533 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
535 <<
binary_le(i->Header.NumberOfAuxSymbols);
537 if (i->FunctionDefinition) {
539 <<
binary_le(i->FunctionDefinition->TotalSize)
540 <<
binary_le(i->FunctionDefinition->PointerToLinenumber)
541 <<
binary_le(i->FunctionDefinition->PointerToNextFunction)
542 <<
zeros(i->FunctionDefinition->unused);
545 if (i->bfAndefSymbol) {
546 OS <<
zeros(i->bfAndefSymbol->unused1)
547 <<
binary_le(i->bfAndefSymbol->Linenumber)
548 <<
zeros(i->bfAndefSymbol->unused2)
549 <<
binary_le(i->bfAndefSymbol->PointerToNextFunction)
550 <<
zeros(i->bfAndefSymbol->unused3);
553 if (i->WeakExternal) {
555 <<
binary_le(i->WeakExternal->Characteristics)
556 <<
zeros(i->WeakExternal->unused);
559 if (!i->File.empty()) {
560 unsigned SymbolSize = CP.getSymbolSize();
562 (i->File.size() + SymbolSize - 1) / SymbolSize;
563 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
564 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
565 OS.
write(i->File.data(), i->File.size());
568 if (i->SectionDefinition) {
570 <<
binary_le(i->SectionDefinition->NumberOfRelocations)
571 <<
binary_le(i->SectionDefinition->NumberOfLinenumbers)
572 <<
binary_le(i->SectionDefinition->CheckSum)
573 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number))
574 <<
binary_le(i->SectionDefinition->Selection)
575 <<
zeros(i->SectionDefinition->unused)
576 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16));
581 <<
binary_le(i->CLRToken->SymbolTableIndex)
582 <<
zeros(i->CLRToken->unused2);
588 if (CP.Obj.Header.PointerToSymbolTable)
589 OS.
write(&CP.StringTable[0], CP.StringTable.size());
596 Size +=
sizeof(*UInt32);
605 OS.
write(
reinterpret_cast<const char *
>(&S),
606 std::min(
sizeof(S),
static_cast<size_t>(S.Size)));
607 if (
sizeof(S) < S.Size)
614 Binary.writeAsBinary(
OS);
626 COFFParser CP(Doc, ErrHandler);
628 ErrHandler(
"failed to parse YAML file");
633 ErrHandler(
"failed to layout optional header for COFF file");
638 ErrHandler(
"failed to layout COFF file");
642 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 void writeLoadConfig(T &S, raw_ostream &OS)
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),...
size_t size() const
size - Get the array size.
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.
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_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))
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::optional< object::coff_load_configuration64 > LoadConfig64
std::optional< object::coff_load_configuration32 > LoadConfig32
void writeAsBinary(raw_ostream &OS) const
std::optional< uint32_t > UInt32
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
uint32_t PointerToRelocations
uint16_t NumberOfLineNumbers
uint32_t PointerToRawData
uint16_t NumberOfRelocations
uint32_t PointerToLineNumbers
Common declarations for yaml2obj.