blob: 31f731c291c5bd54912fa11e42cb58d1878e2788 [file] [log] [blame]
Kevin Enderbye5930f12010-07-28 20:55:35 +00001//===- 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 Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/Hashing.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000014#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/ADT/SmallVector.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000018#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000025#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000026#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000028#include "llvm/MC/MCSection.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000029#include "llvm/MC/MCStreamer.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000030#include "llvm/MC/MCSymbol.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/Endian.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000033#include "llvm/Support/EndianStream.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000035#include "llvm/Support/LEB128.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000036#include "llvm/Support/MathExtras.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000037#include "llvm/Support/Path.h"
38#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include <cassert>
41#include <cstdint>
42#include <string>
43#include <utility>
44#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000045
Kevin Enderbye5930f12010-07-28 20:55:35 +000046using namespace llvm;
47
Ulrich Weigand32d725b2013-06-12 14:46:54 +000048static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000049 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000050 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000051 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000052 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000053 // TODO: report this error, but really only once.
54 ;
55 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000056 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000057}
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 Majnemer8cc30782016-01-21 01:59:03 +000064void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000065 if (!MCOS->getContext().getDwarfLocSeen())
66 return;
67
68 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000069 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000070 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000071 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 Majnemer8cc30782016-01-21 01:59:03 +000077 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +000078
79 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +000080 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000081
Kevin Enderbye46564a2010-09-30 16:52:03 +000082 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000083 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000084 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000085 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000086 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000087}
88
89//
90// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000091//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000092static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
93 const MCSymbol &Start,
94 const MCSymbol &End,
95 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000096 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
97 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +000098 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000099 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000100 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000101 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000102 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000103 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000104 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000105 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000106 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000107 return Res3;
108}
109
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110//
111// This emits the Dwarf line table for the specified section from the entries
112// in the LineSection.
113//
David Blaikief4ad6982014-03-12 22:35:23 +0000114static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000115EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000116 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000117 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 Novillo5b5cf502014-02-14 19:27:53 +0000122 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000123 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000124
David Majnemer8cc30782016-01-21 01:59:03 +0000125 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000126 for (const MCDwarfLineEntry &LineEntry : LineEntries) {
127 int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000128
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000129 if (FileNum != LineEntry.getFileNum()) {
130 FileNum = LineEntry.getFileNum();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000131 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000132 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000133 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000134 if (Column != LineEntry.getColumn()) {
135 Column = LineEntry.getColumn();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000136 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000137 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000138 }
Dehao Chen6e0c8442016-10-07 15:21:31 +0000139 if (Discriminator != LineEntry.getDiscriminator() &&
140 MCOS->getContext().getDwarfVersion() >= 4) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000141 Discriminator = LineEntry.getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000142 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000143 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 Kramer7b4658f2016-06-26 14:49:00 +0000148 if (Isa != LineEntry.getIsa()) {
149 Isa = LineEntry.getIsa();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000150 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000151 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000152 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000153 if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
154 Flags = LineEntry.getFlags();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000155 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
156 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000157 if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000158 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000159 if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000160 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000161 if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000162 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
163
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000164 MCSymbol *Label = LineEntry.getLabel();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000165
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 Wendlingbc07a892013-06-18 07:20:20 +0000169 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000170 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000171 asmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000172
Dehao Chen1b54fce2016-04-28 22:09:37 +0000173 Discriminator = 0;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000174 LastLine = LineEntry.getLine();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000175 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000176 }
177
178 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000179 // 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 Enderbye46564a2010-09-30 16:52:03 +0000183
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000184 // Switch back the dwarf line section, in case endSection had to switch the
185 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000186 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000187 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000188
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000189 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000190 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000191 AsmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000192}
193
194//
195// This emits the Dwarf file and the line tables.
196//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000197void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
198 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000199 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000200
David Blaikie8bf66c42014-04-01 07:35:52 +0000201 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 Blaikie11765bc2014-03-13 17:55:28 +0000207
208 // Switch to the section where the table will be emitted into.
209 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
210
Manman Ren4e042a62013-02-05 21:52:47 +0000211 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000212 for (const auto &CUIDTablePair : LineTables)
Frederic Rissa5ab8442015-08-07 15:14:08 +0000213 CUIDTablePair.second.EmitCU(MCOS, Params);
Manman Ren4e042a62013-02-05 21:52:47 +0000214}
215
Frederic Rissa5ab8442015-08-07 15:14:08 +0000216void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS,
217 MCDwarfLineTableParams Params) const {
218 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000219}
220
Frederic Rissa5ab8442015-08-07 15:14:08 +0000221std::pair<MCSymbol *, MCSymbol *>
222MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
223 MCDwarfLineTableParams Params) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000224 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 Rissa5ab8442015-08-07 15:14:08 +0000238 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000239 (Params.DWARF2LineOpcodeBase - 1U));
Craig Topper0013be12015-09-21 05:32:41 +0000240 return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
241 Params.DWARF2LineOpcodeBase - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000242}
243
Rafael Espindolac23174b2014-08-15 15:12:13 +0000244static 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 Grosbach6f482002015-05-18 18:43:14 +0000250 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000251 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000252 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000253}
254
255static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
256 const MCExpr *ABS = forceExpAbs(OS, Value);
257 OS.EmitValue(ABS, Size);
258}
259
Paul Robinson2bf8f492018-01-30 21:39:28 +0000260void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000261 // First the directory table.
Paul Robinson2bf8f492018-01-30 21:39:28 +0000262 for (auto &Dir : MCDwarfDirs) {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000263 MCOS->EmitBytes(Dir); // The DirectoryName, and...
264 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
265 }
266 MCOS->EmitIntValue(0, 1); // Terminate the directory list.
267
268 // Second the file table.
269 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
270 assert(!MCDwarfFiles[i].Name.empty());
271 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName and...
272 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
273 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
274 MCOS->EmitIntValue(0, 1); // Last modification timestamp (always 0).
275 MCOS->EmitIntValue(0, 1); // File size (always 0).
276 }
277 MCOS->EmitIntValue(0, 1); // Terminate the file list.
278}
279
Paul Robinson2bf8f492018-01-30 21:39:28 +0000280void MCDwarfLineTableHeader::emitV5FileDirTables(MCStreamer *MCOS) const {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000281 // The directory format, which is just inline null-terminated strings.
282 MCOS->EmitIntValue(1, 1);
283 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
284 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_string);
285 // Then the list of directory paths. CompilationDir comes first.
286 MCOS->EmitULEB128IntValue(MCDwarfDirs.size() + 1);
287 MCOS->EmitBytes(CompilationDir);
288 MCOS->EmitBytes(StringRef("\0", 1));
Paul Robinson2bf8f492018-01-30 21:39:28 +0000289 for (auto &Dir : MCDwarfDirs) {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000290 MCOS->EmitBytes(Dir); // The DirectoryName, and...
291 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
292 }
293
294 // The file format, which is the inline null-terminated filename and a
295 // directory index. We don't track file size/timestamp so don't emit them
Paul Robinson29f5f982018-01-09 23:31:48 +0000296 // in the v5 table. Emit MD5 checksums if we have them.
297 MCOS->EmitIntValue(HasMD5 ? 3 : 2, 1);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000298 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
299 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_string);
300 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_directory_index);
301 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_udata);
Paul Robinson29f5f982018-01-09 23:31:48 +0000302 if (HasMD5) {
303 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_MD5);
304 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_data16);
305 }
306 // Then the list of file names. These start at 1.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000307 MCOS->EmitULEB128IntValue(MCDwarfFiles.size() - 1);
308 for (unsigned i = 1; i < MCDwarfFiles.size(); ++i) {
309 assert(!MCDwarfFiles[i].Name.empty());
310 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName and...
311 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
312 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
Paul Robinson29f5f982018-01-09 23:31:48 +0000313 if (HasMD5) {
314 MD5::MD5Result *Cksum = MCDwarfFiles[i].Checksum;
315 MCOS->EmitBinaryData(
316 StringRef(reinterpret_cast<const char *>(Cksum->Bytes.data()),
317 Cksum->Bytes.size()));
318 }
Paul Robinson795ab0d2017-12-05 20:35:00 +0000319 }
320}
321
David Blaikie8287aff2014-03-18 02:13:23 +0000322std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000323MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
David Blaikie8287aff2014-03-18 02:13:23 +0000324 ArrayRef<char> StandardOpcodeLengths) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000325 MCContext &context = MCOS->getContext();
326
327 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000328 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000329 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000330 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000331 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000332 MCOS->EmitLabel(LineStartSym);
333
334 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000335 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000336
337 // The first 4 bytes is the total length of the information for this
338 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000339 emitAbsValue(*MCOS,
340 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000341
Paul Robinson68ba7722017-12-04 21:27:46 +0000342 // Next 2 bytes is the Version.
Davide Italiano23b3f6d2017-12-07 00:57:25 +0000343 // FIXME: On Darwin we still default to V2.
Paul Robinson68ba7722017-12-04 21:27:46 +0000344 unsigned LineTableVersion = context.getDwarfVersion();
Davide Italiano23b3f6d2017-12-07 00:57:25 +0000345 if (context.getObjectFileInfo()->getTargetTriple().isOSDarwin())
346 LineTableVersion = 2;
Paul Robinson68ba7722017-12-04 21:27:46 +0000347 MCOS->EmitIntValue(LineTableVersion, 2);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000348
Paul Robinson795ab0d2017-12-05 20:35:00 +0000349 // Keep track of the bytes between the very start and where the header length
350 // comes out.
351 unsigned PreHeaderLengthBytes = 4 + 2;
352
353 // In v5, we get address info next.
354 if (LineTableVersion >= 5) {
355 MCOS->EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
356 MCOS->EmitIntValue(0, 1); // Segment selector; same as EmitGenDwarfAranges.
357 PreHeaderLengthBytes += 2;
358 }
359
Kevin Enderbye46564a2010-09-30 16:52:03 +0000360 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000361 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000362
Paul Robinson795ab0d2017-12-05 20:35:00 +0000363 // Length of the prologue, is the next 4 bytes. This is actually the length
364 // from after the length word, to the end of the prologue.
365 emitAbsValue(*MCOS,
366 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
367 (PreHeaderLengthBytes + 4)),
368 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000369
370 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000371 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Paul Robinson68ba7722017-12-04 21:27:46 +0000372 // maximum_operations_per_instruction
373 // For non-VLIW architectures this field is always 1.
374 // FIXME: VLIW architectures need to update this field accordingly.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000375 if (LineTableVersion >= 4)
Paul Robinson68ba7722017-12-04 21:27:46 +0000376 MCOS->EmitIntValue(1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000377 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000378 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
379 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000380 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000381
382 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000383 for (char Length : StandardOpcodeLengths)
384 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000385
Paul Robinson795ab0d2017-12-05 20:35:00 +0000386 // Put out the directory and file tables. The formats vary depending on
387 // the version.
388 if (LineTableVersion >= 5)
Paul Robinson2bf8f492018-01-30 21:39:28 +0000389 emitV5FileDirTables(MCOS);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000390 else
Paul Robinson2bf8f492018-01-30 21:39:28 +0000391 emitV2FileDirTables(MCOS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000392
393 // This is the end of the prologue, so set the value of the symbol at the
394 // end of the prologue (that was used in a previous expression).
395 MCOS->EmitLabel(ProEndSym);
396
David Blaikie5cff0e62014-03-13 21:47:12 +0000397 return std::make_pair(LineStartSym, LineEndSym);
398}
399
Frederic Rissa5ab8442015-08-07 15:14:08 +0000400void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
401 MCDwarfLineTableParams Params) const {
402 MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000403
Kevin Enderbye46564a2010-09-30 16:52:03 +0000404 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000405 for (const auto &LineSec : MCLineSections.getMCLineEntries())
406 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000407
Kevin Enderbye46564a2010-09-30 16:52:03 +0000408 // This is the end of the section, so set the value of the symbol at the end
409 // of this section (that was used in a previous expression).
410 MCOS->EmitLabel(LineEndSym);
411}
412
David Blaikiec2df16b2014-03-17 18:13:58 +0000413unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
Paul Robinson29f5f982018-01-09 23:31:48 +0000414 MD5::MD5Result *Checksum,
David Blaikied9012ba2014-03-13 21:59:51 +0000415 unsigned FileNumber) {
Paul Robinson29f5f982018-01-09 23:31:48 +0000416 return Header.getFile(Directory, FileName, Checksum, FileNumber);
David Blaikie5cff0e62014-03-13 21:47:12 +0000417}
418
David Blaikiec2df16b2014-03-17 18:13:58 +0000419unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
420 StringRef &FileName,
Paul Robinson29f5f982018-01-09 23:31:48 +0000421 MD5::MD5Result *Checksum,
David Blaikiec714ef42014-03-17 01:52:11 +0000422 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000423 if (Directory == CompilationDir)
424 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000425 if (FileName.empty()) {
426 FileName = "<stdin>";
427 Directory = "";
428 }
429 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000430 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000431 // File numbers start with 1 and/or after any file numbers
432 // allocated by inline-assembler .file directives.
433 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000434 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000435 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000436 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
437 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000438 if (!IterBool.second)
439 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000440 }
David Blaikie498589c2014-03-13 19:15:04 +0000441 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
Paul Robinsona1cedd62017-12-14 18:46:43 +0000442 if (FileNumber >= MCDwarfFiles.size())
443 MCDwarfFiles.resize(FileNumber + 1);
David Blaikie498589c2014-03-13 19:15:04 +0000444
445 // Get the new MCDwarfFile slot for this FileNumber.
446 MCDwarfFile &File = MCDwarfFiles[FileNumber];
447
448 // It is an error to use see the same number more than once.
449 if (!File.Name.empty())
450 return 0;
451
Paul Robinson29f5f982018-01-09 23:31:48 +0000452 // If any files have an MD5 checksum, they all must.
453 if (FileNumber > 1)
454 assert(HasMD5 == (Checksum != nullptr));
455
David Blaikie498589c2014-03-13 19:15:04 +0000456 if (Directory.empty()) {
457 // Separate the directory part from the basename of the FileName.
458 StringRef tFileName = sys::path::filename(FileName);
459 if (!tFileName.empty()) {
460 Directory = sys::path::parent_path(FileName);
461 if (!Directory.empty())
462 FileName = tFileName;
463 }
464 }
465
466 // Find or make an entry in the MCDwarfDirs vector for this Directory.
467 // Capture directory name.
468 unsigned DirIndex;
469 if (Directory.empty()) {
470 // For FileNames with no directories a DirIndex of 0 is used.
471 DirIndex = 0;
472 } else {
473 DirIndex = 0;
474 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
475 if (Directory == MCDwarfDirs[DirIndex])
476 break;
477 }
478 if (DirIndex >= MCDwarfDirs.size())
479 MCDwarfDirs.push_back(Directory);
480 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
481 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
482 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
483 // are stored at MCDwarfFiles[FileNumber].Name .
484 DirIndex++;
485 }
486
487 File.Name = FileName;
488 File.DirIndex = DirIndex;
Paul Robinson29f5f982018-01-09 23:31:48 +0000489 File.Checksum = Checksum;
490 if (Checksum)
491 HasMD5 = true;
David Blaikie498589c2014-03-13 19:15:04 +0000492
493 // return the allocated FileNumber.
494 return FileNumber;
495}
496
Kevin Enderbye46564a2010-09-30 16:52:03 +0000497/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000498void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
499 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000500 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000501 SmallString<256> Tmp;
502 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000503 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000504 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000505}
506
Frederic Rissa5ab8442015-08-07 15:14:08 +0000507/// Given a special op, return the address skip amount (in units of
508/// DWARF2_LINE_MIN_INSN_LENGTH).
509static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
510 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
511}
512
Kevin Enderbye46564a2010-09-30 16:52:03 +0000513/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000514void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
515 int64_t LineDelta, uint64_t AddrDelta,
516 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000517 uint64_t Temp, Opcode;
518 bool NeedCopy = false;
519
Frederic Rissa5ab8442015-08-07 15:14:08 +0000520 // The maximum address skip amount that can be encoded with a special op.
521 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
522
Kevin Enderbye46564a2010-09-30 16:52:03 +0000523 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000524 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000525
526 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000527 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000528 // end_sequence to emit the matrix entry.
529 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000530 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000531 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000532 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000533 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000534 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000535 }
536 OS << char(dwarf::DW_LNS_extended_op);
537 OS << char(1);
538 OS << char(dwarf::DW_LNE_end_sequence);
539 return;
540 }
541
542 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000543 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000544
545 // If the line increment is out of range of a special opcode, we must encode
546 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000547 if (Temp >= Params.DWARF2LineRange ||
548 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000549 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000550 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000551
552 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000553 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000554 NeedCopy = true;
555 }
556
557 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
558 if (LineDelta == 0 && AddrDelta == 0) {
559 OS << char(dwarf::DW_LNS_copy);
560 return;
561 }
562
563 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000564 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000565
566 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000567 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000568 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000569 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000570 if (Opcode <= 255) {
571 OS << char(Opcode);
572 return;
573 }
574
575 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000576 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000577 if (Opcode <= 255) {
578 OS << char(dwarf::DW_LNS_const_add_pc);
579 OS << char(Opcode);
580 return;
581 }
582 }
583
584 // Otherwise use DW_LNS_advance_pc.
585 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000586 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000587
588 if (NeedCopy)
589 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000590 else {
591 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000592 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000593 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000594}
595
Kevin Enderbye7739d42011-12-09 18:09:40 +0000596// Utility function to write a tuple for .debug_abbrev.
597static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
598 MCOS->EmitULEB128IntValue(Name);
599 MCOS->EmitULEB128IntValue(Form);
600}
601
602// When generating dwarf for assembly source files this emits
603// the data for .debug_abbrev section which contains three DIEs.
604static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
605 MCContext &context = MCOS->getContext();
606 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
607
608 // DW_TAG_compile_unit DIE abbrev (1).
609 MCOS->EmitULEB128IntValue(1);
610 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
611 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000612 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
613 ? dwarf::DW_FORM_sec_offset
614 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000615 if (context.getGenDwarfSectionSyms().size() > 1 &&
616 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000617 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
618 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000619 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000620 } else {
621 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
622 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
623 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000624 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000625 if (!context.getCompilationDir().empty())
626 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000627 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
628 if (!DwarfDebugFlags.empty())
629 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
630 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
631 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
632 EmitAbbrev(MCOS, 0, 0);
633
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000634 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000635 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000636 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000637 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
638 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
639 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
640 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
641 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000642 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
643 EmitAbbrev(MCOS, 0, 0);
644
645 // DW_TAG_unspecified_parameters DIE abbrev (3).
646 MCOS->EmitULEB128IntValue(3);
647 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
648 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
649 EmitAbbrev(MCOS, 0, 0);
650
651 // Terminate the abbreviations for this compilation unit.
652 MCOS->EmitIntValue(0, 1);
653}
654
655// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000656// .debug_aranges section. This section contains a header and a table of pairs
657// of PointerSize'ed values for the address and size of section(s) with line
658// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000659static void EmitGenDwarfAranges(MCStreamer *MCOS,
660 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000661 MCContext &context = MCOS->getContext();
662
Oliver Stannard8b273082014-06-19 15:52:37 +0000663 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000664
665 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
666
667 // This will be the length of the .debug_aranges section, first account for
668 // the size of each item in the header (see below where we emit these items).
669 int Length = 4 + 2 + 4 + 1 + 1;
670
671 // Figure the padding after the header before the table of address and size
672 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000673 const MCAsmInfo *asmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000674 int AddrSize = asmInfo->getCodePointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000675 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
676 if (Pad == 2 * AddrSize)
677 Pad = 0;
678 Length += Pad;
679
680 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000681 // of each section we have in the table.
682 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000683 // And the pair of terminating zeros.
684 Length += 2 * AddrSize;
685
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686 // Emit the header for this section.
687 // The 4 byte length not including the 4 byte value for the length.
688 MCOS->EmitIntValue(Length - 4, 4);
689 // The 2 byte version, which is 2.
690 MCOS->EmitIntValue(2, 2);
691 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000692 // of the .debug_info.
693 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000694 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
695 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000696 else
697 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000698 // The 1 byte size of an address.
699 MCOS->EmitIntValue(AddrSize, 1);
700 // The 1 byte size of a segment descriptor, we use a value of zero.
701 MCOS->EmitIntValue(0, 1);
702 // Align the header with the padding if needed, before we put out the table.
703 for(int i = 0; i < Pad; i++)
704 MCOS->EmitIntValue(0, 1);
705
Oliver Stannard8b273082014-06-19 15:52:37 +0000706 // Now emit the table of pairs of PointerSize'ed values for the section
707 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000708 for (MCSection *Sec : Sections) {
709 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000710 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000711 assert(StartSymbol && "StartSymbol must not be NULL");
712 assert(EndSymbol && "EndSymbol must not be NULL");
713
Jim Grosbach13760bd2015-05-30 01:25:56 +0000714 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000715 StartSymbol, MCSymbolRefExpr::VK_None, context);
716 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
717 *StartSymbol, *EndSymbol, 0);
718 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000719 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000720 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000721
722 // And finally the pair of terminating zeros.
723 MCOS->EmitIntValue(0, AddrSize);
724 MCOS->EmitIntValue(0, AddrSize);
725}
726
727// When generating dwarf for assembly source files this emits the data for
728// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000729// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000730static void EmitGenDwarfInfo(MCStreamer *MCOS,
731 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000732 const MCSymbol *LineSectionSymbol,
733 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000734 MCContext &context = MCOS->getContext();
735
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000736 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000737
738 // Create a symbol at the start and end of this section used in here for the
739 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000740 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000742 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000743
744 // First part: the header.
745
746 // The 4 byte total length of the information for this compilation unit, not
747 // including these 4 bytes.
748 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000749 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000750
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000751 // The 2 byte DWARF version.
752 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000753
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000754 // The DWARF v5 header has unit type, address size, abbrev offset.
755 // Earlier versions have abbrev offset, address size.
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000756 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000757 int AddrSize = AsmInfo.getCodePointerSize();
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000758 if (context.getDwarfVersion() >= 5) {
759 MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
760 MCOS->EmitIntValue(AddrSize, 1);
761 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000762 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
763 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000764 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000765 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000766 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000767 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
768 AsmInfo.needsDwarfSectionOffsetDirective());
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000769 if (context.getDwarfVersion() <= 4)
770 MCOS->EmitIntValue(AddrSize, 1);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000771
772 // Second part: the compile_unit DIE.
773
774 // The DW_TAG_compile_unit DIE abbrev (1).
775 MCOS->EmitULEB128IntValue(1);
776
777 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
778 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000779 if (LineSectionSymbol)
780 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
781 AsmInfo.needsDwarfSectionOffsetDirective());
782 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000783 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000784
Oliver Stannard8b273082014-06-19 15:52:37 +0000785 if (RangesSectionSymbol) {
786 // There are multiple sections containing code, so we must use the
787 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000788
Oliver Stannard8b273082014-06-19 15:52:37 +0000789 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
790 // to the address range list for this compilation unit.
791 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
792 } else {
793 // If we only have one non-empty code section, we can use the simpler
794 // AT_low_pc and AT_high_pc attributes.
795
796 // Find the first (and only) non-empty text section
797 auto &Sections = context.getGenDwarfSectionSyms();
798 const auto TextSection = Sections.begin();
799 assert(TextSection != Sections.end() && "No text section found");
800
Rafael Espindolae0746792015-05-21 16:52:32 +0000801 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
802 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000803 assert(StartSymbol && "StartSymbol must not be NULL");
804 assert(EndSymbol && "EndSymbol must not be NULL");
805
806 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000807 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000808 StartSymbol, MCSymbolRefExpr::VK_None, context);
809 MCOS->EmitValue(Start, AddrSize);
810
811 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000812 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000813 EndSymbol, MCSymbolRefExpr::VK_None, context);
814 MCOS->EmitValue(End, AddrSize);
815 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000816
817 // AT_name, the name of the source file. Reconstruct from the first directory
818 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000819 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000820 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000821 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000822 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000823 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000824 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000825 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000826 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000827 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
828
829 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000830 if (!context.getCompilationDir().empty()) {
831 MCOS->EmitBytes(context.getCompilationDir());
832 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
833 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000834
835 // AT_APPLE_flags, the command line arguments of the assembler tool.
836 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
837 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000838 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000839 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
840 }
841
842 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000843 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000844 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000845 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000846 else
847 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000848 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
849
850 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
851 // draft has no standard code for assembler.
852 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
853
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000854 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000855
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000856 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000857 const std::vector<MCGenDwarfLabelEntry> &Entries =
858 MCOS->getContext().getMCGenDwarfLabelEntries();
859 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000860 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000861 MCOS->EmitULEB128IntValue(2);
862
863 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000864 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000865 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
866
867 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000868 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000869
870 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000871 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000872
873 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000874 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000875 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000876 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000877
Kevin Enderbye7739d42011-12-09 18:09:40 +0000878 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
879 MCOS->EmitIntValue(0, 1);
880
881 // The DW_TAG_unspecified_parameters DIE abbrev (3).
882 MCOS->EmitULEB128IntValue(3);
883
884 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
885 MCOS->EmitIntValue(0, 1);
886 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000887
888 // Add the NULL DIE terminating the Compile Unit DIE's.
889 MCOS->EmitIntValue(0, 1);
890
891 // Now set the value of the symbol at the end of the info section.
892 MCOS->EmitLabel(InfoEnd);
893}
894
Oliver Stannard8b273082014-06-19 15:52:37 +0000895// When generating dwarf for assembly source files this emits the data for
896// .debug_ranges section. We only emit one range list, which spans all of the
897// executable sections of this file.
898static void EmitGenDwarfRanges(MCStreamer *MCOS) {
899 MCContext &context = MCOS->getContext();
900 auto &Sections = context.getGenDwarfSectionSyms();
901
902 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000903 int AddrSize = AsmInfo->getCodePointerSize();
Oliver Stannard8b273082014-06-19 15:52:37 +0000904
905 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
906
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000907 for (MCSection *Sec : Sections) {
908 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000909 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000910 assert(StartSymbol && "StartSymbol must not be NULL");
911 assert(EndSymbol && "EndSymbol must not be NULL");
912
913 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000914 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000915 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +0000916 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +0000917 MCOS->EmitValue(SectionStartAddr, AddrSize);
918
919 // Emit a range list entry spanning this section
920 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
921 *StartSymbol, *EndSymbol, 0);
922 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000923 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000924 }
925
926 // Emit end of list entry
927 MCOS->EmitIntValue(0, AddrSize);
928 MCOS->EmitIntValue(0, AddrSize);
929}
930
Kevin Enderbye7739d42011-12-09 18:09:40 +0000931//
932// When generating dwarf for assembly source files this emits the Dwarf
933// sections.
934//
David Blaikie8bf66c42014-04-01 07:35:52 +0000935void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000936 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000937
938 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000939 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000940 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000941 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000942 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000943 if (CreateDwarfSectionSymbols)
944 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000945 MCSymbol *AbbrevSectionSymbol = nullptr;
946 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000947 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000948
949 // Create end symbols for each section, and remove empty sections
950 MCOS->getContext().finalizeDwarfSections(*MCOS);
951
952 // If there are no sections to generate debug info for, we don't need
953 // to do anything
954 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
955 return;
956
Oliver Stannard14f97d02014-09-22 10:45:16 +0000957 // We only use the .debug_ranges section if we have multiple code sections,
958 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000959 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000960 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
961 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000962 CreateDwarfSectionSymbols |= UseRangesSection;
963
Kevin Enderbye7739d42011-12-09 18:09:40 +0000964 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000965 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000966 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000967 MCOS->EmitLabel(InfoSectionSymbol);
968 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000969 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000970 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000971 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000972 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000973 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000974 if (UseRangesSection) {
975 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
976 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000977 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000978 MCOS->EmitLabel(RangesSectionSymbol);
979 }
980 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000981
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000982 assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
Oliver Stannard8b273082014-06-19 15:52:37 +0000983
984 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000985
986 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000987 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000988
Oliver Stannard8b273082014-06-19 15:52:37 +0000989 if (UseRangesSection)
990 EmitGenDwarfRanges(MCOS);
991
Kevin Enderbye7739d42011-12-09 18:09:40 +0000992 // Output the data for .debug_abbrev section.
993 EmitGenDwarfAbbrev(MCOS);
994
995 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000996 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
997 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000998}
999
1000//
1001// When generating dwarf for assembly source files this is called when symbol
1002// for a label is created. If this symbol is not a temporary and is in the
1003// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001004// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001005//
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001006void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +00001007 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +00001008 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001009 if (Symbol->isTemporary())
1010 return;
1011 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +00001012 // We won't create dwarf labels for symbols in sections that we are not
1013 // generating debug info for.
Eric Christopher445c9522016-10-14 05:47:37 +00001014 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
Kevin Enderbye7739d42011-12-09 18:09:40 +00001015 return;
1016
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001017 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +00001018 // underbar if any.
1019 StringRef Name = Symbol->getName();
1020 if (Name.startswith("_"))
1021 Name = Name.substr(1, Name.size()-1);
1022
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001023 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001024 unsigned FileNumber = context.getGenDwarfFileNumber();
1025
1026 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001027 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +00001028 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001029 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
1030
1031 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
1032 // values so that they don't have things like an ARM thumb bit from the
1033 // original symbol. So when used they won't get a low bit set after
1034 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +00001035 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +00001036 MCOS->EmitLabel(Label);
1037
1038 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001039 MCOS->getContext().addMCGenDwarfLabelEntry(
1040 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +00001041}
1042
Rafael Espindola0a017a62010-12-10 07:39:47 +00001043static int getDataAlignmentFactor(MCStreamer &streamer) {
1044 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001045 const MCAsmInfo *asmInfo = context.getAsmInfo();
1046 int size = asmInfo->getCalleeSaveStackSlotSize();
1047 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +00001048 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +00001049 else
1050 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001051}
1052
Rafael Espindola5395f442011-04-22 00:08:43 +00001053static unsigned getSizeForEncoding(MCStreamer &streamer,
1054 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001055 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001056 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001057 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +00001058 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001059 case dwarf::DW_EH_PE_absptr:
1060 case dwarf::DW_EH_PE_signed:
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001061 return context.getAsmInfo()->getCodePointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001062 case dwarf::DW_EH_PE_udata2:
1063 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +00001064 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001065 case dwarf::DW_EH_PE_udata4:
1066 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +00001067 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001068 case dwarf::DW_EH_PE_udata8:
1069 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +00001070 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001071 }
Rafael Espindola5395f442011-04-22 00:08:43 +00001072}
1073
Rafael Espindolafe963b12014-08-15 03:07:13 +00001074static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
1075 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +00001076 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001077 const MCAsmInfo *asmInfo = context.getAsmInfo();
1078 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1079 symbolEncoding,
1080 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +00001081 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001082 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +00001083 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +00001084 else
1085 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001086}
1087
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001088static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1089 unsigned symbolEncoding) {
1090 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001091 const MCAsmInfo *asmInfo = context.getAsmInfo();
1092 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1093 symbolEncoding,
1094 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001095 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001096 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001097}
1098
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001099namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001100
Rafael Espindola68c21652015-11-05 23:54:18 +00001101class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001102 int CFAOffset = 0;
1103 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001104 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001105 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001106
Rafael Espindola68c21652015-11-05 23:54:18 +00001107public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001108 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1109 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001110
Rafael Espindola68c21652015-11-05 23:54:18 +00001111 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001112 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001113
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001114 const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
Rafael Espindola46be4352015-11-06 03:02:51 +00001115 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001116 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001117 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001118 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001119 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001120};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001121
1122} // end anonymous namespace
1123
Rafael Espindolafe963b12014-08-15 03:07:13 +00001124static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001125 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001126}
1127
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001128void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001129 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001130 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001131
1132 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001133 case MCCFIInstruction::OpRegister: {
1134 unsigned Reg1 = Instr.getRegister();
1135 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001136 if (!IsEH) {
Jake Ehrlich3da79822017-12-01 21:44:27 +00001137 Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
1138 Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001139 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001140 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1141 Streamer.EmitULEB128IntValue(Reg1);
1142 Streamer.EmitULEB128IntValue(Reg2);
1143 return;
1144 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001145 case MCCFIInstruction::OpWindowSave:
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001146 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1147 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001148
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001149 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001150 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001151 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1152 Streamer.EmitULEB128IntValue(Reg);
1153 return;
1154 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001155 case MCCFIInstruction::OpAdjustCfaOffset:
1156 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001157 const bool IsRelative =
1158 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1159
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001160 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1161
1162 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001163 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001164 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001165 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001166
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001167 Streamer.EmitULEB128IntValue(CFAOffset);
1168
1169 return;
1170 }
1171 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001172 unsigned Reg = Instr.getRegister();
1173 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001174 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001175 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001176 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001177 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001178 Streamer.EmitULEB128IntValue(CFAOffset);
1179
1180 return;
1181 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001182 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001183 unsigned Reg = Instr.getRegister();
1184 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001185 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001186 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001187 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001188
1189 return;
1190 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001191 case MCCFIInstruction::OpOffset:
1192 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001193 const bool IsRelative =
1194 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001195
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001196 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001197 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001198 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001199
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001200 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001201 if (IsRelative)
1202 Offset -= CFAOffset;
1203 Offset = Offset / dataAlignmentFactor;
1204
1205 if (Offset < 0) {
1206 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1207 Streamer.EmitULEB128IntValue(Reg);
1208 Streamer.EmitSLEB128IntValue(Offset);
1209 } else if (Reg < 64) {
1210 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001211 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001212 } else {
1213 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001214 Streamer.EmitULEB128IntValue(Reg);
1215 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001216 }
1217 return;
1218 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001219 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001220 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1221 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001222 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001223 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1224 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001225 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001226 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001227 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001228 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001229 return;
1230 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001231 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001232 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001233 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001234 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001235 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1236 return;
1237 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001238 case MCCFIInstruction::OpGnuArgsSize:
Michael Kuperstein259f1502015-10-07 07:01:31 +00001239 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1240 Streamer.EmitULEB128IntValue(Instr.getOffset());
1241 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001242
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001243 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001244 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001245 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001246 }
1247 llvm_unreachable("Unhandled case in switch");
1248}
1249
Rafael Espindolafe963b12014-08-15 03:07:13 +00001250/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001251void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001252 MCSymbol *BaseLabel) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001253 for (const MCCFIInstruction &Instr : Instrs) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001254 MCSymbol *Label = Instr.getLabel();
1255 // Throw out move if the label is invalid.
1256 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1257
1258 // Advance row if new location.
1259 if (BaseLabel && Label) {
1260 MCSymbol *ThisSym = Label;
1261 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001262 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001263 BaseLabel = ThisSym;
1264 }
1265 }
1266
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001267 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001268 }
1269}
1270
Rafael Espindolafe963b12014-08-15 03:07:13 +00001271/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001272void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001273 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001274 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001275
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001276 // range-start range-length compact-unwind-enc personality-func lsda
1277 // _foo LfooEnd-_foo 0x00000023 0 0
1278 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1279 //
1280 // .section __LD,__compact_unwind,regular,debug
1281 //
1282 // # compact unwind for _foo
1283 // .quad _foo
1284 // .set L1,LfooEnd-_foo
1285 // .long L1
1286 // .long 0x01010001
1287 // .quad 0
1288 // .quad 0
1289 //
1290 // # compact unwind for _bar
1291 // .quad _bar
1292 // .set L2,LbarEnd-_bar
1293 // .long L2
1294 // .long 0x01020011
1295 // .quad __gxx_personality
1296 // .quad except_tab1
1297
Bill Wendling49bc6802011-07-19 00:06:12 +00001298 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001299 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001300 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001301
Bill Wendlingdafd5982011-07-14 23:34:45 +00001302 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001303 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001304 Encoding |= 0x40000000;
1305
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001306 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001307 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001308 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001309 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001310
1311 // Range Length
1312 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1313 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001314 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001315
Bill Wendling75174662011-06-30 00:30:52 +00001316 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001317 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1318 Streamer.EmitIntValue(Encoding, Size);
1319
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001320 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001321 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001322 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001323 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001324 else
1325 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001326
1327 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001328 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001329 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001330 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001331 else
1332 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001333}
1334
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001335static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1336 if (IsEH)
1337 return 1;
1338 switch (DwarfVersion) {
1339 case 2:
1340 return 1;
1341 case 3:
1342 return 3;
1343 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001344 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001345 return 4;
1346 }
1347 llvm_unreachable("Unknown version");
1348}
1349
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001350const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001351 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001352 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001353 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001354
Jim Grosbach6f482002015-05-18 18:43:14 +00001355 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001356 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001357
Jim Grosbach6f482002015-05-18 18:43:14 +00001358 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001359
1360 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001361 const MCExpr *Length =
1362 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1363 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001364
1365 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001366 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001367 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001368
1369 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001370 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001371 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001372
1373 // Augmentation String
1374 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001375 if (IsEH) {
1376 Augmentation += "z";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001377 if (Frame.Personality)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001378 Augmentation += "P";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001379 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001380 Augmentation += "L";
1381 Augmentation += "R";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001382 if (Frame.IsSignalFrame)
Rafael Espindola3c47e372012-01-23 21:51:52 +00001383 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001384 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001385 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001386 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001387
Keith Walkerea9483f2015-05-12 15:25:08 +00001388 if (CIEVersion >= 4) {
1389 // Address Size
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001390 Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001391
1392 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001393 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001394 }
1395
Rafael Espindola0a017a62010-12-10 07:39:47 +00001396 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001397 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001398
1399 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001400 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001401
1402 // Return Address Register
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001403 unsigned RAReg = Frame.RAReg;
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001404 if (RAReg == static_cast<unsigned>(INT_MAX))
1405 RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1406
Oliver Stannardf7693f42014-06-19 15:39:33 +00001407 if (CIEVersion == 1) {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001408 assert(RAReg <= 255 &&
Oliver Stannardf7693f42014-06-19 15:39:33 +00001409 "DWARF 2 encodes return_address_register in one byte");
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001410 Streamer.EmitIntValue(RAReg, 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001411 } else {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001412 Streamer.EmitULEB128IntValue(RAReg);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001413 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001414
1415 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001416 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001417 if (IsEH) {
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001418 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001419 // Personality Encoding
1420 augmentationLength += 1;
1421 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001422 augmentationLength +=
1423 getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001424 }
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001425 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001426 augmentationLength += 1;
1427 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001428 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001429
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001430 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001431
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001432 // Augmentation Data (optional)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001433 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001434 // Personality Encoding
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001435 emitEncodingByte(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001436 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001437 EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001438 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001439
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001440 if (Frame.Lsda)
1441 emitEncodingByte(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001442
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001443 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001444 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001445 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001446
1447 // Initial Instructions
1448
Bill Wendlingbc07a892013-06-18 07:20:20 +00001449 const MCAsmInfo *MAI = context.getAsmInfo();
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001450 if (!Frame.IsSimple) {
David Majnemere035cf92014-01-27 17:20:25 +00001451 const std::vector<MCCFIInstruction> &Instructions =
1452 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001453 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001454 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001455
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001456 InitialCFAOffset = CFAOffset;
1457
Rafael Espindola0a017a62010-12-10 07:39:47 +00001458 // Padding
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001459 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001460
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001461 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001462 return *sectionStart;
1463}
1464
Rafael Espindola46be4352015-11-06 03:02:51 +00001465void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1466 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001467 bool LastInSection,
1468 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001469 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001470 MCSymbol *fdeStart = context.createTempSymbol();
1471 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001472 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001473
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001474 CFAOffset = InitialCFAOffset;
1475
Rafael Espindola0a017a62010-12-10 07:39:47 +00001476 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001477 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1478 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001479
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001480 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001481
Rafael Espindola0a017a62010-12-10 07:39:47 +00001482 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001483 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001484 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001485 const MCExpr *offset =
1486 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1487 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001488 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001489 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001490 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001491 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001492 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001493 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001494 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001495
Rafael Espindola0a017a62010-12-10 07:39:47 +00001496 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001497 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001498 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001499 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1500 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001501
1502 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001503 const MCExpr *Range =
1504 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1505 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001506
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001507 if (IsEH) {
1508 // Augmentation Data Length
1509 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001510
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001511 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001512 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001513
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001514 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001515
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001516 // Augmentation Data
1517 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001518 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001519 }
Rafael Espindola68067662011-04-29 03:06:29 +00001520
Rafael Espindola0a017a62010-12-10 07:39:47 +00001521 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001522 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001523
Rafael Espindola0a017a62010-12-10 07:39:47 +00001524 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001525 // The size of a .eh_frame section has to be a multiple of the alignment
1526 // since a null CIE is interpreted as the end. Old systems overaligned
1527 // .eh_frame, so we do too and account for it in the last FDE.
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001528 unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
Rafael Espindola46be4352015-11-06 03:02:51 +00001529 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001530
Rafael Espindola46be4352015-11-06 03:02:51 +00001531 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001532}
1533
Benjamin Kramer570dd782010-12-30 22:34:44 +00001534namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001535
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001536struct CIEKey {
1537 static const CIEKey getEmptyKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001538 return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001539 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001540
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001541 static const CIEKey getTombstoneKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001542 return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001543 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001544
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001545 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001546 unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001547 unsigned RAReg)
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001548 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001549 LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001550 IsSimple(IsSimple), RAReg(RAReg) {}
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001551
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001552 explicit CIEKey(const MCDwarfFrameInfo &Frame)
1553 : Personality(Frame.Personality),
1554 PersonalityEncoding(Frame.PersonalityEncoding),
1555 LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
1556 IsSimple(Frame.IsSimple), RAReg(Frame.RAReg) {}
1557
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001558 const MCSymbol *Personality;
1559 unsigned PersonalityEncoding;
1560 unsigned LsdaEncoding;
1561 bool IsSignalFrame;
1562 bool IsSimple;
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001563 unsigned RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001564};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001565
1566} // end anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001567
1568namespace llvm {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001569
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001570template <> struct DenseMapInfo<CIEKey> {
1571 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1572 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001573
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001574 static unsigned getHashValue(const CIEKey &Key) {
1575 return static_cast<unsigned>(
1576 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001577 Key.IsSignalFrame, Key.IsSimple, Key.RAReg));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001578 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001579
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001580 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1581 return LHS.Personality == RHS.Personality &&
1582 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1583 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1584 LHS.IsSignalFrame == RHS.IsSignalFrame &&
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001585 LHS.IsSimple == RHS.IsSimple &&
1586 LHS.RAReg == RHS.RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001587 }
1588};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001589
1590} // end namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001591
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001592void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001593 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001594 Streamer.generateCompactUnwindEncodings(MAB);
1595
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001596 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001597 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001598 const MCAsmInfo *AsmInfo = Context.getAsmInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001599 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001600 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001601
Bill Wendling9803abb2011-09-06 18:37:11 +00001602 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001603 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001604 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001605 bool SectionEmitted = false;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001606 for (const MCDwarfFrameInfo &Frame : FrameArray) {
Bob Wilson0e180f22013-05-07 20:56:33 +00001607 if (Frame.CompactUnwindEncoding == 0) continue;
1608 if (!SectionEmitted) {
1609 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001610 Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001611 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001612 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001613 NeedsEHFrameSection |=
1614 Frame.CompactUnwindEncoding ==
1615 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001616 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001617 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001618 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001619
Tim Northoverd1c6f512014-03-29 09:03:13 +00001620 if (!NeedsEHFrameSection) return;
1621
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001622 MCSection &Section =
1623 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1624 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001625
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001626 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001627 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001628 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001629
Eric Christopherd29430d2014-06-19 20:00:09 +00001630 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001631
Craig Topperbb694de2014-04-13 04:57:38 +00001632 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001633 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001634 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1635 const MCDwarfFrameInfo &Frame = *I;
1636 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001637 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001638 MOFI->getCompactUnwindDwarfEHFrameOnly())
1639 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1640 // of by the compact unwind encoding.
1641 continue;
1642
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001643 CIEKey Key(Frame);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001644 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1645 if (!CIEStart)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001646 CIEStart = &Emitter.EmitCIE(Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001647
Rafael Espindola97588e12015-11-06 13:14:59 +00001648 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001649 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001650}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001651
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001652void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001653 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001654 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001655 SmallString<256> Tmp;
1656 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001657 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001658 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001659}
1660
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001661void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1662 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001663 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001664 // Scale the address delta by the minimum instruction length.
1665 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1666
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001667 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001668 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001669 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1670 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001671 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001672 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1673 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001674 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001675 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001676 if (Context.getAsmInfo()->isLittleEndian())
1677 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1678 else
1679 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001680 } else {
1681 assert(isUInt<32>(AddrDelta));
1682 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001683 if (Context.getAsmInfo()->isLittleEndian())
1684 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1685 else
1686 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001687 }
1688}