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