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