blob: 47f002f619b495499b9e3f6c93b08ee113e09fe5 [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"
Rafael Espindola85d91982010-12-28 18:36:23 +000025#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000026#include "llvm/Support/LEB128.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000027#include "llvm/Support/Path.h"
28#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/raw_ostream.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000030
Kevin Enderbye5930f12010-07-28 20:55:35 +000031using namespace llvm;
32
Ulrich Weigand32d725b2013-06-12 14:46:54 +000033static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000034 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000035 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000036 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000037 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000038 // TODO: report this error, but really only once.
39 ;
40 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000041 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000042}
43
44//
45// This is called when an instruction is assembled into the specified section
46// and if there is information from the last .loc directive that has yet to have
47// a line entry made for it is made.
48//
David Majnemer8cc30782016-01-21 01:59:03 +000049void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000050 if (!MCOS->getContext().getDwarfLocSeen())
51 return;
52
53 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000054 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000055 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000056 MCOS->EmitLabel(LineSym);
57
58 // Get the current .loc info saved in the context.
59 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
60
61 // Create a (local) line entry with the symbol and the current .loc info.
David Majnemer8cc30782016-01-21 01:59:03 +000062 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +000063
64 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +000065 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000066
Kevin Enderbye46564a2010-09-30 16:52:03 +000067 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000068 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000069 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000070 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000071 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000072}
73
74//
75// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000076//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000077static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
78 const MCSymbol &Start,
79 const MCSymbol &End,
80 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000081 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
82 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +000083 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000084 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +000085 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000086 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000087 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000088 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000089 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000090 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000091 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000092 return Res3;
93}
94
Kevin Enderbye46564a2010-09-30 16:52:03 +000095//
96// This emits the Dwarf line table for the specified section from the entries
97// in the LineSection.
98//
David Blaikief4ad6982014-03-12 22:35:23 +000099static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000100EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000101 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102 unsigned FileNum = 1;
103 unsigned LastLine = 1;
104 unsigned Column = 0;
105 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
106 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000107 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000108 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000109
David Majnemer8cc30782016-01-21 01:59:03 +0000110 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000111 for (auto it = LineEntries.begin(),
112 ie = LineEntries.end();
113 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000114
Dehao Chen18ce9d82016-04-22 21:31:18 +0000115 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
116
Kevin Enderbye46564a2010-09-30 16:52:03 +0000117 if (FileNum != it->getFileNum()) {
118 FileNum = it->getFileNum();
119 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000120 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000121 }
122 if (Column != it->getColumn()) {
123 Column = it->getColumn();
124 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000125 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000126 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000127 if (Discriminator != it->getDiscriminator()) {
128 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000129 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000130 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
131 MCOS->EmitULEB128IntValue(Size + 1);
132 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
133 MCOS->EmitULEB128IntValue(Discriminator);
134 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000135 if (Isa != it->getIsa()) {
136 Isa = it->getIsa();
137 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000138 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000139 }
140 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
141 Flags = it->getFlags();
142 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
143 }
144 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
145 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
146 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
147 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
148 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
149 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
150
Kevin Enderbye46564a2010-09-30 16:52:03 +0000151 MCSymbol *Label = it->getLabel();
152
153 // At this point we want to emit/create the sequence to encode the delta in
154 // line numbers and the increment of the address from the previous Label
155 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000156 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000157 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000158 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000159
Dehao Chen1b54fce2016-04-28 22:09:37 +0000160 Discriminator = 0;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000161 LastLine = it->getLine();
162 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000163 }
164
165 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000166 // Use the section end label to compute the address delta and use INT64_MAX
167 // as the line delta which is the signal that this is actually a
168 // DW_LNE_end_sequence.
169 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000170
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000171 // Switch back the dwarf line section, in case endSection had to switch the
172 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000173 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000174 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000175
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000176 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000177 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000178 AsmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000179}
180
181//
182// This emits the Dwarf file and the line tables.
183//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000184void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
185 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000186 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000187
David Blaikie8bf66c42014-04-01 07:35:52 +0000188 auto &LineTables = context.getMCDwarfLineTables();
189
190 // Bail out early so we don't switch to the debug_line section needlessly and
191 // in doing so create an unnecessary (if empty) section.
192 if (LineTables.empty())
193 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000194
195 // Switch to the section where the table will be emitted into.
196 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
197
Manman Ren4e042a62013-02-05 21:52:47 +0000198 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000199 for (const auto &CUIDTablePair : LineTables)
Frederic Rissa5ab8442015-08-07 15:14:08 +0000200 CUIDTablePair.second.EmitCU(MCOS, Params);
Manman Ren4e042a62013-02-05 21:52:47 +0000201}
202
Frederic Rissa5ab8442015-08-07 15:14:08 +0000203void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS,
204 MCDwarfLineTableParams Params) const {
205 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000206}
207
Frederic Rissa5ab8442015-08-07 15:14:08 +0000208std::pair<MCSymbol *, MCSymbol *>
209MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
210 MCDwarfLineTableParams Params) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000211 static const char StandardOpcodeLengths[] = {
212 0, // length of DW_LNS_copy
213 1, // length of DW_LNS_advance_pc
214 1, // length of DW_LNS_advance_line
215 1, // length of DW_LNS_set_file
216 1, // length of DW_LNS_set_column
217 0, // length of DW_LNS_negate_stmt
218 0, // length of DW_LNS_set_basic_block
219 0, // length of DW_LNS_const_add_pc
220 1, // length of DW_LNS_fixed_advance_pc
221 0, // length of DW_LNS_set_prologue_end
222 0, // length of DW_LNS_set_epilogue_begin
223 1 // DW_LNS_set_isa
224 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000225 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000226 (Params.DWARF2LineOpcodeBase - 1U));
Craig Topper0013be12015-09-21 05:32:41 +0000227 return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
228 Params.DWARF2LineOpcodeBase - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000229}
230
Rafael Espindolac23174b2014-08-15 15:12:13 +0000231static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
232 MCContext &Context = OS.getContext();
233 assert(!isa<MCSymbolRefExpr>(Expr));
234 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
235 return Expr;
236
Jim Grosbach6f482002015-05-18 18:43:14 +0000237 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000238 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000239 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000240}
241
242static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
243 const MCExpr *ABS = forceExpAbs(OS, Value);
244 OS.EmitValue(ABS, Size);
245}
246
David Blaikie8287aff2014-03-18 02:13:23 +0000247std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000248MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
David Blaikie8287aff2014-03-18 02:13:23 +0000249 ArrayRef<char> StandardOpcodeLengths) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000250 MCContext &context = MCOS->getContext();
251
252 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000253 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000254 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000255 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000256 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000257 MCOS->EmitLabel(LineStartSym);
258
259 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000260 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000261
262 // The first 4 bytes is the total length of the information for this
263 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000264 emitAbsValue(*MCOS,
265 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000266
267 // Next 2 bytes is the Version, which is Dwarf 2.
268 MCOS->EmitIntValue(2, 2);
269
270 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000271 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000272
273 // Length of the prologue, is the next 4 bytes. Which is the start of the
274 // section to the end of the prologue. Not including the 4 bytes for the
275 // total length, the 2 bytes for the version, and these 4 bytes for the
276 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000277 emitAbsValue(
278 *MCOS,
279 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000280
281 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000282 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000283 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000284 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
285 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000286 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000287
288 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000289 for (char Length : StandardOpcodeLengths)
290 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000291
292 // Put out the directory and file tables.
293
294 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000295 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000296 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
297 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000298 }
299 MCOS->EmitIntValue(0, 1); // Terminate the directory list
300
301 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000302 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000303 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000304 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000305 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000306 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000307 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
309 MCOS->EmitIntValue(0, 1); // filesize (always 0)
310 }
311 MCOS->EmitIntValue(0, 1); // Terminate the file list
312
313 // This is the end of the prologue, so set the value of the symbol at the
314 // end of the prologue (that was used in a previous expression).
315 MCOS->EmitLabel(ProEndSym);
316
David Blaikie5cff0e62014-03-13 21:47:12 +0000317 return std::make_pair(LineStartSym, LineEndSym);
318}
319
Frederic Rissa5ab8442015-08-07 15:14:08 +0000320void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
321 MCDwarfLineTableParams Params) const {
322 MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000323
Kevin Enderbye46564a2010-09-30 16:52:03 +0000324 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000325 for (const auto &LineSec : MCLineSections.getMCLineEntries())
326 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000327
Kevin Enderbye46564a2010-09-30 16:52:03 +0000328 // This is the end of the section, so set the value of the symbol at the end
329 // of this section (that was used in a previous expression).
330 MCOS->EmitLabel(LineEndSym);
331}
332
David Blaikiec2df16b2014-03-17 18:13:58 +0000333unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000334 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000335 return Header.getFile(Directory, FileName, FileNumber);
336}
337
David Blaikiec2df16b2014-03-17 18:13:58 +0000338unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
339 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000340 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000341 if (Directory == CompilationDir)
342 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000343 if (FileName.empty()) {
344 FileName = "<stdin>";
345 Directory = "";
346 }
347 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000348 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000349 // File numbers start with 1 and/or after any file numbers
350 // allocated by inline-assembler .file directives.
351 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000352 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000353 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000354 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
355 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000356 if (!IterBool.second)
357 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000358 }
David Blaikie498589c2014-03-13 19:15:04 +0000359 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
360 MCDwarfFiles.resize(FileNumber + 1);
361
362 // Get the new MCDwarfFile slot for this FileNumber.
363 MCDwarfFile &File = MCDwarfFiles[FileNumber];
364
365 // It is an error to use see the same number more than once.
366 if (!File.Name.empty())
367 return 0;
368
369 if (Directory.empty()) {
370 // Separate the directory part from the basename of the FileName.
371 StringRef tFileName = sys::path::filename(FileName);
372 if (!tFileName.empty()) {
373 Directory = sys::path::parent_path(FileName);
374 if (!Directory.empty())
375 FileName = tFileName;
376 }
377 }
378
379 // Find or make an entry in the MCDwarfDirs vector for this Directory.
380 // Capture directory name.
381 unsigned DirIndex;
382 if (Directory.empty()) {
383 // For FileNames with no directories a DirIndex of 0 is used.
384 DirIndex = 0;
385 } else {
386 DirIndex = 0;
387 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
388 if (Directory == MCDwarfDirs[DirIndex])
389 break;
390 }
391 if (DirIndex >= MCDwarfDirs.size())
392 MCDwarfDirs.push_back(Directory);
393 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
394 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
395 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
396 // are stored at MCDwarfFiles[FileNumber].Name .
397 DirIndex++;
398 }
399
400 File.Name = FileName;
401 File.DirIndex = DirIndex;
402
403 // return the allocated FileNumber.
404 return FileNumber;
405}
406
Kevin Enderbye46564a2010-09-30 16:52:03 +0000407/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000408void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
409 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000410 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000411 SmallString<256> Tmp;
412 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000413 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000414 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000415}
416
Frederic Rissa5ab8442015-08-07 15:14:08 +0000417/// Given a special op, return the address skip amount (in units of
418/// DWARF2_LINE_MIN_INSN_LENGTH).
419static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
420 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
421}
422
Kevin Enderbye46564a2010-09-30 16:52:03 +0000423/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000424void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
425 int64_t LineDelta, uint64_t AddrDelta,
426 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000427 uint64_t Temp, Opcode;
428 bool NeedCopy = false;
429
Frederic Rissa5ab8442015-08-07 15:14:08 +0000430 // The maximum address skip amount that can be encoded with a special op.
431 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
432
Kevin Enderbye46564a2010-09-30 16:52:03 +0000433 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000434 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000435
436 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000437 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000438 // end_sequence to emit the matrix entry.
439 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000440 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000441 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000442 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000443 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000444 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000445 }
446 OS << char(dwarf::DW_LNS_extended_op);
447 OS << char(1);
448 OS << char(dwarf::DW_LNE_end_sequence);
449 return;
450 }
451
452 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000453 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000454
455 // If the line increment is out of range of a special opcode, we must encode
456 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000457 if (Temp >= Params.DWARF2LineRange ||
458 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000459 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000460 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000461
462 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000463 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000464 NeedCopy = true;
465 }
466
467 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
468 if (LineDelta == 0 && AddrDelta == 0) {
469 OS << char(dwarf::DW_LNS_copy);
470 return;
471 }
472
473 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000474 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000475
476 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000477 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000478 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000479 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000480 if (Opcode <= 255) {
481 OS << char(Opcode);
482 return;
483 }
484
485 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000486 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000487 if (Opcode <= 255) {
488 OS << char(dwarf::DW_LNS_const_add_pc);
489 OS << char(Opcode);
490 return;
491 }
492 }
493
494 // Otherwise use DW_LNS_advance_pc.
495 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000496 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000497
498 if (NeedCopy)
499 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000500 else {
501 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000502 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000503 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000504}
505
Kevin Enderbye7739d42011-12-09 18:09:40 +0000506// Utility function to write a tuple for .debug_abbrev.
507static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
508 MCOS->EmitULEB128IntValue(Name);
509 MCOS->EmitULEB128IntValue(Form);
510}
511
512// When generating dwarf for assembly source files this emits
513// the data for .debug_abbrev section which contains three DIEs.
514static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
515 MCContext &context = MCOS->getContext();
516 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
517
518 // DW_TAG_compile_unit DIE abbrev (1).
519 MCOS->EmitULEB128IntValue(1);
520 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
521 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000522 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
523 ? dwarf::DW_FORM_sec_offset
524 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000525 if (context.getGenDwarfSectionSyms().size() > 1 &&
526 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000527 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
528 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000529 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000530 } else {
531 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
532 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
533 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000534 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000535 if (!context.getCompilationDir().empty())
536 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000537 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
538 if (!DwarfDebugFlags.empty())
539 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
540 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
541 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
542 EmitAbbrev(MCOS, 0, 0);
543
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000544 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000545 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000546 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000547 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
548 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
549 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
550 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
551 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000552 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
553 EmitAbbrev(MCOS, 0, 0);
554
555 // DW_TAG_unspecified_parameters DIE abbrev (3).
556 MCOS->EmitULEB128IntValue(3);
557 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
558 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
559 EmitAbbrev(MCOS, 0, 0);
560
561 // Terminate the abbreviations for this compilation unit.
562 MCOS->EmitIntValue(0, 1);
563}
564
565// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000566// .debug_aranges section. This section contains a header and a table of pairs
567// of PointerSize'ed values for the address and size of section(s) with line
568// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000569static void EmitGenDwarfAranges(MCStreamer *MCOS,
570 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000571 MCContext &context = MCOS->getContext();
572
Oliver Stannard8b273082014-06-19 15:52:37 +0000573 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000574
575 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
576
577 // This will be the length of the .debug_aranges section, first account for
578 // the size of each item in the header (see below where we emit these items).
579 int Length = 4 + 2 + 4 + 1 + 1;
580
581 // Figure the padding after the header before the table of address and size
582 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000583 const MCAsmInfo *asmInfo = context.getAsmInfo();
584 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000585 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
586 if (Pad == 2 * AddrSize)
587 Pad = 0;
588 Length += Pad;
589
590 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000591 // of each section we have in the table.
592 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000593 // And the pair of terminating zeros.
594 Length += 2 * AddrSize;
595
596
597 // Emit the header for this section.
598 // The 4 byte length not including the 4 byte value for the length.
599 MCOS->EmitIntValue(Length - 4, 4);
600 // The 2 byte version, which is 2.
601 MCOS->EmitIntValue(2, 2);
602 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000603 // of the .debug_info.
604 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000605 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
606 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000607 else
608 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000609 // The 1 byte size of an address.
610 MCOS->EmitIntValue(AddrSize, 1);
611 // The 1 byte size of a segment descriptor, we use a value of zero.
612 MCOS->EmitIntValue(0, 1);
613 // Align the header with the padding if needed, before we put out the table.
614 for(int i = 0; i < Pad; i++)
615 MCOS->EmitIntValue(0, 1);
616
Oliver Stannard8b273082014-06-19 15:52:37 +0000617 // Now emit the table of pairs of PointerSize'ed values for the section
618 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000619 for (MCSection *Sec : Sections) {
620 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000621 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000622 assert(StartSymbol && "StartSymbol must not be NULL");
623 assert(EndSymbol && "EndSymbol must not be NULL");
624
Jim Grosbach13760bd2015-05-30 01:25:56 +0000625 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000626 StartSymbol, MCSymbolRefExpr::VK_None, context);
627 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
628 *StartSymbol, *EndSymbol, 0);
629 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000630 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000631 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000632
633 // And finally the pair of terminating zeros.
634 MCOS->EmitIntValue(0, AddrSize);
635 MCOS->EmitIntValue(0, AddrSize);
636}
637
638// When generating dwarf for assembly source files this emits the data for
639// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000640// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000641static void EmitGenDwarfInfo(MCStreamer *MCOS,
642 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000643 const MCSymbol *LineSectionSymbol,
644 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000645 MCContext &context = MCOS->getContext();
646
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000647 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000648
649 // Create a symbol at the start and end of this section used in here for the
650 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000651 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000652 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000653 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000654
655 // First part: the header.
656
657 // The 4 byte total length of the information for this compilation unit, not
658 // including these 4 bytes.
659 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000660 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000661
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000662 // The 2 byte DWARF version.
663 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000664
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000665 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000666 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
667 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000668 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000669 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000670 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000671 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
672 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000673
Bill Wendlingbc07a892013-06-18 07:20:20 +0000674 const MCAsmInfo *asmInfo = context.getAsmInfo();
675 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000676 // The 1 byte size of an address.
677 MCOS->EmitIntValue(AddrSize, 1);
678
679 // Second part: the compile_unit DIE.
680
681 // The DW_TAG_compile_unit DIE abbrev (1).
682 MCOS->EmitULEB128IntValue(1);
683
684 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
685 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000686 if (LineSectionSymbol)
687 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
688 AsmInfo.needsDwarfSectionOffsetDirective());
689 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000690 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000691
Oliver Stannard8b273082014-06-19 15:52:37 +0000692 if (RangesSectionSymbol) {
693 // There are multiple sections containing code, so we must use the
694 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000695
Oliver Stannard8b273082014-06-19 15:52:37 +0000696 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
697 // to the address range list for this compilation unit.
698 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
699 } else {
700 // If we only have one non-empty code section, we can use the simpler
701 // AT_low_pc and AT_high_pc attributes.
702
703 // Find the first (and only) non-empty text section
704 auto &Sections = context.getGenDwarfSectionSyms();
705 const auto TextSection = Sections.begin();
706 assert(TextSection != Sections.end() && "No text section found");
707
Rafael Espindolae0746792015-05-21 16:52:32 +0000708 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
709 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000710 assert(StartSymbol && "StartSymbol must not be NULL");
711 assert(EndSymbol && "EndSymbol must not be NULL");
712
713 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000714 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000715 StartSymbol, MCSymbolRefExpr::VK_None, context);
716 MCOS->EmitValue(Start, AddrSize);
717
718 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000719 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000720 EndSymbol, MCSymbolRefExpr::VK_None, context);
721 MCOS->EmitValue(End, AddrSize);
722 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000723
724 // AT_name, the name of the source file. Reconstruct from the first directory
725 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000726 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000727 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000728 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000729 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000730 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000731 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000732 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000733 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000734 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
735
736 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000737 if (!context.getCompilationDir().empty()) {
738 MCOS->EmitBytes(context.getCompilationDir());
739 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
740 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741
742 // AT_APPLE_flags, the command line arguments of the assembler tool.
743 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
744 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000745 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000746 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
747 }
748
749 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000750 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000751 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000752 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000753 else
754 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000755 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
756
757 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
758 // draft has no standard code for assembler.
759 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
760
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000761 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000762
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000763 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000764 const std::vector<MCGenDwarfLabelEntry> &Entries =
765 MCOS->getContext().getMCGenDwarfLabelEntries();
766 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000767 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000768 MCOS->EmitULEB128IntValue(2);
769
770 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000771 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000772 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
773
774 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000775 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000776
777 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000778 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000779
780 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000781 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000782 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000783 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000784
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
786 MCOS->EmitIntValue(0, 1);
787
788 // The DW_TAG_unspecified_parameters DIE abbrev (3).
789 MCOS->EmitULEB128IntValue(3);
790
791 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
792 MCOS->EmitIntValue(0, 1);
793 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000794
795 // Add the NULL DIE terminating the Compile Unit DIE's.
796 MCOS->EmitIntValue(0, 1);
797
798 // Now set the value of the symbol at the end of the info section.
799 MCOS->EmitLabel(InfoEnd);
800}
801
Oliver Stannard8b273082014-06-19 15:52:37 +0000802// When generating dwarf for assembly source files this emits the data for
803// .debug_ranges section. We only emit one range list, which spans all of the
804// executable sections of this file.
805static void EmitGenDwarfRanges(MCStreamer *MCOS) {
806 MCContext &context = MCOS->getContext();
807 auto &Sections = context.getGenDwarfSectionSyms();
808
809 const MCAsmInfo *AsmInfo = context.getAsmInfo();
810 int AddrSize = AsmInfo->getPointerSize();
811
812 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
813
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000814 for (MCSection *Sec : Sections) {
815 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000816 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000817 assert(StartSymbol && "StartSymbol must not be NULL");
818 assert(EndSymbol && "EndSymbol must not be NULL");
819
820 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000821 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000822 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +0000823 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +0000824 MCOS->EmitValue(SectionStartAddr, AddrSize);
825
826 // Emit a range list entry spanning this section
827 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
828 *StartSymbol, *EndSymbol, 0);
829 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000830 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000831 }
832
833 // Emit end of list entry
834 MCOS->EmitIntValue(0, AddrSize);
835 MCOS->EmitIntValue(0, AddrSize);
836}
837
Kevin Enderbye7739d42011-12-09 18:09:40 +0000838//
839// When generating dwarf for assembly source files this emits the Dwarf
840// sections.
841//
David Blaikie8bf66c42014-04-01 07:35:52 +0000842void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000843 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000844
845 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000846 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000847 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000848 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000849 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000850 if (CreateDwarfSectionSymbols)
851 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000852 MCSymbol *AbbrevSectionSymbol = nullptr;
853 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000854 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000855
856 // Create end symbols for each section, and remove empty sections
857 MCOS->getContext().finalizeDwarfSections(*MCOS);
858
859 // If there are no sections to generate debug info for, we don't need
860 // to do anything
861 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
862 return;
863
Oliver Stannard14f97d02014-09-22 10:45:16 +0000864 // We only use the .debug_ranges section if we have multiple code sections,
865 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000866 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000867 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
868 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000869 CreateDwarfSectionSymbols |= UseRangesSection;
870
Kevin Enderbye7739d42011-12-09 18:09:40 +0000871 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000872 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000873 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000874 MCOS->EmitLabel(InfoSectionSymbol);
875 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000876 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000877 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000878 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000879 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000880 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000881 if (UseRangesSection) {
882 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
883 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000884 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000885 MCOS->EmitLabel(RangesSectionSymbol);
886 }
887 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000888
Oliver Stannard8b273082014-06-19 15:52:37 +0000889 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
890
891 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000892
893 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000894 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000895
Oliver Stannard8b273082014-06-19 15:52:37 +0000896 if (UseRangesSection)
897 EmitGenDwarfRanges(MCOS);
898
Kevin Enderbye7739d42011-12-09 18:09:40 +0000899 // Output the data for .debug_abbrev section.
900 EmitGenDwarfAbbrev(MCOS);
901
902 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000903 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
904 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000905}
906
907//
908// When generating dwarf for assembly source files this is called when symbol
909// for a label is created. If this symbol is not a temporary and is in the
910// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000911// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000912//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000913void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000914 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000915 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000916 if (Symbol->isTemporary())
917 return;
918 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000919 // We won't create dwarf labels for symbols in sections that we are not
920 // generating debug info for.
921 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000922 return;
923
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000924 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000925 // underbar if any.
926 StringRef Name = Symbol->getName();
927 if (Name.startswith("_"))
928 Name = Name.substr(1, Name.size()-1);
929
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000930 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000931 unsigned FileNumber = context.getGenDwarfFileNumber();
932
933 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000934 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000935 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000936 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
937
938 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
939 // values so that they don't have things like an ARM thumb bit from the
940 // original symbol. So when used they won't get a low bit set after
941 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000942 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000943 MCOS->EmitLabel(Label);
944
945 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000946 MCOS->getContext().addMCGenDwarfLabelEntry(
947 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000948}
949
Rafael Espindola0a017a62010-12-10 07:39:47 +0000950static int getDataAlignmentFactor(MCStreamer &streamer) {
951 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000952 const MCAsmInfo *asmInfo = context.getAsmInfo();
953 int size = asmInfo->getCalleeSaveStackSlotSize();
954 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000955 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000956 else
957 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000958}
959
Rafael Espindola5395f442011-04-22 00:08:43 +0000960static unsigned getSizeForEncoding(MCStreamer &streamer,
961 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000962 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000963 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000964 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000965 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000966 case dwarf::DW_EH_PE_absptr:
967 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000968 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000969 case dwarf::DW_EH_PE_udata2:
970 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000971 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000972 case dwarf::DW_EH_PE_udata4:
973 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000974 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000975 case dwarf::DW_EH_PE_udata8:
976 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000977 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000978 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000979}
980
Rafael Espindolafe963b12014-08-15 03:07:13 +0000981static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
982 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000983 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000984 const MCAsmInfo *asmInfo = context.getAsmInfo();
985 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
986 symbolEncoding,
987 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000988 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000989 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000990 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000991 else
992 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000993}
994
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000995static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
996 unsigned symbolEncoding) {
997 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000998 const MCAsmInfo *asmInfo = context.getAsmInfo();
999 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1000 symbolEncoding,
1001 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001002 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001003 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001004}
1005
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001006namespace {
Rafael Espindola68c21652015-11-05 23:54:18 +00001007class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001008 int CFAOffset = 0;
1009 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001010 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001011 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001012
Rafael Espindola68c21652015-11-05 23:54:18 +00001013public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001014 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1015 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001016
Rafael Espindola68c21652015-11-05 23:54:18 +00001017 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001018 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001019
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001020 const MCSymbol &EmitCIE(const MCSymbol *personality,
Rafael Espindola68c21652015-11-05 23:54:18 +00001021 unsigned personalityEncoding, const MCSymbol *lsda,
1022 bool IsSignalFrame, unsigned lsdaEncoding,
1023 bool IsSimple);
Rafael Espindola46be4352015-11-06 03:02:51 +00001024 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001025 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001026 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001027 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001028 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001029};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001030
1031} // end anonymous namespace
1032
Rafael Espindolafe963b12014-08-15 03:07:13 +00001033static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001034 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001035}
1036
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001037void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001038 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001039 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001040
1041 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001042 case MCCFIInstruction::OpRegister: {
1043 unsigned Reg1 = Instr.getRegister();
1044 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001045 if (!IsEH) {
1046 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1047 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1048 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001049 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1050 Streamer.EmitULEB128IntValue(Reg1);
1051 Streamer.EmitULEB128IntValue(Reg2);
1052 return;
1053 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001054 case MCCFIInstruction::OpWindowSave: {
1055 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1056 return;
1057 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001058 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001059 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001060 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1061 Streamer.EmitULEB128IntValue(Reg);
1062 return;
1063 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001064 case MCCFIInstruction::OpAdjustCfaOffset:
1065 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001066 const bool IsRelative =
1067 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1068
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001069 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1070
1071 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001072 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001073 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001074 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001075
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001076 Streamer.EmitULEB128IntValue(CFAOffset);
1077
1078 return;
1079 }
1080 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001081 unsigned Reg = Instr.getRegister();
1082 if (!IsEH)
1083 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001084 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001085 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001086 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001087 Streamer.EmitULEB128IntValue(CFAOffset);
1088
1089 return;
1090 }
1091
1092 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001093 unsigned Reg = Instr.getRegister();
1094 if (!IsEH)
1095 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001096 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001097 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001098
1099 return;
1100 }
1101
1102 case MCCFIInstruction::OpOffset:
1103 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001104 const bool IsRelative =
1105 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001106
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001107 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001108 if (!IsEH)
1109 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1110
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001111 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001112 if (IsRelative)
1113 Offset -= CFAOffset;
1114 Offset = Offset / dataAlignmentFactor;
1115
1116 if (Offset < 0) {
1117 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1118 Streamer.EmitULEB128IntValue(Reg);
1119 Streamer.EmitSLEB128IntValue(Offset);
1120 } else if (Reg < 64) {
1121 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001122 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001123 } else {
1124 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001125 Streamer.EmitULEB128IntValue(Reg);
1126 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001127 }
1128 return;
1129 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001130 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001131 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1132 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001133 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001134 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1135 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001136 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001137 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001138 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001139 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001140 return;
1141 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001142 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001143 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001144 if (!IsEH)
1145 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001146 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1147 return;
1148 }
Michael Kuperstein259f1502015-10-07 07:01:31 +00001149 case MCCFIInstruction::OpGnuArgsSize: {
1150 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1151 Streamer.EmitULEB128IntValue(Instr.getOffset());
1152 return;
1153 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001154 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001155 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001156 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001157 }
1158 llvm_unreachable("Unhandled case in switch");
1159}
1160
Rafael Espindolafe963b12014-08-15 03:07:13 +00001161/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001162void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001163 MCSymbol *BaseLabel) {
1164 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1165 const MCCFIInstruction &Instr = Instrs[i];
1166 MCSymbol *Label = Instr.getLabel();
1167 // Throw out move if the label is invalid.
1168 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1169
1170 // Advance row if new location.
1171 if (BaseLabel && Label) {
1172 MCSymbol *ThisSym = Label;
1173 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001174 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001175 BaseLabel = ThisSym;
1176 }
1177 }
1178
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001179 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001180 }
1181}
1182
Rafael Espindolafe963b12014-08-15 03:07:13 +00001183/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001184void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001185 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001186 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001187
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001188 // range-start range-length compact-unwind-enc personality-func lsda
1189 // _foo LfooEnd-_foo 0x00000023 0 0
1190 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1191 //
1192 // .section __LD,__compact_unwind,regular,debug
1193 //
1194 // # compact unwind for _foo
1195 // .quad _foo
1196 // .set L1,LfooEnd-_foo
1197 // .long L1
1198 // .long 0x01010001
1199 // .quad 0
1200 // .quad 0
1201 //
1202 // # compact unwind for _bar
1203 // .quad _bar
1204 // .set L2,LbarEnd-_bar
1205 // .long L2
1206 // .long 0x01020011
1207 // .quad __gxx_personality
1208 // .quad except_tab1
1209
Bill Wendling49bc6802011-07-19 00:06:12 +00001210 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001211 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001212 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001213
Bill Wendlingdafd5982011-07-14 23:34:45 +00001214 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001215 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001216 Encoding |= 0x40000000;
1217
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001218 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001219 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001220 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001221 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001222
1223 // Range Length
1224 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1225 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001226 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001227
Bill Wendling75174662011-06-30 00:30:52 +00001228 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001229 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1230 Streamer.EmitIntValue(Encoding, Size);
1231
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001232 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001233 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001234 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001235 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001236 else
1237 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001238
1239 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001240 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001241 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001242 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001243 else
1244 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001245}
1246
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001247static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1248 if (IsEH)
1249 return 1;
1250 switch (DwarfVersion) {
1251 case 2:
1252 return 1;
1253 case 3:
1254 return 3;
1255 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001256 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001257 return 4;
1258 }
1259 llvm_unreachable("Unknown version");
1260}
1261
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001262const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001263 unsigned personalityEncoding,
1264 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001265 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001266 unsigned lsdaEncoding,
1267 bool IsSimple) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001268 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001269 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001270 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001271
Jim Grosbach6f482002015-05-18 18:43:14 +00001272 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001273 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001274
Jim Grosbach6f482002015-05-18 18:43:14 +00001275 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001276
1277 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001278 const MCExpr *Length =
1279 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1280 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001281
1282 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001283 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001284 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001285
1286 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001287 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001288 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001289
1290 // Augmentation String
1291 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001292 if (IsEH) {
1293 Augmentation += "z";
1294 if (personality)
1295 Augmentation += "P";
1296 if (lsda)
1297 Augmentation += "L";
1298 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001299 if (IsSignalFrame)
1300 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001301 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001302 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001303 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001304
Keith Walkerea9483f2015-05-12 15:25:08 +00001305 if (CIEVersion >= 4) {
1306 // Address Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001307 Streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001308
1309 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001310 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001311 }
1312
Rafael Espindola0a017a62010-12-10 07:39:47 +00001313 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001314 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001315
1316 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001317 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001318
1319 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001320 if (CIEVersion == 1) {
1321 assert(MRI->getRARegister() <= 255 &&
1322 "DWARF 2 encodes return_address_register in one byte");
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001323 Streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001324 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001325 Streamer.EmitULEB128IntValue(
Frederic Rissadbb3f22015-02-26 19:48:07 +00001326 MRI->getDwarfRegNum(MRI->getRARegister(), IsEH));
Oliver Stannardf7693f42014-06-19 15:39:33 +00001327 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001328
1329 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001330
1331 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001332 if (IsEH) {
1333 if (personality) {
1334 // Personality Encoding
1335 augmentationLength += 1;
1336 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001337 augmentationLength += getSizeForEncoding(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001338 }
1339 if (lsda)
1340 augmentationLength += 1;
1341 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001342 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001343
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001344 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001345
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001346 // Augmentation Data (optional)
1347 if (personality) {
1348 // Personality Encoding
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001349 emitEncodingByte(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001350 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001351 EmitPersonality(Streamer, *personality, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001352 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001353
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001354 if (lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001355 emitEncodingByte(Streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001356
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001357 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001358 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001359 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001360
1361 // Initial Instructions
1362
Bill Wendlingbc07a892013-06-18 07:20:20 +00001363 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001364 if (!IsSimple) {
1365 const std::vector<MCCFIInstruction> &Instructions =
1366 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001367 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001368 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001369
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001370 InitialCFAOffset = CFAOffset;
1371
Rafael Espindola0a017a62010-12-10 07:39:47 +00001372 // Padding
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001373 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001374
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001375 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001376 return *sectionStart;
1377}
1378
Rafael Espindola46be4352015-11-06 03:02:51 +00001379void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1380 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001381 bool LastInSection,
1382 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001383 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001384 MCSymbol *fdeStart = context.createTempSymbol();
1385 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001386 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001387
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001388 CFAOffset = InitialCFAOffset;
1389
Rafael Espindola0a017a62010-12-10 07:39:47 +00001390 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001391 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1392 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001393
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001394 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001395
Rafael Espindola0a017a62010-12-10 07:39:47 +00001396 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001397 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001398 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001399 const MCExpr *offset =
1400 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1401 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001402 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001403 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001404 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001405 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001406 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001407 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001408 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001409
Rafael Espindola0a017a62010-12-10 07:39:47 +00001410 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001411 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001412 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001413 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1414 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001415
1416 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001417 const MCExpr *Range =
1418 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1419 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001420
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001421 if (IsEH) {
1422 // Augmentation Data Length
1423 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001424
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001425 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001426 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001427
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001428 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001429
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001430 // Augmentation Data
1431 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001432 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001433 }
Rafael Espindola68067662011-04-29 03:06:29 +00001434
Rafael Espindola0a017a62010-12-10 07:39:47 +00001435 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001436 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001437
Rafael Espindola0a017a62010-12-10 07:39:47 +00001438 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001439 // The size of a .eh_frame section has to be a multiple of the alignment
1440 // since a null CIE is interpreted as the end. Old systems overaligned
1441 // .eh_frame, so we do too and account for it in the last FDE.
1442 unsigned Align = LastInSection ? asmInfo->getPointerSize() : PCSize;
1443 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001444
Rafael Espindola46be4352015-11-06 03:02:51 +00001445 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001446}
1447
Benjamin Kramer570dd782010-12-30 22:34:44 +00001448namespace {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001449struct CIEKey {
1450 static const CIEKey getEmptyKey() {
1451 return CIEKey(nullptr, 0, -1, false, false);
1452 }
1453 static const CIEKey getTombstoneKey() {
1454 return CIEKey(nullptr, -1, 0, false, false);
1455 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001456
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001457 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1458 unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple)
1459 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1460 LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame),
1461 IsSimple(IsSimple) {}
1462 const MCSymbol *Personality;
1463 unsigned PersonalityEncoding;
1464 unsigned LsdaEncoding;
1465 bool IsSignalFrame;
1466 bool IsSimple;
1467};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001468} // anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001469
1470namespace llvm {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001471template <> struct DenseMapInfo<CIEKey> {
1472 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1473 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1474 static unsigned getHashValue(const CIEKey &Key) {
1475 return static_cast<unsigned>(
1476 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1477 Key.IsSignalFrame, Key.IsSimple));
1478 }
1479 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1480 return LHS.Personality == RHS.Personality &&
1481 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1482 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1483 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1484 LHS.IsSimple == RHS.IsSimple;
1485 }
1486};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001487} // namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001488
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001489void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001490 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001491 Streamer.generateCompactUnwindEncodings(MAB);
1492
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001493 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001494 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001495 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001496 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001497
Bill Wendling9803abb2011-09-06 18:37:11 +00001498 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001499 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001500 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001501 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001502 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1503 const MCDwarfFrameInfo &Frame = FrameArray[i];
1504 if (Frame.CompactUnwindEncoding == 0) continue;
1505 if (!SectionEmitted) {
1506 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001507 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001508 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001509 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001510 NeedsEHFrameSection |=
1511 Frame.CompactUnwindEncoding ==
1512 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001513 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001514 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001515 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001516
Tim Northoverd1c6f512014-03-29 09:03:13 +00001517 if (!NeedsEHFrameSection) return;
1518
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001519 MCSection &Section =
1520 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1521 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001522
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001523 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001524 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001525 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001526
Eric Christopherd29430d2014-06-19 20:00:09 +00001527 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001528
Craig Topperbb694de2014-04-13 04:57:38 +00001529 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001530 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001531 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1532 const MCDwarfFrameInfo &Frame = *I;
1533 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001534 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001535 MOFI->getCompactUnwindDwarfEHFrameOnly())
1536 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1537 // of by the compact unwind encoding.
1538 continue;
1539
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001540 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001541 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001542 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1543 if (!CIEStart)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001544 CIEStart = &Emitter.EmitCIE(Frame.Personality, Frame.PersonalityEncoding,
1545 Frame.Lsda, Frame.IsSignalFrame,
1546 Frame.LsdaEncoding, Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001547
Rafael Espindola97588e12015-11-06 13:14:59 +00001548 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001549 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001550}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001551
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001552void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001553 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001554 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001555 SmallString<256> Tmp;
1556 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001557 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001558 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001559}
1560
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001561void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1562 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001563 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001564 // Scale the address delta by the minimum instruction length.
1565 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1566
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001567 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001568 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001569 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1570 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001571 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001572 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1573 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001574 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001575 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001576 if (Context.getAsmInfo()->isLittleEndian())
1577 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1578 else
1579 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001580 } else {
1581 assert(isUInt<32>(AddrDelta));
1582 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001583 if (Context.getAsmInfo()->isLittleEndian())
1584 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1585 else
1586 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001587 }
1588}