21#define DEBUG_TYPE "BinaryReader"
26 std::string SymbolName(
Name);
27 if (SymbolNames.find(SymbolName) == SymbolNames.end()) {
29 std::piecewise_construct, std::forward_as_tuple(SymbolName),
30 std::forward_as_tuple(
Function, 0, SectionIndex,
false));
33 SymbolNames[SymbolName].Scope =
Function;
35 SymbolNames[SymbolName].SectionIndex = SectionIndex;
38 if (
Function && SymbolNames[SymbolName].IsComdat)
46 std::string SymbolName(
Name);
47 if (SymbolNames.find(SymbolName) == SymbolNames.end())
49 std::piecewise_construct, std::forward_as_tuple(SymbolName),
50 std::forward_as_tuple(
nullptr,
Address, SectionIndex, IsComdat));
53 SymbolNames[SymbolName].Address =
Address;
66 std::string SymbolName(
Name);
68 if (SymbolName.empty() || (SymbolNames.find(SymbolName) == SymbolNames.end()))
75 SymbolNames[SymbolName].Scope =
Function;
76 SectionIndex = SymbolNames[SymbolName].SectionIndex;
81 if (SymbolNames[SymbolName].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";
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())
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"
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";
279 std::string TargetLookupError;
289 "no register info for target " + TheTriple);
290 MRI.reset(RegisterInfo);
297 "no assembly info for target " + TheTriple);
306 "no subtarget info for target " + TheTriple);
307 STI.reset(SubtargetInfo);
311 if (!InstructionInfo)
313 "no instruction info for target " + TheTriple);
314 MII.reset(InstructionInfo);
316 MC = std::make_unique<MCContext>(
Triple(TheTriple),
MAI.get(),
MRI.get(),
323 "no disassembler for target " + TheTriple);
328 if (!InstructionPrinter)
330 "no target assembly language printer for target " +
332 MIP.reset(InstructionPrinter);
345 LVSections::iterator Iter = Sections.find(SectionIndex);
346 if (Iter == Sections.end()) {
348 "invalid section index for: '%s'",
349 Scope->getName().str().c_str());
352 return std::make_pair(Section.getAddress(), Section);
356 LVSectionAddresses::const_iterator Iter =
357 SectionAddresses.upper_bound(
Address);
358 if (Iter == SectionAddresses.begin())
360 "invalid section address for: '%s'",
361 Scope->getName().str().c_str());
364 Iter = SectionAddresses.lower_bound(
Address);
365 if (Iter != SectionAddresses.begin())
367 return std::make_pair(Iter->first, Iter->second);
380 ScopesWithRanges->
addEntry(Scope, LowerAddress, UpperAddress);
385 LVSectionRanges::iterator IterSection = SectionRanges.find(SectionIndex);
386 if (IterSection == SectionRanges.end())
388 SectionRanges.emplace(SectionIndex, std::make_unique<LVRange>()).first;
397 assert(Scope &&
"Scope is null.");
400 if (Scope->getIsDiscarded())
408 dbgs() <<
"\nPublic Name instructions: '" << Scope->getName() <<
"' / '"
409 << Scope->getLinkageName() <<
"'\n"
410 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
417 return SectionOrErr.takeError();
419 uint64_t SectionAddress = (*SectionOrErr).first;
422 if (!SectionContentsOrErr)
423 return SectionOrErr.takeError();
430 <<
", Section Size: " <<
hexValue(Section.getSize()) <<
"\n";
432 Size = std::min(
Size + 1, Section.getSize());
441 if (!SectionNameOrErr)
444 dbgs() <<
"Section Index: " <<
hexValue(Section.getIndex()) <<
" ["
447 <<
"] Name: '" << *SectionNameOrErr <<
"'\n"
454 auto InstructionsSP = std::make_unique<LVLines>();
456 DiscoveredLines.emplace_back(std::move(InstructionsSP));
458 while (Begin <
End) {
469 if (BytesConsumed == 0)
482 std::string BufferCodes;
506 Begin += BytesConsumed;
512 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
513 <<
"Address: " <<
hexValue(FirstAddress)
514 <<
format(
" - Collected instructions lines: %d\n",
523 ScopeInstructions.add(SectionIndex, Scope, &
Instructions);
524 AssemblerMappings.add(SectionIndex, FirstAddress, Scope);
531 if (!
options().getPrintInstructions())
542 if (!
options().getPrintInstructions())
547 dbgs() <<
"\nPublic Names (Scope):\n";
548 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
554 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
556 <<
"Name: '" << Scope->getName() <<
"' / '"
557 << Scope->getLinkageName() <<
"'\n";
563 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
569 if (!Scope->getLinkageNameIndex())
570 Scope->setLinkageName(Scope->getName());
587 assert(DebugLines &&
"DebugLines is null.");
591 if (DebugLines->
empty() && !
options().getPrintInstructions())
602 dbgs() <<
format(
"\nProcess debug lines: %d\n", DebugLines->
size());
605 <<
", (" <<
Line->getLineNumber() <<
")"
606 << ((Index % PerLine) ?
" " :
"\n");
609 dbgs() << ((Index % PerLine) ?
"\n" :
"");
612 bool TraverseLines =
true;
614 while (TraverseLines && Iter != DebugLines->
end()) {
615 uint64_t DebugAddress = (*Iter)->getAddress();
622 Scope = AssemblerMappings.find(SectionIndex, DebugAddress);
631 LVLines *
Lines = ScopeInstructions.find(SectionIndex, Scope);
633 InstructionLines = std::move(*
Lines);
638 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
639 <<
format(
"Process instruction lines: %d\n",
640 InstructionLines.
size());
648 if (InstructionLines.
empty()) {
653 for (
LVLine *InstructionLine : InstructionLines) {
654 uint64_t InstructionAddress = InstructionLine->getAddress();
656 dbgs() <<
"Instruction address: " <<
hexValue(InstructionAddress)
660 while (Iter != DebugLines->
end()) {
661 DebugAddress = (*Iter)->getAddress();
663 bool IsDebug = (*Iter)->getIsLineDebug();
664 dbgs() <<
"Line " << (IsDebug ?
"dbg:" :
"ins:") <<
" ["
667 dbgs() <<
format(
" %d", (*Iter)->getLineNumber());
671 if (InstructionAddress < DebugAddress) {
673 dbgs() <<
"Inserted instruction address: "
674 <<
hexValue(InstructionAddress) <<
" before line: "
675 <<
format(
"%d", (*Iter)->getLineNumber()) <<
" ["
678 Iter = DebugLines->
insert(Iter, InstructionLine);
686 if (Iter == DebugLines->
end()) {
689 TraverseLines =
false;
704 << ((
Line->getIsLineDebug())
705 ?
Line->lineNumberAsStringStripped(
true)
714 if (DebugLines->
empty()) {
723 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
724 <<
format(
"Instruction lines: %d\n",
Lines->size());
731 if (Scope->getIsArtificial()) {
734 Scope->addElement(
Line);
757 << ((
Line->getIsLineDebug())
758 ?
Line->lineNumberAsStringStripped(
true)
765 Scope->addElement(
Line);
768 if (
options().getWarningLines() &&
Line->getIsLineDebug() &&
769 !
Line->getLineNumber())
779 if (
Line->getIsLineDebug())
791 assert(DebugLines &&
"DebugLines is null.");
792 if (DebugLines->
empty() && !ScopeInstructions.findMap(SectionIndex))
803 std::vector<size_t> AddressZero;
805 std::find_if(std::begin(*DebugLines), std::end(*DebugLines),
807 while (It != std::end(*DebugLines)) {
808 AddressZero.emplace_back(std::distance(std::begin(*DebugLines), It));
809 It = std::find_if(std::next(It), std::end(*DebugLines),
816 if (AddressZero.empty()) {
825 using LVBucket = std::tuple<size_t, size_t, LVAddress, bool>;
826 std::vector<LVBucket> Buckets;
832 for (Index = 0; Index < AddressZero.size() - 1; ++Index) {
833 Begin = AddressZero[Index];
834 End = AddressZero[Index + 1] - 1;
836 Buckets.emplace_back(Begin,
End,
Address,
false);
841 Begin = AddressZero[Index];
844 Buckets.emplace_back(Begin,
End,
Address,
false);
848 dbgs() <<
"\nDebug Lines buckets: " << Buckets.size() <<
"\n";
849 for (LVBucket &Bucket : Buckets) {
852 <<
"Address: " <<
hexValue(std::get<2>(Bucket)) <<
"\n";
860 for (LVSections::reference Entry : Sections) {
866 <<
" , Section Size: " <<
hexValue(Section.getSize())
867 <<
" , Section Address: " <<
hexValue(Section.getAddress())
871 for (LVBucket &Bucket : Buckets) {
872 if (std::get<3>(Bucket))
875 if (
Size == std::get<2>(Bucket)) {
880 DebugLines->
begin() + std::get<1>(Bucket) + 1;
882 Group.push_back(*Iter);
884 std::get<3>(Bucket) =
true;
896 std::function<void(
LVScope * Parent)> FindInlinedScopes =
900 LVInlineeLine::iterator Iter = CUInlineeLines.find(Scope);
901 if (Iter != CUInlineeLines.end())
903 FindInlinedScopes(Scope);
909 for (LVInlineeLine::iterator InlineeIter : InlineeIters) {
910 LVScope *Scope = InlineeIter->first;
916 dbgs() <<
"Inlined lines for: " << Scope->getName() <<
"\n";
919 <<
Line->getLineNumber() <<
"\n";
923 <<
Line->getLineNumber() <<
")\n";
935 return Item->getAddress() == InlineeStart;
940 Scope->setCallLineNumber((*Iter)->getLineNumber());
944 (*Iter)->setLineNumber((*
InlineeLines->begin())->getLineNumber());
952 CUInlineeLines.erase(InlineeIter);
960 <<
Line->getLineNumber() <<
")\n";
966 OS <<
"LVBinaryReader\n";
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const Value * getAddress(const DbgVariableIntrinsic *DVI)
Annotations lets you mark points and ranges inside source code, for tests:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
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)
StringRef - 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.
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
MCDisassembler * createMCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) const
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple, const MCTargetOptions &Options) const
createMCAsmInfo - Create a MCAsmInfo implementation for the specified target triple.
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Triple - Helper class for working with autoconf configuration names.
StringRef getName() const
Return a constant reference to the value's name.
Stores all information relating to a compile unit, be it in its original instance in the object file ...
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
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
LVRange * getSectionRanges(LVSectionIndex SectionIndex)
Error loadGenericTargetInfo(StringRef TheTriple, StringRef TheFeatures)
void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope)
Error createInstructions()
std::unique_ptr< MCInstPrinter > MIP
LVAddress WasmCodeSectionOffset
void resolvePatternMatch(LVLine *Line)
LVScope * getEntry(LVAddress Address) const
void addEntry(LVScope *Scope, LVAddress LowerAddress, LVAddress UpperAddress)
LVSectionIndex getDotTextSectionIndex() const
LVScopeCompileUnit * CompileUnit
LVSectionIndex DotTextSectionIndex
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
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.
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)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
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 const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
support::ulittle32_t PointerToRawData
support::ulittle32_t Characteristics
support::ulittle32_t SizeOfRawData
support::ulittle32_t VirtualAddress