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