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