Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCDwarf.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Hashing.h" |
| 12 | #include "llvm/ADT/SmallString.h" |
| 13 | #include "llvm/ADT/Twine.h" |
| 14 | #include "llvm/Config/config.h" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmInfo.h" |
| 16 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCExpr.h" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCObjectFileInfo.h" |
| 19 | #include "llvm/MC/MCObjectWriter.h" |
| 20 | #include "llvm/MC/MCRegisterInfo.h" |
Rafael Espindola | ad8aaa0 | 2010-11-22 11:53:17 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCStreamer.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSymbol.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/LEB128.h" |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 31 | // Given a special op, return the address skip amount (in units of |
| 32 | // DWARF2_LINE_MIN_INSN_LENGTH. |
| 33 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 34 | |
| 35 | // The maximum address skip amount that can be encoded with a special op. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 36 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 37 | |
| 38 | // First special line opcode - leave room for the standard opcodes. |
| 39 | // Note: If you want to change this, you'll have to update the |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 40 | // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 41 | #define DWARF2_LINE_OPCODE_BASE 13 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 42 | |
| 43 | // Minimum line offset in a special line info. opcode. This value |
| 44 | // was chosen to give a reasonable range of values. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 45 | #define DWARF2_LINE_BASE -5 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 46 | |
| 47 | // Range of line offsets in a special line info. opcode. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 48 | #define DWARF2_LINE_RANGE 14 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 49 | |
| 50 | // Define the architecture-dependent minimum instruction length (in bytes). |
| 51 | // This value should be rather too small than too big. |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 52 | #define DWARF2_LINE_MIN_INSN_LENGTH 1 |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 53 | |
| 54 | // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting, |
| 55 | // this routine is a nop and will be optimized away. |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 56 | static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 57 | if (DWARF2_LINE_MIN_INSN_LENGTH == 1) |
| 58 | return AddrDelta; |
| 59 | if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) { |
| 60 | // TODO: report this error, but really only once. |
| 61 | ; |
| 62 | } |
| 63 | return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH; |
| 64 | } |
| 65 | |
| 66 | // |
| 67 | // This is called when an instruction is assembled into the specified section |
| 68 | // and if there is information from the last .loc directive that has yet to have |
| 69 | // a line entry made for it is made. |
| 70 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 71 | void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 72 | if (!MCOS->getContext().getDwarfLocSeen()) |
| 73 | return; |
| 74 | |
| 75 | // Create a symbol at in the current section for use in the line entry. |
| 76 | MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol(); |
| 77 | // Set the value of the symbol to use for the MCLineEntry. |
| 78 | MCOS->EmitLabel(LineSym); |
| 79 | |
| 80 | // Get the current .loc info saved in the context. |
| 81 | const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc(); |
| 82 | |
| 83 | // Create a (local) line entry with the symbol and the current .loc info. |
| 84 | MCLineEntry LineEntry(LineSym, DwarfLoc); |
| 85 | |
| 86 | // clear DwarfLocSeen saying the current .loc info is now used. |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 87 | MCOS->getContext().ClearDwarfLocSeen(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 88 | |
| 89 | // Get the MCLineSection for this section, if one does not exist for this |
| 90 | // section create it. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 91 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 92 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 93 | MCLineSection *LineSection = MCLineSections.lookup(Section); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 94 | if (!LineSection) { |
| 95 | // Create a new MCLineSection. This will be deleted after the dwarf line |
| 96 | // table is created using it by iterating through the MCLineSections |
| 97 | // DenseMap. |
| 98 | LineSection = new MCLineSection; |
| 99 | // Save a pointer to the new LineSection into the MCLineSections DenseMap. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 100 | MCOS->getContext().addMCLineSection(Section, LineSection); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Add the line entry to this section's entries. |
| 104 | LineSection->addLineEntry(LineEntry); |
| 105 | } |
| 106 | |
| 107 | // |
| 108 | // This helper routine returns an expression of End - Start + IntVal . |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 109 | // |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 110 | static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS, |
| 111 | const MCSymbol &Start, |
| 112 | const MCSymbol &End, |
| 113 | int IntVal) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 114 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 115 | const MCExpr *Res = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 116 | MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 117 | const MCExpr *RHS = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 118 | MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 119 | const MCExpr *Res1 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 120 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 121 | const MCExpr *Res2 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 122 | MCConstantExpr::Create(IntVal, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 123 | const MCExpr *Res3 = |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 124 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 125 | return Res3; |
| 126 | } |
| 127 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 128 | // |
| 129 | // This emits the Dwarf line table for the specified section from the entries |
| 130 | // in the LineSection. |
| 131 | // |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 132 | static inline void EmitDwarfLineTable(MCStreamer *MCOS, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 133 | const MCSection *Section, |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 134 | const MCLineSection *LineSection) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 135 | unsigned FileNum = 1; |
| 136 | unsigned LastLine = 1; |
| 137 | unsigned Column = 0; |
| 138 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 139 | unsigned Isa = 0; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 140 | MCSymbol *LastLabel = NULL; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 141 | |
| 142 | // Loop through each MCLineEntry and encode the dwarf line number table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 143 | for (MCLineSection::const_iterator |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 144 | it = LineSection->getMCLineEntries()->begin(), |
| 145 | ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) { |
| 146 | |
| 147 | if (FileNum != it->getFileNum()) { |
| 148 | FileNum = it->getFileNum(); |
| 149 | MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 150 | MCOS->EmitULEB128IntValue(FileNum); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 151 | } |
| 152 | if (Column != it->getColumn()) { |
| 153 | Column = it->getColumn(); |
| 154 | MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 155 | MCOS->EmitULEB128IntValue(Column); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 156 | } |
| 157 | if (Isa != it->getIsa()) { |
| 158 | Isa = it->getIsa(); |
| 159 | MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 160 | MCOS->EmitULEB128IntValue(Isa); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 161 | } |
| 162 | if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { |
| 163 | Flags = it->getFlags(); |
| 164 | MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); |
| 165 | } |
| 166 | if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK) |
| 167 | MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); |
| 168 | if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END) |
| 169 | MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); |
| 170 | if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) |
| 171 | MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); |
| 172 | |
Rafael Espindola | 64185cc | 2010-11-13 01:06:27 +0000 | [diff] [blame] | 173 | int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 174 | MCSymbol *Label = it->getLabel(); |
| 175 | |
| 176 | // At this point we want to emit/create the sequence to encode the delta in |
| 177 | // line numbers and the increment of the address from the previous Label |
| 178 | // and the current Label. |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 179 | const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 180 | MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, |
| 181 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 182 | |
| 183 | LastLine = it->getLine(); |
| 184 | LastLabel = Label; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Emit a DW_LNE_end_sequence for the end of the section. |
| 188 | // Using the pointer Section create a temporary label at the end of the |
| 189 | // section and use that and the LastLabel to compute the address delta |
| 190 | // and use INT64_MAX as the line delta which is the signal that this is |
| 191 | // actually a DW_LNE_end_sequence. |
| 192 | |
| 193 | // Switch to the section to be able to create a symbol at its end. |
| 194 | MCOS->SwitchSection(Section); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 195 | |
| 196 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 197 | // Create a symbol at the end of the section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 198 | MCSymbol *SectionEnd = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 199 | // Set the value of the symbol, as we are at the end of the section. |
| 200 | MCOS->EmitLabel(SectionEnd); |
| 201 | |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 202 | // Switch back the dwarf line section. |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 203 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 204 | |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 205 | const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo(); |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 206 | MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd, |
| 207 | asmInfo.getPointerSize()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // |
| 211 | // This emits the Dwarf file and the line tables. |
| 212 | // |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 213 | const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 214 | MCContext &context = MCOS->getContext(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 215 | // Switch to the section where the table will be emitted into. |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 216 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 217 | |
| 218 | // Create a symbol at the beginning of this section. |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 219 | MCSymbol *LineStartSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 220 | // Set the value of the symbol, as we are at the start of the section. |
| 221 | MCOS->EmitLabel(LineStartSym); |
| 222 | |
| 223 | // Create a symbol for the end of the section (to be set when we get there). |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 224 | MCSymbol *LineEndSym = context.CreateTempSymbol(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 225 | |
| 226 | // The first 4 bytes is the total length of the information for this |
| 227 | // compilation unit (not including these 4 bytes for the length). |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 228 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4), |
Rafael Espindola | 0bbe0b4 | 2010-12-06 17:27:56 +0000 | [diff] [blame] | 229 | 4); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 230 | |
| 231 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 232 | MCOS->EmitIntValue(2, 2); |
| 233 | |
| 234 | // Create a symbol for the end of the prologue (to be set when we get there). |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 235 | MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 236 | |
| 237 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 238 | // section to the end of the prologue. Not including the 4 bytes for the |
| 239 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 240 | // length of the prologue. |
Rafael Espindola | 5fad7a9 | 2010-12-09 23:08:35 +0000 | [diff] [blame] | 241 | MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 242 | (4 + 2 + 4)), |
Rafael Espindola | 5d4918d | 2010-12-04 03:21:47 +0000 | [diff] [blame] | 243 | 4, 0); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 244 | |
| 245 | // Parameters of the state machine, are next. |
| 246 | MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 247 | MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 248 | MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); |
| 249 | MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); |
| 250 | MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1); |
| 251 | |
| 252 | // Standard opcode lengths |
| 253 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy |
| 254 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 255 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 256 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 257 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 258 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 259 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 260 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 261 | MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 262 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 263 | MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 264 | MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa |
| 265 | |
| 266 | // Put out the directory and file tables. |
| 267 | |
| 268 | // First the directory table. |
| 269 | const std::vector<StringRef> &MCDwarfDirs = |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 270 | context.getMCDwarfDirs(); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 271 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 272 | MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName |
| 273 | MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 274 | } |
| 275 | MCOS->EmitIntValue(0, 1); // Terminate the directory list |
| 276 | |
| 277 | // Second the file table. |
| 278 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 279 | MCOS->getContext().getMCDwarfFiles(); |
| 280 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 281 | MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName |
| 282 | MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 283 | // the Directory num |
| 284 | MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 285 | MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 286 | MCOS->EmitIntValue(0, 1); // filesize (always 0) |
| 287 | } |
| 288 | MCOS->EmitIntValue(0, 1); // Terminate the file list |
| 289 | |
| 290 | // This is the end of the prologue, so set the value of the symbol at the |
| 291 | // end of the prologue (that was used in a previous expression). |
| 292 | MCOS->EmitLabel(ProEndSym); |
| 293 | |
| 294 | // Put out the line tables. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 295 | const DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 296 | MCOS->getContext().getMCLineSections(); |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 297 | const std::vector<const MCSection *> &MCLineSectionOrder = |
| 298 | MCOS->getContext().getMCLineSectionOrder(); |
| 299 | for (std::vector<const MCSection*>::const_iterator it = |
Bill Wendling | c388216 | 2011-06-30 23:59:38 +0000 | [diff] [blame] | 300 | MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie; |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 301 | ++it) { |
| 302 | const MCSection *Sec = *it; |
| 303 | const MCLineSection *Line = MCLineSections.lookup(Sec); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 304 | EmitDwarfLineTable(MCOS, Sec, Line); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 305 | |
| 306 | // Now delete the MCLineSections that were created in MCLineEntry::Make() |
| 307 | // and used to emit the line table. |
Rafael Espindola | 17fd7bd | 2010-11-19 07:41:23 +0000 | [diff] [blame] | 308 | delete Line; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Rafael Espindola | 767b1be | 2010-12-04 00:31:13 +0000 | [diff] [blame] | 311 | if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines() |
| 312 | && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) { |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 313 | // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 314 | // it requires: |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 315 | // total_length >= prologue_length + 10 |
| 316 | // We are 4 bytes short, since we have total_length = 51 and |
| 317 | // prologue_length = 45 |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 318 | |
Rafael Espindola | a8de83c | 2010-12-03 23:36:59 +0000 | [diff] [blame] | 319 | // The regular end_sequence should be sufficient. |
| 320 | MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0); |
Devang Patel | 5113cdb | 2010-12-03 00:10:48 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 323 | // This is the end of the section, so set the value of the symbol at the end |
| 324 | // of this section (that was used in a previous expression). |
| 325 | MCOS->EmitLabel(LineEndSym); |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 326 | |
| 327 | return LineStartSym; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 330 | /// Utility function to write the encoding to an object writer. |
| 331 | void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta, |
| 332 | uint64_t AddrDelta) { |
| 333 | SmallString<256> Tmp; |
| 334 | raw_svector_ostream OS(Tmp); |
| 335 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
| 336 | OW->WriteBytes(OS.str()); |
| 337 | } |
| 338 | |
| 339 | /// Utility function to emit the encoding to a streamer. |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 340 | void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 341 | uint64_t AddrDelta) { |
| 342 | SmallString<256> Tmp; |
| 343 | raw_svector_ostream OS(Tmp); |
| 344 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS); |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 345 | MCOS->EmitBytes(OS.str()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. |
| 349 | void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta, |
| 350 | raw_ostream &OS) { |
| 351 | uint64_t Temp, Opcode; |
| 352 | bool NeedCopy = false; |
| 353 | |
| 354 | // Scale the address delta by the minimum instruction length. |
| 355 | AddrDelta = ScaleAddrDelta(AddrDelta); |
| 356 | |
| 357 | // A LineDelta of INT64_MAX is a signal that this is actually a |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 358 | // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 359 | // end_sequence to emit the matrix entry. |
| 360 | if (LineDelta == INT64_MAX) { |
| 361 | if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) |
| 362 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 363 | else { |
| 364 | OS << char(dwarf::DW_LNS_advance_pc); |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 365 | encodeULEB128(AddrDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 366 | } |
| 367 | OS << char(dwarf::DW_LNS_extended_op); |
| 368 | OS << char(1); |
| 369 | OS << char(dwarf::DW_LNE_end_sequence); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | // Bias the line delta by the base. |
| 374 | Temp = LineDelta - DWARF2_LINE_BASE; |
| 375 | |
| 376 | // If the line increment is out of range of a special opcode, we must encode |
| 377 | // it with DW_LNS_advance_line. |
| 378 | if (Temp >= DWARF2_LINE_RANGE) { |
| 379 | OS << char(dwarf::DW_LNS_advance_line); |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 380 | encodeSLEB128(LineDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 381 | |
| 382 | LineDelta = 0; |
| 383 | Temp = 0 - DWARF2_LINE_BASE; |
| 384 | NeedCopy = true; |
| 385 | } |
| 386 | |
| 387 | // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode. |
| 388 | if (LineDelta == 0 && AddrDelta == 0) { |
| 389 | OS << char(dwarf::DW_LNS_copy); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | // Bias the opcode by the special opcode base. |
| 394 | Temp += DWARF2_LINE_OPCODE_BASE; |
| 395 | |
| 396 | // Avoid overflow when addr_delta is large. |
| 397 | if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { |
| 398 | // Try using a special opcode. |
| 399 | Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; |
| 400 | if (Opcode <= 255) { |
| 401 | OS << char(Opcode); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | // Try using DW_LNS_const_add_pc followed by special op. |
| 406 | Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 407 | if (Opcode <= 255) { |
| 408 | OS << char(dwarf::DW_LNS_const_add_pc); |
| 409 | OS << char(Opcode); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Otherwise use DW_LNS_advance_pc. |
| 415 | OS << char(dwarf::DW_LNS_advance_pc); |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 416 | encodeULEB128(AddrDelta, OS); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 417 | |
| 418 | if (NeedCopy) |
| 419 | OS << char(dwarf::DW_LNS_copy); |
| 420 | else |
| 421 | OS << char(Temp); |
| 422 | } |
| 423 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 424 | void MCDwarfFile::print(raw_ostream &OS) const { |
| 425 | OS << '"' << getName() << '"'; |
| 426 | } |
| 427 | |
Manman Ren | 286c4dc | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 428 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 429 | void MCDwarfFile::dump() const { |
| 430 | print(dbgs()); |
| 431 | } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 432 | #endif |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 433 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 434 | // Utility function to write a tuple for .debug_abbrev. |
| 435 | static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) { |
| 436 | MCOS->EmitULEB128IntValue(Name); |
| 437 | MCOS->EmitULEB128IntValue(Form); |
| 438 | } |
| 439 | |
| 440 | // When generating dwarf for assembly source files this emits |
| 441 | // the data for .debug_abbrev section which contains three DIEs. |
| 442 | static void EmitGenDwarfAbbrev(MCStreamer *MCOS) { |
| 443 | MCContext &context = MCOS->getContext(); |
| 444 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); |
| 445 | |
| 446 | // DW_TAG_compile_unit DIE abbrev (1). |
| 447 | MCOS->EmitULEB128IntValue(1); |
| 448 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit); |
| 449 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); |
| 450 | EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4); |
| 451 | EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); |
| 452 | EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr); |
| 453 | EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); |
| 454 | EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string); |
| 455 | StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); |
| 456 | if (!DwarfDebugFlags.empty()) |
| 457 | EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string); |
| 458 | EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string); |
| 459 | EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2); |
| 460 | EmitAbbrev(MCOS, 0, 0); |
| 461 | |
Kevin Enderby | 38fdb7d | 2012-01-10 17:52:29 +0000 | [diff] [blame] | 462 | // DW_TAG_label DIE abbrev (2). |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 463 | MCOS->EmitULEB128IntValue(2); |
Kevin Enderby | 38fdb7d | 2012-01-10 17:52:29 +0000 | [diff] [blame] | 464 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 465 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); |
| 466 | EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string); |
| 467 | EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4); |
| 468 | EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4); |
| 469 | EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 470 | EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag); |
| 471 | EmitAbbrev(MCOS, 0, 0); |
| 472 | |
| 473 | // DW_TAG_unspecified_parameters DIE abbrev (3). |
| 474 | MCOS->EmitULEB128IntValue(3); |
| 475 | MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters); |
| 476 | MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1); |
| 477 | EmitAbbrev(MCOS, 0, 0); |
| 478 | |
| 479 | // Terminate the abbreviations for this compilation unit. |
| 480 | MCOS->EmitIntValue(0, 1); |
| 481 | } |
| 482 | |
| 483 | // When generating dwarf for assembly source files this emits the data for |
| 484 | // .debug_aranges section. Which contains a header and a table of pairs of |
| 485 | // PointerSize'ed values for the address and size of section(s) with line table |
| 486 | // entries (just the default .text in our case) and a terminating pair of zeros. |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 487 | static void EmitGenDwarfAranges(MCStreamer *MCOS, |
| 488 | const MCSymbol *InfoSectionSymbol) { |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 489 | MCContext &context = MCOS->getContext(); |
| 490 | |
| 491 | // Create a symbol at the end of the section that we are creating the dwarf |
| 492 | // debugging info to use later in here as part of the expression to calculate |
| 493 | // the size of the section for the table. |
| 494 | MCOS->SwitchSection(context.getGenDwarfSection()); |
| 495 | MCSymbol *SectionEndSym = context.CreateTempSymbol(); |
| 496 | MCOS->EmitLabel(SectionEndSym); |
| 497 | context.setGenDwarfSectionEndSym(SectionEndSym); |
| 498 | |
| 499 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); |
| 500 | |
| 501 | // This will be the length of the .debug_aranges section, first account for |
| 502 | // the size of each item in the header (see below where we emit these items). |
| 503 | int Length = 4 + 2 + 4 + 1 + 1; |
| 504 | |
| 505 | // Figure the padding after the header before the table of address and size |
| 506 | // pairs who's values are PointerSize'ed. |
| 507 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 508 | int AddrSize = asmInfo.getPointerSize(); |
| 509 | int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1)); |
| 510 | if (Pad == 2 * AddrSize) |
| 511 | Pad = 0; |
| 512 | Length += Pad; |
| 513 | |
| 514 | // Add the size of the pair of PointerSize'ed values for the address and size |
| 515 | // of the one default .text section we have in the table. |
| 516 | Length += 2 * AddrSize; |
| 517 | // And the pair of terminating zeros. |
| 518 | Length += 2 * AddrSize; |
| 519 | |
| 520 | |
| 521 | // Emit the header for this section. |
| 522 | // The 4 byte length not including the 4 byte value for the length. |
| 523 | MCOS->EmitIntValue(Length - 4, 4); |
| 524 | // The 2 byte version, which is 2. |
| 525 | MCOS->EmitIntValue(2, 2); |
| 526 | // The 4 byte offset to the compile unit in the .debug_info from the start |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 527 | // of the .debug_info. |
| 528 | if (InfoSectionSymbol) |
| 529 | MCOS->EmitSymbolValue(InfoSectionSymbol, 4); |
| 530 | else |
| 531 | MCOS->EmitIntValue(0, 4); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 532 | // The 1 byte size of an address. |
| 533 | MCOS->EmitIntValue(AddrSize, 1); |
| 534 | // The 1 byte size of a segment descriptor, we use a value of zero. |
| 535 | MCOS->EmitIntValue(0, 1); |
| 536 | // Align the header with the padding if needed, before we put out the table. |
| 537 | for(int i = 0; i < Pad; i++) |
| 538 | MCOS->EmitIntValue(0, 1); |
| 539 | |
| 540 | // Now emit the table of pairs of PointerSize'ed values for the section(s) |
| 541 | // address and size, in our case just the one default .text section. |
| 542 | const MCExpr *Addr = MCSymbolRefExpr::Create( |
| 543 | context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); |
| 544 | const MCExpr *Size = MakeStartMinusEndExpr(*MCOS, |
| 545 | *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0); |
| 546 | MCOS->EmitAbsValue(Addr, AddrSize); |
| 547 | MCOS->EmitAbsValue(Size, AddrSize); |
| 548 | |
| 549 | // And finally the pair of terminating zeros. |
| 550 | MCOS->EmitIntValue(0, AddrSize); |
| 551 | MCOS->EmitIntValue(0, AddrSize); |
| 552 | } |
| 553 | |
| 554 | // When generating dwarf for assembly source files this emits the data for |
| 555 | // .debug_info section which contains three parts. The header, the compile_unit |
Kevin Enderby | 38fdb7d | 2012-01-10 17:52:29 +0000 | [diff] [blame] | 556 | // DIE and a list of label DIEs. |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 557 | static void EmitGenDwarfInfo(MCStreamer *MCOS, |
| 558 | const MCSymbol *AbbrevSectionSymbol, |
| 559 | const MCSymbol *LineSectionSymbol) { |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 560 | MCContext &context = MCOS->getContext(); |
| 561 | |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 562 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 563 | |
| 564 | // Create a symbol at the start and end of this section used in here for the |
| 565 | // expression to calculate the length in the header. |
| 566 | MCSymbol *InfoStart = context.CreateTempSymbol(); |
| 567 | MCOS->EmitLabel(InfoStart); |
| 568 | MCSymbol *InfoEnd = context.CreateTempSymbol(); |
| 569 | |
| 570 | // First part: the header. |
| 571 | |
| 572 | // The 4 byte total length of the information for this compilation unit, not |
| 573 | // including these 4 bytes. |
| 574 | const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4); |
| 575 | MCOS->EmitAbsValue(Length, 4); |
| 576 | |
| 577 | // The 2 byte DWARF version, which is 2. |
| 578 | MCOS->EmitIntValue(2, 2); |
| 579 | |
| 580 | // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev, |
| 581 | // it is at the start of that section so this is zero. |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 582 | if (AbbrevSectionSymbol) { |
| 583 | MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4); |
| 584 | } else { |
| 585 | MCOS->EmitIntValue(0, 4); |
| 586 | } |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 587 | |
| 588 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 589 | int AddrSize = asmInfo.getPointerSize(); |
| 590 | // The 1 byte size of an address. |
| 591 | MCOS->EmitIntValue(AddrSize, 1); |
| 592 | |
| 593 | // Second part: the compile_unit DIE. |
| 594 | |
| 595 | // The DW_TAG_compile_unit DIE abbrev (1). |
| 596 | MCOS->EmitULEB128IntValue(1); |
| 597 | |
| 598 | // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section, |
| 599 | // which is at the start of that section so this is zero. |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 600 | if (LineSectionSymbol) { |
| 601 | MCOS->EmitSymbolValue(LineSectionSymbol, 4); |
| 602 | } else { |
| 603 | MCOS->EmitIntValue(0, 4); |
| 604 | } |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 605 | |
| 606 | // AT_low_pc, the first address of the default .text section. |
| 607 | const MCExpr *Start = MCSymbolRefExpr::Create( |
| 608 | context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context); |
| 609 | MCOS->EmitAbsValue(Start, AddrSize); |
| 610 | |
| 611 | // AT_high_pc, the last address of the default .text section. |
| 612 | const MCExpr *End = MCSymbolRefExpr::Create( |
| 613 | context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context); |
| 614 | MCOS->EmitAbsValue(End, AddrSize); |
| 615 | |
| 616 | // AT_name, the name of the source file. Reconstruct from the first directory |
| 617 | // and file table entries. |
| 618 | const std::vector<StringRef> &MCDwarfDirs = |
| 619 | context.getMCDwarfDirs(); |
| 620 | if (MCDwarfDirs.size() > 0) { |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 621 | MCOS->EmitBytes(MCDwarfDirs[0]); |
| 622 | MCOS->EmitBytes("/"); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 623 | } |
| 624 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 625 | MCOS->getContext().getMCDwarfFiles(); |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 626 | MCOS->EmitBytes(MCDwarfFiles[1]->getName()); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 627 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 628 | |
| 629 | // AT_comp_dir, the working directory the assembly was done in. |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 630 | MCOS->EmitBytes(context.getCompilationDir()); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 631 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 632 | |
| 633 | // AT_APPLE_flags, the command line arguments of the assembler tool. |
| 634 | StringRef DwarfDebugFlags = context.getDwarfDebugFlags(); |
| 635 | if (!DwarfDebugFlags.empty()){ |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 636 | MCOS->EmitBytes(DwarfDebugFlags); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 637 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 638 | } |
| 639 | |
| 640 | // AT_producer, the version of the assembler tool. |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 641 | MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM ")); |
| 642 | MCOS->EmitBytes(StringRef(PACKAGE_VERSION)); |
| 643 | MCOS->EmitBytes(StringRef(")")); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 644 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 645 | |
| 646 | // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2 |
| 647 | // draft has no standard code for assembler. |
| 648 | MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2); |
| 649 | |
Kevin Enderby | 38fdb7d | 2012-01-10 17:52:29 +0000 | [diff] [blame] | 650 | // Third part: the list of label DIEs. |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 651 | |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 652 | // Loop on saved info for dwarf labels and create the DIEs for them. |
| 653 | const std::vector<const MCGenDwarfLabelEntry *> &Entries = |
| 654 | MCOS->getContext().getMCGenDwarfLabelEntries(); |
| 655 | for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it = |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 656 | Entries.begin(), ie = Entries.end(); it != ie; |
| 657 | ++it) { |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 658 | const MCGenDwarfLabelEntry *Entry = *it; |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 659 | |
Kevin Enderby | 38fdb7d | 2012-01-10 17:52:29 +0000 | [diff] [blame] | 660 | // The DW_TAG_label DIE abbrev (2). |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 661 | MCOS->EmitULEB128IntValue(2); |
| 662 | |
| 663 | // AT_name, of the label without any leading underbar. |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 664 | MCOS->EmitBytes(Entry->getName()); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 665 | MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. |
| 666 | |
| 667 | // AT_decl_file, index into the file table. |
| 668 | MCOS->EmitIntValue(Entry->getFileNumber(), 4); |
| 669 | |
| 670 | // AT_decl_line, source line number. |
| 671 | MCOS->EmitIntValue(Entry->getLineNumber(), 4); |
| 672 | |
| 673 | // AT_low_pc, start address of the label. |
| 674 | const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(), |
| 675 | MCSymbolRefExpr::VK_None, context); |
| 676 | MCOS->EmitAbsValue(AT_low_pc, AddrSize); |
| 677 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 678 | // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype. |
| 679 | MCOS->EmitIntValue(0, 1); |
| 680 | |
| 681 | // The DW_TAG_unspecified_parameters DIE abbrev (3). |
| 682 | MCOS->EmitULEB128IntValue(3); |
| 683 | |
| 684 | // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's. |
| 685 | MCOS->EmitIntValue(0, 1); |
| 686 | } |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 687 | // Deallocate the MCGenDwarfLabelEntry classes that saved away the info |
| 688 | // for the dwarf labels. |
| 689 | for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it = |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 690 | Entries.begin(), ie = Entries.end(); it != ie; |
| 691 | ++it) { |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 692 | const MCGenDwarfLabelEntry *Entry = *it; |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 693 | delete Entry; |
| 694 | } |
| 695 | |
| 696 | // Add the NULL DIE terminating the Compile Unit DIE's. |
| 697 | MCOS->EmitIntValue(0, 1); |
| 698 | |
| 699 | // Now set the value of the symbol at the end of the info section. |
| 700 | MCOS->EmitLabel(InfoEnd); |
| 701 | } |
| 702 | |
| 703 | // |
| 704 | // When generating dwarf for assembly source files this emits the Dwarf |
| 705 | // sections. |
| 706 | // |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 707 | void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) { |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 708 | // Create the dwarf sections in this order (.debug_line already created). |
| 709 | MCContext &context = MCOS->getContext(); |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 710 | const MCAsmInfo &AsmInfo = context.getAsmInfo(); |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 711 | bool CreateDwarfSectionSymbols = |
| 712 | AsmInfo.doesDwarfUseRelocationsAcrossSections(); |
| 713 | if (!CreateDwarfSectionSymbols) |
| 714 | LineSectionSymbol = NULL; |
| 715 | MCSymbol *AbbrevSectionSymbol = NULL; |
| 716 | MCSymbol *InfoSectionSymbol = NULL; |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 717 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 718 | if (CreateDwarfSectionSymbols) { |
| 719 | InfoSectionSymbol = context.CreateTempSymbol(); |
| 720 | MCOS->EmitLabel(InfoSectionSymbol); |
| 721 | } |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 722 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 723 | if (CreateDwarfSectionSymbols) { |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 724 | AbbrevSectionSymbol = context.CreateTempSymbol(); |
| 725 | MCOS->EmitLabel(AbbrevSectionSymbol); |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 726 | } |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 727 | MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); |
| 728 | |
| 729 | // If there are no line table entries then do not emit any section contents. |
| 730 | if (context.getMCLineSections().empty()) |
| 731 | return; |
| 732 | |
| 733 | // Output the data for .debug_aranges section. |
Alexey Samsonov | 15ab115 | 2012-11-14 09:55:38 +0000 | [diff] [blame] | 734 | EmitGenDwarfAranges(MCOS, InfoSectionSymbol); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 735 | |
| 736 | // Output the data for .debug_abbrev section. |
| 737 | EmitGenDwarfAbbrev(MCOS); |
| 738 | |
| 739 | // Output the data for .debug_info section. |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 740 | EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // |
| 744 | // When generating dwarf for assembly source files this is called when symbol |
| 745 | // for a label is created. If this symbol is not a temporary and is in the |
| 746 | // section that dwarf is being generated for, save the needed info to create |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 747 | // a dwarf label. |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 748 | // |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 749 | void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS, |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 750 | SourceMgr &SrcMgr, SMLoc &Loc) { |
Eric Christopher | 3c3cdcc | 2012-02-25 01:02:44 +0000 | [diff] [blame] | 751 | // We won't create dwarf labels for temporary symbols or symbols not in |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 752 | // the default text. |
| 753 | if (Symbol->isTemporary()) |
| 754 | return; |
| 755 | MCContext &context = MCOS->getContext(); |
| 756 | if (context.getGenDwarfSection() != MCOS->getCurrentSection()) |
| 757 | return; |
| 758 | |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 759 | // The dwarf label's name does not have the symbol name's leading |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 760 | // underbar if any. |
| 761 | StringRef Name = Symbol->getName(); |
| 762 | if (Name.startswith("_")) |
| 763 | Name = Name.substr(1, Name.size()-1); |
| 764 | |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 765 | // Get the dwarf file number to be used for the dwarf label. |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 766 | unsigned FileNumber = context.getGenDwarfFileNumber(); |
| 767 | |
| 768 | // Finding the line number is the expensive part which is why we just don't |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 769 | // pass it in as for some symbols we won't create a dwarf label. |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 770 | int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); |
| 771 | unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer); |
| 772 | |
| 773 | // We create a temporary symbol for use for the AT_high_pc and AT_low_pc |
| 774 | // values so that they don't have things like an ARM thumb bit from the |
| 775 | // original symbol. So when used they won't get a low bit set after |
| 776 | // relocation. |
| 777 | MCSymbol *Label = context.CreateTempSymbol(); |
| 778 | MCOS->EmitLabel(Label); |
| 779 | |
| 780 | // Create and entry for the info and add it to the other entries. |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 781 | MCGenDwarfLabelEntry *Entry = |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 782 | new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label); |
| 783 | MCOS->getContext().addMCGenDwarfLabelEntry(Entry); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 786 | static int getDataAlignmentFactor(MCStreamer &streamer) { |
| 787 | MCContext &context = streamer.getContext(); |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 788 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 789 | int size = asmInfo.getPointerSize(); |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 790 | if (asmInfo.isStackGrowthDirectionUp()) |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 791 | return size; |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 792 | else |
| 793 | return -size; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 796 | static unsigned getSizeForEncoding(MCStreamer &streamer, |
| 797 | unsigned symbolEncoding) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 798 | MCContext &context = streamer.getContext(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 799 | unsigned format = symbolEncoding & 0x0f; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 800 | switch (format) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 801 | default: llvm_unreachable("Unknown Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 802 | case dwarf::DW_EH_PE_absptr: |
| 803 | case dwarf::DW_EH_PE_signed: |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 804 | return context.getAsmInfo().getPointerSize(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 805 | case dwarf::DW_EH_PE_udata2: |
| 806 | case dwarf::DW_EH_PE_sdata2: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 807 | return 2; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 808 | case dwarf::DW_EH_PE_udata4: |
| 809 | case dwarf::DW_EH_PE_sdata4: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 810 | return 4; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 811 | case dwarf::DW_EH_PE_udata8: |
| 812 | case dwarf::DW_EH_PE_sdata8: |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 813 | return 8; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 814 | } |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol, |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 818 | unsigned symbolEncoding, const char *comment = 0) { |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 819 | MCContext &context = streamer.getContext(); |
| 820 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
| 821 | const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 822 | symbolEncoding, |
Rafael Espindola | a0057ca | 2011-04-28 21:04:39 +0000 | [diff] [blame] | 823 | streamer); |
Rafael Espindola | abf9af6 | 2011-04-22 00:08:43 +0000 | [diff] [blame] | 824 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 825 | if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 826 | streamer.EmitAbsValue(v, size); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 829 | static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, |
| 830 | unsigned symbolEncoding) { |
| 831 | MCContext &context = streamer.getContext(); |
| 832 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 833 | const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol, |
| 834 | symbolEncoding, |
| 835 | streamer); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 836 | unsigned size = getSizeForEncoding(streamer, symbolEncoding); |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 837 | streamer.EmitValue(v, size); |
Rafael Espindola | bfa27cc | 2011-04-28 16:09:09 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 840 | static const MachineLocation TranslateMachineLocation( |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 841 | const MCRegisterInfo &MRI, |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 842 | const MachineLocation &Loc) { |
| 843 | unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ? |
| 844 | MachineLocation::VirtualFP : |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 845 | unsigned(MRI.getDwarfRegNum(Loc.getReg(), true)); |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 846 | const MachineLocation &NewLoc = Loc.isReg() ? |
| 847 | MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset()); |
| 848 | return NewLoc; |
| 849 | } |
| 850 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 851 | namespace { |
| 852 | class FrameEmitterImpl { |
| 853 | int CFAOffset; |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 854 | int CIENum; |
Rafael Espindola | 5426a9e | 2011-05-01 04:49:54 +0000 | [diff] [blame] | 855 | bool UsingCFI; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 856 | bool IsEH; |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 857 | const MCSymbol *SectionStart; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 858 | public: |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 859 | FrameEmitterImpl(bool usingCFI, bool isEH) |
| 860 | : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH), |
| 861 | SectionStart(0) {} |
| 862 | |
| 863 | void setSectionStart(const MCSymbol *Label) { SectionStart = Label; } |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 864 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 865 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If |
| 866 | /// we're successful, return 'true'. Otherwise, return 'false' and it will |
| 867 | /// emit the normal CIE and FDE. |
| 868 | bool EmitCompactUnwind(MCStreamer &streamer, |
| 869 | const MCDwarfFrameInfo &frame); |
| 870 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 871 | const MCSymbol &EmitCIE(MCStreamer &streamer, |
| 872 | const MCSymbol *personality, |
| 873 | unsigned personalityEncoding, |
| 874 | const MCSymbol *lsda, |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 875 | bool IsSignalFrame, |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 876 | unsigned lsdaEncoding); |
| 877 | MCSymbol *EmitFDE(MCStreamer &streamer, |
| 878 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 879 | const MCDwarfFrameInfo &frame); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 880 | void EmitCFIInstructions(MCStreamer &streamer, |
| 881 | const std::vector<MCCFIInstruction> &Instrs, |
| 882 | MCSymbol *BaseLabel); |
| 883 | void EmitCFIInstruction(MCStreamer &Streamer, |
| 884 | const MCCFIInstruction &Instr); |
| 885 | }; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 886 | |
| 887 | } // end anonymous namespace |
| 888 | |
| 889 | static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding, |
| 890 | StringRef Prefix) { |
| 891 | if (Streamer.isVerboseAsm()) { |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 892 | const char *EncStr; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 893 | switch (Encoding) { |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 894 | default: EncStr = "<unknown encoding>"; break; |
| 895 | case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break; |
| 896 | case dwarf::DW_EH_PE_omit: EncStr = "omit"; break; |
| 897 | case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break; |
| 898 | case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break; |
| 899 | case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break; |
| 900 | case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break; |
| 901 | case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break; |
| 902 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: |
| 903 | EncStr = "pcrel udata4"; |
| 904 | break; |
| 905 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: |
| 906 | EncStr = "pcrel sdata4"; |
| 907 | break; |
| 908 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: |
| 909 | EncStr = "pcrel udata8"; |
| 910 | break; |
| 911 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: |
| 912 | EncStr = "screl sdata8"; |
| 913 | break; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 914 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4: |
| 915 | EncStr = "indirect pcrel udata4"; |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 916 | break; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 917 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4: |
| 918 | EncStr = "indirect pcrel sdata4"; |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 919 | break; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 920 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8: |
| 921 | EncStr = "indirect pcrel udata8"; |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 922 | break; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 923 | case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8: |
| 924 | EncStr = "indirect pcrel sdata8"; |
Benjamin Kramer | eb9fa66 | 2012-01-20 14:42:37 +0000 | [diff] [blame] | 925 | break; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | Streamer.AddComment(Twine(Prefix) + " = " + EncStr); |
| 929 | } |
| 930 | |
| 931 | Streamer.EmitIntValue(Encoding, 1); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, |
| 935 | const MCCFIInstruction &Instr) { |
| 936 | int dataAlignmentFactor = getDataAlignmentFactor(Streamer); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 937 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 938 | |
| 939 | switch (Instr.getOperation()) { |
Rafael Espindola | f4f14f6 | 2012-11-25 15:14:49 +0000 | [diff] [blame] | 940 | case MCCFIInstruction::OpRegister: { |
| 941 | unsigned Reg1 = Instr.getRegister(); |
| 942 | unsigned Reg2 = Instr.getRegister2(); |
| 943 | if (VerboseAsm) { |
| 944 | Streamer.AddComment("DW_CFA_register"); |
| 945 | Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1)); |
| 946 | Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2)); |
| 947 | } |
| 948 | Streamer.EmitIntValue(dwarf::DW_CFA_register, 1); |
| 949 | Streamer.EmitULEB128IntValue(Reg1); |
| 950 | Streamer.EmitULEB128IntValue(Reg2); |
| 951 | return; |
| 952 | } |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 953 | case MCCFIInstruction::OpUndefined: { |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 954 | unsigned Reg = Instr.getRegister(); |
Rafael Espindola | c8fec7e | 2012-11-23 16:59:41 +0000 | [diff] [blame] | 955 | if (VerboseAsm) { |
| 956 | Streamer.AddComment("DW_CFA_undefined"); |
| 957 | Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
| 958 | } |
| 959 | Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1); |
| 960 | Streamer.EmitULEB128IntValue(Reg); |
| 961 | return; |
| 962 | } |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 963 | case MCCFIInstruction::OpAdjustCfaOffset: |
| 964 | case MCCFIInstruction::OpDefCfaOffset: { |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 965 | const bool IsRelative = |
| 966 | Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset; |
| 967 | |
| 968 | if (VerboseAsm) |
| 969 | Streamer.AddComment("DW_CFA_def_cfa_offset"); |
| 970 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); |
| 971 | |
| 972 | if (IsRelative) |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 973 | CFAOffset += Instr.getOffset(); |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 974 | else |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 975 | CFAOffset = -Instr.getOffset(); |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 976 | |
| 977 | if (VerboseAsm) |
| 978 | Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); |
| 979 | Streamer.EmitULEB128IntValue(CFAOffset); |
| 980 | |
| 981 | return; |
| 982 | } |
| 983 | case MCCFIInstruction::OpDefCfa: { |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 984 | if (VerboseAsm) |
| 985 | Streamer.AddComment("DW_CFA_def_cfa"); |
| 986 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); |
| 987 | |
| 988 | if (VerboseAsm) |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 989 | Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister())); |
| 990 | Streamer.EmitULEB128IntValue(Instr.getRegister()); |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 991 | |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 992 | CFAOffset = -Instr.getOffset(); |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 993 | |
| 994 | if (VerboseAsm) |
| 995 | Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); |
| 996 | Streamer.EmitULEB128IntValue(CFAOffset); |
| 997 | |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | case MCCFIInstruction::OpDefCfaRegister: { |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1002 | if (VerboseAsm) |
| 1003 | Streamer.AddComment("DW_CFA_def_cfa_register"); |
| 1004 | Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); |
| 1005 | |
| 1006 | if (VerboseAsm) |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 1007 | Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister())); |
| 1008 | Streamer.EmitULEB128IntValue(Instr.getRegister()); |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1009 | |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | case MCCFIInstruction::OpOffset: |
| 1014 | case MCCFIInstruction::OpRelOffset: { |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1015 | const bool IsRelative = |
| 1016 | Instr.getOperation() == MCCFIInstruction::OpRelOffset; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1017 | |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 1018 | unsigned Reg = Instr.getRegister(); |
| 1019 | int Offset = Instr.getOffset(); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1020 | if (IsRelative) |
| 1021 | Offset -= CFAOffset; |
| 1022 | Offset = Offset / dataAlignmentFactor; |
| 1023 | |
| 1024 | if (Offset < 0) { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1025 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1026 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1027 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1028 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1029 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1030 | Streamer.EmitSLEB128IntValue(Offset); |
| 1031 | } else if (Reg < 64) { |
Bill Wendling | e08d433 | 2011-06-30 23:47:40 +0000 | [diff] [blame] | 1032 | if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") + |
| 1033 | Twine(Reg) + ")"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1034 | Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1035 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 1036 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1037 | } else { |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1038 | if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1039 | Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1040 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 1041 | Streamer.EmitULEB128IntValue(Reg); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1042 | if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset)); |
Rafael Espindola | eccbad7 | 2011-04-21 23:26:40 +0000 | [diff] [blame] | 1043 | Streamer.EmitULEB128IntValue(Offset); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1044 | } |
| 1045 | return; |
| 1046 | } |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1047 | case MCCFIInstruction::OpRememberState: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1048 | if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1049 | Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1); |
| 1050 | return; |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1051 | case MCCFIInstruction::OpRestoreState: |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1052 | if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1053 | Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1); |
| 1054 | return; |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1055 | case MCCFIInstruction::OpSameValue: { |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 1056 | unsigned Reg = Instr.getRegister(); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1057 | if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1058 | Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1); |
Bill Wendling | efd24dd | 2011-06-30 21:45:12 +0000 | [diff] [blame] | 1059 | if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 1060 | Streamer.EmitULEB128IntValue(Reg); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | } |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1063 | case MCCFIInstruction::OpRestore: { |
Rafael Espindola | ff233c9 | 2012-11-24 04:33:48 +0000 | [diff] [blame] | 1064 | unsigned Reg = Instr.getRegister(); |
Rafael Espindola | ed23bdb | 2011-12-29 21:43:03 +0000 | [diff] [blame] | 1065 | if (VerboseAsm) { |
| 1066 | Streamer.AddComment("DW_CFA_restore"); |
| 1067 | Streamer.AddComment(Twine("Reg ") + Twine(Reg)); |
| 1068 | } |
| 1069 | Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1); |
| 1070 | return; |
| 1071 | } |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1072 | case MCCFIInstruction::OpEscape: |
Rafael Espindola | 6f0b181 | 2011-12-29 20:24:47 +0000 | [diff] [blame] | 1073 | if (VerboseAsm) Streamer.AddComment("Escape bytes"); |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 1074 | Streamer.EmitBytes(Instr.getValues()); |
Rafael Espindola | 6f0b181 | 2011-12-29 20:24:47 +0000 | [diff] [blame] | 1075 | return; |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1076 | } |
| 1077 | llvm_unreachable("Unhandled case in switch"); |
| 1078 | } |
| 1079 | |
| 1080 | /// EmitFrameMoves - Emit frame instructions to describe the layout of the |
| 1081 | /// frame. |
| 1082 | void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer, |
| 1083 | const std::vector<MCCFIInstruction> &Instrs, |
| 1084 | MCSymbol *BaseLabel) { |
| 1085 | for (unsigned i = 0, N = Instrs.size(); i < N; ++i) { |
| 1086 | const MCCFIInstruction &Instr = Instrs[i]; |
| 1087 | MCSymbol *Label = Instr.getLabel(); |
| 1088 | // Throw out move if the label is invalid. |
| 1089 | if (Label && !Label->isDefined()) continue; // Not emitted, in dead code. |
| 1090 | |
| 1091 | // Advance row if new location. |
| 1092 | if (BaseLabel && Label) { |
| 1093 | MCSymbol *ThisSym = Label; |
| 1094 | if (ThisSym != BaseLabel) { |
Bill Wendling | d221cd6 | 2011-06-30 22:35:49 +0000 | [diff] [blame] | 1095 | if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4"); |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1096 | streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); |
| 1097 | BaseLabel = ThisSym; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | EmitCFIInstruction(streamer, Instr); |
| 1102 | } |
| 1103 | } |
| 1104 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1105 | /// EmitCompactUnwind - Emit the unwind information in a compact way. If we're |
| 1106 | /// successful, return 'true'. Otherwise, return 'false' and it will emit the |
| 1107 | /// normal CIE and FDE. |
| 1108 | bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer, |
| 1109 | const MCDwarfFrameInfo &Frame) { |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1110 | MCContext &Context = Streamer.getContext(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1111 | const MCObjectFileInfo *MOFI = Context.getObjectFileInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1112 | bool VerboseAsm = Streamer.isVerboseAsm(); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1113 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1114 | // range-start range-length compact-unwind-enc personality-func lsda |
| 1115 | // _foo LfooEnd-_foo 0x00000023 0 0 |
| 1116 | // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1 |
| 1117 | // |
| 1118 | // .section __LD,__compact_unwind,regular,debug |
| 1119 | // |
| 1120 | // # compact unwind for _foo |
| 1121 | // .quad _foo |
| 1122 | // .set L1,LfooEnd-_foo |
| 1123 | // .long L1 |
| 1124 | // .long 0x01010001 |
| 1125 | // .quad 0 |
| 1126 | // .quad 0 |
| 1127 | // |
| 1128 | // # compact unwind for _bar |
| 1129 | // .quad _bar |
| 1130 | // .set L2,LbarEnd-_bar |
| 1131 | // .long L2 |
| 1132 | // .long 0x01020011 |
| 1133 | // .quad __gxx_personality |
| 1134 | // .quad except_tab1 |
| 1135 | |
Bill Wendling | e52e3f2 | 2011-07-19 00:06:12 +0000 | [diff] [blame] | 1136 | uint32_t Encoding = Frame.CompactUnwindEncoding; |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 1137 | if (!Encoding) return false; |
| 1138 | |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame] | 1139 | // The encoding needs to know we have an LSDA. |
| 1140 | if (Frame.Lsda) |
| 1141 | Encoding |= 0x40000000; |
| 1142 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1143 | Streamer.SwitchSection(MOFI->getCompactUnwindSection()); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1144 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1145 | // Range Start |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 1146 | unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI); |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1147 | unsigned Size = getSizeForEncoding(Streamer, FDEEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1148 | if (VerboseAsm) Streamer.AddComment("Range Start"); |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1149 | Streamer.EmitSymbolValue(Frame.Function, Size); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Range Length |
| 1152 | const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin, |
| 1153 | *Frame.End, 0); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1154 | if (VerboseAsm) Streamer.AddComment("Range Length"); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1155 | Streamer.EmitAbsValue(Range, 4); |
| 1156 | |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1157 | // Compact Encoding |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1158 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4); |
Benjamin Kramer | 70be28a | 2011-11-07 21:00:59 +0000 | [diff] [blame] | 1159 | if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" + |
| 1160 | Twine::utohexstr(Encoding)); |
Bill Wendling | 9287a6e | 2011-06-30 00:30:52 +0000 | [diff] [blame] | 1161 | Streamer.EmitIntValue(Encoding, Size); |
| 1162 | |
Bill Wendling | e52e3f2 | 2011-07-19 00:06:12 +0000 | [diff] [blame] | 1163 | |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1164 | // Personality Function |
Bill Wendling | 4bb5b03 | 2011-07-14 23:34:45 +0000 | [diff] [blame] | 1165 | Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1166 | if (VerboseAsm) Streamer.AddComment("Personality Function"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1167 | if (Frame.Personality) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1168 | Streamer.EmitSymbolValue(Frame.Personality, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1169 | else |
| 1170 | Streamer.EmitIntValue(0, Size); // No personality fn |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1171 | |
| 1172 | // LSDA |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1173 | Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1174 | if (VerboseAsm) Streamer.AddComment("LSDA"); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1175 | if (Frame.Lsda) |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1176 | Streamer.EmitSymbolValue(Frame.Lsda, Size); |
Bill Wendling | 4498d39 | 2011-06-29 23:53:16 +0000 | [diff] [blame] | 1177 | else |
| 1178 | Streamer.EmitIntValue(0, Size); // No LSDA |
Bill Wendling | 9056e90 | 2011-06-29 23:49:12 +0000 | [diff] [blame] | 1179 | |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1180 | return true; |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1183 | const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer, |
| 1184 | const MCSymbol *personality, |
| 1185 | unsigned personalityEncoding, |
| 1186 | const MCSymbol *lsda, |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1187 | bool IsSignalFrame, |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1188 | unsigned lsdaEncoding) { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1189 | MCContext &context = streamer.getContext(); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1190 | const MCRegisterInfo &MRI = context.getRegisterInfo(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1191 | const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1192 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1193 | |
| 1194 | MCSymbol *sectionStart; |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1195 | if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH) |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1196 | sectionStart = context.CreateTempSymbol(); |
| 1197 | else |
| 1198 | sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum)); |
| 1199 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1200 | streamer.EmitLabel(sectionStart); |
Rafael Espindola | 514cecc | 2011-04-28 03:26:11 +0000 | [diff] [blame] | 1201 | CIENum++; |
| 1202 | |
Bill Wendling | 6a6b8c3 | 2011-07-07 00:54:13 +0000 | [diff] [blame] | 1203 | MCSymbol *sectionEnd = context.CreateTempSymbol(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Length |
| 1206 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, |
| 1207 | *sectionEnd, 4); |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1208 | if (verboseAsm) streamer.AddComment("CIE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 1209 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1210 | |
| 1211 | // CIE ID |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1212 | unsigned CIE_ID = IsEH ? 0 : -1; |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1213 | if (verboseAsm) streamer.AddComment("CIE ID Tag"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1214 | streamer.EmitIntValue(CIE_ID, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Version |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1217 | if (verboseAsm) streamer.AddComment("DW_CIE_VERSION"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1218 | streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1); |
| 1219 | |
| 1220 | // Augmentation String |
| 1221 | SmallString<8> Augmentation; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1222 | if (IsEH) { |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1223 | if (verboseAsm) streamer.AddComment("CIE Augmentation"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1224 | Augmentation += "z"; |
| 1225 | if (personality) |
| 1226 | Augmentation += "P"; |
| 1227 | if (lsda) |
| 1228 | Augmentation += "L"; |
| 1229 | Augmentation += "R"; |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1230 | if (IsSignalFrame) |
| 1231 | Augmentation += "S"; |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 1232 | streamer.EmitBytes(Augmentation.str()); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1233 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1234 | streamer.EmitIntValue(0, 1); |
| 1235 | |
| 1236 | // Code Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1237 | if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1238 | streamer.EmitULEB128IntValue(1); |
| 1239 | |
| 1240 | // Data Alignment Factor |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1241 | if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1242 | streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); |
| 1243 | |
| 1244 | // Return Address Register |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1245 | if (verboseAsm) streamer.AddComment("CIE Return Address Column"); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1246 | streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true)); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Augmentation Data Length (optional) |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1249 | |
| 1250 | unsigned augmentationLength = 0; |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1251 | if (IsEH) { |
| 1252 | if (personality) { |
| 1253 | // Personality Encoding |
| 1254 | augmentationLength += 1; |
| 1255 | // Personality |
| 1256 | augmentationLength += getSizeForEncoding(streamer, personalityEncoding); |
| 1257 | } |
| 1258 | if (lsda) |
| 1259 | augmentationLength += 1; |
| 1260 | // Encoding of the FDE pointers |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1261 | augmentationLength += 1; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1262 | |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1263 | if (verboseAsm) streamer.AddComment("Augmentation Size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1264 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1265 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1266 | // Augmentation Data (optional) |
| 1267 | if (personality) { |
| 1268 | // Personality Encoding |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1269 | EmitEncodingByte(streamer, personalityEncoding, |
| 1270 | "Personality Encoding"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1271 | // Personality |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1272 | if (verboseAsm) streamer.AddComment("Personality"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1273 | EmitPersonality(streamer, *personality, personalityEncoding); |
| 1274 | } |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1275 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1276 | if (lsda) |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1277 | EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding"); |
| 1278 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1279 | // Encoding of the FDE pointers |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 1280 | EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI), |
Bill Wendling | 3c163cf | 2011-06-30 21:25:51 +0000 | [diff] [blame] | 1281 | "FDE Encoding"); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1282 | } |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Initial Instructions |
| 1285 | |
Evan Cheng | 2d28617 | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 1286 | const MCAsmInfo &MAI = context.getAsmInfo(); |
| 1287 | const std::vector<MachineMove> &Moves = MAI.getInitialFrameState(); |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1288 | std::vector<MCCFIInstruction> Instructions; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1289 | |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1290 | for (int i = 0, n = Moves.size(); i != n; ++i) { |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1291 | MCSymbol *Label = Moves[i].getLabel(); |
| 1292 | const MachineLocation &Dst = |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1293 | TranslateMachineLocation(MRI, Moves[i].getDestination()); |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1294 | const MachineLocation &Src = |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 1295 | TranslateMachineLocation(MRI, Moves[i].getSource()); |
Rafael Espindola | 28c9ea3 | 2012-11-24 02:01:08 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (Dst.isReg()) { |
| 1298 | assert(Dst.getReg() == MachineLocation::VirtualFP); |
| 1299 | assert(!Src.isReg()); |
| 1300 | MCCFIInstruction Inst = |
| 1301 | MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset()); |
| 1302 | Instructions.push_back(Inst); |
| 1303 | } else { |
| 1304 | assert(Src.isReg()); |
| 1305 | unsigned Reg = Src.getReg(); |
| 1306 | int Offset = Dst.getOffset(); |
| 1307 | MCCFIInstruction Inst = |
Rafael Espindola | 7f74d2c | 2012-11-24 03:10:54 +0000 | [diff] [blame] | 1308 | MCCFIInstruction::createOffset(Label, Reg, Offset); |
Rafael Espindola | 28c9ea3 | 2012-11-24 02:01:08 +0000 | [diff] [blame] | 1309 | Instructions.push_back(Inst); |
| 1310 | } |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1313 | EmitCFIInstructions(streamer, Instructions, NULL); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1314 | |
| 1315 | // Padding |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 1316 | streamer.EmitValueToAlignment(IsEH |
| 1317 | ? 4 : context.getAsmInfo().getPointerSize()); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1318 | |
| 1319 | streamer.EmitLabel(sectionEnd); |
| 1320 | return *sectionStart; |
| 1321 | } |
| 1322 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 1323 | MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer, |
| 1324 | const MCSymbol &cieStart, |
Rafael Espindola | 9f270da | 2011-05-10 03:01:39 +0000 | [diff] [blame] | 1325 | const MCDwarfFrameInfo &frame) { |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1326 | MCContext &context = streamer.getContext(); |
| 1327 | MCSymbol *fdeStart = context.CreateTempSymbol(); |
| 1328 | MCSymbol *fdeEnd = context.CreateTempSymbol(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1329 | const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1330 | bool verboseAsm = streamer.isVerboseAsm(); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1331 | |
Evan Cheng | 5fbe5e7 | 2011-08-24 22:31:37 +0000 | [diff] [blame] | 1332 | if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) { |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1333 | MCSymbol *EHSym = |
| 1334 | context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh")); |
Rafael Espindola | 8bca410 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 1335 | streamer.EmitEHSymAttributes(frame.Function, EHSym); |
Rafael Espindola | a8cfb87 | 2011-04-28 02:46:42 +0000 | [diff] [blame] | 1336 | streamer.EmitLabel(EHSym); |
| 1337 | } |
| 1338 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1339 | // Length |
| 1340 | const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1341 | if (verboseAsm) streamer.AddComment("FDE Length"); |
Rafael Espindola | 9266cc4 | 2011-04-27 01:43:49 +0000 | [diff] [blame] | 1342 | streamer.EmitAbsValue(Length, 4); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1343 | |
| 1344 | streamer.EmitLabel(fdeStart); |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1345 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1346 | // CIE Pointer |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1347 | const MCAsmInfo &asmInfo = context.getAsmInfo(); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1348 | if (IsEH) { |
| 1349 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, |
| 1350 | 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1351 | if (verboseAsm) streamer.AddComment("FDE CIE Offset"); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1352 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | 2241e51 | 2012-06-22 13:24:07 +0000 | [diff] [blame] | 1353 | } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) { |
Rafael Espindola | e3a0e98 | 2011-05-10 20:59:42 +0000 | [diff] [blame] | 1354 | const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart, |
| 1355 | cieStart, 0); |
| 1356 | streamer.EmitAbsValue(offset, 4); |
Rafael Espindola | 0d450dc | 2011-05-10 15:20:23 +0000 | [diff] [blame] | 1357 | } else { |
| 1358 | streamer.EmitSymbolValue(&cieStart, 4); |
| 1359 | } |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1360 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1361 | // PC Begin |
Nick Lewycky | 0c5602d | 2012-08-12 08:09:45 +0000 | [diff] [blame] | 1362 | unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI) |
| 1363 | : (unsigned)dwarf::DW_EH_PE_absptr; |
| 1364 | unsigned PCSize = getSizeForEncoding(streamer, PCEncoding); |
| 1365 | EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location"); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1366 | |
| 1367 | // PC Range |
| 1368 | const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, |
| 1369 | *frame.End, 0); |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1370 | if (verboseAsm) streamer.AddComment("FDE address range"); |
Nick Lewycky | 0c5602d | 2012-08-12 08:09:45 +0000 | [diff] [blame] | 1371 | streamer.EmitAbsValue(Range, PCSize); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1372 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1373 | if (IsEH) { |
| 1374 | // Augmentation Data Length |
| 1375 | unsigned augmentationLength = 0; |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1376 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1377 | if (frame.Lsda) |
| 1378 | augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding); |
Rafael Espindola | 1f6c875 | 2011-04-29 21:50:57 +0000 | [diff] [blame] | 1379 | |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1380 | if (verboseAsm) streamer.AddComment("Augmentation size"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1381 | streamer.EmitULEB128IntValue(augmentationLength); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1382 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1383 | // Augmentation Data |
| 1384 | if (frame.Lsda) |
Bill Wendling | 2541c41 | 2011-06-30 22:02:20 +0000 | [diff] [blame] | 1385 | EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding, |
| 1386 | "Language Specific Data Area"); |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1387 | } |
Rafael Espindola | 4892dff | 2011-04-29 03:06:29 +0000 | [diff] [blame] | 1388 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1389 | // Call Frame Instructions |
| 1390 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 1391 | EmitCFIInstructions(streamer, frame.Instructions, frame.Begin); |
Rafael Espindola | 5bba084 | 2010-12-28 04:15:37 +0000 | [diff] [blame] | 1392 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1393 | // Padding |
Nick Lewycky | 0c5602d | 2012-08-12 08:09:45 +0000 | [diff] [blame] | 1394 | streamer.EmitValueToAlignment(PCSize); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1395 | |
| 1396 | return fdeEnd; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1399 | namespace { |
| 1400 | struct CIEKey { |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1401 | static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); } |
| 1402 | static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1403 | |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1404 | CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_, |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1405 | unsigned LsdaEncoding_, bool IsSignalFrame_) : |
| 1406 | Personality(Personality_), PersonalityEncoding(PersonalityEncoding_), |
| 1407 | LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1408 | } |
| 1409 | const MCSymbol* Personality; |
| 1410 | unsigned PersonalityEncoding; |
| 1411 | unsigned LsdaEncoding; |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1412 | bool IsSignalFrame; |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1413 | }; |
| 1414 | } |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1415 | |
| 1416 | namespace llvm { |
| 1417 | template <> |
| 1418 | struct DenseMapInfo<CIEKey> { |
| 1419 | static CIEKey getEmptyKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1420 | return CIEKey::getEmptyKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1421 | } |
| 1422 | static CIEKey getTombstoneKey() { |
Benjamin Kramer | 1928236 | 2010-12-30 22:34:44 +0000 | [diff] [blame] | 1423 | return CIEKey::getTombstoneKey(); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1424 | } |
| 1425 | static unsigned getHashValue(const CIEKey &Key) { |
Benjamin Kramer | 7484920 | 2012-04-11 14:06:39 +0000 | [diff] [blame] | 1426 | return static_cast<unsigned>(hash_combine(Key.Personality, |
| 1427 | Key.PersonalityEncoding, |
| 1428 | Key.LsdaEncoding, |
| 1429 | Key.IsSignalFrame)); |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1430 | } |
| 1431 | static bool isEqual(const CIEKey &LHS, |
| 1432 | const CIEKey &RHS) { |
| 1433 | return LHS.Personality == RHS.Personality && |
| 1434 | LHS.PersonalityEncoding == RHS.PersonalityEncoding && |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1435 | LHS.LsdaEncoding == RHS.LsdaEncoding && |
| 1436 | LHS.IsSignalFrame == RHS.IsSignalFrame; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1437 | } |
| 1438 | }; |
| 1439 | } |
| 1440 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1441 | void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, |
| 1442 | bool UsingCFI, |
| 1443 | bool IsEH) { |
| 1444 | MCContext &Context = Streamer.getContext(); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1445 | MCObjectFileInfo *MOFI = |
| 1446 | const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo()); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1447 | FrameEmitterImpl Emitter(UsingCFI, IsEH); |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1448 | ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos(); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1449 | |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1450 | // Emit the compact unwind info if available. |
Bill Wendling | 7a13b72 | 2011-12-15 00:14:24 +0000 | [diff] [blame] | 1451 | if (IsEH && MOFI->getCompactUnwindSection()) |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1452 | for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) { |
| 1453 | const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i); |
Bill Wendling | a2ff3e2 | 2011-11-08 22:23:43 +0000 | [diff] [blame] | 1454 | if (Frame.CompactUnwindEncoding) |
Bill Wendling | d8ab8e9 | 2011-09-06 18:37:11 +0000 | [diff] [blame] | 1455 | Emitter.EmitCompactUnwind(Streamer, Frame); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 1458 | const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() : |
| 1459 | *MOFI->getDwarfFrameSection(); |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1460 | Streamer.SwitchSection(&Section); |
| 1461 | MCSymbol *SectionStart = Context.CreateTempSymbol(); |
| 1462 | Streamer.EmitLabel(SectionStart); |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1463 | Emitter.setSectionStart(SectionStart); |
Rafael Espindola | 9099813 | 2011-04-29 02:42:28 +0000 | [diff] [blame] | 1464 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1465 | MCSymbol *FDEEnd = NULL; |
Rafael Espindola | bdc3167 | 2010-12-27 15:56:22 +0000 | [diff] [blame] | 1466 | DenseMap<CIEKey, const MCSymbol*> CIEStarts; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1467 | |
Rafael Espindola | 40a7dbb | 2011-05-10 03:54:12 +0000 | [diff] [blame] | 1468 | const MCSymbol *DummyDebugKey = NULL; |
Bill Wendling | 1424c3c | 2011-07-22 21:18:59 +0000 | [diff] [blame] | 1469 | for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) { |
| 1470 | const MCDwarfFrameInfo &Frame = FrameArray[i]; |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1471 | CIEKey Key(Frame.Personality, Frame.PersonalityEncoding, |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1472 | Frame.LsdaEncoding, Frame.IsSignalFrame); |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1473 | const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey; |
| 1474 | if (!CIEStart) |
| 1475 | CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality, |
| 1476 | Frame.PersonalityEncoding, Frame.Lsda, |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 1477 | Frame.IsSignalFrame, |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1478 | Frame.LsdaEncoding); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1479 | |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1480 | FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame); |
Bill Wendling | e3cd13f | 2011-06-23 01:06:23 +0000 | [diff] [blame] | 1481 | |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1482 | if (i != n - 1) |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1483 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | dfe125c | 2010-12-17 00:28:02 +0000 | [diff] [blame] | 1484 | } |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 1485 | |
Evan Cheng | 1be0e27 | 2011-07-15 02:09:41 +0000 | [diff] [blame] | 1486 | Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize()); |
Bill Wendling | a8c9e6a | 2011-06-23 07:44:54 +0000 | [diff] [blame] | 1487 | if (FDEEnd) |
| 1488 | Streamer.EmitLabel(FDEEnd); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 1489 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1490 | |
| 1491 | void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer, |
| 1492 | uint64_t AddrDelta) { |
| 1493 | SmallString<256> Tmp; |
| 1494 | raw_svector_ostream OS(Tmp); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1495 | MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS); |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame^] | 1496 | Streamer.EmitBytes(OS.str()); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta, |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1500 | raw_ostream &OS) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1501 | // FIXME: Assumes the code alignment factor is 1. |
Rafael Espindola | 3b78cdc | 2010-12-28 23:38:03 +0000 | [diff] [blame] | 1502 | if (AddrDelta == 0) { |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1503 | } else if (isUIntN(6, AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1504 | uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta; |
| 1505 | OS << Opcode; |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1506 | } else if (isUInt<8>(AddrDelta)) { |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1507 | OS << uint8_t(dwarf::DW_CFA_advance_loc1); |
| 1508 | OS << uint8_t(AddrDelta); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 1509 | } else if (isUInt<16>(AddrDelta)) { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1510 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1511 | OS << uint8_t(dwarf::DW_CFA_advance_loc2); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1512 | OS << uint8_t( AddrDelta & 0xff); |
| 1513 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1514 | } else { |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1515 | // FIXME: check what is the correct behavior on a big endian machine. |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1516 | assert(isUInt<32>(AddrDelta)); |
| 1517 | OS << uint8_t(dwarf::DW_CFA_advance_loc4); |
Rafael Espindola | a7e4505 | 2010-12-29 02:30:49 +0000 | [diff] [blame] | 1518 | OS << uint8_t( AddrDelta & 0xff); |
| 1519 | OS << uint8_t((AddrDelta >> 8) & 0xff); |
| 1520 | OS << uint8_t((AddrDelta >> 16) & 0xff); |
| 1521 | OS << uint8_t((AddrDelta >> 24) & 0xff); |
| 1522 | |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1523 | } |
| 1524 | } |