blob: 57252a07d9ed135211e8f442773bb002e1d19c52 [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
10#include "llvm/MC/MCDwarf.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/Hashing.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000012#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000019#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000020#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000021#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000022#include "llvm/MC/MCSection.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000023#include "llvm/MC/MCSymbol.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000024#include "llvm/Support/Debug.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000025#include "llvm/Support/EndianStream.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000026#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000027#include "llvm/Support/LEB128.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000028#include "llvm/Support/Path.h"
29#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/raw_ostream.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000031
Kevin Enderbye5930f12010-07-28 20:55:35 +000032using namespace llvm;
33
Ulrich Weigand32d725b2013-06-12 14:46:54 +000034static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000035 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000036 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000037 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000038 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000039 // TODO: report this error, but really only once.
40 ;
41 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000042 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000043}
44
45//
46// This is called when an instruction is assembled into the specified section
47// and if there is information from the last .loc directive that has yet to have
48// a line entry made for it is made.
49//
David Majnemer8cc30782016-01-21 01:59:03 +000050void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000051 if (!MCOS->getContext().getDwarfLocSeen())
52 return;
53
54 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000055 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000056 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000057 MCOS->EmitLabel(LineSym);
58
59 // Get the current .loc info saved in the context.
60 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
61
62 // Create a (local) line entry with the symbol and the current .loc info.
David Majnemer8cc30782016-01-21 01:59:03 +000063 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +000064
65 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +000066 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000067
Kevin Enderbye46564a2010-09-30 16:52:03 +000068 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000069 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000070 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000071 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000072 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000073}
74
75//
76// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000077//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000078static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
79 const MCSymbol &Start,
80 const MCSymbol &End,
81 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000082 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
83 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +000084 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000085 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +000086 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000087 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000088 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000089 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000090 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000091 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000092 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000093 return Res3;
94}
95
Kevin Enderbye46564a2010-09-30 16:52:03 +000096//
97// This emits the Dwarf line table for the specified section from the entries
98// in the LineSection.
99//
David Blaikief4ad6982014-03-12 22:35:23 +0000100static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000101EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000102 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000103 unsigned FileNum = 1;
104 unsigned LastLine = 1;
105 unsigned Column = 0;
106 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
107 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000108 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000109 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110
David Majnemer8cc30782016-01-21 01:59:03 +0000111 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000112 for (auto it = LineEntries.begin(),
113 ie = LineEntries.end();
114 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000115
Dehao Chen18ce9d82016-04-22 21:31:18 +0000116 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
117
Kevin Enderbye46564a2010-09-30 16:52:03 +0000118 if (FileNum != it->getFileNum()) {
119 FileNum = it->getFileNum();
120 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000121 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000122 }
123 if (Column != it->getColumn()) {
124 Column = it->getColumn();
125 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000126 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000127 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000128 if (Discriminator != it->getDiscriminator()) {
129 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000130 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000131 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
132 MCOS->EmitULEB128IntValue(Size + 1);
133 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
134 MCOS->EmitULEB128IntValue(Discriminator);
135 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000136 if (Isa != it->getIsa()) {
137 Isa = it->getIsa();
138 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000139 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000140 }
141 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
142 Flags = it->getFlags();
143 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
144 }
145 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
146 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
147 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
148 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
149 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
150 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
151
Kevin Enderbye46564a2010-09-30 16:52:03 +0000152 MCSymbol *Label = it->getLabel();
153
154 // At this point we want to emit/create the sequence to encode the delta in
155 // line numbers and the increment of the address from the previous Label
156 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000157 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000158 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000159 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000160
Dehao Chen1b54fce2016-04-28 22:09:37 +0000161 Discriminator = 0;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000162 LastLine = it->getLine();
163 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000164 }
165
166 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000167 // Use the section end label to compute the address delta and use INT64_MAX
168 // as the line delta which is the signal that this is actually a
169 // DW_LNE_end_sequence.
170 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000171
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000172 // Switch back the dwarf line section, in case endSection had to switch the
173 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000174 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000175 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000176
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000177 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000178 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000179 AsmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000180}
181
182//
183// This emits the Dwarf file and the line tables.
184//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000185void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
186 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000187 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000188
David Blaikie8bf66c42014-04-01 07:35:52 +0000189 auto &LineTables = context.getMCDwarfLineTables();
190
191 // Bail out early so we don't switch to the debug_line section needlessly and
192 // in doing so create an unnecessary (if empty) section.
193 if (LineTables.empty())
194 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000195
196 // Switch to the section where the table will be emitted into.
197 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
198
Manman Ren4e042a62013-02-05 21:52:47 +0000199 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000200 for (const auto &CUIDTablePair : LineTables)
Frederic Rissa5ab8442015-08-07 15:14:08 +0000201 CUIDTablePair.second.EmitCU(MCOS, Params);
Manman Ren4e042a62013-02-05 21:52:47 +0000202}
203
Frederic Rissa5ab8442015-08-07 15:14:08 +0000204void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS,
205 MCDwarfLineTableParams Params) const {
206 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000207}
208
Frederic Rissa5ab8442015-08-07 15:14:08 +0000209std::pair<MCSymbol *, MCSymbol *>
210MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
211 MCDwarfLineTableParams Params) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000212 static const char StandardOpcodeLengths[] = {
213 0, // length of DW_LNS_copy
214 1, // length of DW_LNS_advance_pc
215 1, // length of DW_LNS_advance_line
216 1, // length of DW_LNS_set_file
217 1, // length of DW_LNS_set_column
218 0, // length of DW_LNS_negate_stmt
219 0, // length of DW_LNS_set_basic_block
220 0, // length of DW_LNS_const_add_pc
221 1, // length of DW_LNS_fixed_advance_pc
222 0, // length of DW_LNS_set_prologue_end
223 0, // length of DW_LNS_set_epilogue_begin
224 1 // DW_LNS_set_isa
225 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000226 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000227 (Params.DWARF2LineOpcodeBase - 1U));
Craig Topper0013be12015-09-21 05:32:41 +0000228 return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
229 Params.DWARF2LineOpcodeBase - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000230}
231
Rafael Espindolac23174b2014-08-15 15:12:13 +0000232static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
233 MCContext &Context = OS.getContext();
234 assert(!isa<MCSymbolRefExpr>(Expr));
235 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
236 return Expr;
237
Jim Grosbach6f482002015-05-18 18:43:14 +0000238 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000239 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000240 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000241}
242
243static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
244 const MCExpr *ABS = forceExpAbs(OS, Value);
245 OS.EmitValue(ABS, Size);
246}
247
David Blaikie8287aff2014-03-18 02:13:23 +0000248std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000249MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
David Blaikie8287aff2014-03-18 02:13:23 +0000250 ArrayRef<char> StandardOpcodeLengths) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000251 MCContext &context = MCOS->getContext();
252
253 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000254 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000255 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000256 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000257 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000258 MCOS->EmitLabel(LineStartSym);
259
260 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000261 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000262
263 // The first 4 bytes is the total length of the information for this
264 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000265 emitAbsValue(*MCOS,
266 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000267
268 // Next 2 bytes is the Version, which is Dwarf 2.
269 MCOS->EmitIntValue(2, 2);
270
271 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000272 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000273
274 // Length of the prologue, is the next 4 bytes. Which is the start of the
275 // section to the end of the prologue. Not including the 4 bytes for the
276 // total length, the 2 bytes for the version, and these 4 bytes for the
277 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000278 emitAbsValue(
279 *MCOS,
280 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000281
282 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000283 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000284 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000285 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
286 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000287 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000288
289 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000290 for (char Length : StandardOpcodeLengths)
291 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000292
293 // Put out the directory and file tables.
294
295 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000296 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000297 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
298 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000299 }
300 MCOS->EmitIntValue(0, 1); // Terminate the directory list
301
302 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000303 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000304 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000305 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000306 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000307 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000308 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000309 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
310 MCOS->EmitIntValue(0, 1); // filesize (always 0)
311 }
312 MCOS->EmitIntValue(0, 1); // Terminate the file list
313
314 // This is the end of the prologue, so set the value of the symbol at the
315 // end of the prologue (that was used in a previous expression).
316 MCOS->EmitLabel(ProEndSym);
317
David Blaikie5cff0e62014-03-13 21:47:12 +0000318 return std::make_pair(LineStartSym, LineEndSym);
319}
320
Frederic Rissa5ab8442015-08-07 15:14:08 +0000321void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
322 MCDwarfLineTableParams Params) const {
323 MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000324
Kevin Enderbye46564a2010-09-30 16:52:03 +0000325 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000326 for (const auto &LineSec : MCLineSections.getMCLineEntries())
327 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000328
Kevin Enderbye46564a2010-09-30 16:52:03 +0000329 // This is the end of the section, so set the value of the symbol at the end
330 // of this section (that was used in a previous expression).
331 MCOS->EmitLabel(LineEndSym);
332}
333
David Blaikiec2df16b2014-03-17 18:13:58 +0000334unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000335 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000336 return Header.getFile(Directory, FileName, FileNumber);
337}
338
David Blaikiec2df16b2014-03-17 18:13:58 +0000339unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
340 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000341 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000342 if (Directory == CompilationDir)
343 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000344 if (FileName.empty()) {
345 FileName = "<stdin>";
346 Directory = "";
347 }
348 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000349 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000350 // File numbers start with 1 and/or after any file numbers
351 // allocated by inline-assembler .file directives.
352 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000353 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000354 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000355 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
356 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000357 if (!IterBool.second)
358 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000359 }
David Blaikie498589c2014-03-13 19:15:04 +0000360 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
361 MCDwarfFiles.resize(FileNumber + 1);
362
363 // Get the new MCDwarfFile slot for this FileNumber.
364 MCDwarfFile &File = MCDwarfFiles[FileNumber];
365
366 // It is an error to use see the same number more than once.
367 if (!File.Name.empty())
368 return 0;
369
370 if (Directory.empty()) {
371 // Separate the directory part from the basename of the FileName.
372 StringRef tFileName = sys::path::filename(FileName);
373 if (!tFileName.empty()) {
374 Directory = sys::path::parent_path(FileName);
375 if (!Directory.empty())
376 FileName = tFileName;
377 }
378 }
379
380 // Find or make an entry in the MCDwarfDirs vector for this Directory.
381 // Capture directory name.
382 unsigned DirIndex;
383 if (Directory.empty()) {
384 // For FileNames with no directories a DirIndex of 0 is used.
385 DirIndex = 0;
386 } else {
387 DirIndex = 0;
388 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
389 if (Directory == MCDwarfDirs[DirIndex])
390 break;
391 }
392 if (DirIndex >= MCDwarfDirs.size())
393 MCDwarfDirs.push_back(Directory);
394 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
395 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
396 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
397 // are stored at MCDwarfFiles[FileNumber].Name .
398 DirIndex++;
399 }
400
401 File.Name = FileName;
402 File.DirIndex = DirIndex;
403
404 // return the allocated FileNumber.
405 return FileNumber;
406}
407
Kevin Enderbye46564a2010-09-30 16:52:03 +0000408/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000409void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
410 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000411 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000412 SmallString<256> Tmp;
413 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000414 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000415 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000416}
417
Frederic Rissa5ab8442015-08-07 15:14:08 +0000418/// Given a special op, return the address skip amount (in units of
419/// DWARF2_LINE_MIN_INSN_LENGTH).
420static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
421 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
422}
423
Kevin Enderbye46564a2010-09-30 16:52:03 +0000424/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000425void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
426 int64_t LineDelta, uint64_t AddrDelta,
427 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000428 uint64_t Temp, Opcode;
429 bool NeedCopy = false;
430
Frederic Rissa5ab8442015-08-07 15:14:08 +0000431 // The maximum address skip amount that can be encoded with a special op.
432 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
433
Kevin Enderbye46564a2010-09-30 16:52:03 +0000434 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000435 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000436
437 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000438 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000439 // end_sequence to emit the matrix entry.
440 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000441 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000442 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000443 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000444 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000445 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000446 }
447 OS << char(dwarf::DW_LNS_extended_op);
448 OS << char(1);
449 OS << char(dwarf::DW_LNE_end_sequence);
450 return;
451 }
452
453 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000454 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000455
456 // If the line increment is out of range of a special opcode, we must encode
457 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000458 if (Temp >= Params.DWARF2LineRange ||
459 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000460 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000461 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000462
463 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000464 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000465 NeedCopy = true;
466 }
467
468 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
469 if (LineDelta == 0 && AddrDelta == 0) {
470 OS << char(dwarf::DW_LNS_copy);
471 return;
472 }
473
474 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000475 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000476
477 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000478 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000479 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000480 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000481 if (Opcode <= 255) {
482 OS << char(Opcode);
483 return;
484 }
485
486 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000487 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000488 if (Opcode <= 255) {
489 OS << char(dwarf::DW_LNS_const_add_pc);
490 OS << char(Opcode);
491 return;
492 }
493 }
494
495 // Otherwise use DW_LNS_advance_pc.
496 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000497 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000498
499 if (NeedCopy)
500 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000501 else {
502 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000503 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000504 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000505}
506
Kevin Enderbye7739d42011-12-09 18:09:40 +0000507// Utility function to write a tuple for .debug_abbrev.
508static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
509 MCOS->EmitULEB128IntValue(Name);
510 MCOS->EmitULEB128IntValue(Form);
511}
512
513// When generating dwarf for assembly source files this emits
514// the data for .debug_abbrev section which contains three DIEs.
515static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
516 MCContext &context = MCOS->getContext();
517 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
518
519 // DW_TAG_compile_unit DIE abbrev (1).
520 MCOS->EmitULEB128IntValue(1);
521 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
522 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000523 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
524 ? dwarf::DW_FORM_sec_offset
525 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000526 if (context.getGenDwarfSectionSyms().size() > 1 &&
527 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000528 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
529 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000530 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000531 } else {
532 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
533 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
534 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000535 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000536 if (!context.getCompilationDir().empty())
537 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000538 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
539 if (!DwarfDebugFlags.empty())
540 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
541 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
542 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
543 EmitAbbrev(MCOS, 0, 0);
544
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000545 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000546 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000547 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000548 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
549 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
550 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
551 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
552 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000553 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
554 EmitAbbrev(MCOS, 0, 0);
555
556 // DW_TAG_unspecified_parameters DIE abbrev (3).
557 MCOS->EmitULEB128IntValue(3);
558 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
559 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
560 EmitAbbrev(MCOS, 0, 0);
561
562 // Terminate the abbreviations for this compilation unit.
563 MCOS->EmitIntValue(0, 1);
564}
565
566// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000567// .debug_aranges section. This section contains a header and a table of pairs
568// of PointerSize'ed values for the address and size of section(s) with line
569// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000570static void EmitGenDwarfAranges(MCStreamer *MCOS,
571 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000572 MCContext &context = MCOS->getContext();
573
Oliver Stannard8b273082014-06-19 15:52:37 +0000574 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000575
576 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
577
578 // This will be the length of the .debug_aranges section, first account for
579 // the size of each item in the header (see below where we emit these items).
580 int Length = 4 + 2 + 4 + 1 + 1;
581
582 // Figure the padding after the header before the table of address and size
583 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000584 const MCAsmInfo *asmInfo = context.getAsmInfo();
585 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000586 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
587 if (Pad == 2 * AddrSize)
588 Pad = 0;
589 Length += Pad;
590
591 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000592 // of each section we have in the table.
593 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000594 // And the pair of terminating zeros.
595 Length += 2 * AddrSize;
596
597
598 // Emit the header for this section.
599 // The 4 byte length not including the 4 byte value for the length.
600 MCOS->EmitIntValue(Length - 4, 4);
601 // The 2 byte version, which is 2.
602 MCOS->EmitIntValue(2, 2);
603 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000604 // of the .debug_info.
605 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000606 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
607 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000608 else
609 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000610 // The 1 byte size of an address.
611 MCOS->EmitIntValue(AddrSize, 1);
612 // The 1 byte size of a segment descriptor, we use a value of zero.
613 MCOS->EmitIntValue(0, 1);
614 // Align the header with the padding if needed, before we put out the table.
615 for(int i = 0; i < Pad; i++)
616 MCOS->EmitIntValue(0, 1);
617
Oliver Stannard8b273082014-06-19 15:52:37 +0000618 // Now emit the table of pairs of PointerSize'ed values for the section
619 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000620 for (MCSection *Sec : Sections) {
621 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000622 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000623 assert(StartSymbol && "StartSymbol must not be NULL");
624 assert(EndSymbol && "EndSymbol must not be NULL");
625
Jim Grosbach13760bd2015-05-30 01:25:56 +0000626 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000627 StartSymbol, MCSymbolRefExpr::VK_None, context);
628 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
629 *StartSymbol, *EndSymbol, 0);
630 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000631 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000632 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633
634 // And finally the pair of terminating zeros.
635 MCOS->EmitIntValue(0, AddrSize);
636 MCOS->EmitIntValue(0, AddrSize);
637}
638
639// When generating dwarf for assembly source files this emits the data for
640// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000641// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000642static void EmitGenDwarfInfo(MCStreamer *MCOS,
643 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000644 const MCSymbol *LineSectionSymbol,
645 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000646 MCContext &context = MCOS->getContext();
647
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000648 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000649
650 // Create a symbol at the start and end of this section used in here for the
651 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000652 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000653 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000654 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000655
656 // First part: the header.
657
658 // The 4 byte total length of the information for this compilation unit, not
659 // including these 4 bytes.
660 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000661 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000662
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000663 // The 2 byte DWARF version.
664 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000665
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000666 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000667 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
668 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000669 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000670 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000671 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000672 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
673 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000674
Bill Wendlingbc07a892013-06-18 07:20:20 +0000675 const MCAsmInfo *asmInfo = context.getAsmInfo();
676 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000677 // The 1 byte size of an address.
678 MCOS->EmitIntValue(AddrSize, 1);
679
680 // Second part: the compile_unit DIE.
681
682 // The DW_TAG_compile_unit DIE abbrev (1).
683 MCOS->EmitULEB128IntValue(1);
684
685 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
686 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000687 if (LineSectionSymbol)
688 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
689 AsmInfo.needsDwarfSectionOffsetDirective());
690 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000691 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000692
Oliver Stannard8b273082014-06-19 15:52:37 +0000693 if (RangesSectionSymbol) {
694 // There are multiple sections containing code, so we must use the
695 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000696
Oliver Stannard8b273082014-06-19 15:52:37 +0000697 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
698 // to the address range list for this compilation unit.
699 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
700 } else {
701 // If we only have one non-empty code section, we can use the simpler
702 // AT_low_pc and AT_high_pc attributes.
703
704 // Find the first (and only) non-empty text section
705 auto &Sections = context.getGenDwarfSectionSyms();
706 const auto TextSection = Sections.begin();
707 assert(TextSection != Sections.end() && "No text section found");
708
Rafael Espindolae0746792015-05-21 16:52:32 +0000709 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
710 MCSymbol *EndSymbol = (*TextSection)->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
714 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000715 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000716 StartSymbol, MCSymbolRefExpr::VK_None, context);
717 MCOS->EmitValue(Start, AddrSize);
718
719 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000720 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000721 EndSymbol, MCSymbolRefExpr::VK_None, context);
722 MCOS->EmitValue(End, AddrSize);
723 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000724
725 // AT_name, the name of the source file. Reconstruct from the first directory
726 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000727 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000728 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000729 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000730 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000731 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000732 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000733 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000734 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000735 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
736
737 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000738 if (!context.getCompilationDir().empty()) {
739 MCOS->EmitBytes(context.getCompilationDir());
740 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
741 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000742
743 // AT_APPLE_flags, the command line arguments of the assembler tool.
744 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
745 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000746 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000747 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
748 }
749
750 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000751 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000752 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000753 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000754 else
755 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000756 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
757
758 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
759 // draft has no standard code for assembler.
760 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
761
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000762 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000763
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000764 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000765 const std::vector<MCGenDwarfLabelEntry> &Entries =
766 MCOS->getContext().getMCGenDwarfLabelEntries();
767 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000768 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000769 MCOS->EmitULEB128IntValue(2);
770
771 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000772 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
774
775 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000776 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777
778 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000779 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000780
781 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000782 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000784 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785
Kevin Enderbye7739d42011-12-09 18:09:40 +0000786 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
787 MCOS->EmitIntValue(0, 1);
788
789 // The DW_TAG_unspecified_parameters DIE abbrev (3).
790 MCOS->EmitULEB128IntValue(3);
791
792 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
793 MCOS->EmitIntValue(0, 1);
794 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795
796 // Add the NULL DIE terminating the Compile Unit DIE's.
797 MCOS->EmitIntValue(0, 1);
798
799 // Now set the value of the symbol at the end of the info section.
800 MCOS->EmitLabel(InfoEnd);
801}
802
Oliver Stannard8b273082014-06-19 15:52:37 +0000803// When generating dwarf for assembly source files this emits the data for
804// .debug_ranges section. We only emit one range list, which spans all of the
805// executable sections of this file.
806static void EmitGenDwarfRanges(MCStreamer *MCOS) {
807 MCContext &context = MCOS->getContext();
808 auto &Sections = context.getGenDwarfSectionSyms();
809
810 const MCAsmInfo *AsmInfo = context.getAsmInfo();
811 int AddrSize = AsmInfo->getPointerSize();
812
813 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
814
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000815 for (MCSection *Sec : Sections) {
816 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000817 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000818 assert(StartSymbol && "StartSymbol must not be NULL");
819 assert(EndSymbol && "EndSymbol must not be NULL");
820
821 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000822 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000823 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +0000824 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +0000825 MCOS->EmitValue(SectionStartAddr, AddrSize);
826
827 // Emit a range list entry spanning this section
828 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
829 *StartSymbol, *EndSymbol, 0);
830 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000831 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000832 }
833
834 // Emit end of list entry
835 MCOS->EmitIntValue(0, AddrSize);
836 MCOS->EmitIntValue(0, AddrSize);
837}
838
Kevin Enderbye7739d42011-12-09 18:09:40 +0000839//
840// When generating dwarf for assembly source files this emits the Dwarf
841// sections.
842//
David Blaikie8bf66c42014-04-01 07:35:52 +0000843void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000844 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000845
846 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000847 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000848 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000849 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000850 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000851 if (CreateDwarfSectionSymbols)
852 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000853 MCSymbol *AbbrevSectionSymbol = nullptr;
854 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000855 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000856
857 // Create end symbols for each section, and remove empty sections
858 MCOS->getContext().finalizeDwarfSections(*MCOS);
859
860 // If there are no sections to generate debug info for, we don't need
861 // to do anything
862 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
863 return;
864
Oliver Stannard14f97d02014-09-22 10:45:16 +0000865 // We only use the .debug_ranges section if we have multiple code sections,
866 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000867 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000868 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
869 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000870 CreateDwarfSectionSymbols |= UseRangesSection;
871
Kevin Enderbye7739d42011-12-09 18:09:40 +0000872 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000873 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000874 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000875 MCOS->EmitLabel(InfoSectionSymbol);
876 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000877 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000878 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000879 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000880 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000881 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000882 if (UseRangesSection) {
883 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
884 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000885 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000886 MCOS->EmitLabel(RangesSectionSymbol);
887 }
888 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000889
Oliver Stannard8b273082014-06-19 15:52:37 +0000890 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
891
892 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000893
894 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000895 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000896
Oliver Stannard8b273082014-06-19 15:52:37 +0000897 if (UseRangesSection)
898 EmitGenDwarfRanges(MCOS);
899
Kevin Enderbye7739d42011-12-09 18:09:40 +0000900 // Output the data for .debug_abbrev section.
901 EmitGenDwarfAbbrev(MCOS);
902
903 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000904 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
905 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000906}
907
908//
909// When generating dwarf for assembly source files this is called when symbol
910// for a label is created. If this symbol is not a temporary and is in the
911// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000912// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000913//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000914void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000915 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000916 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000917 if (Symbol->isTemporary())
918 return;
919 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000920 // We won't create dwarf labels for symbols in sections that we are not
921 // generating debug info for.
922 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000923 return;
924
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000925 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000926 // underbar if any.
927 StringRef Name = Symbol->getName();
928 if (Name.startswith("_"))
929 Name = Name.substr(1, Name.size()-1);
930
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000931 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000932 unsigned FileNumber = context.getGenDwarfFileNumber();
933
934 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000935 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000936 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000937 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
938
939 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
940 // values so that they don't have things like an ARM thumb bit from the
941 // original symbol. So when used they won't get a low bit set after
942 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000943 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000944 MCOS->EmitLabel(Label);
945
946 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000947 MCOS->getContext().addMCGenDwarfLabelEntry(
948 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000949}
950
Rafael Espindola0a017a62010-12-10 07:39:47 +0000951static int getDataAlignmentFactor(MCStreamer &streamer) {
952 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000953 const MCAsmInfo *asmInfo = context.getAsmInfo();
954 int size = asmInfo->getCalleeSaveStackSlotSize();
955 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000956 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000957 else
958 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000959}
960
Rafael Espindola5395f442011-04-22 00:08:43 +0000961static unsigned getSizeForEncoding(MCStreamer &streamer,
962 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000963 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000964 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000965 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000966 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000967 case dwarf::DW_EH_PE_absptr:
968 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000969 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000970 case dwarf::DW_EH_PE_udata2:
971 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000972 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000973 case dwarf::DW_EH_PE_udata4:
974 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000975 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000976 case dwarf::DW_EH_PE_udata8:
977 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000978 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000979 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000980}
981
Rafael Espindolafe963b12014-08-15 03:07:13 +0000982static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
983 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000984 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000985 const MCAsmInfo *asmInfo = context.getAsmInfo();
986 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
987 symbolEncoding,
988 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000989 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000990 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000991 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000992 else
993 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000994}
995
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000996static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
997 unsigned symbolEncoding) {
998 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000999 const MCAsmInfo *asmInfo = context.getAsmInfo();
1000 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1001 symbolEncoding,
1002 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001003 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001004 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001005}
1006
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001007namespace {
Rafael Espindola68c21652015-11-05 23:54:18 +00001008class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001009 int CFAOffset = 0;
1010 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001011 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001012 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001013
Rafael Espindola68c21652015-11-05 23:54:18 +00001014public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001015 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1016 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001017
Rafael Espindola68c21652015-11-05 23:54:18 +00001018 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001019 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001020
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001021 const MCSymbol &EmitCIE(const MCSymbol *personality,
Rafael Espindola68c21652015-11-05 23:54:18 +00001022 unsigned personalityEncoding, const MCSymbol *lsda,
1023 bool IsSignalFrame, unsigned lsdaEncoding,
1024 bool IsSimple);
Rafael Espindola46be4352015-11-06 03:02:51 +00001025 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001026 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001027 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001028 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001029 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001030};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001031
1032} // end anonymous namespace
1033
Rafael Espindolafe963b12014-08-15 03:07:13 +00001034static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001035 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001036}
1037
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001038void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001039 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001040 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001041
1042 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001043 case MCCFIInstruction::OpRegister: {
1044 unsigned Reg1 = Instr.getRegister();
1045 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001046 if (!IsEH) {
1047 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1048 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1049 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001050 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1051 Streamer.EmitULEB128IntValue(Reg1);
1052 Streamer.EmitULEB128IntValue(Reg2);
1053 return;
1054 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001055 case MCCFIInstruction::OpWindowSave: {
1056 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1057 return;
1058 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001059 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001060 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001061 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1062 Streamer.EmitULEB128IntValue(Reg);
1063 return;
1064 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001065 case MCCFIInstruction::OpAdjustCfaOffset:
1066 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001067 const bool IsRelative =
1068 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1069
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1071
1072 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001073 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001074 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001075 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001076
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001077 Streamer.EmitULEB128IntValue(CFAOffset);
1078
1079 return;
1080 }
1081 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001082 unsigned Reg = Instr.getRegister();
1083 if (!IsEH)
1084 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001085 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001086 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001087 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001088 Streamer.EmitULEB128IntValue(CFAOffset);
1089
1090 return;
1091 }
1092
1093 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001094 unsigned Reg = Instr.getRegister();
1095 if (!IsEH)
1096 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001097 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001098 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001099
1100 return;
1101 }
1102
1103 case MCCFIInstruction::OpOffset:
1104 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001105 const bool IsRelative =
1106 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001107
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001108 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001109 if (!IsEH)
1110 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1111
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001112 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001113 if (IsRelative)
1114 Offset -= CFAOffset;
1115 Offset = Offset / dataAlignmentFactor;
1116
1117 if (Offset < 0) {
1118 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1119 Streamer.EmitULEB128IntValue(Reg);
1120 Streamer.EmitSLEB128IntValue(Offset);
1121 } else if (Reg < 64) {
1122 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001123 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001124 } else {
1125 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001126 Streamer.EmitULEB128IntValue(Reg);
1127 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001128 }
1129 return;
1130 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001131 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001132 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1133 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001134 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001135 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1136 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001137 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001138 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001139 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001140 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001141 return;
1142 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001143 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001144 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001145 if (!IsEH)
1146 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001147 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1148 return;
1149 }
Michael Kuperstein259f1502015-10-07 07:01:31 +00001150 case MCCFIInstruction::OpGnuArgsSize: {
1151 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1152 Streamer.EmitULEB128IntValue(Instr.getOffset());
1153 return;
1154 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001155 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001156 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001157 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001158 }
1159 llvm_unreachable("Unhandled case in switch");
1160}
1161
Rafael Espindolafe963b12014-08-15 03:07:13 +00001162/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001163void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001164 MCSymbol *BaseLabel) {
1165 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1166 const MCCFIInstruction &Instr = Instrs[i];
1167 MCSymbol *Label = Instr.getLabel();
1168 // Throw out move if the label is invalid.
1169 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1170
1171 // Advance row if new location.
1172 if (BaseLabel && Label) {
1173 MCSymbol *ThisSym = Label;
1174 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001175 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001176 BaseLabel = ThisSym;
1177 }
1178 }
1179
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001180 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001181 }
1182}
1183
Rafael Espindolafe963b12014-08-15 03:07:13 +00001184/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001185void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001186 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001187 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001188
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001189 // range-start range-length compact-unwind-enc personality-func lsda
1190 // _foo LfooEnd-_foo 0x00000023 0 0
1191 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1192 //
1193 // .section __LD,__compact_unwind,regular,debug
1194 //
1195 // # compact unwind for _foo
1196 // .quad _foo
1197 // .set L1,LfooEnd-_foo
1198 // .long L1
1199 // .long 0x01010001
1200 // .quad 0
1201 // .quad 0
1202 //
1203 // # compact unwind for _bar
1204 // .quad _bar
1205 // .set L2,LbarEnd-_bar
1206 // .long L2
1207 // .long 0x01020011
1208 // .quad __gxx_personality
1209 // .quad except_tab1
1210
Bill Wendling49bc6802011-07-19 00:06:12 +00001211 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001212 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001213 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001214
Bill Wendlingdafd5982011-07-14 23:34:45 +00001215 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001216 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001217 Encoding |= 0x40000000;
1218
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001219 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001220 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001221 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001222 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001223
1224 // Range Length
1225 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1226 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001227 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001228
Bill Wendling75174662011-06-30 00:30:52 +00001229 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001230 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1231 Streamer.EmitIntValue(Encoding, Size);
1232
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001233 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001234 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001235 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001236 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001237 else
1238 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001239
1240 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001241 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001242 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001243 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001244 else
1245 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001246}
1247
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001248static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1249 if (IsEH)
1250 return 1;
1251 switch (DwarfVersion) {
1252 case 2:
1253 return 1;
1254 case 3:
1255 return 3;
1256 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001257 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001258 return 4;
1259 }
1260 llvm_unreachable("Unknown version");
1261}
1262
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001263const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001264 unsigned personalityEncoding,
1265 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001266 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001267 unsigned lsdaEncoding,
1268 bool IsSimple) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001269 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001270 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001271 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001272
Jim Grosbach6f482002015-05-18 18:43:14 +00001273 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001274 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001275
Jim Grosbach6f482002015-05-18 18:43:14 +00001276 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001277
1278 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001279 const MCExpr *Length =
1280 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1281 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001282
1283 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001284 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001285 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001286
1287 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001288 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001289 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001290
1291 // Augmentation String
1292 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001293 if (IsEH) {
1294 Augmentation += "z";
1295 if (personality)
1296 Augmentation += "P";
1297 if (lsda)
1298 Augmentation += "L";
1299 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001300 if (IsSignalFrame)
1301 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001302 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001303 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001304 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001305
Keith Walkerea9483f2015-05-12 15:25:08 +00001306 if (CIEVersion >= 4) {
1307 // Address Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001308 Streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001309
1310 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001311 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001312 }
1313
Rafael Espindola0a017a62010-12-10 07:39:47 +00001314 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001315 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001316
1317 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001318 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001319
1320 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001321 if (CIEVersion == 1) {
1322 assert(MRI->getRARegister() <= 255 &&
1323 "DWARF 2 encodes return_address_register in one byte");
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001324 Streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001325 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001326 Streamer.EmitULEB128IntValue(
Frederic Rissadbb3f22015-02-26 19:48:07 +00001327 MRI->getDwarfRegNum(MRI->getRARegister(), IsEH));
Oliver Stannardf7693f42014-06-19 15:39:33 +00001328 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001329
1330 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001331
1332 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001333 if (IsEH) {
1334 if (personality) {
1335 // Personality Encoding
1336 augmentationLength += 1;
1337 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001338 augmentationLength += getSizeForEncoding(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001339 }
1340 if (lsda)
1341 augmentationLength += 1;
1342 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001343 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001344
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001345 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001346
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001347 // Augmentation Data (optional)
1348 if (personality) {
1349 // Personality Encoding
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001350 emitEncodingByte(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001351 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001352 EmitPersonality(Streamer, *personality, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001353 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001354
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001355 if (lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001356 emitEncodingByte(Streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001357
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001358 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001359 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001360 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001361
1362 // Initial Instructions
1363
Bill Wendlingbc07a892013-06-18 07:20:20 +00001364 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001365 if (!IsSimple) {
1366 const std::vector<MCCFIInstruction> &Instructions =
1367 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001368 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001369 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001370
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001371 InitialCFAOffset = CFAOffset;
1372
Rafael Espindola0a017a62010-12-10 07:39:47 +00001373 // Padding
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001374 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001375
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001376 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001377 return *sectionStart;
1378}
1379
Rafael Espindola46be4352015-11-06 03:02:51 +00001380void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1381 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001382 bool LastInSection,
1383 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001384 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001385 MCSymbol *fdeStart = context.createTempSymbol();
1386 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001387 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001388
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001389 CFAOffset = InitialCFAOffset;
1390
Rafael Espindola0a017a62010-12-10 07:39:47 +00001391 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001392 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1393 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001394
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001395 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001396
Rafael Espindola0a017a62010-12-10 07:39:47 +00001397 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001398 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001399 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001400 const MCExpr *offset =
1401 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1402 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001403 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001404 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001405 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001406 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001407 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001408 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001409 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001410
Rafael Espindola0a017a62010-12-10 07:39:47 +00001411 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001412 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001413 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001414 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1415 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001416
1417 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001418 const MCExpr *Range =
1419 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1420 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001421
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001422 if (IsEH) {
1423 // Augmentation Data Length
1424 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001425
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001426 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001427 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001428
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001429 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001430
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001431 // Augmentation Data
1432 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001433 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001434 }
Rafael Espindola68067662011-04-29 03:06:29 +00001435
Rafael Espindola0a017a62010-12-10 07:39:47 +00001436 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001437 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001438
Rafael Espindola0a017a62010-12-10 07:39:47 +00001439 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001440 // The size of a .eh_frame section has to be a multiple of the alignment
1441 // since a null CIE is interpreted as the end. Old systems overaligned
1442 // .eh_frame, so we do too and account for it in the last FDE.
1443 unsigned Align = LastInSection ? asmInfo->getPointerSize() : PCSize;
1444 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001445
Rafael Espindola46be4352015-11-06 03:02:51 +00001446 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001447}
1448
Benjamin Kramer570dd782010-12-30 22:34:44 +00001449namespace {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001450struct CIEKey {
1451 static const CIEKey getEmptyKey() {
1452 return CIEKey(nullptr, 0, -1, false, false);
1453 }
1454 static const CIEKey getTombstoneKey() {
1455 return CIEKey(nullptr, -1, 0, false, false);
1456 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001457
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001458 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1459 unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple)
1460 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1461 LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame),
1462 IsSimple(IsSimple) {}
1463 const MCSymbol *Personality;
1464 unsigned PersonalityEncoding;
1465 unsigned LsdaEncoding;
1466 bool IsSignalFrame;
1467 bool IsSimple;
1468};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001469} // anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001470
1471namespace llvm {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001472template <> struct DenseMapInfo<CIEKey> {
1473 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1474 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1475 static unsigned getHashValue(const CIEKey &Key) {
1476 return static_cast<unsigned>(
1477 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1478 Key.IsSignalFrame, Key.IsSimple));
1479 }
1480 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1481 return LHS.Personality == RHS.Personality &&
1482 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1483 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1484 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1485 LHS.IsSimple == RHS.IsSimple;
1486 }
1487};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001488} // namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001489
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001490void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001491 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001492 Streamer.generateCompactUnwindEncodings(MAB);
1493
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001494 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001495 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001496 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001497 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001498
Bill Wendling9803abb2011-09-06 18:37:11 +00001499 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001500 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001501 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001502 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001503 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1504 const MCDwarfFrameInfo &Frame = FrameArray[i];
1505 if (Frame.CompactUnwindEncoding == 0) continue;
1506 if (!SectionEmitted) {
1507 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001508 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001509 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001510 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001511 NeedsEHFrameSection |=
1512 Frame.CompactUnwindEncoding ==
1513 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001514 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001515 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001516 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001517
Tim Northoverd1c6f512014-03-29 09:03:13 +00001518 if (!NeedsEHFrameSection) return;
1519
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001520 MCSection &Section =
1521 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1522 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001523
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001524 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001525 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001526 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001527
Eric Christopherd29430d2014-06-19 20:00:09 +00001528 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001529
Craig Topperbb694de2014-04-13 04:57:38 +00001530 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001531 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001532 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1533 const MCDwarfFrameInfo &Frame = *I;
1534 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001535 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001536 MOFI->getCompactUnwindDwarfEHFrameOnly())
1537 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1538 // of by the compact unwind encoding.
1539 continue;
1540
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001541 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001542 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001543 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1544 if (!CIEStart)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001545 CIEStart = &Emitter.EmitCIE(Frame.Personality, Frame.PersonalityEncoding,
1546 Frame.Lsda, Frame.IsSignalFrame,
1547 Frame.LsdaEncoding, Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001548
Rafael Espindola97588e12015-11-06 13:14:59 +00001549 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001550 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001551}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001552
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001553void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001554 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001555 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001556 SmallString<256> Tmp;
1557 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001558 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001559 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001560}
1561
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001562void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1563 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001564 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001565 // Scale the address delta by the minimum instruction length.
1566 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1567
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001568 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001569 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001570 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1571 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001572 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001573 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1574 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001575 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001576 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001577 if (Context.getAsmInfo()->isLittleEndian())
1578 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1579 else
1580 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001581 } else {
1582 assert(isUInt<32>(AddrDelta));
1583 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001584 if (Context.getAsmInfo()->isLittleEndian())
1585 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1586 else
1587 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001588 }
1589}