38 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
44 bool useBigObj()
const {
45 return static_cast<int32_t
>(Obj.
Sections.size()) >
59 unsigned getHeaderSize()
const {
63 unsigned getSymbolSize()
const {
67 bool parseSections() {
68 for (std::vector<COFFYAML::Section>::iterator i = Obj.
Sections.begin(),
81 unsigned Index = getStringIndex(Name);
82 std::string str =
utostr(Index);
84 ErrHandler(
"string table got too large");
93 ErrHandler(
"section alignment is too large");
97 ErrHandler(
"section alignment is not a power of 2");
106 bool parseSymbols() {
107 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.
Symbols.begin(),
119 unsigned Index = getStringIndex(Name);
131 if (!parseSections())
140 if (i == StringTableMap.
end()) {
144 StringTableMap[Str] =
Index;
162 enum { DOSStubSize = 128 };
173 CP.Obj.Header.SizeOfOptionalHeader =
182 using namespace codeview;
183 ExitOnError Err(
"Error occurred writing .debug$S section");
187 std::vector<DebugSubsectionRecordBuilder> Builders;
189 for (
auto &
SS : CVSS) {
190 DebugSubsectionRecordBuilder
B(
SS, CodeViewContainer::ObjectFile);
191 Size += B.calculateSerializedLength();
192 Builders.push_back(std::move(B));
199 for (
const auto &
B : Builders) {
200 Err(
B.commit(Writer));
210 CP.SectionTableStart =
211 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
217 CP.SectionTableStart + CP.SectionTableSize;
224 CP.StringsAndChecksums);
225 if (CP.StringsAndChecksums.hasChecksums() &&
226 CP.StringsAndChecksums.hasStrings())
233 if (S.
Name ==
".debug$S") {
235 assert(CP.StringsAndChecksums.hasStrings() &&
236 "Object file does not have debug string table!");
241 }
else if (S.
Name ==
".debug$T") {
244 }
else if (S.
Name ==
".debug$P") {
247 }
else if (S.
Name ==
".debug$H") {
253 CurrentSectionDataOffset =
alignTo(CurrentSectionDataOffset,
254 CP.isPE() ? CP.getFileAlignment() : 4);
277 uint32_t SymbolTableStart = CurrentSectionDataOffset;
281 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
282 e = CP.Obj.Symbols.end();
285 if (i->FunctionDefinition)
286 NumberOfAuxSymbols += 1;
287 if (i->bfAndefSymbol)
288 NumberOfAuxSymbols += 1;
290 NumberOfAuxSymbols += 1;
291 if (!i->File.
empty())
292 NumberOfAuxSymbols +=
293 (i->File.
size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
294 if (i->SectionDefinition)
295 NumberOfAuxSymbols += 1;
297 NumberOfAuxSymbols += 1;
298 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
299 NumberOfSymbols += 1 + NumberOfAuxSymbols;
303 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
304 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
305 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
306 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
308 CP.Obj.Header.PointerToSymbolTable = 0;
310 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0]) =
311 CP.StringTable.size();
321 template <
typename value_type>
324 char Buffer[
sizeof(BLE.
Value)];
325 support::endian::write<value_type, support::little, support::unaligned>(
331 template <
typename value_type>
338 template <
size_t NumBytes>
339 raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
340 char Buffer[NumBytes];
341 memset(Buffer, 0,
sizeof(Buffer));
342 OS.write(Buffer,
sizeof(Buffer));
350 template <
typename T>
353 memset(Header, 0,
sizeof(*Header));
354 Header->Magic =
Magic;
355 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
356 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
357 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
358 SizeOfUninitializedData = 0;
359 uint32_t SizeOfHeaders =
alignTo(CP.SectionTableStart + CP.SectionTableSize,
360 Header->FileAlignment);
361 uint32_t SizeOfImage =
alignTo(SizeOfHeaders, Header->SectionAlignment);
377 Header->SizeOfCode = SizeOfCode;
378 Header->SizeOfInitializedData = SizeOfInitializedData;
379 Header->SizeOfUninitializedData = SizeOfUninitializedData;
380 Header->AddressOfEntryPoint =
381 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint;
382 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
383 Header->MajorOperatingSystemVersion =
384 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
385 Header->MinorOperatingSystemVersion =
386 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
387 Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion;
388 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion;
389 Header->MajorSubsystemVersion =
390 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
391 Header->MinorSubsystemVersion =
392 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
393 Header->SizeOfImage = SizeOfImage;
394 Header->SizeOfHeaders = SizeOfHeaders;
395 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
396 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
397 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
398 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
399 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
400 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
409 memset(&DH, 0,
sizeof(DH));
422 OS.
write(reinterpret_cast<char *>(&DH),
sizeof(DH));
429 if (CP.useBigObj()) {
431 <<
binary_le(static_cast<uint16_t>(0xffff))
435 <<
binary_le(CP.Obj.Header.TimeDateStamp);
439 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
440 <<
binary_le(CP.Obj.Header.NumberOfSymbols);
443 <<
binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
444 <<
binary_le(CP.Obj.Header.TimeDateStamp)
445 <<
binary_le(CP.Obj.Header.PointerToSymbolTable)
446 <<
binary_le(CP.Obj.Header.NumberOfSymbols)
447 <<
binary_le(CP.Obj.Header.SizeOfOptionalHeader)
448 <<
binary_le(CP.Obj.Header.Characteristics);
454 OS.
write(reinterpret_cast<char *>(&PEH),
sizeof(PEH));
460 OS.
write(reinterpret_cast<char *>(&PEH),
sizeof(PEH));
463 CP.Obj.OptionalHeader->DataDirectories) {
478 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
479 e = CP.Obj.Sections.end();
486 <<
binary_le(i->Header.PointerToRelocations)
487 <<
binary_le(i->Header.PointerToLineNumbers)
488 <<
binary_le(i->Header.NumberOfRelocations)
489 <<
binary_le(i->Header.NumberOfLineNumbers)
492 assert(OS.
tell() == CP.SectionTableStart + CP.SectionTableSize);
494 unsigned CurSymbol = 0;
496 for (std::vector<COFFYAML::Symbol>::iterator
I = CP.Obj.Symbols.begin(),
497 E = CP.Obj.Symbols.end();
499 SymbolTableIndexMap[
I->Name] = CurSymbol;
500 CurSymbol += 1 +
I->Header.NumberOfAuxSymbols;
513 OS << binary_le<uint32_t>( S.
Relocations.size() + 1)
514 << binary_le<uint32_t>( 0)
515 << binary_le<uint16_t>( 0);
521 <<
"Both SymbolName and SymbolTableIndex specified\n";
524 SymbolTableIndex = SymbolTableIndexMap[R.
SymbolName];
533 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
534 e = CP.Obj.Symbols.end();
539 OS <<
binary_le(i->Header.SectionNumber);
541 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
543 <<
binary_le(i->Header.NumberOfAuxSymbols);
545 if (i->FunctionDefinition) {
546 OS <<
binary_le(i->FunctionDefinition->TagIndex)
547 <<
binary_le(i->FunctionDefinition->TotalSize)
548 <<
binary_le(i->FunctionDefinition->PointerToLinenumber)
549 <<
binary_le(i->FunctionDefinition->PointerToNextFunction)
550 <<
zeros(i->FunctionDefinition->unused);
553 if (i->bfAndefSymbol) {
554 OS <<
zeros(i->bfAndefSymbol->unused1)
555 <<
binary_le(i->bfAndefSymbol->Linenumber)
556 <<
zeros(i->bfAndefSymbol->unused2)
557 <<
binary_le(i->bfAndefSymbol->PointerToNextFunction)
558 <<
zeros(i->bfAndefSymbol->unused3);
561 if (i->WeakExternal) {
562 OS <<
binary_le(i->WeakExternal->TagIndex)
563 <<
binary_le(i->WeakExternal->Characteristics)
564 <<
zeros(i->WeakExternal->unused);
567 if (!i->File.
empty()) {
568 unsigned SymbolSize = CP.getSymbolSize();
570 (i->File.
size() + SymbolSize - 1) / SymbolSize;
571 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
573 OS.
write(i->File.data(), i->File.
size());
576 if (i->SectionDefinition) {
577 OS <<
binary_le(i->SectionDefinition->Length)
578 <<
binary_le(i->SectionDefinition->NumberOfRelocations)
579 <<
binary_le(i->SectionDefinition->NumberOfLinenumbers)
580 <<
binary_le(i->SectionDefinition->CheckSum)
581 <<
binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
582 <<
binary_le(i->SectionDefinition->Selection)
583 <<
zeros(i->SectionDefinition->unused)
584 <<
binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16));
588 OS <<
binary_le(i->CLRToken->AuxType) <<
zeros(i->CLRToken->unused1)
589 <<
binary_le(i->CLRToken->SymbolTableIndex)
590 <<
zeros(i->CLRToken->unused2);
596 if (CP.Obj.Header.PointerToSymbolTable)
597 OS.
write(&CP.StringTable[0], CP.StringTable.size());
606 COFFParser
CP(Doc, ErrHandler);
608 ErrHandler(
"failed to parse YAML file");
613 ErrHandler(
"failed to layout optional header for COFF file");
618 ErrHandler(
"failed to layout COFF file");
622 ErrHandler(
"failed to write COFF file");
COFF::SymbolBaseType SimpleType
This class represents lattice values for constants.
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Helper for check-and-exit error handling.
An efficient, type-erasing, non-owning reference to a callable.
iterator find(StringRef Key)
static const char BigObjMagic[]
std::vector< Relocation > Relocations
const int32_t MaxNumberOfSections16
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
std::vector< Section > Sections
static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header)
void initializeStringsAndChecksums(ArrayRef< YAMLDebugSubsection > Sections, codeview::StringsAndChecksums &SC)
ArrayRef< uint8_t > toDebugH(const DebugHSection &DebugH, BumpPtrAllocator &Alloc)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
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...
std::vector< CodeViewYAML::LeafRecord > DebugP
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Allocate memory in an ever growing pool, as if by bump-pointer.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static yaml::BinaryRef toDebugS(ArrayRef< CodeViewYAML::YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator)
static bool layoutCOFF(COFFParser &CP)
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.
raw_ostream & write(unsigned char C)
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
static const char *const Magic
yaml::BinaryRef SectionData
binary_le_impl< value_type > binary_le(value_type V)
uint32_t PointerToRelocations
std::vector< CodeViewYAML::LeafRecord > DebugT
Common declarations for yaml2obj.
CHAIN = SC CHAIN, Imm128 - System call.
std::string utostr(uint64_t X, bool isNeg=false)
uint32_t PointerToRawData
uint32_t RelativeVirtualAddress
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Specialized YAMLIO scalar type for representing a binary blob.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
binary_le_impl(value_type V)
static const char PEMagic[]
ArrayRef< uint8_t > toDebugT(ArrayRef< LeafRecord >, BumpPtrAllocator &Alloc, StringRef SectionName)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
zeros_impl< sizeof(T)> zeros(const T &)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint16_t NumberOfRelocations
Optional< uint32_t > SymbolTableIndex
std::vector< Symbol > Symbols
uint64_t tell() const
tell - Return the current offset with the file.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
COFF::SymbolComplexType ComplexType
Optional< CodeViewYAML::DebugHSection > DebugH
static llvm::Error parse(DataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Optional< PEHeader > OptionalHeader
Expected< std::vector< std::shared_ptr< codeview::DebugSubsection > > > toCodeViewSubsectionList(BumpPtrAllocator &Allocator, ArrayRef< YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC)
OutputIt copy(R &&Range, OutputIt Out)
static bool is64Bit(const char *name)
static bool layoutOptionalHeader(COFFParser &CP)
static bool writeCOFF(COFFParser &CP, raw_ostream &OS)