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