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";
149 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
154 Sections.emplace(Section.getIndex(), Section);
155 addSectionAddress(Section);
159 if (!SectionNameOrErr) {
163 if ((*SectionNameOrErr).equals(
".text") ||
164 (*SectionNameOrErr).equals(
".code"))
172 dbgs() <<
"\nSections Information:\n";
173 for (LVSections::reference Entry : Sections) {
177 if (!SectionNameOrErr)
180 <<
" Name: " << *SectionNameOrErr <<
"\n"
181 <<
"Size: " <<
hexValue(Section.getSize()) <<
"\n"
182 <<
"VirtualAddress: " <<
hexValue(VirtualAddress) <<
"\n"
183 <<
"SectionAddress: " <<
hexValue(Section.getAddress()) <<
"\n";
185 dbgs() <<
"\nObject Section Information:\n";
186 for (LVSectionAddresses::const_reference Entry : SectionAddresses)
188 <<
hexValue(Entry.first + Entry.second.getSize())
189 <<
"] Size: " <<
hexValue(Entry.second.getSize()) <<
"\n";
195 std::string TargetLookupError;
205 "no register info for target " + TheTriple);
206 MRI.reset(RegisterInfo);
213 "no assembly info for target " + TheTriple);
222 "no subtarget info for target " + TheTriple);
223 STI.reset(SubtargetInfo);
227 if (!InstructionInfo)
229 "no instruction info for target " + TheTriple);
230 MII.reset(InstructionInfo);
232 MC = std::make_unique<MCContext>(
Triple(TheTriple),
MAI.get(),
MRI.get(),
239 "no disassembler for target " + TheTriple);
244 if (!InstructionPrinter)
246 "no target assembly language printer for target " +
248 MIP.reset(InstructionPrinter);
261 LVSections::iterator Iter = Sections.find(SectionIndex);
262 if (Iter == Sections.end()) {
264 "invalid section index for: '%s'",
265 Scope->getName().str().c_str());
268 return std::make_pair(Section.getAddress(), Section);
272 LVSectionAddresses::const_iterator Iter =
273 SectionAddresses.upper_bound(
Address);
274 if (Iter == SectionAddresses.begin())
276 "invalid section address for: '%s'",
277 Scope->getName().str().c_str());
280 Iter = SectionAddresses.lower_bound(
Address);
281 if (Iter != SectionAddresses.begin())
283 return std::make_pair(Iter->first, Iter->second);
296 ScopesWithRanges->
addEntry(Scope, LowerAddress, UpperAddress);
302 LVSectionRanges::iterator IterSection = SectionRanges.find(SectionIndex);
303 if (IterSection == SectionRanges.end()) {
305 SectionRanges.emplace(SectionIndex,
Range);
307 Range = IterSection->second;
315 std::vector<LVLines *> AllInstructionLines = ScopeInstructions.find();
316 for (
LVLines *Entry : AllInstructionLines)
319 for (LVSectionRanges::reference Entry : SectionRanges)
326 assert(Scope &&
"Scope is null.");
329 if (Scope->getIsDiscarded())
337 dbgs() <<
"\nPublic Name instructions: '" << Scope->getName() <<
"' / '"
338 << Scope->getLinkageName() <<
"'\n"
339 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
346 return SectionOrErr.takeError();
348 uint64_t SectionAddress = (*SectionOrErr).first;
351 if (!SectionContentsOrErr)
352 return SectionOrErr.takeError();
359 <<
", Section Size: " <<
hexValue(Section.getSize()) <<
"\n";
361 Size = std::min(
Size + 1, Section.getSize());
370 if (!SectionNameOrErr)
373 dbgs() <<
"Section Index: " <<
hexValue(Section.getIndex()) <<
" ["
376 <<
"] Name: '" << *SectionNameOrErr <<
"'\n"
385 while (Begin < End) {
396 if (BytesConsumed == 0)
409 std::string BufferCodes;
433 Begin += BytesConsumed;
439 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
440 <<
"Address: " <<
hexValue(FirstAddress)
441 <<
format(
" - Collected instructions lines: %d\n",
450 ScopeInstructions.add(SectionIndex, Scope,
Instructions);
451 AssemblerMappings.add(SectionIndex, FirstAddress, Scope);
458 if (!
options().getPrintInstructions())
469 if (!
options().getPrintInstructions())
474 dbgs() <<
"\nPublic Names (Scope):\n";
475 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
481 <<
"DIE Offset: " <<
hexValue(Scope->getOffset()) <<
" Range: ["
483 <<
"Name: '" << Scope->getName() <<
"' / '"
484 << Scope->getLinkageName() <<
"'\n";
490 for (LVPublicNames::const_reference
Name :
CompileUnit->getPublicNames()) {
496 if (!Scope->getLinkageNameIndex())
497 Scope->setLinkageName(Scope->getName());
514 assert(DebugLines &&
"DebugLines is null.");
518 if (DebugLines->
empty() && !
options().getPrintInstructions())
529 dbgs() <<
format(
"\nProcess debug lines: %d\n", DebugLines->
size());
532 <<
", (" <<
Line->getLineNumber() <<
")"
533 << ((
Index % PerLine) ?
" " :
"\n");
536 dbgs() << ((
Index % PerLine) ?
"\n" :
"");
539 bool TraverseLines =
true;
541 while (TraverseLines && Iter != DebugLines->
end()) {
542 uint64_t DebugAddress = (*Iter)->getAddress();
549 Scope = AssemblerMappings.find(SectionIndex, DebugAddress);
558 LVLines *Lines = ScopeInstructions.find(SectionIndex, Scope);
560 InstructionLines = std::move(*Lines);
565 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
566 <<
format(
"Process instruction lines: %d\n",
567 InstructionLines.
size());
575 if (InstructionLines.
empty()) {
580 for (
LVLine *InstructionLine : InstructionLines) {
581 uint64_t InstructionAddress = InstructionLine->getAddress();
583 dbgs() <<
"Instruction address: " <<
hexValue(InstructionAddress)
587 while (Iter != DebugLines->
end()) {
588 DebugAddress = (*Iter)->getAddress();
590 bool IsDebug = (*Iter)->getIsLineDebug();
591 dbgs() <<
"Line " << (IsDebug ?
"dbg:" :
"ins:") <<
" ["
594 dbgs() <<
format(
" %d", (*Iter)->getLineNumber());
598 if (InstructionAddress < DebugAddress) {
600 dbgs() <<
"Inserted instruction address: "
601 <<
hexValue(InstructionAddress) <<
" before line: "
602 <<
format(
"%d", (*Iter)->getLineNumber()) <<
" ["
605 Iter = DebugLines->
insert(Iter, InstructionLine);
613 if (Iter == DebugLines->
end()) {
616 TraverseLines =
false;
631 << ((
Line->getIsLineDebug())
632 ?
Line->lineNumberAsStringStripped(
true)
641 if (DebugLines->
empty()) {
644 LVLines *Lines = ScopeInstructions.find(Scope);
650 <<
" Scope DIE: " <<
hexValue(Scope->getOffset()) <<
"\n"
651 <<
format(
"Instruction lines: %d\n", Lines->size());
658 if (Scope->getIsArtificial()) {
661 Scope->addElement(
Line);
663 DebugLines->
append(*Lines);
684 << ((
Line->getIsLineDebug())
685 ?
Line->lineNumberAsStringStripped(
true)
692 Scope->addElement(
Line);
695 if (
options().getWarningLines() &&
Line->getIsLineDebug() &&
696 !
Line->getLineNumber())
706 if (
Line->getIsLineDebug())
718 assert(DebugLines &&
"DebugLines is null.");
719 if (DebugLines->
empty() && !ScopeInstructions.findMap(SectionIndex))
730 std::vector<size_t> AddressZero;
732 std::find_if(std::begin(*DebugLines), std::end(*DebugLines),
734 while (It != std::end(*DebugLines)) {
735 AddressZero.emplace_back(std::distance(std::begin(*DebugLines), It));
736 It = std::find_if(std::next(It), std::end(*DebugLines),
743 if (AddressZero.empty()) {
752 using LVBucket = std::tuple<size_t, size_t, LVAddress, bool>;
753 std::vector<LVBucket> Buckets;
760 Begin = AddressZero[
Index];
761 End = AddressZero[
Index + 1] - 1;
762 Address = (*DebugLines)[End]->getAddress();
763 Buckets.emplace_back(Begin, End,
Address,
false);
768 Begin = AddressZero[
Index];
769 End = DebugLines->
size() - 1;
770 Address = (*DebugLines)[End]->getAddress();
771 Buckets.emplace_back(Begin, End,
Address,
false);
775 dbgs() <<
"\nDebug Lines buckets: " << Buckets.size() <<
"\n";
776 for (LVBucket &Bucket : Buckets) {
779 <<
"Address: " <<
hexValue(std::get<2>(Bucket)) <<
"\n";
787 for (LVSections::reference Entry : Sections) {
793 <<
" , Section Size: " <<
hexValue(Section.getSize())
794 <<
" , Section Address: " <<
hexValue(Section.getAddress())
798 for (LVBucket &Bucket : Buckets) {
799 if (std::get<3>(Bucket))
802 if (
Size == std::get<2>(Bucket)) {
807 DebugLines->
begin() + std::get<1>(Bucket) + 1;
809 Group.push_back(*Iter);
811 std::get<3>(Bucket) =
true;
819 OS <<
"LVBinaryReader\n";
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
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),...
Stores all information relating to a compile unit, be it in its original instance in the object file ...
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.
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
virtual ~LVBinaryReader()
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
void resolvePatternMatch(LVLine *Line)
LVScope * getEntry(LVAddress Address) const
void addEntry(LVScope *Scope, LVAddress LowerAddress, LVAddress UpperAddress)
LVSectionIndex getDotTextSectionIndex() const
LVScopeCompileUnit * CompileUnit
LVSectionIndex DotTextSectionIndex
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)
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< LVLine *, 8 > LVLines
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(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.