xref: /freebsd-src/contrib/llvm-project/llvm/lib/MC/MCDisassembler/MCExternalSymbolizer.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- MCExternalSymbolizer.cpp - External symbolizer --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
100b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
110b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
120b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
130b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
140b57cec5SDimitry Andric #include <cstring>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric namespace llvm {
190b57cec5SDimitry Andric class Triple;
200b57cec5SDimitry Andric }
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric // This function tries to add a symbolic operand in place of the immediate
230b57cec5SDimitry Andric // Value in the MCInst. The immediate Value has had any PC adjustment made by
240b57cec5SDimitry Andric // the caller. If the instruction is a branch instruction then IsBranch is true,
250b57cec5SDimitry Andric // else false. If the getOpInfo() function was set as part of the
260b57cec5SDimitry Andric // setupForSymbolicDisassembly() call then that function is called to get any
270b57cec5SDimitry Andric // symbolic information at the Address for this instruction. If that returns
280b57cec5SDimitry Andric // non-zero then the symbolic information it returns is used to create an MCExpr
290b57cec5SDimitry Andric // and that is added as an operand to the MCInst. If getOpInfo() returns zero
300b57cec5SDimitry Andric // and IsBranch is true then a symbol look up for Value is done and if a symbol
310b57cec5SDimitry Andric // is found an MCExpr is created with that, else an MCExpr with Value is
320b57cec5SDimitry Andric // created. This function returns true if it adds an operand to the MCInst and
330b57cec5SDimitry Andric // false otherwise.
tryAddingSymbolicOperand(MCInst & MI,raw_ostream & cStream,int64_t Value,uint64_t Address,bool IsBranch,uint64_t Offset,uint64_t OpSize,uint64_t InstSize)34*81ad6265SDimitry Andric bool MCExternalSymbolizer::tryAddingSymbolicOperand(
35*81ad6265SDimitry Andric     MCInst &MI, raw_ostream &cStream, int64_t Value, uint64_t Address,
36*81ad6265SDimitry Andric     bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) {
370b57cec5SDimitry Andric   struct LLVMOpInfo1 SymbolicOp;
380b57cec5SDimitry Andric   std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
390b57cec5SDimitry Andric   SymbolicOp.Value = Value;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   if (!GetOpInfo ||
42*81ad6265SDimitry Andric       !GetOpInfo(DisInfo, Address, Offset, OpSize, InstSize, 1, &SymbolicOp)) {
430b57cec5SDimitry Andric     // Clear SymbolicOp.Value from above and also all other fields.
440b57cec5SDimitry Andric     std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric     // At this point, GetOpInfo() did not find any relocation information about
470b57cec5SDimitry Andric     // this operand and we are left to use the SymbolLookUp() call back to guess
480b57cec5SDimitry Andric     // if the Value is the address of a symbol.  In the case this is a branch
490b57cec5SDimitry Andric     // that always makes sense to guess.  But in the case of an immediate it is
500b57cec5SDimitry Andric     // a bit more questionable if it is an address of a symbol or some other
510b57cec5SDimitry Andric     // reference.  So if the immediate Value comes from a width of 1 byte,
52*81ad6265SDimitry Andric     // OpSize, we will not guess it is an address of a symbol.  Because in
530b57cec5SDimitry Andric     // object files assembled starting at address 0 this usually leads to
540b57cec5SDimitry Andric     // incorrect symbolication.
55*81ad6265SDimitry Andric     if (!SymbolLookUp || (OpSize == 1 && !IsBranch))
560b57cec5SDimitry Andric       return false;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric     uint64_t ReferenceType;
590b57cec5SDimitry Andric     if (IsBranch)
600b57cec5SDimitry Andric        ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
610b57cec5SDimitry Andric     else
620b57cec5SDimitry Andric        ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
630b57cec5SDimitry Andric     const char *ReferenceName;
640b57cec5SDimitry Andric     const char *Name = SymbolLookUp(DisInfo, Value, &ReferenceType, Address,
650b57cec5SDimitry Andric                                     &ReferenceName);
660b57cec5SDimitry Andric     if (Name) {
670b57cec5SDimitry Andric       SymbolicOp.AddSymbol.Name = Name;
680b57cec5SDimitry Andric       SymbolicOp.AddSymbol.Present = true;
690b57cec5SDimitry Andric       // If Name is a C++ symbol name put the human readable name in a comment.
700b57cec5SDimitry Andric       if(ReferenceType == LLVMDisassembler_ReferenceType_DeMangled_Name)
710b57cec5SDimitry Andric         cStream << ReferenceName;
720b57cec5SDimitry Andric     }
730b57cec5SDimitry Andric     // For branches always create an MCExpr so it gets printed as hex address.
740b57cec5SDimitry Andric     else if (IsBranch) {
750b57cec5SDimitry Andric       SymbolicOp.Value = Value;
760b57cec5SDimitry Andric     }
770b57cec5SDimitry Andric     if(ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
780b57cec5SDimitry Andric       cStream << "symbol stub for: " << ReferenceName;
790b57cec5SDimitry Andric     else if(ReferenceType == LLVMDisassembler_ReferenceType_Out_Objc_Message)
800b57cec5SDimitry Andric       cStream << "Objc message: " << ReferenceName;
810b57cec5SDimitry Andric     if (!Name && !IsBranch)
820b57cec5SDimitry Andric       return false;
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   const MCExpr *Add = nullptr;
860b57cec5SDimitry Andric   if (SymbolicOp.AddSymbol.Present) {
870b57cec5SDimitry Andric     if (SymbolicOp.AddSymbol.Name) {
880b57cec5SDimitry Andric       StringRef Name(SymbolicOp.AddSymbol.Name);
890b57cec5SDimitry Andric       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
900b57cec5SDimitry Andric       Add = MCSymbolRefExpr::create(Sym, Ctx);
910b57cec5SDimitry Andric     } else {
920b57cec5SDimitry Andric       Add = MCConstantExpr::create((int)SymbolicOp.AddSymbol.Value, Ctx);
930b57cec5SDimitry Andric     }
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   const MCExpr *Sub = nullptr;
970b57cec5SDimitry Andric   if (SymbolicOp.SubtractSymbol.Present) {
980b57cec5SDimitry Andric       if (SymbolicOp.SubtractSymbol.Name) {
990b57cec5SDimitry Andric       StringRef Name(SymbolicOp.SubtractSymbol.Name);
1000b57cec5SDimitry Andric       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
1010b57cec5SDimitry Andric       Sub = MCSymbolRefExpr::create(Sym, Ctx);
1020b57cec5SDimitry Andric     } else {
1030b57cec5SDimitry Andric       Sub = MCConstantExpr::create((int)SymbolicOp.SubtractSymbol.Value, Ctx);
1040b57cec5SDimitry Andric     }
1050b57cec5SDimitry Andric   }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   const MCExpr *Off = nullptr;
1080b57cec5SDimitry Andric   if (SymbolicOp.Value != 0)
1090b57cec5SDimitry Andric     Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   const MCExpr *Expr;
1120b57cec5SDimitry Andric   if (Sub) {
1130b57cec5SDimitry Andric     const MCExpr *LHS;
1140b57cec5SDimitry Andric     if (Add)
1150b57cec5SDimitry Andric       LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
1160b57cec5SDimitry Andric     else
1170b57cec5SDimitry Andric       LHS = MCUnaryExpr::createMinus(Sub, Ctx);
1180b57cec5SDimitry Andric     if (Off)
1190b57cec5SDimitry Andric       Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
1200b57cec5SDimitry Andric     else
1210b57cec5SDimitry Andric       Expr = LHS;
1220b57cec5SDimitry Andric   } else if (Add) {
1230b57cec5SDimitry Andric     if (Off)
1240b57cec5SDimitry Andric       Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
1250b57cec5SDimitry Andric     else
1260b57cec5SDimitry Andric       Expr = Add;
1270b57cec5SDimitry Andric   } else {
1280b57cec5SDimitry Andric     if (Off)
1290b57cec5SDimitry Andric       Expr = Off;
1300b57cec5SDimitry Andric     else
1310b57cec5SDimitry Andric       Expr = MCConstantExpr::create(0, Ctx);
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   Expr = RelInfo->createExprForCAPIVariantKind(Expr, SymbolicOp.VariantKind);
1350b57cec5SDimitry Andric   if (!Expr)
1360b57cec5SDimitry Andric     return false;
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   MI.addOperand(MCOperand::createExpr(Expr));
1390b57cec5SDimitry Andric   return true;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric // This function tries to add a comment as to what is being referenced by a load
1430b57cec5SDimitry Andric // instruction with the base register that is the Pc.  These can often be values
1440b57cec5SDimitry Andric // in a literal pool near the Address of the instruction. The Address of the
1450b57cec5SDimitry Andric // instruction and its immediate Value are used as a possible literal pool entry.
1460b57cec5SDimitry Andric // The SymbolLookUp call back will return the name of a symbol referenced by the
1470b57cec5SDimitry Andric // literal pool's entry if the referenced address is that of a symbol. Or it
1480b57cec5SDimitry Andric // will return a pointer to a literal 'C' string if the referenced address of
1490b57cec5SDimitry Andric // the literal pool's entry is an address into a section with C string literals.
1500b57cec5SDimitry Andric // Or if the reference is to an Objective-C data structure it will return a
1510b57cec5SDimitry Andric // specific reference type for it and a string.
tryAddingPcLoadReferenceComment(raw_ostream & cStream,int64_t Value,uint64_t Address)1520b57cec5SDimitry Andric void MCExternalSymbolizer::tryAddingPcLoadReferenceComment(raw_ostream &cStream,
1530b57cec5SDimitry Andric                                                            int64_t Value,
1540b57cec5SDimitry Andric                                                            uint64_t Address) {
1550b57cec5SDimitry Andric   if (SymbolLookUp) {
1560b57cec5SDimitry Andric     uint64_t ReferenceType = LLVMDisassembler_ReferenceType_In_PCrel_Load;
1570b57cec5SDimitry Andric     const char *ReferenceName;
1580b57cec5SDimitry Andric     (void)SymbolLookUp(DisInfo, Value, &ReferenceType, Address, &ReferenceName);
1590b57cec5SDimitry Andric     if(ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
1600b57cec5SDimitry Andric       cStream << "literal pool symbol address: " << ReferenceName;
1610b57cec5SDimitry Andric     else if(ReferenceType ==
1620b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
1630b57cec5SDimitry Andric       cStream << "literal pool for: \"";
1640b57cec5SDimitry Andric       cStream.write_escaped(ReferenceName);
1650b57cec5SDimitry Andric       cStream << "\"";
1660b57cec5SDimitry Andric     }
1670b57cec5SDimitry Andric     else if(ReferenceType ==
1680b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
1690b57cec5SDimitry Andric       cStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
1700b57cec5SDimitry Andric     else if(ReferenceType ==
1710b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_Objc_Message)
1720b57cec5SDimitry Andric       cStream << "Objc message: " << ReferenceName;
1730b57cec5SDimitry Andric     else if(ReferenceType ==
1740b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
1750b57cec5SDimitry Andric       cStream << "Objc message ref: " << ReferenceName;
1760b57cec5SDimitry Andric     else if(ReferenceType ==
1770b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
1780b57cec5SDimitry Andric       cStream << "Objc selector ref: " << ReferenceName;
1790b57cec5SDimitry Andric     else if(ReferenceType ==
1800b57cec5SDimitry Andric             LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
1810b57cec5SDimitry Andric       cStream << "Objc class ref: " << ReferenceName;
1820b57cec5SDimitry Andric   }
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric namespace llvm {
createMCSymbolizer(const Triple & TT,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp,void * DisInfo,MCContext * Ctx,std::unique_ptr<MCRelocationInfo> && RelInfo)1860b57cec5SDimitry Andric MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
1870b57cec5SDimitry Andric                                  LLVMSymbolLookupCallback SymbolLookUp,
1880b57cec5SDimitry Andric                                  void *DisInfo, MCContext *Ctx,
1890b57cec5SDimitry Andric                                  std::unique_ptr<MCRelocationInfo> &&RelInfo) {
1900b57cec5SDimitry Andric   assert(Ctx && "No MCContext given for symbolic disassembly");
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   return new MCExternalSymbolizer(*Ctx, std::move(RelInfo), GetOpInfo,
1930b57cec5SDimitry Andric                                   SymbolLookUp, DisInfo);
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric }
196