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(std::string(
Name));
91 return Iter != SymbolNames.end() ? Iter->second :
Empty;
94 LVSymbolNames::iterator Iter = SymbolNames.find(std::string(
Name));
95 return Iter != SymbolNames.end() ? Iter->second.Address : 0;
98 LVSymbolNames::iterator Iter = SymbolNames.find(std::string(
Name));
99 return Iter != SymbolNames.end() ? Iter->second.SectionIndex
103 LVSymbolNames::iterator Iter = SymbolNames.find(std::string(
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).equals(
".text") ||
188 (*SectionNameOrErr).equals(
"CODE") ||
189 (*SectionNameOrErr).equals(
".code")) {
202 dbgs() <<
"\nSections Information:\n";
203 for (LVSections::reference Entry : Sections) {
207 if (!SectionNameOrErr)
210 <<
" Name: " << *SectionNameOrErr <<
"\n"
211 <<
"Size: " <<
hexValue(Section.getSize()) <<
"\n"
212 <<
"VirtualAddress: " <<
hexValue(VirtualAddress) <<
"\n"
213 <<
"SectionAddress: " <<
hexValue(Section.getAddress()) <<
"\n";
215 dbgs() <<
"\nObject Section Information:\n";
216 for (LVSectionAddresses::const_reference Entry : SectionAddresses)
218 <<
hexValue(Entry.first + Entry.second.getSize())
219 <<
"] Size: " <<
hexValue(Entry.second.getSize()) <<
"\n";
226 ImageBaseAddress = ImageBase.
get();
229 dbgs() <<
"ImageBaseAddress: " <<
hexValue(ImageBaseAddress) <<
"\n";
235 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
244 Sections.emplace(Section.getIndex() + 1, Section);
245 addSectionAddress(Section);
252 dbgs() <<
"\nSections Information:\n";
253 for (LVSections::reference Entry : Sections) {
258 if (!SectionNameOrErr)
261 <<
" Name: " << *SectionNameOrErr <<
"\n"
262 <<
"Size: " <<
hexValue(Section.getSize()) <<
"\n"
263 <<
"VirtualAddress: " <<
hexValue(VirtualAddress) <<
"\n"
264 <<
"SectionAddress: " <<
hexValue(Section.getAddress()) <<
"\n"
270 dbgs() <<
"\nObject Section Information:\n";
271 for (LVSectionAddresses::const_reference Entry : SectionAddresses)
273 <<
hexValue(Entry.first + Entry.second.getSize())
274 <<
"] Size: " <<
hexValue(Entry.second.getSize()) <<
"\n";
280 std::string TargetLookupError;
290 "no register info for target " + TheTriple);
291 MRI.reset(RegisterInfo);
298 "no assembly info for target " + TheTriple);
307 "no subtarget info for target " + TheTriple);
308 STI.reset(SubtargetInfo);
312 if (!InstructionInfo)
314 "no instruction info for target " + TheTriple);
315 MII.reset(InstructionInfo);
317 MC = std::make_unique<MCContext>(
Triple(TheTriple),
MAI.get(),
MRI.get(),
324 "no disassembler for target " + TheTriple);
329 if (!InstructionPrinter)
331 "no target assembly language printer for target " +
333 MIP.reset(InstructionPrinter);
346 LVSections::iterator Iter = Sections.find(SectionIndex);
347 if (Iter == Sections.end()) {
349 "invalid section index for: '%s'",
350 Scope->getName().str().c_str());
353 return std::make_pair(Section.getAddress(), Section);
357 LVSectionAddresses::const_iterator Iter =
358 SectionAddresses.upper_bound(
Address);
359 if (Iter == SectionAddresses.begin())
361 "invalid section address for: '%s'",
362 Scope->getName().str().c_str());
365 Iter = SectionAddresses.lower_bound(
Address);
366 if (Iter != SectionAddresses.begin())
368 return std::make_pair(Iter->first, Iter->second);
381 ScopesWithRanges->
addEntry(Scope, LowerAddress, UpperAddress);
386 LVSectionRanges::iterator IterSection = SectionRanges.find(SectionIndex);
387 if (IterSection == SectionRanges.end())
389 SectionRanges.emplace(SectionIndex, std::make_unique<LVRange>()).first;
398 assert(Scope &&
"Scope is null.");
401 if (Scope->getIsDiscarded())
409 dbgs() <<
"\nPublic Name instructions: '" << Scope->getName() <<
"' / '"
410 << Scope->getLinkageName() <<
"'\n"
411 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
418 return SectionOrErr.takeError();
420 uint64_t SectionAddress = (*SectionOrErr).first;
423 if (!SectionContentsOrErr)
424 return SectionOrErr.takeError();
431 <<
", Section Size: " <<
hexValue(Section.getSize()) <<
"\n";
433 Size = std::min(
Size + 1, Section.getSize());
442 if (!SectionNameOrErr)
445 dbgs() <<
"Section Index: " <<
hexValue(Section.getIndex()) <<
" ["
448 <<
"] Name: '" << *SectionNameOrErr <<
"'\n"
455 auto InstructionsSP = std::make_unique<LVLines>();
457 DiscoveredLines.emplace_back(std::move(InstructionsSP));
459 while (Begin <
End) {
470 if (BytesConsumed == 0)
483 std::string BufferCodes;
507 Begin += BytesConsumed;
513 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
514 <<
"Address: " <<
hexValue(FirstAddress)
515 <<
format(
" - Collected instructions lines: %d\n",
524 ScopeInstructions.add(SectionIndex, Scope, &
Instructions);
525 AssemblerMappings.add(SectionIndex, FirstAddress, Scope);
532 if (!
options().getPrintInstructions())
543 if (!
options().getPrintInstructions())
548 dbgs() <<
"\nPublic Names (Scope):\n";
549 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
555 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
557 <<
"Name: '" << Scope->getName() <<
"' / '"
558 << Scope->getLinkageName() <<
"'\n";
564 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
570 if (!Scope->getLinkageNameIndex())
571 Scope->setLinkageName(Scope->getName());
588 assert(DebugLines &&
"DebugLines is null.");
592 if (DebugLines->
empty() && !
options().getPrintInstructions())
603 dbgs() <<
format(
"\nProcess debug lines: %d\n", DebugLines->
size());
606 <<
", (" <<
Line->getLineNumber() <<
")"
607 << ((
Index % PerLine) ?
" " :
"\n");
610 dbgs() << ((
Index % PerLine) ?
"\n" :
"");
613 bool TraverseLines =
true;
615 while (TraverseLines && Iter != DebugLines->
end()) {
616 uint64_t DebugAddress = (*Iter)->getAddress();
623 Scope = AssemblerMappings.find(SectionIndex, DebugAddress);
632 LVLines *
Lines = ScopeInstructions.find(SectionIndex, Scope);
634 InstructionLines = std::move(*
Lines);
639 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
640 <<
format(
"Process instruction lines: %d\n",
641 InstructionLines.
size());
649 if (InstructionLines.
empty()) {
654 for (
LVLine *InstructionLine : InstructionLines) {
655 uint64_t InstructionAddress = InstructionLine->getAddress();
657 dbgs() <<
"Instruction address: " <<
hexValue(InstructionAddress)
661 while (Iter != DebugLines->
end()) {
662 DebugAddress = (*Iter)->getAddress();
664 bool IsDebug = (*Iter)->getIsLineDebug();
665 dbgs() <<
"Line " << (IsDebug ?
"dbg:" :
"ins:") <<
" ["
668 dbgs() <<
format(
" %d", (*Iter)->getLineNumber());
672 if (InstructionAddress < DebugAddress) {
674 dbgs() <<
"Inserted instruction address: "
675 <<
hexValue(InstructionAddress) <<
" before line: "
676 <<
format(
"%d", (*Iter)->getLineNumber()) <<
" ["
679 Iter = DebugLines->
insert(Iter, InstructionLine);
687 if (Iter == DebugLines->
end()) {
690 TraverseLines =
false;
705 << ((
Line->getIsLineDebug())
706 ?
Line->lineNumberAsStringStripped(
true)
715 if (DebugLines->
empty()) {
724 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
725 <<
format(
"Instruction lines: %d\n",
Lines->size());
732 if (Scope->getIsArtificial()) {
735 Scope->addElement(
Line);
758 << ((
Line->getIsLineDebug())
759 ?
Line->lineNumberAsStringStripped(
true)
766 Scope->addElement(
Line);
769 if (
options().getWarningLines() &&
Line->getIsLineDebug() &&
770 !
Line->getLineNumber())
780 if (
Line->getIsLineDebug())
792 assert(DebugLines &&
"DebugLines is null.");
793 if (DebugLines->
empty() && !ScopeInstructions.findMap(SectionIndex))
804 std::vector<size_t> AddressZero;
806 std::find_if(std::begin(*DebugLines), std::end(*DebugLines),
808 while (It != std::end(*DebugLines)) {
809 AddressZero.emplace_back(std::distance(std::begin(*DebugLines), It));
810 It = std::find_if(std::next(It), std::end(*DebugLines),
817 if (AddressZero.empty()) {
826 using LVBucket = std::tuple<size_t, size_t, LVAddress, bool>;
827 std::vector<LVBucket> Buckets;
834 Begin = AddressZero[
Index];
837 Buckets.emplace_back(Begin,
End,
Address,
false);
842 Begin = AddressZero[
Index];
845 Buckets.emplace_back(Begin,
End,
Address,
false);
849 dbgs() <<
"\nDebug Lines buckets: " << Buckets.size() <<
"\n";
850 for (LVBucket &Bucket : Buckets) {
853 <<
"Address: " <<
hexValue(std::get<2>(Bucket)) <<
"\n";
861 for (LVSections::reference Entry : Sections) {
867 <<
" , Section Size: " <<
hexValue(Section.getSize())
868 <<
" , Section Address: " <<
hexValue(Section.getAddress())
872 for (LVBucket &Bucket : Buckets) {
873 if (std::get<3>(Bucket))
876 if (
Size == std::get<2>(Bucket)) {
881 DebugLines->
begin() + std::get<1>(Bucket) + 1;
883 Group.push_back(*Iter);
885 std::get<3>(Bucket) =
true;
897 std::function<void(
LVScope * Parent)> FindInlinedScopes =
901 LVInlineeLine::iterator Iter = CUInlineeLines.find(Scope);
902 if (Iter != CUInlineeLines.end())
904 FindInlinedScopes(Scope);
910 for (LVInlineeLine::iterator InlineeIter : InlineeIters) {
911 LVScope *Scope = InlineeIter->first;
917 dbgs() <<
"Inlined lines for: " << Scope->getName() <<
"\n";
920 <<
Line->getLineNumber() <<
"\n";
924 <<
Line->getLineNumber() <<
")\n";
936 return Item->getAddress() == InlineeStart;
941 Scope->setCallLineNumber((*Iter)->getLineNumber());
945 (*Iter)->setLineNumber((*
InlineeLines->begin())->getLineNumber());
953 CUInlineeLines.erase(InlineeIter);
961 <<
Line->getLineNumber() <<
")\n";
967 OS <<
"LVBinaryReader\n";
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
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.
detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
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.
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