39 : Obj(Obj), 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;
57 unsigned getSymbolSize()
const {
61 bool parseSections() {
62 for (COFFYAML::Section &Sec : Obj.Sections) {
72 std::string str =
utostr(Index);
74 ErrHandler(
"string table got too large");
83 ErrHandler(
"section alignment is too large");
87 ErrHandler(
"section alignment is not a power of 2");
97 for (COFFYAML::Symbol &Sym : Obj.Symbols) {
117 if (!parseSections())
125 auto [It,
Inserted] = StringTableMap.try_emplace(Str, StringTable.size());
127 StringTable.append(Str.begin(), Str.end());
128 StringTable.push_back(0);
133 COFFYAML::Object &Obj;
135 codeview::StringsAndChecksums StringsAndChecksums;
137 StringMap<unsigned> StringTableMap;
138 std::string StringTable;
139 uint32_t SectionTableStart;
140 uint32_t SectionTableSize;
145enum { DOSStubSize = 128 };
153 ExitOnError Err(
"Error occurred writing .debug$S section");
157 std::vector<DebugSubsectionRecordBuilder> Builders;
159 for (
auto &SS : CVSS) {
160 DebugSubsectionRecordBuilder
B(SS);
161 Size +=
B.calculateSerializedLength();
162 Builders.push_back(std::move(
B));
169 for (
const auto &
B : Builders) {
170 Err(
B.commit(Writer, CodeViewContainer::ObjectFile));
180 if (S.
Name ==
".debug$S") {
182 "Object file does not have debug string table!");
184 }
else if (S.
Name ==
".debug$T") {
186 }
else if (S.
Name ==
".debug$P") {
188 }
else if (S.
Name ==
".debug$H" && S.
DebugH) {
195 HasContent |=
E.size() != 0;
207 E.writeAsBinary(CBA);
218 memset(Header, 0,
sizeof(*Header));
219 Header->Magic = Magic;
220 Header->SectionAlignment = CP.Obj.
OptionalHeader->Header.SectionAlignment;
221 Header->FileAlignment = CP.Obj.
OptionalHeader->Header.FileAlignment;
222 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
223 SizeOfUninitializedData = 0;
224 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
225 Header->FileAlignment);
226 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
235 if (S.
Name ==
".text")
237 else if (S.
Name ==
".data")
242 Header->SizeOfCode = SizeOfCode;
243 Header->SizeOfInitializedData = SizeOfInitializedData;
244 Header->SizeOfUninitializedData = SizeOfUninitializedData;
245 Header->AddressOfEntryPoint =
248 Header->MajorOperatingSystemVersion =
250 Header->MinorOperatingSystemVersion =
252 Header->MajorImageVersion = CP.Obj.
OptionalHeader->Header.MajorImageVersion;
253 Header->MinorImageVersion = CP.Obj.
OptionalHeader->Header.MinorImageVersion;
254 Header->MajorSubsystemVersion =
256 Header->MinorSubsystemVersion =
258 Header->SizeOfImage = SizeOfImage;
259 Header->SizeOfHeaders = SizeOfHeaders;
261 Header->DLLCharacteristics = CP.Obj.
OptionalHeader->Header.DLLCharacteristics;
262 Header->SizeOfStackReserve = CP.Obj.
OptionalHeader->Header.SizeOfStackReserve;
263 Header->SizeOfStackCommit = CP.Obj.
OptionalHeader->Header.SizeOfStackCommit;
264 Header->SizeOfHeapReserve = CP.Obj.
OptionalHeader->Header.SizeOfHeapReserve;
265 Header->SizeOfHeapCommit = CP.Obj.
OptionalHeader->Header.SizeOfHeapCommit;
266 Header->NumberOfRvaAndSize = CP.Obj.
OptionalHeader->Header.NumberOfRvaAndSize;
276 NumberOfAuxSymbols += 1;
278 NumberOfAuxSymbols += 1;
280 NumberOfAuxSymbols += 1;
282 NumberOfAuxSymbols +=
283 (Sym.
File.
size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
285 NumberOfAuxSymbols += 1;
287 NumberOfAuxSymbols += 1;
302 uint64_t PointerToSymbolTableOffset;
307 memset(&DH, 0,
sizeof(DH));
320 CBA.
write(
reinterpret_cast<const char *
>(&DH),
sizeof(DH));
327 if (CP.useBigObj()) {
338 PointerToSymbolTableOffset = CBA.
getOffset();
347 PointerToSymbolTableOffset = CBA.
getOffset();
363 const std::optional<COFF::DataDirectory> *DataDirectories =
366 if (
I >= NumDataDir || !DataDirectories[
I]) {
369 CBA.
write(DataDirectories[
I]->RelativeVirtualAddress, LittleEndian);
370 CBA.
write(DataDirectories[
I]->
Size, LittleEndian);
381 unsigned CurSymbol = 0;
384 SymbolTableIndexMap[Sym.
Name] = CurSymbol;
394 CP.StringsAndChecksums);
420 if (R.SymbolTableIndex) {
421 if (!R.SymbolName.empty())
423 <<
"Both SymbolName and SymbolTableIndex specified\n";
424 SymbolTableIndex = *R.SymbolTableIndex;
426 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
428 CBA.
write(R.VirtualAddress, LittleEndian);
429 CBA.
write(SymbolTableIndex, LittleEndian);
430 CBA.
write(R.Type, LittleEndian);
439 CBA.
updateDataAt(OptionalHeaderOffset, &PEH,
sizeof(PEH));
445 CBA.
updateDataAt(OptionalHeaderOffset, &PEH,
sizeof(PEH));
451 "unexpected COFF section header size");
452 uint64_t SectionHeaderOffset = CP.SectionTableStart;
465 CBA.
updateDataAt(SectionHeaderOffset, &Header,
sizeof(Header));
466 SectionHeaderOffset +=
sizeof(Header);
478 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.
Symbols.begin(),
482 CBA.
write(i->Header.Value, LittleEndian);
484 CBA.
write(i->Header.SectionNumber, LittleEndian);
486 CBA.
write(
static_cast<int16_t
>(i->Header.SectionNumber), LittleEndian);
487 CBA.
write(i->Header.Type, LittleEndian);
488 CBA.
write(i->Header.StorageClass, LittleEndian);
489 CBA.
write(i->Header.NumberOfAuxSymbols, LittleEndian);
491 if (i->FunctionDefinition) {
492 CBA.
write(i->FunctionDefinition->TagIndex, LittleEndian);
493 CBA.
write(i->FunctionDefinition->TotalSize, LittleEndian);
494 CBA.
write(i->FunctionDefinition->PointerToLinenumber, LittleEndian);
495 CBA.
write(i->FunctionDefinition->PointerToNextFunction, LittleEndian);
496 CBA.
writeZeros(
sizeof(i->FunctionDefinition->unused));
499 if (i->bfAndefSymbol) {
500 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused1));
501 CBA.
write(i->bfAndefSymbol->Linenumber, LittleEndian);
502 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused2));
503 CBA.
write(i->bfAndefSymbol->PointerToNextFunction, LittleEndian);
504 CBA.
writeZeros(
sizeof(i->bfAndefSymbol->unused3));
507 if (i->WeakExternal) {
508 CBA.
write(i->WeakExternal->TagIndex, LittleEndian);
509 CBA.
write(i->WeakExternal->Characteristics, LittleEndian);
510 CBA.
writeZeros(
sizeof(i->WeakExternal->unused));
513 if (!i->File.empty()) {
514 unsigned SymbolSize = CP.getSymbolSize();
516 (i->File.size() + SymbolSize - 1) / SymbolSize;
517 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
518 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
519 CBA.
write(i->File.data(), i->File.size());
522 if (i->SectionDefinition) {
523 CBA.
write(i->SectionDefinition->Length, LittleEndian);
524 CBA.
write(i->SectionDefinition->NumberOfRelocations, LittleEndian);
525 CBA.
write(i->SectionDefinition->NumberOfLinenumbers, LittleEndian);
526 CBA.
write(i->SectionDefinition->CheckSum, LittleEndian);
527 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number),
529 CBA.
write(i->SectionDefinition->Selection, LittleEndian);
530 CBA.
writeZeros(
sizeof(i->SectionDefinition->unused));
531 CBA.
write(
static_cast<int16_t
>(i->SectionDefinition->Number >> 16),
536 CBA.
write(i->CLRToken->AuxType, LittleEndian);
538 CBA.
write(i->CLRToken->SymbolTableIndex, LittleEndian);
547 CP.StringTable.size();
548 CBA.
write(CP.StringTable.data(), CP.StringTable.size());
556 Size +=
sizeof(*UInt32);
566 CBA.
write(
reinterpret_cast<const char *
>(&S),
567 std::min(
sizeof(S),
static_cast<size_t>(S.Size)));
568 if (
sizeof(S) < S.Size)
588 COFFParser CP(Doc, ErrHandler);
590 ErrHandler(
"failed to parse YAML file");
597 ErrHandler(
"failed to write COFF file");
603 ErrHandler(
"the desired output size is greater than permitted. Use the "
604 "--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 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 writeSectionContent(COFFParser &CP, COFFYAML::Section &S, ContiguousBlobAccumulator &CBA)
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 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 uint64_t padToAlignment(unsigned Align)
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.