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