21#define DEBUG_TYPE "BinaryReader"
26 std::string SymbolName(
Name);
28 SymbolNames.try_emplace(SymbolName,
Function, 0, SectionIndex,
false);
33 It->second.SectionIndex = SectionIndex;
44 std::string SymbolName(
Name);
45 auto [It,
Inserted] = SymbolNames.try_emplace(SymbolName,
nullptr,
Address,
46 SectionIndex, IsComdat);
62 std::string SymbolName(
Name);
64 if (SymbolName.empty())
67 auto It = SymbolNames.find(SymbolName);
68 if (It == SymbolNames.end())
76 SectionIndex = It->second.SectionIndex;
81 if (It->second.IsComdat)
90 LVSymbolNames::iterator Iter = SymbolNames.find(
Name);
91 return Iter != SymbolNames.end() ? Iter->second :
Empty;
94 LVSymbolNames::iterator Iter = SymbolNames.find(
Name);
95 return Iter != SymbolNames.end() ? Iter->second.Address : 0;
98 LVSymbolNames::iterator Iter = SymbolNames.find(
Name);
99 return Iter != SymbolNames.end() ? Iter->second.SectionIndex
103 LVSymbolNames::iterator Iter = SymbolNames.find(
Name);
104 return Iter != SymbolNames.end() ? Iter->second.IsComdat :
false;
108 OS <<
"Symbol Table\n";
109 for (LVSymbolNames::reference Entry : SymbolNames) {
111 LVScope *Scope = SymbolName.Scope;
113 OS <<
"Index: " <<
hexValue(SymbolName.SectionIndex, 5)
114 <<
" Comdat: " << (SymbolName.IsComdat ?
"Y" :
"N")
116 <<
" Address: " <<
hexValue(SymbolName.Address)
117 <<
" Name: " << Entry.first <<
"\n";
128 SymbolTable.add(
Name,
Address, SectionIndex, IsComdat);
131 return SymbolTable.update(
Function);
135 return SymbolTable.getEntry(
Name);
138 return SymbolTable.getAddress(
Name);
141 return SymbolTable.getIndex(
Name);
144 return SymbolTable.getIsComdat(
Name);
152 if (!SectionNameOrErr)
155 Name = *SectionNameOrErr;
157 <<
"Address: " <<
hexValue(Section.getAddress()) <<
", "
158 <<
"Size: " <<
hexValue(Section.getSize()) <<
", "
159 <<
"Name: " <<
Name <<
"\n";
160 dbgs() <<
"isCompressed: " << Section.isCompressed() <<
", "
161 <<
"isText: " << Section.isText() <<
", "
162 <<
"isData: " << Section.isData() <<
", "
163 <<
"isBSS: " << Section.isBSS() <<
", "
164 <<
"isVirtual: " << Section.isVirtual() <<
"\n";
165 dbgs() <<
"isBitcode: " << Section.isBitcode() <<
", "
166 <<
"isStripped: " << Section.isStripped() <<
", "
167 <<
"isBerkeleyText: " << Section.isBerkeleyText() <<
", "
168 <<
"isBerkeleyData: " << Section.isBerkeleyData() <<
", "
169 <<
"isDebugSection: " << Section.isDebugSection() <<
"\n";
173 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
178 Sections.emplace(Section.getIndex(), Section);
179 addSectionAddress(Section);
183 if (!SectionNameOrErr) {
187 if (*SectionNameOrErr ==
".text" || *SectionNameOrErr ==
"CODE" ||
188 *SectionNameOrErr ==
".code") {
201 dbgs() <<
"\nSections Information:\n";
202 for (LVSections::reference Entry : Sections) {
206 if (!SectionNameOrErr)
209 <<
" Name: " << *SectionNameOrErr <<
"\n"
210 <<
"Size: " <<
hexValue(Section.getSize()) <<
"\n"
211 <<
"VirtualAddress: " <<
hexValue(VirtualAddress) <<
"\n"
212 <<
"SectionAddress: " <<
hexValue(Section.getAddress()) <<
"\n";
214 dbgs() <<
"\nObject Section Information:\n";
215 for (LVSectionAddresses::const_reference Entry : SectionAddresses)
217 <<
hexValue(Entry.first + Entry.second.getSize())
218 <<
"] Size: " <<
hexValue(Entry.second.getSize()) <<
"\n";
225 ImageBaseAddress = ImageBase.
get();
228 dbgs() <<
"ImageBaseAddress: " <<
hexValue(ImageBaseAddress) <<
"\n";
234 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
239 bool IsComdat = (COFFSection->Characteristics & Flags) == Flags;
243 Sections.emplace(Section.getIndex() + 1, Section);
244 addSectionAddress(Section);
251 dbgs() <<
"\nSections Information:\n";
252 for (LVSections::reference Entry : Sections) {
257 if (!SectionNameOrErr)
260 <<
" Name: " << *SectionNameOrErr <<
"\n"
261 <<
"Size: " <<
hexValue(Section.getSize()) <<
"\n"
262 <<
"VirtualAddress: " <<
hexValue(VirtualAddress) <<
"\n"
263 <<
"SectionAddress: " <<
hexValue(Section.getAddress()) <<
"\n"
264 <<
"PointerToRawData: " <<
hexValue(COFFSection->PointerToRawData)
266 <<
"SizeOfRawData: " <<
hexValue(COFFSection->SizeOfRawData)
269 dbgs() <<
"\nObject Section Information:\n";
270 for (LVSectionAddresses::const_reference Entry : SectionAddresses)
272 <<
hexValue(Entry.first + Entry.second.getSize())
273 <<
"] Size: " <<
hexValue(Entry.second.getSize()) <<
"\n";
280 Triple TheTriple(TripleName);
281 std::string TargetLookupError;
291 "no register info for target " + TripleName);
292 MRI.reset(RegisterInfo);
298 "no assembly info for target " + TripleName);
306 "no subtarget info for target " + TripleName);
307 STI.reset(SubtargetInfo);
311 if (!InstructionInfo)
313 "no instruction info for target " + TripleName);
314 MII.reset(InstructionInfo);
322 "no disassembler for target " + TripleName);
327 if (!InstructionPrinter)
329 "no target assembly language printer for target " +
331 MIP.reset(InstructionPrinter);
344 LVSections::iterator Iter = Sections.find(SectionIndex);
345 if (Iter == Sections.end()) {
347 "invalid section index for: '%s'",
348 Scope->getName().str().c_str());
351 return std::make_pair(Section.getAddress(), Section);
355 LVSectionAddresses::const_iterator Iter =
356 SectionAddresses.upper_bound(
Address);
357 if (Iter == SectionAddresses.begin())
359 "invalid section address for: '%s'",
360 Scope->getName().str().c_str());
363 Iter = SectionAddresses.lower_bound(
Address);
364 if (Iter != SectionAddresses.begin())
366 return std::make_pair(Iter->first, Iter->second);
372 assert(Scope &&
"Scope is null.");
375 if (Scope->getIsDiscarded())
383 dbgs() <<
"\nPublic Name instructions: '" << Scope->getName() <<
"' / '"
384 << Scope->getLinkageName() <<
"'\n"
385 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
392 return SectionOrErr.takeError();
394 uint64_t SectionAddress = (*SectionOrErr).first;
397 if (!SectionContentsOrErr)
398 return SectionOrErr.takeError();
405 <<
", Section Size: " <<
hexValue(Section.getSize()) <<
"\n";
407 Size = std::min(
Size + 1, Section.getSize());
418 "Failed to parse instructions; offset beyond section size");
425 if (!SectionNameOrErr)
428 dbgs() <<
"Section Index: " <<
hexValue(Section.getIndex()) <<
" ["
431 <<
"] Name: '" << *SectionNameOrErr <<
"'\n"
438 auto InstructionsSP = std::make_unique<LVLines>();
440 DiscoveredLines.emplace_back(std::move(InstructionsSP));
442 while (Begin < End) {
453 if (BytesConsumed == 0)
466 std::string BufferCodes;
490 Begin += BytesConsumed;
496 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
497 <<
"Address: " <<
hexValue(FirstAddress)
498 <<
formatv(
" - Collected instructions lines: {0}\n",
507 ScopeInstructions.add(SectionIndex, Scope, &
Instructions);
508 AssemblerMappings.add(SectionIndex, FirstAddress, Scope);
515 if (!
options().getPrintInstructions())
526 if (!
options().getPrintInstructions())
531 dbgs() <<
"\nPublic Names (Scope):\n";
532 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
538 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
540 <<
"Name: '" << Scope->getName() <<
"' / '"
541 << Scope->getLinkageName() <<
"'\n";
547 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
553 if (!Scope->getLinkageNameIndex())
554 Scope->setLinkageName(Scope->getName());
571 assert(DebugLines &&
"DebugLines is null.");
575 if (DebugLines->
empty() && !
options().getPrintInstructions())
589 <<
", (" <<
Line->getLineNumber() <<
")"
590 << ((Index % PerLine) ?
" " :
"\n");
593 dbgs() << ((Index % PerLine) ?
"\n" :
"");
596 bool TraverseLines =
true;
598 while (TraverseLines && Iter != DebugLines->
end()) {
599 uint64_t DebugAddress = (*Iter)->getAddress();
606 Scope = AssemblerMappings.find(SectionIndex, DebugAddress);
615 LVLines *Lines = ScopeInstructions.find(SectionIndex, Scope);
617 InstructionLines = std::move(*Lines);
622 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
623 <<
formatv(
"Process instruction lines: {0}\n",
624 InstructionLines.
size());
632 if (InstructionLines.
empty()) {
637 for (
LVLine *InstructionLine : InstructionLines) {
638 uint64_t InstructionAddress = InstructionLine->getAddress();
640 dbgs() <<
"Instruction address: " <<
hexValue(InstructionAddress)
644 while (Iter != DebugLines->
end()) {
645 DebugAddress = (*Iter)->getAddress();
647 bool IsDebug = (*Iter)->getIsLineDebug();
648 dbgs() <<
"Line " << (IsDebug ?
"dbg:" :
"ins:") <<
" ["
651 dbgs() <<
formatv(
" {0}", (*Iter)->getLineNumber());
655 if (InstructionAddress < DebugAddress) {
657 dbgs() <<
"Inserted instruction address: "
658 <<
hexValue(InstructionAddress) <<
" before line: "
659 <<
formatv(
"{0}", (*Iter)->getLineNumber()) <<
" ["
662 Iter = DebugLines->
insert(Iter, InstructionLine);
670 if (Iter == DebugLines->
end()) {
673 TraverseLines =
false;
688 << ((
Line->getIsLineDebug())
689 ?
Line->lineNumberAsStringStripped(
true)
698 if (DebugLines->
empty()) {
700 for (
LVScope *Scope : *Scopes) {
701 LVLines *Lines = ScopeInstructions.find(Scope);
707 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
708 <<
formatv(
"Instruction lines: {0}\n", Lines->size());
715 if (Scope->getIsArtificial()) {
718 Scope->addElement(
Line);
720 DebugLines->
append(*Lines);
741 << ((
Line->getIsLineDebug())
742 ?
Line->lineNumberAsStringStripped(
true)
752 if (
options().getWarningLines() &&
Line->getIsLineDebug() &&
753 !
Line->getLineNumber())
763 if (
Line->getIsLineDebug())
775 assert(DebugLines &&
"DebugLines is null.");
776 if (DebugLines->
empty() && !ScopeInstructions.findMap(SectionIndex))
787 std::vector<size_t> AddressZero;
790 while (It != std::end(*DebugLines)) {
791 AddressZero.emplace_back(std::distance(std::begin(*DebugLines), It));
792 It = std::find_if(std::next(It), std::end(*DebugLines),
799 if (AddressZero.empty()) {
808 using LVBucket = std::tuple<size_t, size_t, LVAddress, bool>;
809 std::vector<LVBucket> Buckets;
815 for (Index = 0; Index < AddressZero.size() - 1; ++Index) {
816 Begin = AddressZero[Index];
817 End = AddressZero[Index + 1] - 1;
818 Address = (*DebugLines)[End]->getAddress();
819 Buckets.emplace_back(Begin, End,
Address,
false);
824 Begin = AddressZero[Index];
825 End = DebugLines->
size() - 1;
826 Address = (*DebugLines)[End]->getAddress();
827 Buckets.emplace_back(Begin, End,
Address,
false);
831 dbgs() <<
"\nDebug Lines buckets: " << Buckets.size() <<
"\n";
832 for (LVBucket &Bucket : Buckets) {
835 <<
"Address: " <<
hexValue(std::get<2>(Bucket)) <<
"\n";
843 for (LVSections::reference Entry : Sections) {
849 <<
" , Section Size: " <<
hexValue(Section.getSize())
850 <<
" , Section Address: " <<
hexValue(Section.getAddress())
854 for (LVBucket &Bucket : Buckets) {
855 if (std::get<3>(Bucket))
858 if (
Size == std::get<2>(Bucket)) {
863 DebugLines->
begin() + std::get<1>(Bucket) + 1;
865 Group.push_back(*Iter);
867 std::get<3>(Bucket) =
true;
879 std::function<void(
LVScope * Parent)> FindInlinedScopes =
882 for (
LVScope *Scope : *Scopes) {
883 LVInlineeLine::iterator Iter = CUInlineeLines.find(Scope);
884 if (Iter != CUInlineeLines.end())
886 FindInlinedScopes(Scope);
892 for (LVInlineeLine::iterator InlineeIter : InlineeIters) {
893 LVScope *Scope = InlineeIter->first;
899 dbgs() <<
"Inlined lines for: " << Scope->getName() <<
"\n";
902 <<
Line->getLineNumber() <<
"\n";
906 <<
Line->getLineNumber() <<
")\n";
923 Scope->setCallLineNumber((*Iter)->getLineNumber());
927 (*Iter)->setLineNumber((*
InlineeLines->begin())->getLineNumber());
935 CUInlineeLines.erase(InlineeIter);
939 dbgs() <<
"Merged Inlined lines for: " <<
Function->getName() <<
"\n";
943 <<
Line->getLineNumber() <<
")\n";
949 OS <<
"LVBinaryReader\n";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
Annotations lets you mark points and ranges inside source code, for tests:
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Represents either an error or a value T.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
This class is intended to be used as a base class for asm properties and features specific to the tar...
unsigned getAssemblerDialect() const
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
void setPrintImmHex(bool Value)
Instances of this class represent a single low-level machine instruction.
Interface to description of machine instruction set.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Target - Wrapper for Target specific information.
MCRegisterInfo * createMCRegInfo(const Triple &TT) const
Create a MCRegisterInfo implementation.
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple, const MCTargetOptions &Options) const
Create a MCAsmInfo implementation for the specified target triple.
MCDisassembler * createMCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) const
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
MCSubtargetInfo * createMCSubtargetInfo(const Triple &TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Triple - Helper class for working with autoconf configuration names.
const LVSymbolTableEntry & getSymbolTableEntry(StringRef Name)
LVSectionIndex updateSymbolTable(LVScope *Function)
Expected< std::pair< LVSectionIndex, object::SectionRef > > getSection(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex)
std::unique_ptr< MCContext > MC
void includeInlineeLines(LVSectionIndex SectionIndex, LVScope *Function)
std::unique_ptr< const MCInstrInfo > MII
Error loadGenericTargetInfo(StringRef TheTriple, StringRef TheFeatures, StringRef TheCPU)
LVAddress getSymbolTableAddress(StringRef Name)
void print(raw_ostream &OS) const
std::unique_ptr< const MCSubtargetInfo > STI
void addToSymbolTable(StringRef Name, LVScope *Function, LVSectionIndex SectionIndex=0)
virtual void mapRangeAddress(const object::ObjectFile &Obj)
void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex)
void mapVirtualAddress(const object::ObjectFile &Obj)
std::unique_ptr< const MCAsmInfo > MAI
LVSectionIndex getSymbolTableIndex(StringRef Name)
bool getSymbolTableIsComdat(StringRef Name)
std::unique_ptr< const MCRegisterInfo > MRI
std::unique_ptr< const MCDisassembler > MD
Error createInstructions()
std::unique_ptr< MCInstPrinter > MIP
LVAddress WasmCodeSectionOffset
MCTargetOptions MCOptions
uint64_t getAddress() const
void resolvePatternMatch(LVLine *Line)
LVScope * getEntry(LVAddress Address) const
LVRange * getSectionRanges(LVSectionIndex SectionIndex)
LVSectionIndex getDotTextSectionIndex() const
LVScopeCompileUnit * CompileUnit
LVSectionIndex DotTextSectionIndex
void addElement(LVElement *Element)
const LVScopes * getScopes() const
LVSectionIndex getIndex(StringRef Name)
bool getIsComdat(StringRef Name)
void print(raw_ostream &OS)
LVAddress getAddress(StringRef Name)
void add(StringRef Name, LVScope *Function, LVSectionIndex SectionIndex=0)
LVSectionIndex update(LVScope *Function)
const LVSymbolTableEntry & getEntry(StringRef Name)
uint64_t getImageBase() const
const coff_section * getCOFFSection(const SectionRef &Section) const
This class is the base class for all object file types.
section_iterator_range sections() const
This is a value type class that represents a single section in the list of sections in the object fil...
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
A raw_ostream that writes to an SmallVector or SmallString.
FormattedNumber hexValue(uint64_t N, unsigned Width=HEX_WIDTH, bool Upper=false)
std::pair< LVAddress, uint64_t > LVNameInfo
constexpr LVSectionIndex UndefinedSectionIndex
SmallVector< LVScope *, 8 > LVScopes
SmallVector< LVLine *, 8 > LVLines
This is an optimization pass for GlobalISel generic memory operations.
FormattedNumber format_decimal(int64_t N, unsigned Width)
format_decimal - Output N as a right justified, fixed-width decimal.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
void consumeError(Error Err)
Consume a Error without doing anything.
FormattedBytes format_bytes(ArrayRef< uint8_t > Bytes, std::optional< uint64_t > FirstByteOffset=std::nullopt, uint32_t NumPerLine=16, uint8_t ByteGroupSize=4, uint32_t IndentLevel=0, bool Upper=false)
static LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
support::ulittle32_t VirtualAddress