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