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