Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp
35269 views
//===-- DWARFExpression.cpp -----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/DWARF/DWARFExpression.h"9#include "llvm/ADT/SmallString.h"10#include "llvm/DebugInfo/DWARF/DWARFUnit.h"11#include "llvm/Support/Format.h"12#include <cassert>13#include <cstdint>14#include <vector>1516using namespace llvm;17using namespace dwarf;1819namespace llvm {2021typedef DWARFExpression::Operation Op;22typedef Op::Description Desc;2324static std::vector<Desc> getOpDescriptions() {25std::vector<Desc> Descriptions;26Descriptions.resize(0xff);27Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr);28Descriptions[DW_OP_deref] = Desc(Op::Dwarf2);29Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1);30Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1);31Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2);32Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2);33Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4);34Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4);35Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8);36Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8);37Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB);38Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB);39Descriptions[DW_OP_dup] = Desc(Op::Dwarf2);40Descriptions[DW_OP_drop] = Desc(Op::Dwarf2);41Descriptions[DW_OP_over] = Desc(Op::Dwarf2);42Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1);43Descriptions[DW_OP_swap] = Desc(Op::Dwarf2);44Descriptions[DW_OP_rot] = Desc(Op::Dwarf2);45Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2);46Descriptions[DW_OP_abs] = Desc(Op::Dwarf2);47Descriptions[DW_OP_and] = Desc(Op::Dwarf2);48Descriptions[DW_OP_div] = Desc(Op::Dwarf2);49Descriptions[DW_OP_minus] = Desc(Op::Dwarf2);50Descriptions[DW_OP_mod] = Desc(Op::Dwarf2);51Descriptions[DW_OP_mul] = Desc(Op::Dwarf2);52Descriptions[DW_OP_neg] = Desc(Op::Dwarf2);53Descriptions[DW_OP_not] = Desc(Op::Dwarf2);54Descriptions[DW_OP_or] = Desc(Op::Dwarf2);55Descriptions[DW_OP_plus] = Desc(Op::Dwarf2);56Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB);57Descriptions[DW_OP_shl] = Desc(Op::Dwarf2);58Descriptions[DW_OP_shr] = Desc(Op::Dwarf2);59Descriptions[DW_OP_shra] = Desc(Op::Dwarf2);60Descriptions[DW_OP_xor] = Desc(Op::Dwarf2);61Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2);62Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2);63Descriptions[DW_OP_eq] = Desc(Op::Dwarf2);64Descriptions[DW_OP_ge] = Desc(Op::Dwarf2);65Descriptions[DW_OP_gt] = Desc(Op::Dwarf2);66Descriptions[DW_OP_le] = Desc(Op::Dwarf2);67Descriptions[DW_OP_lt] = Desc(Op::Dwarf2);68Descriptions[DW_OP_ne] = Desc(Op::Dwarf2);69for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA)70Descriptions[LA] = Desc(Op::Dwarf2);71for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA)72Descriptions[LA] = Desc(Op::Dwarf2);73for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA)74Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB);75Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB);76Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB);77Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB);78Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB);79Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1);80Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1);81Descriptions[DW_OP_nop] = Desc(Op::Dwarf2);82Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3);83Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2);84Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4);85Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr);86Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3);87Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3);88Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB);89Descriptions[DW_OP_implicit_value] =90Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock);91Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3);92Descriptions[DW_OP_WASM_location] =93Desc(Op::Dwarf4, Op::SizeLEB, Op::WasmLocationArg);94Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);95Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);96Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB);97Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB);98Descriptions[DW_OP_addrx] = Desc(Op::Dwarf5, Op::SizeLEB);99Descriptions[DW_OP_constx] = Desc(Op::Dwarf5, Op::SizeLEB);100Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef);101Descriptions[DW_OP_entry_value] = Desc(Op::Dwarf5, Op::SizeLEB);102Descriptions[DW_OP_regval_type] =103Desc(Op::Dwarf5, Op::SizeLEB, Op::BaseTypeRef);104// This Description acts as a marker that getSubOpDesc must be called105// to fetch the final Description for the operation. Each such final106// Description must share the same first SizeSubOpLEB operand.107Descriptions[DW_OP_LLVM_user] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);108return Descriptions;109}110111static Desc getDescImpl(ArrayRef<Desc> Descriptions, unsigned Opcode) {112// Handle possible corrupted or unsupported operation.113if (Opcode >= Descriptions.size())114return {};115return Descriptions[Opcode];116}117118static Desc getOpDesc(unsigned Opcode) {119static std::vector<Desc> Descriptions = getOpDescriptions();120return getDescImpl(Descriptions, Opcode);121}122123static std::vector<Desc> getSubOpDescriptions() {124static constexpr unsigned LlvmUserDescriptionsSize = 1125#define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) +1126#include "llvm/BinaryFormat/Dwarf.def"127;128std::vector<Desc> Descriptions;129Descriptions.resize(LlvmUserDescriptionsSize);130Descriptions[DW_OP_LLVM_nop] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);131return Descriptions;132}133134static Desc getSubOpDesc(unsigned Opcode, unsigned SubOpcode) {135assert(Opcode == DW_OP_LLVM_user);136static std::vector<Desc> Descriptions = getSubOpDescriptions();137return getDescImpl(Descriptions, SubOpcode);138}139140bool DWARFExpression::Operation::extract(DataExtractor Data,141uint8_t AddressSize, uint64_t Offset,142std::optional<DwarfFormat> Format) {143EndOffset = Offset;144Opcode = Data.getU8(&Offset);145146Desc = getOpDesc(Opcode);147if (Desc.Version == Operation::DwarfNA)148return false;149150Operands.resize(Desc.Op.size());151OperandEndOffsets.resize(Desc.Op.size());152for (unsigned Operand = 0; Operand < Desc.Op.size(); ++Operand) {153unsigned Size = Desc.Op[Operand];154unsigned Signed = Size & Operation::SignBit;155156switch (Size & ~Operation::SignBit) {157case Operation::SizeSubOpLEB:158assert(Operand == 0 && "SubOp operand must be the first operand");159Operands[Operand] = Data.getULEB128(&Offset);160Desc = getSubOpDesc(Opcode, Operands[Operand]);161if (Desc.Version == Operation::DwarfNA)162return false;163assert(Desc.Op[Operand] == Operation::SizeSubOpLEB &&164"SizeSubOpLEB Description must begin with SizeSubOpLEB operand");165break;166case Operation::Size1:167Operands[Operand] = Data.getU8(&Offset);168if (Signed)169Operands[Operand] = (int8_t)Operands[Operand];170break;171case Operation::Size2:172Operands[Operand] = Data.getU16(&Offset);173if (Signed)174Operands[Operand] = (int16_t)Operands[Operand];175break;176case Operation::Size4:177Operands[Operand] = Data.getU32(&Offset);178if (Signed)179Operands[Operand] = (int32_t)Operands[Operand];180break;181case Operation::Size8:182Operands[Operand] = Data.getU64(&Offset);183break;184case Operation::SizeAddr:185Operands[Operand] = Data.getUnsigned(&Offset, AddressSize);186break;187case Operation::SizeRefAddr:188if (!Format)189return false;190Operands[Operand] =191Data.getUnsigned(&Offset, dwarf::getDwarfOffsetByteSize(*Format));192break;193case Operation::SizeLEB:194if (Signed)195Operands[Operand] = Data.getSLEB128(&Offset);196else197Operands[Operand] = Data.getULEB128(&Offset);198break;199case Operation::BaseTypeRef:200Operands[Operand] = Data.getULEB128(&Offset);201break;202case Operation::WasmLocationArg:203assert(Operand == 1);204switch (Operands[0]) {205case 0:206case 1:207case 2:208case 4:209Operands[Operand] = Data.getULEB128(&Offset);210break;211case 3: // global as uint32212Operands[Operand] = Data.getU32(&Offset);213break;214default:215return false; // Unknown Wasm location216}217break;218case Operation::SizeBlock:219// We need a size, so this cannot be the first operand220if (Operand == 0)221return false;222// Store the offset of the block as the value.223Operands[Operand] = Offset;224Offset += Operands[Operand - 1];225break;226default:227llvm_unreachable("Unknown DWARFExpression Op size");228}229230OperandEndOffsets[Operand] = Offset;231}232233EndOffset = Offset;234return true;235}236237static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,238DIDumpOptions DumpOpts,239ArrayRef<uint64_t> Operands,240unsigned Operand) {241assert(Operand < Operands.size() && "operand out of bounds");242if (!U) {243OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);244return;245}246auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);247if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {248OS << " (";249if (DumpOpts.Verbose)250OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);251OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);252if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))253OS << " \"" << *Name << "\"";254} else {255OS << format(" <invalid base_type ref: 0x%" PRIx64 ">", Operands[Operand]);256}257}258259bool DWARFExpression::prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS,260DIDumpOptions DumpOpts,261uint8_t Opcode,262ArrayRef<uint64_t> Operands) {263if (!DumpOpts.GetNameForDWARFReg)264return false;265266uint64_t DwarfRegNum;267unsigned OpNum = 0;268269if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||270Opcode == DW_OP_regval_type)271DwarfRegNum = Operands[OpNum++];272else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)273DwarfRegNum = Opcode - DW_OP_breg0;274else275DwarfRegNum = Opcode - DW_OP_reg0;276277auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH);278if (!RegName.empty()) {279if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||280Opcode == DW_OP_bregx)281OS << ' ' << RegName << format("%+" PRId64, Operands[OpNum]);282else283OS << ' ' << RegName.data();284285if (Opcode == DW_OP_regval_type)286prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);287return true;288}289290return false;291}292293std::optional<unsigned> DWARFExpression::Operation::getSubCode() const {294if (!Desc.Op.size() || Desc.Op[0] != Operation::SizeSubOpLEB)295return std::nullopt;296return Operands[0];297}298299bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts,300const DWARFExpression *Expr,301DWARFUnit *U) const {302if (Error) {303OS << "<decoding error>";304return false;305}306307StringRef Name = OperationEncodingString(Opcode);308assert(!Name.empty() && "DW_OP has no name!");309OS << Name;310311if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||312(Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) ||313Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||314Opcode == DW_OP_regval_type)315if (prettyPrintRegisterOp(U, OS, DumpOpts, Opcode, Operands))316return true;317318for (unsigned Operand = 0; Operand < Desc.Op.size(); ++Operand) {319unsigned Size = Desc.Op[Operand];320unsigned Signed = Size & Operation::SignBit;321322if (Size == Operation::SizeSubOpLEB) {323StringRef SubName = SubOperationEncodingString(Opcode, Operands[Operand]);324assert(!SubName.empty() && "DW_OP SubOp has no name!");325OS << " " << SubName;326} else if (Size == Operation::BaseTypeRef && U) {327// For DW_OP_convert the operand may be 0 to indicate that conversion to328// the generic type should be done. The same holds for DW_OP_reinterpret,329// which is currently not supported.330if (Opcode == DW_OP_convert && Operands[Operand] == 0)331OS << " 0x0";332else333prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, Operand);334} else if (Size == Operation::WasmLocationArg) {335assert(Operand == 1);336switch (Operands[0]) {337case 0:338case 1:339case 2:340case 3: // global as uint32341case 4:342OS << format(" 0x%" PRIx64, Operands[Operand]);343break;344default: assert(false);345}346} else if (Size == Operation::SizeBlock) {347uint64_t Offset = Operands[Operand];348for (unsigned i = 0; i < Operands[Operand - 1]; ++i)349OS << format(" 0x%02x", Expr->Data.getU8(&Offset));350} else {351if (Signed)352OS << format(" %+" PRId64, (int64_t)Operands[Operand]);353else if (Opcode != DW_OP_entry_value &&354Opcode != DW_OP_GNU_entry_value)355OS << format(" 0x%" PRIx64, Operands[Operand]);356}357}358return true;359}360361void DWARFExpression::print(raw_ostream &OS, DIDumpOptions DumpOpts,362DWARFUnit *U, bool IsEH) const {363uint32_t EntryValExprSize = 0;364uint64_t EntryValStartOffset = 0;365if (Data.getData().empty())366OS << "<empty>";367368for (auto &Op : *this) {369DumpOpts.IsEH = IsEH;370if (!Op.print(OS, DumpOpts, this, U)) {371uint64_t FailOffset = Op.getEndOffset();372while (FailOffset < Data.getData().size())373OS << format(" %02x", Data.getU8(&FailOffset));374return;375}376377if (Op.getCode() == DW_OP_entry_value ||378Op.getCode() == DW_OP_GNU_entry_value) {379OS << "(";380EntryValExprSize = Op.getRawOperand(0);381EntryValStartOffset = Op.getEndOffset();382continue;383}384385if (EntryValExprSize) {386EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;387if (EntryValExprSize == 0)388OS << ")";389}390391if (Op.getEndOffset() < Data.getData().size())392OS << ", ";393}394}395396bool DWARFExpression::Operation::verify(const Operation &Op, DWARFUnit *U) {397for (unsigned Operand = 0; Operand < Op.Desc.Op.size(); ++Operand) {398unsigned Size = Op.Desc.Op[Operand];399400if (Size == Operation::BaseTypeRef) {401// For DW_OP_convert the operand may be 0 to indicate that conversion to402// the generic type should be done, so don't look up a base type in that403// case. The same holds for DW_OP_reinterpret, which is currently not404// supported.405if (Op.Opcode == DW_OP_convert && Op.Operands[Operand] == 0)406continue;407auto Die = U->getDIEForOffset(U->getOffset() + Op.Operands[Operand]);408if (!Die || Die.getTag() != dwarf::DW_TAG_base_type)409return false;410}411}412413return true;414}415416bool DWARFExpression::verify(DWARFUnit *U) {417for (auto &Op : *this)418if (!Operation::verify(Op, U))419return false;420421return true;422}423424/// A user-facing string representation of a DWARF expression. This might be an425/// Address expression, in which case it will be implicitly dereferenced, or a426/// Value expression.427struct PrintedExpr {428enum ExprKind {429Address,430Value,431};432ExprKind Kind;433SmallString<16> String;434435PrintedExpr(ExprKind K = Address) : Kind(K) {}436};437438static bool printCompactDWARFExpr(439raw_ostream &OS, DWARFExpression::iterator I,440const DWARFExpression::iterator E,441std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg =442nullptr) {443SmallVector<PrintedExpr, 4> Stack;444445while (I != E) {446const DWARFExpression::Operation &Op = *I;447uint8_t Opcode = Op.getCode();448switch (Opcode) {449case dwarf::DW_OP_regx: {450// DW_OP_regx: A register, with the register num given as an operand.451// Printed as the plain register name.452uint64_t DwarfRegNum = Op.getRawOperand(0);453auto RegName = GetNameForDWARFReg(DwarfRegNum, false);454if (RegName.empty())455return false;456raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);457S << RegName;458break;459}460case dwarf::DW_OP_bregx: {461int DwarfRegNum = Op.getRawOperand(0);462int64_t Offset = Op.getRawOperand(1);463auto RegName = GetNameForDWARFReg(DwarfRegNum, false);464if (RegName.empty())465return false;466raw_svector_ostream S(Stack.emplace_back().String);467S << RegName;468if (Offset)469S << format("%+" PRId64, Offset);470break;471}472case dwarf::DW_OP_entry_value:473case dwarf::DW_OP_GNU_entry_value: {474// DW_OP_entry_value contains a sub-expression which must be rendered475// separately.476uint64_t SubExprLength = Op.getRawOperand(0);477DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);478++I;479raw_svector_ostream S(Stack.emplace_back().String);480S << "entry(";481printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg);482S << ")";483I = SubExprEnd;484continue;485}486case dwarf::DW_OP_stack_value: {487// The top stack entry should be treated as the actual value of tne488// variable, rather than the address of the variable in memory.489assert(!Stack.empty());490Stack.back().Kind = PrintedExpr::Value;491break;492}493case dwarf::DW_OP_nop: {494break;495}496case dwarf::DW_OP_LLVM_user: {497assert(Op.getSubCode() && *Op.getSubCode() == dwarf::DW_OP_LLVM_nop);498break;499}500default:501if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {502// DW_OP_reg<N>: A register, with the register num implied by the503// opcode. Printed as the plain register name.504uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;505auto RegName = GetNameForDWARFReg(DwarfRegNum, false);506if (RegName.empty())507return false;508raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);509S << RegName;510} else if (Opcode >= dwarf::DW_OP_breg0 &&511Opcode <= dwarf::DW_OP_breg31) {512int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;513int64_t Offset = Op.getRawOperand(0);514auto RegName = GetNameForDWARFReg(DwarfRegNum, false);515if (RegName.empty())516return false;517raw_svector_ostream S(Stack.emplace_back().String);518S << RegName;519if (Offset)520S << format("%+" PRId64, Offset);521} else {522// If we hit an unknown operand, we don't know its effect on the stack,523// so bail out on the whole expression.524OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("525<< (int)Opcode << ")>";526return false;527}528break;529}530++I;531}532533if (Stack.size() != 1) {534OS << "<stack of size " << Stack.size() << ", expected 1>";535return false;536}537538if (Stack.front().Kind == PrintedExpr::Address)539OS << "[" << Stack.front().String << "]";540else541OS << Stack.front().String;542543return true;544}545546bool DWARFExpression::printCompact(547raw_ostream &OS,548std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {549return printCompactDWARFExpr(OS, begin(), end(), GetNameForDWARFReg);550}551552bool DWARFExpression::operator==(const DWARFExpression &RHS) const {553if (AddressSize != RHS.AddressSize || Format != RHS.Format)554return false;555return Data.getData() == RHS.Data.getData();556}557558} // namespace llvm559560561