35 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
41 bool useBigObj()
const {
42 return static_cast<int32_t
>(Obj.
Sections.size()) >
53 unsigned getHeaderSize()
const {
57 unsigned getSymbolSize()
const {
61 bool parseSections() {
72 std::string str = utostr(Index);
74 ErrHandler(
"string table got too large");
78 std::copy(str.begin(), str.end(), Sec.
Header.
Name + 1);
83 ErrHandler(
"section alignment is too large");
87 ErrHandler(
"section alignment is not a power of 2");
102 std::copy(
Name.begin(),
Name.end(),
Sym.Header.Name);
110 Sym.Header.Type =
Sym.SimpleType;
117 if (!parseSections())
145enum { DOSStubSize = 128 };
156 CP.Obj.Header.SizeOfOptionalHeader =
158 CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
165 using namespace codeview;
166 ExitOnError Err(
"Error occurred writing .debug$S section");
170 std::vector<DebugSubsectionRecordBuilder> Builders;
172 for (
auto &SS : CVSS) {
173 DebugSubsectionRecordBuilder
B(SS);
174 Size +=
B.calculateSerializedLength();
175 Builders.push_back(std::move(
B));
182 for (
const auto &
B : Builders) {
183 Err(
B.commit(Writer, CodeViewContainer::ObjectFile));
193 CP.SectionTableStart =
194 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
200 CP.SectionTableStart + CP.SectionTableSize;
207 CP.StringsAndChecksums);
208 if (CP.StringsAndChecksums.hasChecksums() &&
209 CP.StringsAndChecksums.hasStrings())
216 if (S.
Name ==
".debug$S") {
218 assert(CP.StringsAndChecksums.hasStrings() &&
219 "Object file does not have debug string table!");
224 }
else if (S.
Name ==
".debug$T") {
227 }
else if (S.
Name ==
".debug$P") {
230 }
else if (S.
Name ==
".debug$H") {
237 DataSize += E.
size();
239 CurrentSectionDataOffset =
alignTo(CurrentSectionDataOffset,
240 CP.isPE() ? CP.getFileAlignment() : 4);
263 uint32_t SymbolTableStart = CurrentSectionDataOffset;
267 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
268 e = CP.Obj.Symbols.end();
271 if (i->FunctionDefinition)
272 NumberOfAuxSymbols += 1;
273 if (i->bfAndefSymbol)
274 NumberOfAuxSymbols += 1;
276 NumberOfAuxSymbols += 1;
277 if (!i->File.empty())
278 NumberOfAuxSymbols +=
279 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
280 if (i->SectionDefinition)
281 NumberOfAuxSymbols += 1;
283 NumberOfAuxSymbols += 1;
284 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
285 NumberOfSymbols += 1 + NumberOfAuxSymbols;
289 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
290 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
291 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
292 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
294 CP.Obj.Header.PointerToSymbolTable = 0;
297 CP.StringTable.size();
307template <
typename value_type>
310 char Buffer[
sizeof(BLE.
Value)];
311 support::endian::write<value_type, llvm::endianness::little>(Buffer,
317template <
typename value_type>
324template <
size_t NumBytes>
326 char Buffer[NumBytes];
327 memset(Buffer, 0,
sizeof(Buffer));
328 OS.
write(Buffer,
sizeof(Buffer));
339 memset(Header, 0,
sizeof(*Header));
340 Header->Magic = Magic;
341 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
342 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
343 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
344 SizeOfUninitializedData = 0;
345 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
346 Header->FileAlignment);
347 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
356 if (S.
Name ==
".text")
358 else if (S.
Name ==
".data")
363 Header->SizeOfCode = SizeOfCode;
364 Header->SizeOfInitializedData = SizeOfInitializedData;
365 Header->SizeOfUninitializedData = SizeOfUninitializedData;
366 Header->AddressOfEntryPoint =
367 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint;
368 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
369 Header->MajorOperatingSystemVersion =
370 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
371 Header->MinorOperatingSystemVersion =
372 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
373 Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion;
374 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion;
375 Header->MajorSubsystemVersion =
376 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
377 Header->MinorSubsystemVersion =
378 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
379 Header->SizeOfImage = SizeOfImage;
380 Header->SizeOfHeaders = SizeOfHeaders;
381 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
382 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
383 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
384 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
385 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
386 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
387 Header->NumberOfRvaAndSize = CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
395 memset(&DH, 0,
sizeof(DH));
408 OS.
write(
reinterpret_cast<char *
>(&DH),
sizeof(DH));
415 if (CP.useBigObj()) {
421 <<
binary_le(CP.Obj.Header.TimeDateStamp);
425 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
426 <<
binary_le(CP.Obj.Header.NumberOfSymbols);
429 <<
binary_le(
static_cast<int16_t
>(CP.Obj.Header.NumberOfSections))
430 <<
binary_le(CP.Obj.Header.TimeDateStamp)
431 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
432 <<
binary_le(CP.Obj.Header.NumberOfSymbols)
433 <<
binary_le(CP.Obj.Header.SizeOfOptionalHeader)
434 <<
binary_le(CP.Obj.Header.Characteristics);
440 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
446 OS.
write(
reinterpret_cast<char *
>(&PEH),
sizeof(PEH));
448 for (
uint32_t I = 0;
I < CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
450 const std::optional<COFF::DataDirectory> *DataDirectories =
451 CP.Obj.OptionalHeader->DataDirectories;
452 uint32_t NumDataDir = std::size(CP.Obj.OptionalHeader->DataDirectories);
453 if (
I >= NumDataDir || !DataDirectories[
I]) {
457 OS <<
binary_le(DataDirectories[
I]->RelativeVirtualAddress);
477 assert(
OS.
tell() == CP.SectionTableStart + CP.SectionTableSize);
479 unsigned CurSymbol = 0;
482 SymbolTableIndexMap[
Sym.Name] = CurSymbol;
483 CurSymbol += 1 +
Sym.Header.NumberOfAuxSymbols;
499 OS << binary_le<uint32_t>( S.
Relocations.size() + 1)
500 << binary_le<uint32_t>( 0)
501 << binary_le<uint16_t>( 0);
504 if (R.SymbolTableIndex) {
505 if (!R.SymbolName.empty())
507 <<
"Both SymbolName and SymbolTableIndex specified\n";
508 SymbolTableIndex = *R.SymbolTableIndex;
510 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
519 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
520 e = CP.Obj.Symbols.end();
527 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
529 <<
binary_le(i->Header.NumberOfAuxSymbols);
531 if (i->FunctionDefinition) {
533 <<
binary_le(i->FunctionDefinition->TotalSize)
534 <<
binary_le(i->FunctionDefinition->PointerToLinenumber)
535 <<
binary_le(i->FunctionDefinition->PointerToNextFunction)
536 <<
zeros(i->FunctionDefinition->unused);
539 if (i->bfAndefSymbol) {
540 OS <<
zeros(i->bfAndefSymbol->unused1)
541 <<
binary_le(i->bfAndefSymbol->Linenumber)
542 <<
zeros(i->bfAndefSymbol->unused2)
543 <<
binary_le(i->bfAndefSymbol->PointerToNextFunction)
544 <<
zeros(i->bfAndefSymbol->unused3);
547 if (i->WeakExternal) {
549 <<
binary_le(i->WeakExternal->Characteristics)
550 <<
zeros(i->WeakExternal->unused);
553 if (!i->File.empty()) {
554 unsigned SymbolSize = CP.getSymbolSize();
556 (i->File.size() + SymbolSize - 1) / SymbolSize;
557 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
558 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
559 OS.
write(i->File.data(), i->File.size());
562 if (i->SectionDefinition) {
564 <<
binary_le(i->SectionDefinition->NumberOfRelocations)
565 <<
binary_le(i->SectionDefinition->NumberOfLinenumbers)
566 <<
binary_le(i->SectionDefinition->CheckSum)
567 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number))
568 <<
binary_le(i->SectionDefinition->Selection)
569 <<
zeros(i->SectionDefinition->unused)
570 <<
binary_le(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16));
575 <<
binary_le(i->CLRToken->SymbolTableIndex)
576 <<
zeros(i->CLRToken->unused2);
582 if (CP.Obj.Header.PointerToSymbolTable)
583 OS.
write(&CP.StringTable[0], CP.StringTable.size());
590 Size +=
sizeof(*UInt32);
599 OS.
write(
reinterpret_cast<const char *
>(&S),
600 std::min(
sizeof(S),
static_cast<size_t>(S.Size)));
601 if (
sizeof(S) < S.Size)
608 Binary.writeAsBinary(
OS);
620 COFFParser CP(Doc, ErrHandler);
622 ErrHandler(
"failed to parse YAML file");
627 ErrHandler(
"failed to layout optional header for COFF file");
632 ErrHandler(
"failed to layout COFF file");
636 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.
A table of densely packed, null-terminated strings indexed by offset.
constexpr size_t size() const
Returns the byte size of the table.
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.