Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
35271 views
//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//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//===----------------------------------------------------------------------===//7//8// This file implements the Dwarf emissions parts of AsmPrinter.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ADT/Twine.h"13#include "llvm/BinaryFormat/Dwarf.h"14#include "llvm/CodeGen/AsmPrinter.h"15#include "llvm/CodeGen/DIE.h"16#include "llvm/CodeGen/MachineFunction.h"17#include "llvm/IR/DataLayout.h"18#include "llvm/MC/MCAsmInfo.h"19#include "llvm/MC/MCDwarf.h"20#include "llvm/MC/MCSection.h"21#include "llvm/MC/MCStreamer.h"22#include "llvm/MC/MCSymbol.h"23#include "llvm/Support/ErrorHandling.h"24#include "llvm/Target/TargetLoweringObjectFile.h"25#include <cstdint>26using namespace llvm;2728#define DEBUG_TYPE "asm-printer"2930//===----------------------------------------------------------------------===//31// Dwarf Emission Helper Routines32//===----------------------------------------------------------------------===//3334static const char *DecodeDWARFEncoding(unsigned Encoding) {35switch (Encoding) {36case dwarf::DW_EH_PE_absptr:37return "absptr";38case dwarf::DW_EH_PE_omit:39return "omit";40case dwarf::DW_EH_PE_pcrel:41return "pcrel";42case dwarf::DW_EH_PE_uleb128:43return "uleb128";44case dwarf::DW_EH_PE_sleb128:45return "sleb128";46case dwarf::DW_EH_PE_udata4:47return "udata4";48case dwarf::DW_EH_PE_udata8:49return "udata8";50case dwarf::DW_EH_PE_sdata4:51return "sdata4";52case dwarf::DW_EH_PE_sdata8:53return "sdata8";54case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:55return "pcrel udata4";56case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:57return "pcrel sdata4";58case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:59return "pcrel udata8";60case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:61return "pcrel sdata8";62case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata463:64return "indirect pcrel udata4";65case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata466:67return "indirect pcrel sdata4";68case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata869:70return "indirect pcrel udata8";71case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata872:73return "indirect pcrel sdata8";74case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel |75dwarf::DW_EH_PE_sdata4:76return "indirect datarel sdata4";77case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel |78dwarf::DW_EH_PE_sdata8:79return "indirect datarel sdata8";80}8182return "<unknown encoding>";83}8485/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an86/// encoding. If verbose assembly output is enabled, we output comments87/// describing the encoding. Desc is an optional string saying what the88/// encoding is specifying (e.g. "LSDA").89void AsmPrinter::emitEncodingByte(unsigned Val, const char *Desc) const {90if (isVerbose()) {91if (Desc)92OutStreamer->AddComment(Twine(Desc) + " Encoding = " +93Twine(DecodeDWARFEncoding(Val)));94else95OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));96}9798OutStreamer->emitIntValue(Val, 1);99}100101/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.102unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {103if (Encoding == dwarf::DW_EH_PE_omit)104return 0;105106switch (Encoding & 0x07) {107default:108llvm_unreachable("Invalid encoded value.");109case dwarf::DW_EH_PE_absptr:110return MAI->getCodePointerSize();111case dwarf::DW_EH_PE_udata2:112return 2;113case dwarf::DW_EH_PE_udata4:114return 4;115case dwarf::DW_EH_PE_udata8:116return 8;117}118}119120void AsmPrinter::emitTTypeReference(const GlobalValue *GV, unsigned Encoding) {121if (GV) {122const TargetLoweringObjectFile &TLOF = getObjFileLowering();123124const MCExpr *Exp =125TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);126OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));127} else128OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));129}130131void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,132bool ForceOffset) const {133if (!ForceOffset) {134// On COFF targets, we have to emit the special .secrel32 directive.135if (MAI->needsDwarfSectionOffsetDirective()) {136assert(!isDwarf64() &&137"emitting DWARF64 is not implemented for COFF targets");138OutStreamer->emitCOFFSecRel32(Label, /*Offset=*/0);139return;140}141142// If the format uses relocations with dwarf, refer to the symbol directly.143if (doesDwarfUseRelocationsAcrossSections()) {144OutStreamer->emitSymbolValue(Label, getDwarfOffsetByteSize());145return;146}147}148149// Otherwise, emit it as a label difference from the start of the section.150emitLabelDifference(Label, Label->getSection().getBeginSymbol(),151getDwarfOffsetByteSize());152}153154void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {155if (doesDwarfUseRelocationsAcrossSections()) {156assert(S.Symbol && "No symbol available");157emitDwarfSymbolReference(S.Symbol);158return;159}160161// Just emit the offset directly; no need for symbol math.162OutStreamer->emitIntValue(S.Offset, getDwarfOffsetByteSize());163}164165void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {166emitLabelPlusOffset(Label, Offset, getDwarfOffsetByteSize());167}168169void AsmPrinter::emitDwarfLengthOrOffset(uint64_t Value) const {170assert(isDwarf64() || Value <= UINT32_MAX);171OutStreamer->emitIntValue(Value, getDwarfOffsetByteSize());172}173174void AsmPrinter::emitDwarfUnitLength(uint64_t Length,175const Twine &Comment) const {176OutStreamer->emitDwarfUnitLength(Length, Comment);177}178179MCSymbol *AsmPrinter::emitDwarfUnitLength(const Twine &Prefix,180const Twine &Comment) const {181return OutStreamer->emitDwarfUnitLength(Prefix, Comment);182}183184void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,185unsigned Encoding) const {186// The least significant 3 bits specify the width of the encoding187if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)188emitLabelDifferenceAsULEB128(Hi, Lo);189else190emitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding));191}192193void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {194// The least significant 3 bits specify the width of the encoding195if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)196emitULEB128(Value);197else198OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding));199}200201//===----------------------------------------------------------------------===//202// Dwarf Lowering Routines203//===----------------------------------------------------------------------===//204205void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {206SMLoc Loc = Inst.getLoc();207switch (Inst.getOperation()) {208default:209llvm_unreachable("Unexpected instruction");210case MCCFIInstruction::OpDefCfaOffset:211OutStreamer->emitCFIDefCfaOffset(Inst.getOffset(), Loc);212break;213case MCCFIInstruction::OpAdjustCfaOffset:214OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset(), Loc);215break;216case MCCFIInstruction::OpDefCfa:217OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset(), Loc);218break;219case MCCFIInstruction::OpDefCfaRegister:220OutStreamer->emitCFIDefCfaRegister(Inst.getRegister(), Loc);221break;222case MCCFIInstruction::OpLLVMDefAspaceCfa:223OutStreamer->emitCFILLVMDefAspaceCfa(Inst.getRegister(), Inst.getOffset(),224Inst.getAddressSpace(), Loc);225break;226case MCCFIInstruction::OpOffset:227OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset(), Loc);228break;229case MCCFIInstruction::OpRegister:230OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2(), Loc);231break;232case MCCFIInstruction::OpWindowSave:233OutStreamer->emitCFIWindowSave(Loc);234break;235case MCCFIInstruction::OpNegateRAState:236OutStreamer->emitCFINegateRAState(Loc);237break;238case MCCFIInstruction::OpSameValue:239OutStreamer->emitCFISameValue(Inst.getRegister(), Loc);240break;241case MCCFIInstruction::OpGnuArgsSize:242OutStreamer->emitCFIGnuArgsSize(Inst.getOffset(), Loc);243break;244case MCCFIInstruction::OpEscape:245OutStreamer->AddComment(Inst.getComment());246OutStreamer->emitCFIEscape(Inst.getValues(), Loc);247break;248case MCCFIInstruction::OpRestore:249OutStreamer->emitCFIRestore(Inst.getRegister(), Loc);250break;251case MCCFIInstruction::OpUndefined:252OutStreamer->emitCFIUndefined(Inst.getRegister(), Loc);253break;254case MCCFIInstruction::OpRememberState:255OutStreamer->emitCFIRememberState(Loc);256break;257case MCCFIInstruction::OpRestoreState:258OutStreamer->emitCFIRestoreState(Loc);259break;260}261}262263void AsmPrinter::emitDwarfDIE(const DIE &Die) const {264// Emit the code (index) for the abbreviation.265if (isVerbose())266OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +267Twine::utohexstr(Die.getOffset()) + ":0x" +268Twine::utohexstr(Die.getSize()) + " " +269dwarf::TagString(Die.getTag()));270emitULEB128(Die.getAbbrevNumber());271272// Emit the DIE attribute values.273for (const auto &V : Die.values()) {274dwarf::Attribute Attr = V.getAttribute();275assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");276277if (isVerbose()) {278OutStreamer->AddComment(dwarf::AttributeString(Attr));279if (Attr == dwarf::DW_AT_accessibility)280OutStreamer->AddComment(281dwarf::AccessibilityString(V.getDIEInteger().getValue()));282}283284// Emit an attribute using the defined form.285V.emitValue(this);286}287288// Emit the DIE children if any.289if (Die.hasChildren()) {290for (const auto &Child : Die.children())291emitDwarfDIE(Child);292293OutStreamer->AddComment("End Of Children Mark");294emitInt8(0);295}296}297298void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {299// Emit the abbreviations code (base 1 index.)300emitULEB128(Abbrev.getNumber(), "Abbreviation Code");301302// Emit the abbreviations data.303Abbrev.Emit(this);304}305306307