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