MCDwarf.cpp revision c8fec7e21f5c24303eab8a8592f3b8faff347d86
17cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===// 27cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// 37cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// The LLVM Compiler Infrastructure 47cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// 57cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// This file is distributed under the University of Illinois Open Source 67cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// License. See LICENSE.TXT for details. 77cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// 87cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//===----------------------------------------------------------------------===// 97cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby 10767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola#include "llvm/MC/MCDwarf.h" 117cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/MC/MCAsmInfo.h" 12c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCContext.h" 13ad8aaa069cfdb3bdc32b1becc8881f67b5272e14Rafael Espindola#include "llvm/MC/MCObjectFileInfo.h" 14c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCObjectWriter.h" 15c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCRegisterInfo.h" 16c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCStreamer.h" 17c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCSymbol.h" 18c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCExpr.h" 197cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/Debug.h" 207cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/ErrorHandling.h" 21c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/Support/raw_ostream.h" 2289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola#include "llvm/Support/LEB128.h" 237cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/Path.h" 247cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/SourceMgr.h" 25c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/ADT/Hashing.h" 26c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/ADT/SmallString.h" 27c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/ADT/Twine.h" 28c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/Config/config.h" 29c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyusing namespace llvm; 30c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 31c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Given a special op, return the address skip amount (in units of 32c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// DWARF2_LINE_MIN_INSN_LENGTH. 33c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) 34c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 35c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// The maximum address skip amount that can be encoded with a special op. 36c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) 37c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 38c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// First special line opcode - leave room for the standard opcodes. 39c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: If you want to change this, you'll have to update the 40c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). 41c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_OPCODE_BASE 13 42c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 43c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Minimum line offset in a special line info. opcode. This value 44c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// was chosen to give a reasonable range of values. 45c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_BASE -5 46c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 47c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Range of line offsets in a special line info. opcode. 48c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_RANGE 14 49c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 50c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Define the architecture-dependent minimum instruction length (in bytes). 51c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This value should be rather too small than too big. 52c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_MIN_INSN_LENGTH 1 53c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 54c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, 55c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// this routine is a nop and will be optimized away. 56c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbystatic inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) { 57c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (DWARF2_LINE_MIN_INSN_LENGTH == 1) 58c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return AddrDelta; 59c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) { 60c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // TODO: report this error, but really only once. 61c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby ; 62c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 63c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH; 64c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 65c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 66195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola// 67c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This is called when an instruction is assembled into the specified section 68c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// and if there is information from the last .loc directive that has yet to have 69c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// a line entry made for it is made. 70c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// 71c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) { 72c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (!MCOS->getContext().getDwarfLocSeen()) 73c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return; 74c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 75c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Create a symbol at in the current section for use in the line entry. 76c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol(); 77c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Set the value of the symbol to use for the MCLineEntry. 78c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitLabel(LineSym); 79c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 80c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Get the current .loc info saved in the context. 81c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc(); 823f55c24df9527de345f6cc960944840a7a101c6aKevin Enderby 83c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Create a (local) line entry with the symbol and the current .loc info. 84c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCLineEntry LineEntry(LineSym, DwarfLoc); 85c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 8617fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola // clear DwarfLocSeen saying the current .loc info is now used. 87c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().ClearDwarfLocSeen(); 8817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola 89c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Get the MCLineSection for this section, if one does not exist for this 90c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // section create it. 91c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = 92c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().getMCLineSections(); 93c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCLineSection *LineSection = MCLineSections.lookup(Section); 94c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (!LineSection) { 9517fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola // Create a new MCLineSection. This will be deleted after the dwarf line 96c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // table is created using it by iterating through the MCLineSections 97c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // DenseMap. 98c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby LineSection = new MCLineSection; 99c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Save a pointer to the new LineSection into the MCLineSections DenseMap. 100c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().addMCLineSection(Section, LineSection); 101c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 102c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 103c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Add the line entry to this section's entries. 104c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby LineSection->addLineEntry(LineEntry); 1055fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola} 1065fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola 1075fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola// 1085fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola// This helper routine returns an expression of End - Start + IntVal . 109c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// 110c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbystatic inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS, 1115fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola const MCSymbol &Start, 112c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCSymbol &End, 1135fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola int IntVal) { 114c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1155fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola const MCExpr *Res = 116c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext()); 1175fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola const MCExpr *RHS = 118c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext()); 1195fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola const MCExpr *Res1 = 120c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext()); 121c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCExpr *Res2 = 122c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCConstantExpr::Create(IntVal, MCOS.getContext()); 123c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCExpr *Res3 = 124c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext()); 125c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return Res3; 126c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 127195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola 128c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// 12989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// This emits the Dwarf line table for the specified section from the entries 130c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// in the LineSection. 131c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// 132c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbystatic inline void EmitDwarfLineTable(MCStreamer *MCOS, 133c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCSection *Section, 134c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCLineSection *LineSection) { 135c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby unsigned FileNum = 1; 136c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby unsigned LastLine = 1; 137c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby unsigned Column = 0; 13817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 139c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby unsigned Isa = 0; 140c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbol *LastLabel = NULL; 141c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 142c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Loop through each MCLineEntry and encode the dwarf line number table. 143c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby for (MCLineSection::const_iterator 144c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby it = LineSection->getMCLineEntries()->begin(), 1453ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) { 146c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 147c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (FileNum != it->getFileNum()) { 148c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby FileNum = it->getFileNum(); 149c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1); 1503ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola MCOS->EmitULEB128IntValue(FileNum); 151c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 152c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (Column != it->getColumn()) { 153c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Column = it->getColumn(); 154c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1); 1553ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola MCOS->EmitULEB128IntValue(Column); 156c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 157c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (Isa != it->getIsa()) { 158c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Isa = it->getIsa(); 159c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); 160c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitULEB128IntValue(Isa); 161c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 162c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { 163c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Flags = it->getFlags(); 164c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); 165c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 166c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK) 167c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); 16864185cc6090f695c4f97c51cf2adc731f56d1a20Rafael Espindola if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END) 169c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); 170c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) 171c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); 172c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 173c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine; 17432a006e606742b1c5401e49607e33717bb5441f0Rafael Espindola MCSymbol *Label = it->getLabel(); 175c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 176c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // At this point we want to emit/create the sequence to encode the delta in 177c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // line numbers and the increment of the address from the previous Label 178c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // and the current Label. 179c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); 180c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, 181c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby asmInfo.getPointerSize()); 182c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 183c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby LastLine = it->getLine(); 184c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby LastLabel = Label; 185c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 186c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 187c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Emit a DW_LNE_end_sequence for the end of the section. 18889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Using the pointer Section create a temporary label at the end of the 18989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // section and use that and the LastLabel to compute the address delta 190c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // and use INT64_MAX as the line delta which is the signal that this is 19189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // actually a DW_LNE_end_sequence. 192c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 193c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Switch to the section to be able to create a symbol at its end. 194c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->SwitchSection(Section); 195c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 19689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCContext &context = MCOS->getContext(); 197195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola // Create a symbol at the end of the section. 19832a006e606742b1c5401e49607e33717bb5441f0Rafael Espindola MCSymbol *SectionEnd = context.CreateTempSymbol(); 199c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Set the value of the symbol, as we are at the end of the section. 200c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitLabel(SectionEnd); 201c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 202c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Switch back the dwarf line section. 203c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); 20489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 20589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); 206c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd, 20789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola asmInfo.getPointerSize()); 208c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 209c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 21089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// 211c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf file and the line tables. 212c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// 213c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyconst MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) { 214c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCContext &context = MCOS->getContext(); 21589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Switch to the section where the table will be emitted into. 216c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); 217c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 218c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Create a symbol at the beginning of this section. 2195fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola MCSymbol *LineStartSym = context.CreateTempSymbol(); 2200bbe0b440ee2cef47dcb7b281825eb70341c16ddRafael Espindola // Set the value of the symbol, as we are at the start of the section. 221c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitLabel(LineStartSym); 222c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 223c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Create a symbol for the end of the section (to be set when we get there). 224c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbol *LineEndSym = context.CreateTempSymbol(); 225c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 22689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The first 4 bytes is the total length of the information for this 227c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // compilation unit (not including these 4 bytes for the length). 228c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4), 229c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 4); 230c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 231c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Next 2 bytes is the Version, which is Dwarf 2. 2325fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola MCOS->EmitIntValue(2, 2); 233c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 2345d4918dbd116b0b5e561c431b1ea527ee1b9302aRafael Espindola // Create a symbol for the end of the prologue (to be set when we get there). 235c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end 236c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 237c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Length of the prologue, is the next 4 bytes. Which is the start of the 238c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // section to the end of the prologue. Not including the 4 bytes for the 239c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // total length, the 2 bytes for the version, and these 4 bytes for the 240c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // length of the prologue. 241c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, 242c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby (4 + 2 + 4)), 243c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 4, 0); 244c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 245c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Parameters of the state machine, are next. 246c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); 247c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); 248c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); 249c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); 250c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); 251c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 252c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Standard opcode lengths 253c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy 254c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc 255c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line 256c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file 257c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column 258c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt 259c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block 260c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc 26189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc 262c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end 263c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin 264c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa 265c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 266c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Put out the directory and file tables. 267c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 268c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // First the directory table. 269c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const std::vector<StringRef> &MCDwarfDirs = 270c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby context.getMCDwarfDirs(); 271c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { 272c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName 273c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string 2743ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola } 2753ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola MCOS->EmitIntValue(0, 1); // Terminate the directory list 276c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 277c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Second the file table. 278c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const std::vector<MCDwarfFile *> &MCDwarfFiles = 279c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().getMCDwarfFiles(); 280c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { 281c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName 282c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string 283c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // the Directory num 284c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); 285c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) 28617fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola MCOS->EmitIntValue(0, 1); // filesize (always 0) 287c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 28817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola MCOS->EmitIntValue(0, 1); // Terminate the file list 28917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola 29017fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola // This is the end of the prologue, so set the value of the symbol at the 29117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola // end of the prologue (that was used in a previous expression). 29217fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola MCOS->EmitLabel(ProEndSym); 29317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola 29417fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola // Put out the line tables. 29589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = 296c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().getMCLineSections(); 297c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby const std::vector<const MCSection *> &MCLineSectionOrder = 298c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->getContext().getMCLineSectionOrder(); 29917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola for (std::vector<const MCSection*>::const_iterator it = 300c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; 301c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby ++it) { 302767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola const MCSection *Sec = *it; 303767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola const MCLineSection *Line = MCLineSections.lookup(Sec); 304a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola EmitDwarfLineTable(MCOS, Sec, Line); 305a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola 306a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola // Now delete the MCLineSections that were created in MCLineEntry::Make() 307a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola // and used to emit the line table. 308a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola delete Line; 309a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola } 310a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola 311a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines() 3125113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { 3135113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures 314c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // it requires: 315c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // total_length >= prologue_length + 10 316c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // We are 4 bytes short, since we have total_length = 51 and 317c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // prologue_length = 45 318c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 319c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // The regular end_sequence should be sufficient. 320c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); 321c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 322c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 323c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // This is the end of the section, so set the value of the symbol at the end 324c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // of this section (that was used in a previous expression). 325c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitLabel(LineEndSym); 326c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 327c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return LineStartSym; 328c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 329195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola 330c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to write the encoding to an object writer. 331c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, 332c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby uint64_t AddrDelta) { 333c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby SmallString<256> Tmp; 334c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby raw_svector_ostream OS(Tmp); 335c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); 336c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OW->WriteBytes(OS.str()); 337c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 338c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 339c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to emit the encoding to a streamer. 340c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, 341c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby uint64_t AddrDelta) { 342c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby SmallString<256> Tmp; 343c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby raw_svector_ostream OS(Tmp); 344c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); 345c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0); 346c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby} 347c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 348c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. 349c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, 350c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby raw_ostream &OS) { 351c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby uint64_t Temp, Opcode; 352c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby bool NeedCopy = false; 353c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 354c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Scale the address delta by the minimum instruction length. 355c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby AddrDelta = ScaleAddrDelta(AddrDelta); 356c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 357c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // A LineDelta of INT64_MAX is a signal that this is actually a 358c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the 359c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // end_sequence to emit the matrix entry. 360c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (LineDelta == INT64_MAX) { 361c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) 362c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_const_add_pc); 363c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby else { 364c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_advance_pc); 365c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby encodeULEB128(AddrDelta, OS); 366c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 367c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_extended_op); 368c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(1); 369c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNE_end_sequence); 370c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return; 371c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 372c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 373c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Bias the line delta by the base. 374c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Temp = LineDelta - DWARF2_LINE_BASE; 375c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 376c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // If the line increment is out of range of a special opcode, we must encode 377c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // it with DW_LNS_advance_line. 378c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (Temp >= DWARF2_LINE_RANGE) { 379c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_advance_line); 380c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby encodeSLEB128(LineDelta, OS); 381c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 382c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby LineDelta = 0; 383c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Temp = 0 - DWARF2_LINE_BASE; 384c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby NeedCopy = true; 385c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 386c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 387c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode. 388c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (LineDelta == 0 && AddrDelta == 0) { 389c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_copy); 390c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return; 391c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 392c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 393c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Bias the opcode by the special opcode base. 394c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Temp += DWARF2_LINE_OPCODE_BASE; 395c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 396c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Avoid overflow when addr_delta is large. 397c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { 398c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Try using a special opcode. 399c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; 400c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (Opcode <= 255) { 401c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(Opcode); 402c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return; 403c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 404c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 405c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Try using DW_LNS_const_add_pc followed by special op. 406c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; 407c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (Opcode <= 255) { 408c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_const_add_pc); 409c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(Opcode); 410c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby return; 411c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 412c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby } 413c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 414c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby // Otherwise use DW_LNS_advance_pc. 415c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_advance_pc); 416c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby encodeULEB128(AddrDelta, OS); 417c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby 418c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby if (NeedCopy) 419c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(dwarf::DW_LNS_copy); 420c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby else 421c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby OS << char(Temp); 4227cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby} 4237cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby 4247cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::print(raw_ostream &OS) const { 4257cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby OS << '"' << getName() << '"'; 4267cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby} 4277cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby 4287cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 429c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfFile::dump() const { 43089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola print(dbgs()); 43189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola} 43289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola#endif 43389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 43489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// Utility function to write a tuple for .debug_abbrev. 43589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) { 43689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(Name); 43789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(Form); 43889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola} 43989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 44089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// When generating dwarf for assembly source files this emits 44189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// the data for .debug_abbrev section which contains three DIEs. 44289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic void EmitGenDwarfAbbrev(MCStreamer *MCOS) { 44389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCContext &context = MCOS->getContext(); 44489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); 44589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 44689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // DW_TAG_compile_unit DIE abbrev (1). 44789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(1); 44889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit); 44989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); 45089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4); 45189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); 45289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr); 45389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); 45489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string); 45589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); 45689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola if (!DwarfDebugFlags.empty()) 45789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string); 45889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string); 45989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2); 46089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, 0, 0); 46189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 46289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // DW_TAG_label DIE abbrev (2). 46389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(2); 46489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label); 46589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); 46689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); 46789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4); 46889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4); 46989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); 47089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag); 47189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, 0, 0); 47289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 47389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // DW_TAG_unspecified_parameters DIE abbrev (3). 47489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(3); 47589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters); 47689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1); 47789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola EmitAbbrev(MCOS, 0, 0); 47889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 47989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Terminate the abbreviations for this compilation unit. 48089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, 1); 48189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola} 48289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 48389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// When generating dwarf for assembly source files this emits the data for 48489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// .debug_aranges section. Which contains a header and a table of pairs of 48589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// PointerSize'ed values for the address and size of section(s) with line table 48689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// entries (just the default .text in our case) and a terminating pair of zeros. 48789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic void EmitGenDwarfAranges(MCStreamer *MCOS, 48889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCSymbol *InfoSectionSymbol) { 48989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCContext &context = MCOS->getContext(); 49089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 49189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Create a symbol at the end of the section that we are creating the dwarf 49289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // debugging info to use later in here as part of the expression to calculate 49389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // the size of the section for the table. 49489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->SwitchSection(context.getGenDwarfSection()); 49589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCSymbol *SectionEndSym = context.CreateTempSymbol(); 49689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitLabel(SectionEndSym); 49789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola context.setGenDwarfSectionEndSym(SectionEndSym); 49889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 49989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); 50089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 50189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // This will be the length of the .debug_aranges section, first account for 50289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // the size of each item in the header (see below where we emit these items). 50389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola int Length = 4 + 2 + 4 + 1 + 1; 50489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 50589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Figure the padding after the header before the table of address and size 50689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // pairs who's values are PointerSize'ed. 50789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCAsmInfo &asmInfo = context.getAsmInfo(); 50889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola int AddrSize = asmInfo.getPointerSize(); 50989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1)); 51089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola if (Pad == 2 * AddrSize) 51189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola Pad = 0; 51289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola Length += Pad; 51389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 51489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Add the size of the pair of PointerSize'ed values for the address and size 51589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // of the one default .text section we have in the table. 51689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola Length += 2 * AddrSize; 51789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // And the pair of terminating zeros. 51889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola Length += 2 * AddrSize; 51989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 52089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 52189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Emit the header for this section. 52289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 4 byte length not including the 4 byte value for the length. 52389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(Length - 4, 4); 52489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 2 byte version, which is 2. 52589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(2, 2); 52689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 4 byte offset to the compile unit in the .debug_info from the start 52789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // of the .debug_info. 52889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola if (InfoSectionSymbol) 52989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitSymbolValue(InfoSectionSymbol, 4); 53089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola else 53189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, 4); 53289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 1 byte size of an address. 53389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(AddrSize, 1); 53489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 1 byte size of a segment descriptor, we use a value of zero. 53589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, 1); 53689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Align the header with the padding if needed, before we put out the table. 53789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola for(int i = 0; i < Pad; i++) 53889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, 1); 53989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 54089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Now emit the table of pairs of PointerSize'ed values for the section(s) 54189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // address and size, in our case just the one default .text section. 54289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCExpr *Addr = MCSymbolRefExpr::Create( 54389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); 54489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCExpr *Size = MakeStartMinusEndExpr(*MCOS, 54589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0); 54689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitAbsValue(Addr, AddrSize); 54789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitAbsValue(Size, AddrSize); 54889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 54989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // And finally the pair of terminating zeros. 55089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, AddrSize); 55189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, AddrSize); 55289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola} 55389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 55489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// When generating dwarf for assembly source files this emits the data for 55589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// .debug_info section which contains three parts. The header, the compile_unit 55689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola// DIE and a list of label DIEs. 55789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic void EmitGenDwarfInfo(MCStreamer *MCOS, 55889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCSymbol *AbbrevSectionSymbol, 55989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCSymbol *LineSectionSymbol) { 56089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCContext &context = MCOS->getContext(); 56189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 56289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); 56389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 56489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Create a symbol at the start and end of this section used in here for the 56589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // expression to calculate the length in the header. 56689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCSymbol *InfoStart = context.CreateTempSymbol(); 56789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitLabel(InfoStart); 56889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCSymbol *InfoEnd = context.CreateTempSymbol(); 56989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 57089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // First part: the header. 57189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 572dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola // The 4 byte total length of the information for this compilation unit, not 573dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola // including these 4 bytes. 574dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4); 57589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitAbsValue(Length, 4); 57689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 57789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 2 byte DWARF version, which is 2. 57889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(2, 2); 57989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 58089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev, 58189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // it is at the start of that section so this is zero. 58289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola if (AbbrevSectionSymbol) { 58389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4); 58489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola } else { 58589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(0, 4); 58689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola } 58789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 58889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCAsmInfo &asmInfo = context.getAsmInfo(); 58989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola int AddrSize = asmInfo.getPointerSize(); 59089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The 1 byte size of an address. 59189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitIntValue(AddrSize, 1); 59289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 59389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // Second part: the compile_unit DIE. 59489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 59589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // The DW_TAG_compile_unit DIE abbrev (1). 59689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitULEB128IntValue(1); 59789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola 59889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section, 59989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // which is at the start of that section so this is zero. 60089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola if (LineSectionSymbol) { 60189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitSymbolValue(LineSectionSymbol, 4); 60289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola } else { 603dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola MCOS->EmitIntValue(0, 4); 604dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola } 605dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola 60689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // AT_low_pc, the first address of the default .text section. 60789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola const MCExpr *Start = MCSymbolRefExpr::Create( 60889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); 609dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola MCOS->EmitAbsValue(Start, AddrSize); 610dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola 61189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola // AT_high_pc, the last address of the default .text section. 612dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola const MCExpr *End = MCSymbolRefExpr::Create( 613dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context); 614dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola MCOS->EmitAbsValue(End, AddrSize); 615dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola 616dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola // AT_name, the name of the source file. Reconstruct from the first directory 617dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola // and file table entries. 618dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola const std::vector<StringRef> &MCDwarfDirs = 619dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola context.getMCDwarfDirs(); 620dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola if (MCDwarfDirs.size() > 0) { 62189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola MCOS->EmitBytes(MCDwarfDirs[0], 0); 622 MCOS->EmitBytes("/", 0); 623 } 624 const std::vector<MCDwarfFile *> &MCDwarfFiles = 625 MCOS->getContext().getMCDwarfFiles(); 626 MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0); 627 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. 628 629 // AT_comp_dir, the working directory the assembly was done in. 630 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); 631 MCOS->EmitBytes(StringRef(CWD.c_str()), 0); 632 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. 633 634 // AT_APPLE_flags, the command line arguments of the assembler tool. 635 StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); 636 if (!DwarfDebugFlags.empty()){ 637 MCOS->EmitBytes(DwarfDebugFlags, 0); 638 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. 639 } 640 641 // AT_producer, the version of the assembler tool. 642 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0); 643 MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0); 644 MCOS->EmitBytes(StringRef(")"), 0); 645 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. 646 647 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2 648 // draft has no standard code for assembler. 649 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2); 650 651 // Third part: the list of label DIEs. 652 653 // Loop on saved info for dwarf labels and create the DIEs for them. 654 const std::vector<const MCGenDwarfLabelEntry *> &Entries = 655 MCOS->getContext().getMCGenDwarfLabelEntries(); 656 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it = 657 Entries.begin(), ie = Entries.end(); it != ie; 658 ++it) { 659 const MCGenDwarfLabelEntry *Entry = *it; 660 661 // The DW_TAG_label DIE abbrev (2). 662 MCOS->EmitULEB128IntValue(2); 663 664 // AT_name, of the label without any leading underbar. 665 MCOS->EmitBytes(Entry->getName(), 0); 666 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. 667 668 // AT_decl_file, index into the file table. 669 MCOS->EmitIntValue(Entry->getFileNumber(), 4); 670 671 // AT_decl_line, source line number. 672 MCOS->EmitIntValue(Entry->getLineNumber(), 4); 673 674 // AT_low_pc, start address of the label. 675 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(), 676 MCSymbolRefExpr::VK_None, context); 677 MCOS->EmitAbsValue(AT_low_pc, AddrSize); 678 679 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype. 680 MCOS->EmitIntValue(0, 1); 681 682 // The DW_TAG_unspecified_parameters DIE abbrev (3). 683 MCOS->EmitULEB128IntValue(3); 684 685 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's. 686 MCOS->EmitIntValue(0, 1); 687 } 688 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info 689 // for the dwarf labels. 690 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it = 691 Entries.begin(), ie = Entries.end(); it != ie; 692 ++it) { 693 const MCGenDwarfLabelEntry *Entry = *it; 694 delete Entry; 695 } 696 697 // Add the NULL DIE terminating the Compile Unit DIE's. 698 MCOS->EmitIntValue(0, 1); 699 700 // Now set the value of the symbol at the end of the info section. 701 MCOS->EmitLabel(InfoEnd); 702} 703 704// 705// When generating dwarf for assembly source files this emits the Dwarf 706// sections. 707// 708void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) { 709 // Create the dwarf sections in this order (.debug_line already created). 710 MCContext &context = MCOS->getContext(); 711 const MCAsmInfo &AsmInfo = context.getAsmInfo(); 712 bool CreateDwarfSectionSymbols = 713 AsmInfo.doesDwarfUseRelocationsAcrossSections(); 714 if (!CreateDwarfSectionSymbols) 715 LineSectionSymbol = NULL; 716 MCSymbol *AbbrevSectionSymbol = NULL; 717 MCSymbol *InfoSectionSymbol = NULL; 718 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); 719 if (CreateDwarfSectionSymbols) { 720 InfoSectionSymbol = context.CreateTempSymbol(); 721 MCOS->EmitLabel(InfoSectionSymbol); 722 } 723 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); 724 if (CreateDwarfSectionSymbols) { 725 AbbrevSectionSymbol = context.CreateTempSymbol(); 726 MCOS->EmitLabel(AbbrevSectionSymbol); 727 } 728 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); 729 730 // If there are no line table entries then do not emit any section contents. 731 if (context.getMCLineSections().empty()) 732 return; 733 734 // Output the data for .debug_aranges section. 735 EmitGenDwarfAranges(MCOS, InfoSectionSymbol); 736 737 // Output the data for .debug_abbrev section. 738 EmitGenDwarfAbbrev(MCOS); 739 740 // Output the data for .debug_info section. 741 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol); 742} 743 744// 745// When generating dwarf for assembly source files this is called when symbol 746// for a label is created. If this symbol is not a temporary and is in the 747// section that dwarf is being generated for, save the needed info to create 748// a dwarf label. 749// 750void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS, 751 SourceMgr &SrcMgr, SMLoc &Loc) { 752 // We won't create dwarf labels for temporary symbols or symbols not in 753 // the default text. 754 if (Symbol->isTemporary()) 755 return; 756 MCContext &context = MCOS->getContext(); 757 if (context.getGenDwarfSection() != MCOS->getCurrentSection()) 758 return; 759 760 // The dwarf label's name does not have the symbol name's leading 761 // underbar if any. 762 StringRef Name = Symbol->getName(); 763 if (Name.startswith("_")) 764 Name = Name.substr(1, Name.size()-1); 765 766 // Get the dwarf file number to be used for the dwarf label. 767 unsigned FileNumber = context.getGenDwarfFileNumber(); 768 769 // Finding the line number is the expensive part which is why we just don't 770 // pass it in as for some symbols we won't create a dwarf label. 771 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); 772 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer); 773 774 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc 775 // values so that they don't have things like an ARM thumb bit from the 776 // original symbol. So when used they won't get a low bit set after 777 // relocation. 778 MCSymbol *Label = context.CreateTempSymbol(); 779 MCOS->EmitLabel(Label); 780 781 // Create and entry for the info and add it to the other entries. 782 MCGenDwarfLabelEntry *Entry = 783 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label); 784 MCOS->getContext().addMCGenDwarfLabelEntry(Entry); 785} 786 787static int getDataAlignmentFactor(MCStreamer &streamer) { 788 MCContext &context = streamer.getContext(); 789 const MCAsmInfo &asmInfo = context.getAsmInfo(); 790 int size = asmInfo.getPointerSize(); 791 if (asmInfo.isStackGrowthDirectionUp()) 792 return size; 793 else 794 return -size; 795} 796 797static unsigned getSizeForEncoding(MCStreamer &streamer, 798 unsigned symbolEncoding) { 799 MCContext &context = streamer.getContext(); 800 unsigned format = symbolEncoding & 0x0f; 801 switch (format) { 802 default: llvm_unreachable("Unknown Encoding"); 803 case dwarf::DW_EH_PE_absptr: 804 case dwarf::DW_EH_PE_signed: 805 return context.getAsmInfo().getPointerSize(); 806 case dwarf::DW_EH_PE_udata2: 807 case dwarf::DW_EH_PE_sdata2: 808 return 2; 809 case dwarf::DW_EH_PE_udata4: 810 case dwarf::DW_EH_PE_sdata4: 811 return 4; 812 case dwarf::DW_EH_PE_udata8: 813 case dwarf::DW_EH_PE_sdata8: 814 return 8; 815 } 816} 817 818static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol, 819 unsigned symbolEncoding, const char *comment = 0) { 820 MCContext &context = streamer.getContext(); 821 const MCAsmInfo &asmInfo = context.getAsmInfo(); 822 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol, 823 symbolEncoding, 824 streamer); 825 unsigned size = getSizeForEncoding(streamer, symbolEncoding); 826 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment); 827 streamer.EmitAbsValue(v, size); 828} 829 830static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, 831 unsigned symbolEncoding) { 832 MCContext &context = streamer.getContext(); 833 const MCAsmInfo &asmInfo = context.getAsmInfo(); 834 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol, 835 symbolEncoding, 836 streamer); 837 unsigned size = getSizeForEncoding(streamer, symbolEncoding); 838 streamer.EmitValue(v, size); 839} 840 841static const MachineLocation TranslateMachineLocation( 842 const MCRegisterInfo &MRI, 843 const MachineLocation &Loc) { 844 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ? 845 MachineLocation::VirtualFP : 846 unsigned(MRI.getDwarfRegNum(Loc.getReg(), true)); 847 const MachineLocation &NewLoc = Loc.isReg() ? 848 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset()); 849 return NewLoc; 850} 851 852namespace { 853 class FrameEmitterImpl { 854 int CFAOffset; 855 int CIENum; 856 bool UsingCFI; 857 bool IsEH; 858 const MCSymbol *SectionStart; 859 public: 860 FrameEmitterImpl(bool usingCFI, bool isEH) 861 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH), 862 SectionStart(0) {} 863 864 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; } 865 866 /// EmitCompactUnwind - Emit the unwind information in a compact way. If 867 /// we're successful, return 'true'. Otherwise, return 'false' and it will 868 /// emit the normal CIE and FDE. 869 bool EmitCompactUnwind(MCStreamer &streamer, 870 const MCDwarfFrameInfo &frame); 871 872 const MCSymbol &EmitCIE(MCStreamer &streamer, 873 const MCSymbol *personality, 874 unsigned personalityEncoding, 875 const MCSymbol *lsda, 876 bool IsSignalFrame, 877 unsigned lsdaEncoding); 878 MCSymbol *EmitFDE(MCStreamer &streamer, 879 const MCSymbol &cieStart, 880 const MCDwarfFrameInfo &frame); 881 void EmitCFIInstructions(MCStreamer &streamer, 882 const std::vector<MCCFIInstruction> &Instrs, 883 MCSymbol *BaseLabel); 884 void EmitCFIInstruction(MCStreamer &Streamer, 885 const MCCFIInstruction &Instr); 886 }; 887 888} // end anonymous namespace 889 890static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding, 891 StringRef Prefix) { 892 if (Streamer.isVerboseAsm()) { 893 const char *EncStr; 894 switch (Encoding) { 895 default: EncStr = "<unknown encoding>"; break; 896 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break; 897 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break; 898 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break; 899 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break; 900 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break; 901 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break; 902 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break; 903 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: 904 EncStr = "pcrel udata4"; 905 break; 906 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: 907 EncStr = "pcrel sdata4"; 908 break; 909 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: 910 EncStr = "pcrel udata8"; 911 break; 912 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: 913 EncStr = "screl sdata8"; 914 break; 915 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4: 916 EncStr = "indirect pcrel udata4"; 917 break; 918 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4: 919 EncStr = "indirect pcrel sdata4"; 920 break; 921 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8: 922 EncStr = "indirect pcrel udata8"; 923 break; 924 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8: 925 EncStr = "indirect pcrel sdata8"; 926 break; 927 } 928 929 Streamer.AddComment(Twine(Prefix) + " = " + EncStr); 930 } 931 932 Streamer.EmitIntValue(Encoding, 1); 933} 934 935void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, 936 const MCCFIInstruction &Instr) { 937 int dataAlignmentFactor = getDataAlignmentFactor(Streamer); 938 bool VerboseAsm = Streamer.isVerboseAsm(); 939 940 switch (Instr.getOperation()) { 941 case MCCFIInstruction::Undefined: { 942 unsigned Reg = Instr.getDestination().getReg(); 943 if (VerboseAsm) { 944 Streamer.AddComment("DW_CFA_undefined"); 945 Streamer.AddComment(Twine("Reg ") + Twine(Reg)); 946 } 947 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1); 948 Streamer.EmitULEB128IntValue(Reg); 949 return; 950 } 951 case MCCFIInstruction::Move: 952 case MCCFIInstruction::RelMove: { 953 const MachineLocation &Dst = Instr.getDestination(); 954 const MachineLocation &Src = Instr.getSource(); 955 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove; 956 957 // If advancing cfa. 958 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { 959 if (Src.getReg() == MachineLocation::VirtualFP) { 960 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset"); 961 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); 962 } else { 963 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa"); 964 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); 965 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + 966 Twine(Src.getReg())); 967 Streamer.EmitULEB128IntValue(Src.getReg()); 968 } 969 970 if (IsRelative) 971 CFAOffset += Src.getOffset(); 972 else 973 CFAOffset = -Src.getOffset(); 974 975 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); 976 Streamer.EmitULEB128IntValue(CFAOffset); 977 return; 978 } 979 980 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { 981 assert(Dst.isReg() && "Machine move not supported yet."); 982 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register"); 983 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); 984 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg())); 985 Streamer.EmitULEB128IntValue(Dst.getReg()); 986 return; 987 } 988 989 unsigned Reg = Src.getReg(); 990 int Offset = Dst.getOffset(); 991 if (IsRelative) 992 Offset -= CFAOffset; 993 Offset = Offset / dataAlignmentFactor; 994 995 if (Offset < 0) { 996 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf"); 997 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1); 998 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); 999 Streamer.EmitULEB128IntValue(Reg); 1000 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); 1001 Streamer.EmitSLEB128IntValue(Offset); 1002 } else if (Reg < 64) { 1003 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") + 1004 Twine(Reg) + ")"); 1005 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1); 1006 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); 1007 Streamer.EmitULEB128IntValue(Offset); 1008 } else { 1009 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended"); 1010 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1); 1011 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); 1012 Streamer.EmitULEB128IntValue(Reg); 1013 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); 1014 Streamer.EmitULEB128IntValue(Offset); 1015 } 1016 return; 1017 } 1018 case MCCFIInstruction::RememberState: 1019 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state"); 1020 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1); 1021 return; 1022 case MCCFIInstruction::RestoreState: 1023 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state"); 1024 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1); 1025 return; 1026 case MCCFIInstruction::SameValue: { 1027 unsigned Reg = Instr.getDestination().getReg(); 1028 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value"); 1029 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1); 1030 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); 1031 Streamer.EmitULEB128IntValue(Reg); 1032 return; 1033 } 1034 case MCCFIInstruction::Restore: { 1035 unsigned Reg = Instr.getDestination().getReg(); 1036 if (VerboseAsm) { 1037 Streamer.AddComment("DW_CFA_restore"); 1038 Streamer.AddComment(Twine("Reg ") + Twine(Reg)); 1039 } 1040 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1); 1041 return; 1042 } 1043 case MCCFIInstruction::Escape: 1044 if (VerboseAsm) Streamer.AddComment("Escape bytes"); 1045 Streamer.EmitBytes(Instr.getValues(), 0); 1046 return; 1047 } 1048 llvm_unreachable("Unhandled case in switch"); 1049} 1050 1051/// EmitFrameMoves - Emit frame instructions to describe the layout of the 1052/// frame. 1053void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer, 1054 const std::vector<MCCFIInstruction> &Instrs, 1055 MCSymbol *BaseLabel) { 1056 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) { 1057 const MCCFIInstruction &Instr = Instrs[i]; 1058 MCSymbol *Label = Instr.getLabel(); 1059 // Throw out move if the label is invalid. 1060 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code. 1061 1062 // Advance row if new location. 1063 if (BaseLabel && Label) { 1064 MCSymbol *ThisSym = Label; 1065 if (ThisSym != BaseLabel) { 1066 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4"); 1067 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); 1068 BaseLabel = ThisSym; 1069 } 1070 } 1071 1072 EmitCFIInstruction(streamer, Instr); 1073 } 1074} 1075 1076/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're 1077/// successful, return 'true'. Otherwise, return 'false' and it will emit the 1078/// normal CIE and FDE. 1079bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer, 1080 const MCDwarfFrameInfo &Frame) { 1081 MCContext &Context = Streamer.getContext(); 1082 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo(); 1083 bool VerboseAsm = Streamer.isVerboseAsm(); 1084 1085 // range-start range-length compact-unwind-enc personality-func lsda 1086 // _foo LfooEnd-_foo 0x00000023 0 0 1087 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1 1088 // 1089 // .section __LD,__compact_unwind,regular,debug 1090 // 1091 // # compact unwind for _foo 1092 // .quad _foo 1093 // .set L1,LfooEnd-_foo 1094 // .long L1 1095 // .long 0x01010001 1096 // .quad 0 1097 // .quad 0 1098 // 1099 // # compact unwind for _bar 1100 // .quad _bar 1101 // .set L2,LbarEnd-_bar 1102 // .long L2 1103 // .long 0x01020011 1104 // .quad __gxx_personality 1105 // .quad except_tab1 1106 1107 uint32_t Encoding = Frame.CompactUnwindEncoding; 1108 if (!Encoding) return false; 1109 1110 // The encoding needs to know we have an LSDA. 1111 if (Frame.Lsda) 1112 Encoding |= 0x40000000; 1113 1114 Streamer.SwitchSection(MOFI->getCompactUnwindSection()); 1115 1116 // Range Start 1117 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI); 1118 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding); 1119 if (VerboseAsm) Streamer.AddComment("Range Start"); 1120 Streamer.EmitSymbolValue(Frame.Function, Size); 1121 1122 // Range Length 1123 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin, 1124 *Frame.End, 0); 1125 if (VerboseAsm) Streamer.AddComment("Range Length"); 1126 Streamer.EmitAbsValue(Range, 4); 1127 1128 // Compact Encoding 1129 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4); 1130 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" + 1131 Twine::utohexstr(Encoding)); 1132 Streamer.EmitIntValue(Encoding, Size); 1133 1134 1135 // Personality Function 1136 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr); 1137 if (VerboseAsm) Streamer.AddComment("Personality Function"); 1138 if (Frame.Personality) 1139 Streamer.EmitSymbolValue(Frame.Personality, Size); 1140 else 1141 Streamer.EmitIntValue(0, Size); // No personality fn 1142 1143 // LSDA 1144 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding); 1145 if (VerboseAsm) Streamer.AddComment("LSDA"); 1146 if (Frame.Lsda) 1147 Streamer.EmitSymbolValue(Frame.Lsda, Size); 1148 else 1149 Streamer.EmitIntValue(0, Size); // No LSDA 1150 1151 return true; 1152} 1153 1154const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer, 1155 const MCSymbol *personality, 1156 unsigned personalityEncoding, 1157 const MCSymbol *lsda, 1158 bool IsSignalFrame, 1159 unsigned lsdaEncoding) { 1160 MCContext &context = streamer.getContext(); 1161 const MCRegisterInfo &MRI = context.getRegisterInfo(); 1162 const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); 1163 bool verboseAsm = streamer.isVerboseAsm(); 1164 1165 MCSymbol *sectionStart; 1166 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH) 1167 sectionStart = context.CreateTempSymbol(); 1168 else 1169 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum)); 1170 1171 streamer.EmitLabel(sectionStart); 1172 CIENum++; 1173 1174 MCSymbol *sectionEnd = context.CreateTempSymbol(); 1175 1176 // Length 1177 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, 1178 *sectionEnd, 4); 1179 if (verboseAsm) streamer.AddComment("CIE Length"); 1180 streamer.EmitAbsValue(Length, 4); 1181 1182 // CIE ID 1183 unsigned CIE_ID = IsEH ? 0 : -1; 1184 if (verboseAsm) streamer.AddComment("CIE ID Tag"); 1185 streamer.EmitIntValue(CIE_ID, 4); 1186 1187 // Version 1188 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION"); 1189 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1); 1190 1191 // Augmentation String 1192 SmallString<8> Augmentation; 1193 if (IsEH) { 1194 if (verboseAsm) streamer.AddComment("CIE Augmentation"); 1195 Augmentation += "z"; 1196 if (personality) 1197 Augmentation += "P"; 1198 if (lsda) 1199 Augmentation += "L"; 1200 Augmentation += "R"; 1201 if (IsSignalFrame) 1202 Augmentation += "S"; 1203 streamer.EmitBytes(Augmentation.str(), 0); 1204 } 1205 streamer.EmitIntValue(0, 1); 1206 1207 // Code Alignment Factor 1208 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor"); 1209 streamer.EmitULEB128IntValue(1); 1210 1211 // Data Alignment Factor 1212 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor"); 1213 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); 1214 1215 // Return Address Register 1216 if (verboseAsm) streamer.AddComment("CIE Return Address Column"); 1217 streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true)); 1218 1219 // Augmentation Data Length (optional) 1220 1221 unsigned augmentationLength = 0; 1222 if (IsEH) { 1223 if (personality) { 1224 // Personality Encoding 1225 augmentationLength += 1; 1226 // Personality 1227 augmentationLength += getSizeForEncoding(streamer, personalityEncoding); 1228 } 1229 if (lsda) 1230 augmentationLength += 1; 1231 // Encoding of the FDE pointers 1232 augmentationLength += 1; 1233 1234 if (verboseAsm) streamer.AddComment("Augmentation Size"); 1235 streamer.EmitULEB128IntValue(augmentationLength); 1236 1237 // Augmentation Data (optional) 1238 if (personality) { 1239 // Personality Encoding 1240 EmitEncodingByte(streamer, personalityEncoding, 1241 "Personality Encoding"); 1242 // Personality 1243 if (verboseAsm) streamer.AddComment("Personality"); 1244 EmitPersonality(streamer, *personality, personalityEncoding); 1245 } 1246 1247 if (lsda) 1248 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding"); 1249 1250 // Encoding of the FDE pointers 1251 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI), 1252 "FDE Encoding"); 1253 } 1254 1255 // Initial Instructions 1256 1257 const MCAsmInfo &MAI = context.getAsmInfo(); 1258 const std::vector<MachineMove> &Moves = MAI.getInitialFrameState(); 1259 std::vector<MCCFIInstruction> Instructions; 1260 1261 for (int i = 0, n = Moves.size(); i != n; ++i) { 1262 MCSymbol *Label = Moves[i].getLabel(); 1263 const MachineLocation &Dst = 1264 TranslateMachineLocation(MRI, Moves[i].getDestination()); 1265 const MachineLocation &Src = 1266 TranslateMachineLocation(MRI, Moves[i].getSource()); 1267 MCCFIInstruction Inst(Label, Dst, Src); 1268 Instructions.push_back(Inst); 1269 } 1270 1271 EmitCFIInstructions(streamer, Instructions, NULL); 1272 1273 // Padding 1274 streamer.EmitValueToAlignment(IsEH 1275 ? 4 : context.getAsmInfo().getPointerSize()); 1276 1277 streamer.EmitLabel(sectionEnd); 1278 return *sectionStart; 1279} 1280 1281MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer, 1282 const MCSymbol &cieStart, 1283 const MCDwarfFrameInfo &frame) { 1284 MCContext &context = streamer.getContext(); 1285 MCSymbol *fdeStart = context.CreateTempSymbol(); 1286 MCSymbol *fdeEnd = context.CreateTempSymbol(); 1287 const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); 1288 bool verboseAsm = streamer.isVerboseAsm(); 1289 1290 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) { 1291 MCSymbol *EHSym = 1292 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh")); 1293 streamer.EmitEHSymAttributes(frame.Function, EHSym); 1294 streamer.EmitLabel(EHSym); 1295 } 1296 1297 // Length 1298 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); 1299 if (verboseAsm) streamer.AddComment("FDE Length"); 1300 streamer.EmitAbsValue(Length, 4); 1301 1302 streamer.EmitLabel(fdeStart); 1303 1304 // CIE Pointer 1305 const MCAsmInfo &asmInfo = context.getAsmInfo(); 1306 if (IsEH) { 1307 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, 1308 0); 1309 if (verboseAsm) streamer.AddComment("FDE CIE Offset"); 1310 streamer.EmitAbsValue(offset, 4); 1311 } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) { 1312 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart, 1313 cieStart, 0); 1314 streamer.EmitAbsValue(offset, 4); 1315 } else { 1316 streamer.EmitSymbolValue(&cieStart, 4); 1317 } 1318 1319 // PC Begin 1320 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI) 1321 : (unsigned)dwarf::DW_EH_PE_absptr; 1322 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding); 1323 EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location"); 1324 1325 // PC Range 1326 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, 1327 *frame.End, 0); 1328 if (verboseAsm) streamer.AddComment("FDE address range"); 1329 streamer.EmitAbsValue(Range, PCSize); 1330 1331 if (IsEH) { 1332 // Augmentation Data Length 1333 unsigned augmentationLength = 0; 1334 1335 if (frame.Lsda) 1336 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding); 1337 1338 if (verboseAsm) streamer.AddComment("Augmentation size"); 1339 streamer.EmitULEB128IntValue(augmentationLength); 1340 1341 // Augmentation Data 1342 if (frame.Lsda) 1343 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding, 1344 "Language Specific Data Area"); 1345 } 1346 1347 // Call Frame Instructions 1348 1349 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin); 1350 1351 // Padding 1352 streamer.EmitValueToAlignment(PCSize); 1353 1354 return fdeEnd; 1355} 1356 1357namespace { 1358 struct CIEKey { 1359 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); } 1360 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); } 1361 1362 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_, 1363 unsigned LsdaEncoding_, bool IsSignalFrame_) : 1364 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_), 1365 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) { 1366 } 1367 const MCSymbol* Personality; 1368 unsigned PersonalityEncoding; 1369 unsigned LsdaEncoding; 1370 bool IsSignalFrame; 1371 }; 1372} 1373 1374namespace llvm { 1375 template <> 1376 struct DenseMapInfo<CIEKey> { 1377 static CIEKey getEmptyKey() { 1378 return CIEKey::getEmptyKey(); 1379 } 1380 static CIEKey getTombstoneKey() { 1381 return CIEKey::getTombstoneKey(); 1382 } 1383 static unsigned getHashValue(const CIEKey &Key) { 1384 return static_cast<unsigned>(hash_combine(Key.Personality, 1385 Key.PersonalityEncoding, 1386 Key.LsdaEncoding, 1387 Key.IsSignalFrame)); 1388 } 1389 static bool isEqual(const CIEKey &LHS, 1390 const CIEKey &RHS) { 1391 return LHS.Personality == RHS.Personality && 1392 LHS.PersonalityEncoding == RHS.PersonalityEncoding && 1393 LHS.LsdaEncoding == RHS.LsdaEncoding && 1394 LHS.IsSignalFrame == RHS.IsSignalFrame; 1395 } 1396 }; 1397} 1398 1399void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, 1400 bool UsingCFI, 1401 bool IsEH) { 1402 MCContext &Context = Streamer.getContext(); 1403 MCObjectFileInfo *MOFI = 1404 const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo()); 1405 FrameEmitterImpl Emitter(UsingCFI, IsEH); 1406 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos(); 1407 1408 // Emit the compact unwind info if available. 1409 if (IsEH && MOFI->getCompactUnwindSection()) 1410 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) { 1411 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i); 1412 if (Frame.CompactUnwindEncoding) 1413 Emitter.EmitCompactUnwind(Streamer, Frame); 1414 } 1415 1416 const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() : 1417 *MOFI->getDwarfFrameSection(); 1418 Streamer.SwitchSection(&Section); 1419 MCSymbol *SectionStart = Context.CreateTempSymbol(); 1420 Streamer.EmitLabel(SectionStart); 1421 Emitter.setSectionStart(SectionStart); 1422 1423 MCSymbol *FDEEnd = NULL; 1424 DenseMap<CIEKey, const MCSymbol*> CIEStarts; 1425 1426 const MCSymbol *DummyDebugKey = NULL; 1427 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) { 1428 const MCDwarfFrameInfo &Frame = FrameArray[i]; 1429 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding, 1430 Frame.LsdaEncoding, Frame.IsSignalFrame); 1431 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey; 1432 if (!CIEStart) 1433 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality, 1434 Frame.PersonalityEncoding, Frame.Lsda, 1435 Frame.IsSignalFrame, 1436 Frame.LsdaEncoding); 1437 1438 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame); 1439 1440 if (i != n - 1) 1441 Streamer.EmitLabel(FDEEnd); 1442 } 1443 1444 Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize()); 1445 if (FDEEnd) 1446 Streamer.EmitLabel(FDEEnd); 1447} 1448 1449void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer, 1450 uint64_t AddrDelta) { 1451 SmallString<256> Tmp; 1452 raw_svector_ostream OS(Tmp); 1453 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS); 1454 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0); 1455} 1456 1457void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta, 1458 raw_ostream &OS) { 1459 // FIXME: Assumes the code alignment factor is 1. 1460 if (AddrDelta == 0) { 1461 } else if (isUIntN(6, AddrDelta)) { 1462 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta; 1463 OS << Opcode; 1464 } else if (isUInt<8>(AddrDelta)) { 1465 OS << uint8_t(dwarf::DW_CFA_advance_loc1); 1466 OS << uint8_t(AddrDelta); 1467 } else if (isUInt<16>(AddrDelta)) { 1468 // FIXME: check what is the correct behavior on a big endian machine. 1469 OS << uint8_t(dwarf::DW_CFA_advance_loc2); 1470 OS << uint8_t( AddrDelta & 0xff); 1471 OS << uint8_t((AddrDelta >> 8) & 0xff); 1472 } else { 1473 // FIXME: check what is the correct behavior on a big endian machine. 1474 assert(isUInt<32>(AddrDelta)); 1475 OS << uint8_t(dwarf::DW_CFA_advance_loc4); 1476 OS << uint8_t( AddrDelta & 0xff); 1477 OS << uint8_t((AddrDelta >> 8) & 0xff); 1478 OS << uint8_t((AddrDelta >> 16) & 0xff); 1479 OS << uint8_t((AddrDelta >> 24) & 0xff); 1480 1481 } 1482} 1483