blob: ae27686819869a0ab95f46a5ca9492339d8ff740 [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"
Kevin Enderbye5930f12010-07-28 20:55:35 +000030using namespace llvm;
31
Kevin Enderbye46564a2010-09-30 16:52:03 +000032// Given a special op, return the address skip amount (in units of
33// DWARF2_LINE_MIN_INSN_LENGTH.
34#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
35
36// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc737ac12011-06-30 23:59:38 +000037#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbye46564a2010-09-30 16:52:03 +000038
39// First special line opcode - leave room for the standard opcodes.
40// Note: If you want to change this, you'll have to update the
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000041// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc737ac12011-06-30 23:59:38 +000042#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbye46564a2010-09-30 16:52:03 +000043
44// Minimum line offset in a special line info. opcode. This value
45// was chosen to give a reasonable range of values.
Bill Wendlingc737ac12011-06-30 23:59:38 +000046#define DWARF2_LINE_BASE -5
Kevin Enderbye46564a2010-09-30 16:52:03 +000047
48// Range of line offsets in a special line info. opcode.
Bill Wendlingc737ac12011-06-30 23:59:38 +000049#define DWARF2_LINE_RANGE 14
Kevin Enderbye46564a2010-09-30 16:52:03 +000050
Ulrich Weigand32d725b2013-06-12 14:46:54 +000051static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000052 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000053 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000054 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000055 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000056 // TODO: report this error, but really only once.
57 ;
58 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000059 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000060}
61
62//
63// This is called when an instruction is assembled into the specified section
64// and if there is information from the last .loc directive that has yet to have
65// a line entry made for it is made.
66//
Rafael Espindola4066e8d2014-05-12 14:28:48 +000067void MCLineEntry::Make(MCObjectStreamer *MCOS, const MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000068 if (!MCOS->getContext().getDwarfLocSeen())
69 return;
70
71 // Create a symbol at in the current section for use in the line entry.
72 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
73 // Set the value of the symbol to use for the MCLineEntry.
74 MCOS->EmitLabel(LineSym);
75
76 // Get the current .loc info saved in the context.
77 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
78
79 // Create a (local) line entry with the symbol and the current .loc info.
80 MCLineEntry LineEntry(LineSym, DwarfLoc);
81
82 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderbya68d0042010-10-04 20:17:24 +000083 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000084
Kevin Enderbye46564a2010-09-30 16:52:03 +000085 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000086 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000087 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000088 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000089 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000090}
91
92//
93// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000094//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000095static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
96 const MCSymbol &Start,
97 const MCSymbol &End,
98 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000099 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
100 const MCExpr *Res =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000101 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102 const MCExpr *RHS =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000103 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000104 const MCExpr *Res1 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000105 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000106 const MCExpr *Res2 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000107 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000108 const MCExpr *Res3 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000109 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110 return Res3;
111}
112
Kevin Enderbye46564a2010-09-30 16:52:03 +0000113//
114// This emits the Dwarf line table for the specified section from the entries
115// in the LineSection.
116//
David Blaikief4ad6982014-03-12 22:35:23 +0000117static inline void
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +0000118EmitDwarfLineTable(MCObjectStreamer *MCOS, const MCSection *Section,
David Blaikief4ad6982014-03-12 22:35:23 +0000119 const MCLineSection::MCLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000120 unsigned FileNum = 1;
121 unsigned LastLine = 1;
122 unsigned Column = 0;
123 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
124 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000125 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000126 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000127
128 // Loop through each MCLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000129 for (auto it = LineEntries.begin(),
130 ie = LineEntries.end();
131 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000132
133 if (FileNum != it->getFileNum()) {
134 FileNum = it->getFileNum();
135 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000136 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000137 }
138 if (Column != it->getColumn()) {
139 Column = it->getColumn();
140 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000141 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000142 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000143 if (Discriminator != it->getDiscriminator()) {
144 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000145 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000146 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
147 MCOS->EmitULEB128IntValue(Size + 1);
148 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
149 MCOS->EmitULEB128IntValue(Discriminator);
150 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000151 if (Isa != it->getIsa()) {
152 Isa = it->getIsa();
153 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000154 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000155 }
156 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
157 Flags = it->getFlags();
158 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
159 }
160 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
161 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
162 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
163 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
164 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
165 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
166
Rafael Espindola1d37f352010-11-13 01:06:27 +0000167 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000168 MCSymbol *Label = it->getLabel();
169
170 // At this point we want to emit/create the sequence to encode the delta in
171 // line numbers and the increment of the address from the previous Label
172 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000173 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000174 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000175 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000176
177 LastLine = it->getLine();
178 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000179 }
180
181 // Emit a DW_LNE_end_sequence for the end of the section.
182 // Using the pointer Section create a temporary label at the end of the
183 // section and use that and the LastLabel to compute the address delta
184 // and use INT64_MAX as the line delta which is the signal that this is
185 // actually a DW_LNE_end_sequence.
186
187 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000188 // TODO: keep track of the last subsection so that this symbol appears in the
189 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000190 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000191
192 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000193 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000194 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000195 // Set the value of the symbol, as we are at the end of the section.
196 MCOS->EmitLabel(SectionEnd);
197
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000198 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000199 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000200
Bill Wendlingbc07a892013-06-18 07:20:20 +0000201 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000202 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000203 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000204}
205
206//
207// This emits the Dwarf file and the line tables.
208//
Rafael Espindola4066e8d2014-05-12 14:28:48 +0000209void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000210 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000211
David Blaikie8bf66c42014-04-01 07:35:52 +0000212 auto &LineTables = context.getMCDwarfLineTables();
213
214 // Bail out early so we don't switch to the debug_line section needlessly and
215 // in doing so create an unnecessary (if empty) section.
216 if (LineTables.empty())
217 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000218
219 // Switch to the section where the table will be emitted into.
220 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
221
Manman Ren4e042a62013-02-05 21:52:47 +0000222 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000223 for (const auto &CUIDTablePair : LineTables)
224 CUIDTablePair.second.EmitCU(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000225}
226
David Blaikie8287aff2014-03-18 02:13:23 +0000227void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS) const {
228 MCOS.EmitLabel(Header.Emit(&MCOS, None).second);
229}
230
David Blaikie5cff0e62014-03-13 21:47:12 +0000231std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000232 static const char StandardOpcodeLengths[] = {
233 0, // length of DW_LNS_copy
234 1, // length of DW_LNS_advance_pc
235 1, // length of DW_LNS_advance_line
236 1, // length of DW_LNS_set_file
237 1, // length of DW_LNS_set_column
238 0, // length of DW_LNS_negate_stmt
239 0, // length of DW_LNS_set_basic_block
240 0, // length of DW_LNS_const_add_pc
241 1, // length of DW_LNS_fixed_advance_pc
242 0, // length of DW_LNS_set_prologue_end
243 0, // length of DW_LNS_set_epilogue_begin
244 1 // DW_LNS_set_isa
245 };
246 assert(array_lengthof(StandardOpcodeLengths) == (DWARF2_LINE_OPCODE_BASE - 1));
247 return Emit(MCOS, StandardOpcodeLengths);
248}
249
250std::pair<MCSymbol *, MCSymbol *>
251MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
252 ArrayRef<char> StandardOpcodeLengths) const {
253
Manman Ren4e042a62013-02-05 21:52:47 +0000254 MCContext &context = MCOS->getContext();
255
256 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000257 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000258 if (!LineStartSym)
259 LineStartSym = context.CreateTempSymbol();
260 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000261 MCOS->EmitLabel(LineStartSym);
262
263 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000264 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000265
266 // The first 4 bytes is the total length of the information for this
267 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000268 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000269 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000270
271 // Next 2 bytes is the Version, which is Dwarf 2.
272 MCOS->EmitIntValue(2, 2);
273
274 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000275 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000276
277 // Length of the prologue, is the next 4 bytes. Which is the start of the
278 // section to the end of the prologue. Not including the 4 bytes for the
279 // total length, the 2 bytes for the version, and these 4 bytes for the
280 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000281 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000282 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000283
284 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000285 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000286 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
287 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
288 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000289 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000290
291 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000292 for (char Length : StandardOpcodeLengths)
293 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000294
295 // Put out the directory and file tables.
296
297 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000298 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000299 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
300 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000301 }
302 MCOS->EmitIntValue(0, 1); // Terminate the directory list
303
304 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000305 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000306 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000307 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000308 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000309 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000310 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
312 MCOS->EmitIntValue(0, 1); // filesize (always 0)
313 }
314 MCOS->EmitIntValue(0, 1); // Terminate the file list
315
316 // This is the end of the prologue, so set the value of the symbol at the
317 // end of the prologue (that was used in a previous expression).
318 MCOS->EmitLabel(ProEndSym);
319
David Blaikie5cff0e62014-03-13 21:47:12 +0000320 return std::make_pair(LineStartSym, LineEndSym);
321}
322
Rafael Espindola4066e8d2014-05-12 14:28:48 +0000323void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS) const {
David Blaikie8bf66c42014-04-01 07:35:52 +0000324 MCSymbol *LineEndSym = Header.Emit(MCOS).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000325
Kevin Enderbye46564a2010-09-30 16:52:03 +0000326 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000327 for (const auto &LineSec : MCLineSections.getMCLineEntries())
328 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000329
David Blaikiea55e64f2014-03-12 22:28:56 +0000330 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines() &&
David Blaikie11765bc2014-03-13 17:55:28 +0000331 MCLineSections.getMCLineEntries().empty()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000332 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000333 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000334 // total_length >= prologue_length + 10
335 // We are 4 bytes short, since we have total_length = 51 and
336 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000337
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000338 // The regular end_sequence should be sufficient.
339 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000340 }
341
Kevin Enderbye46564a2010-09-30 16:52:03 +0000342 // This is the end of the section, so set the value of the symbol at the end
343 // of this section (that was used in a previous expression).
344 MCOS->EmitLabel(LineEndSym);
345}
346
David Blaikiec2df16b2014-03-17 18:13:58 +0000347unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000348 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000349 return Header.getFile(Directory, FileName, FileNumber);
350}
351
David Blaikiec2df16b2014-03-17 18:13:58 +0000352unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
353 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000354 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000355 if (Directory == CompilationDir)
356 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000357 if (FileName.empty()) {
358 FileName = "<stdin>";
359 Directory = "";
360 }
361 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000362 if (FileNumber == 0) {
363 FileNumber = SourceIdMap.size() + 1;
364 assert((MCDwarfFiles.empty() || FileNumber == MCDwarfFiles.size()) &&
365 "Don't mix autonumbered and explicit numbered line table usage");
366 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(
367 (Directory + Twine('\0') + FileName).str(), FileNumber);
368 if (Ent.getValue() != FileNumber)
369 return Ent.getValue();
370 }
David Blaikie498589c2014-03-13 19:15:04 +0000371 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
372 MCDwarfFiles.resize(FileNumber + 1);
373
374 // Get the new MCDwarfFile slot for this FileNumber.
375 MCDwarfFile &File = MCDwarfFiles[FileNumber];
376
377 // It is an error to use see the same number more than once.
378 if (!File.Name.empty())
379 return 0;
380
381 if (Directory.empty()) {
382 // Separate the directory part from the basename of the FileName.
383 StringRef tFileName = sys::path::filename(FileName);
384 if (!tFileName.empty()) {
385 Directory = sys::path::parent_path(FileName);
386 if (!Directory.empty())
387 FileName = tFileName;
388 }
389 }
390
391 // Find or make an entry in the MCDwarfDirs vector for this Directory.
392 // Capture directory name.
393 unsigned DirIndex;
394 if (Directory.empty()) {
395 // For FileNames with no directories a DirIndex of 0 is used.
396 DirIndex = 0;
397 } else {
398 DirIndex = 0;
399 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
400 if (Directory == MCDwarfDirs[DirIndex])
401 break;
402 }
403 if (DirIndex >= MCDwarfDirs.size())
404 MCDwarfDirs.push_back(Directory);
405 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
406 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
407 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
408 // are stored at MCDwarfFiles[FileNumber].Name .
409 DirIndex++;
410 }
411
412 File.Name = FileName;
413 File.DirIndex = DirIndex;
414
415 // return the allocated FileNumber.
416 return FileNumber;
417}
418
Kevin Enderbye46564a2010-09-30 16:52:03 +0000419/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000420void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000421 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000422 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000423 SmallString<256> Tmp;
424 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000425 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000426 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000427}
428
429/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000430void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
431 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000432 uint64_t Temp, Opcode;
433 bool NeedCopy = false;
434
435 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000436 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000437
438 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000439 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000440 // end_sequence to emit the matrix entry.
441 if (LineDelta == INT64_MAX) {
442 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
443 OS << char(dwarf::DW_LNS_const_add_pc);
444 else {
445 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000446 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000447 }
448 OS << char(dwarf::DW_LNS_extended_op);
449 OS << char(1);
450 OS << char(dwarf::DW_LNE_end_sequence);
451 return;
452 }
453
454 // Bias the line delta by the base.
455 Temp = LineDelta - DWARF2_LINE_BASE;
456
457 // If the line increment is out of range of a special opcode, we must encode
458 // it with DW_LNS_advance_line.
459 if (Temp >= DWARF2_LINE_RANGE) {
460 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000461 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000462
463 LineDelta = 0;
464 Temp = 0 - DWARF2_LINE_BASE;
465 NeedCopy = true;
466 }
467
468 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
469 if (LineDelta == 0 && AddrDelta == 0) {
470 OS << char(dwarf::DW_LNS_copy);
471 return;
472 }
473
474 // Bias the opcode by the special opcode base.
475 Temp += DWARF2_LINE_OPCODE_BASE;
476
477 // Avoid overflow when addr_delta is large.
478 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
479 // Try using a special opcode.
480 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
481 if (Opcode <= 255) {
482 OS << char(Opcode);
483 return;
484 }
485
486 // Try using DW_LNS_const_add_pc followed by special op.
487 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
488 if (Opcode <= 255) {
489 OS << char(dwarf::DW_LNS_const_add_pc);
490 OS << char(Opcode);
491 return;
492 }
493 }
494
495 // Otherwise use DW_LNS_advance_pc.
496 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000497 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000498
499 if (NeedCopy)
500 OS << char(dwarf::DW_LNS_copy);
501 else
502 OS << char(Temp);
503}
504
Kevin Enderbye7739d42011-12-09 18:09:40 +0000505// Utility function to write a tuple for .debug_abbrev.
506static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
507 MCOS->EmitULEB128IntValue(Name);
508 MCOS->EmitULEB128IntValue(Form);
509}
510
511// When generating dwarf for assembly source files this emits
512// the data for .debug_abbrev section which contains three DIEs.
513static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
514 MCContext &context = MCOS->getContext();
515 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
516
517 // DW_TAG_compile_unit DIE abbrev (1).
518 MCOS->EmitULEB128IntValue(1);
519 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
520 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
521 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000522 if (MCOS->getContext().getGenDwarfSectionSyms().size() > 1) {
523 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4);
524 } else {
525 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
526 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
527 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000528 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000529 if (!context.getCompilationDir().empty())
530 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000531 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
532 if (!DwarfDebugFlags.empty())
533 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
534 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
535 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
536 EmitAbbrev(MCOS, 0, 0);
537
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000538 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000539 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000540 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000541 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
542 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
543 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
544 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
545 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000546 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
547 EmitAbbrev(MCOS, 0, 0);
548
549 // DW_TAG_unspecified_parameters DIE abbrev (3).
550 MCOS->EmitULEB128IntValue(3);
551 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
552 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
553 EmitAbbrev(MCOS, 0, 0);
554
555 // Terminate the abbreviations for this compilation unit.
556 MCOS->EmitIntValue(0, 1);
557}
558
559// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000560// .debug_aranges section. This section contains a header and a table of pairs
561// of PointerSize'ed values for the address and size of section(s) with line
562// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000563static void EmitGenDwarfAranges(MCStreamer *MCOS,
564 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000565 MCContext &context = MCOS->getContext();
566
Oliver Stannard8b273082014-06-19 15:52:37 +0000567 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000568
569 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
570
571 // This will be the length of the .debug_aranges section, first account for
572 // the size of each item in the header (see below where we emit these items).
573 int Length = 4 + 2 + 4 + 1 + 1;
574
575 // Figure the padding after the header before the table of address and size
576 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000577 const MCAsmInfo *asmInfo = context.getAsmInfo();
578 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000579 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
580 if (Pad == 2 * AddrSize)
581 Pad = 0;
582 Length += Pad;
583
584 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000585 // of each section we have in the table.
586 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000587 // And the pair of terminating zeros.
588 Length += 2 * AddrSize;
589
590
591 // Emit the header for this section.
592 // The 4 byte length not including the 4 byte value for the length.
593 MCOS->EmitIntValue(Length - 4, 4);
594 // The 2 byte version, which is 2.
595 MCOS->EmitIntValue(2, 2);
596 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000597 // of the .debug_info.
598 if (InfoSectionSymbol)
599 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
600 else
601 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000602 // The 1 byte size of an address.
603 MCOS->EmitIntValue(AddrSize, 1);
604 // The 1 byte size of a segment descriptor, we use a value of zero.
605 MCOS->EmitIntValue(0, 1);
606 // Align the header with the padding if needed, before we put out the table.
607 for(int i = 0; i < Pad; i++)
608 MCOS->EmitIntValue(0, 1);
609
Oliver Stannard8b273082014-06-19 15:52:37 +0000610 // Now emit the table of pairs of PointerSize'ed values for the section
611 // addresses and sizes.
612 for (const auto &sec : Sections) {
Eric Christopherd29430d2014-06-19 20:00:09 +0000613 MCSymbol *StartSymbol = sec.second.first;
614 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000615 assert(StartSymbol && "StartSymbol must not be NULL");
616 assert(EndSymbol && "EndSymbol must not be NULL");
617
618 const MCExpr *Addr = MCSymbolRefExpr::Create(
619 StartSymbol, MCSymbolRefExpr::VK_None, context);
620 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
621 *StartSymbol, *EndSymbol, 0);
622 MCOS->EmitValue(Addr, AddrSize);
623 MCOS->EmitAbsValue(Size, AddrSize);
624 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000625
626 // And finally the pair of terminating zeros.
627 MCOS->EmitIntValue(0, AddrSize);
628 MCOS->EmitIntValue(0, AddrSize);
629}
630
631// When generating dwarf for assembly source files this emits the data for
632// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000633// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000634static void EmitGenDwarfInfo(MCStreamer *MCOS,
635 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000636 const MCSymbol *LineSectionSymbol,
637 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000638 MCContext &context = MCOS->getContext();
639
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000640 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000641
642 // Create a symbol at the start and end of this section used in here for the
643 // expression to calculate the length in the header.
644 MCSymbol *InfoStart = context.CreateTempSymbol();
645 MCOS->EmitLabel(InfoStart);
646 MCSymbol *InfoEnd = context.CreateTempSymbol();
647
648 // First part: the header.
649
650 // The 4 byte total length of the information for this compilation unit, not
651 // including these 4 bytes.
652 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
653 MCOS->EmitAbsValue(Length, 4);
654
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000655 // The 2 byte DWARF version.
656 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000657
658 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
659 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000660 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000661 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000662 else if (context.getAsmInfo()->needsDwarfSectionOffsetDirective())
663 MCOS->EmitCOFFSecRel32(AbbrevSectionSymbol);
664 else
665 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000666
Bill Wendlingbc07a892013-06-18 07:20:20 +0000667 const MCAsmInfo *asmInfo = context.getAsmInfo();
668 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000669 // The 1 byte size of an address.
670 MCOS->EmitIntValue(AddrSize, 1);
671
672 // Second part: the compile_unit DIE.
673
674 // The DW_TAG_compile_unit DIE abbrev (1).
675 MCOS->EmitULEB128IntValue(1);
676
677 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
678 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000679 if (LineSectionSymbol) {
680 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
681 } else {
682 MCOS->EmitIntValue(0, 4);
683 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000684
Oliver Stannard8b273082014-06-19 15:52:37 +0000685 if (RangesSectionSymbol) {
686 // There are multiple sections containing code, so we must use the
687 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000688
Oliver Stannard8b273082014-06-19 15:52:37 +0000689 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
690 // to the address range list for this compilation unit.
691 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
692 } else {
693 // If we only have one non-empty code section, we can use the simpler
694 // AT_low_pc and AT_high_pc attributes.
695
696 // Find the first (and only) non-empty text section
697 auto &Sections = context.getGenDwarfSectionSyms();
698 const auto TextSection = Sections.begin();
699 assert(TextSection != Sections.end() && "No text section found");
700
Eric Christopherd29430d2014-06-19 20:00:09 +0000701 MCSymbol *StartSymbol = TextSection->second.first;
702 MCSymbol *EndSymbol = TextSection->second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000703 assert(StartSymbol && "StartSymbol must not be NULL");
704 assert(EndSymbol && "EndSymbol must not be NULL");
705
706 // AT_low_pc, the first address of the default .text section.
707 const MCExpr *Start = MCSymbolRefExpr::Create(
708 StartSymbol, MCSymbolRefExpr::VK_None, context);
709 MCOS->EmitValue(Start, AddrSize);
710
711 // AT_high_pc, the last address of the default .text section.
712 const MCExpr *End = MCSymbolRefExpr::Create(
713 EndSymbol, MCSymbolRefExpr::VK_None, context);
714 MCOS->EmitValue(End, AddrSize);
715 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000716
717 // AT_name, the name of the source file. Reconstruct from the first directory
718 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000719 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000720 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000721 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000722 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000723 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000724 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000725 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000726 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000727 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
728
729 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000730 if (!context.getCompilationDir().empty()) {
731 MCOS->EmitBytes(context.getCompilationDir());
732 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
733 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000734
735 // AT_APPLE_flags, the command line arguments of the assembler tool.
736 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
737 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000738 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000739 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
740 }
741
742 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000743 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000744 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000745 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000746 else
747 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000748 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
749
750 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
751 // draft has no standard code for assembler.
752 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
753
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000754 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000755
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000756 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000757 const std::vector<MCGenDwarfLabelEntry> &Entries =
758 MCOS->getContext().getMCGenDwarfLabelEntries();
759 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000760 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000761 MCOS->EmitULEB128IntValue(2);
762
763 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000764 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000765 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
766
767 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000768 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000769
770 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000771 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000772
773 // AT_low_pc, start address of the label.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000774 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000775 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000776 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777
Kevin Enderbye7739d42011-12-09 18:09:40 +0000778 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
779 MCOS->EmitIntValue(0, 1);
780
781 // The DW_TAG_unspecified_parameters DIE abbrev (3).
782 MCOS->EmitULEB128IntValue(3);
783
784 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
785 MCOS->EmitIntValue(0, 1);
786 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000787
788 // Add the NULL DIE terminating the Compile Unit DIE's.
789 MCOS->EmitIntValue(0, 1);
790
791 // Now set the value of the symbol at the end of the info section.
792 MCOS->EmitLabel(InfoEnd);
793}
794
Oliver Stannard8b273082014-06-19 15:52:37 +0000795// When generating dwarf for assembly source files this emits the data for
796// .debug_ranges section. We only emit one range list, which spans all of the
797// executable sections of this file.
798static void EmitGenDwarfRanges(MCStreamer *MCOS) {
799 MCContext &context = MCOS->getContext();
800 auto &Sections = context.getGenDwarfSectionSyms();
801
802 const MCAsmInfo *AsmInfo = context.getAsmInfo();
803 int AddrSize = AsmInfo->getPointerSize();
804
805 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
806
807 for (const auto sec : Sections) {
808
Eric Christopherd29430d2014-06-19 20:00:09 +0000809 MCSymbol *StartSymbol = sec.second.first;
810 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000811 assert(StartSymbol && "StartSymbol must not be NULL");
812 assert(EndSymbol && "EndSymbol must not be NULL");
813
814 // Emit a base address selection entry for the start of this section
815 const MCExpr *SectionStartAddr = MCSymbolRefExpr::Create(
816 StartSymbol, MCSymbolRefExpr::VK_None, context);
817 MCOS->EmitFill(AddrSize, 0xFF);
818 MCOS->EmitValue(SectionStartAddr, AddrSize);
819
820 // Emit a range list entry spanning this section
821 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
822 *StartSymbol, *EndSymbol, 0);
823 MCOS->EmitIntValue(0, AddrSize);
824 MCOS->EmitAbsValue(SectionSize, AddrSize);
825 }
826
827 // Emit end of list entry
828 MCOS->EmitIntValue(0, AddrSize);
829 MCOS->EmitIntValue(0, AddrSize);
830}
831
Kevin Enderbye7739d42011-12-09 18:09:40 +0000832//
833// When generating dwarf for assembly source files this emits the Dwarf
834// sections.
835//
David Blaikie8bf66c42014-04-01 07:35:52 +0000836void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000837 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000838
839 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000840 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000841 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000842 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000843 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000844 if (CreateDwarfSectionSymbols)
845 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000846 MCSymbol *AbbrevSectionSymbol = nullptr;
847 MCSymbol *InfoSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000848 MCSymbol *RangesSectionSymbol = NULL;
849
850 // Create end symbols for each section, and remove empty sections
851 MCOS->getContext().finalizeDwarfSections(*MCOS);
852
853 // If there are no sections to generate debug info for, we don't need
854 // to do anything
855 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
856 return;
857
858 // We only need to use the .debug_ranges section if we have multiple
859 // code sections.
860 const bool UseRangesSection =
861 MCOS->getContext().getGenDwarfSectionSyms().size() > 1;
862 CreateDwarfSectionSymbols |= UseRangesSection;
863
Kevin Enderbye7739d42011-12-09 18:09:40 +0000864 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000865 if (CreateDwarfSectionSymbols) {
866 InfoSectionSymbol = context.CreateTempSymbol();
867 MCOS->EmitLabel(InfoSectionSymbol);
868 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000869 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000870 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000871 AbbrevSectionSymbol = context.CreateTempSymbol();
872 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000873 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000874 if (UseRangesSection) {
875 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
876 if (CreateDwarfSectionSymbols) {
877 RangesSectionSymbol = context.CreateTempSymbol();
878 MCOS->EmitLabel(RangesSectionSymbol);
879 }
880 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000881
Oliver Stannard8b273082014-06-19 15:52:37 +0000882 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
883
884 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000885
886 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000887 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000888
Oliver Stannard8b273082014-06-19 15:52:37 +0000889 if (UseRangesSection)
890 EmitGenDwarfRanges(MCOS);
891
Kevin Enderbye7739d42011-12-09 18:09:40 +0000892 // Output the data for .debug_abbrev section.
893 EmitGenDwarfAbbrev(MCOS);
894
895 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000896 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
897 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000898}
899
900//
901// When generating dwarf for assembly source files this is called when symbol
902// for a label is created. If this symbol is not a temporary and is in the
903// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000904// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000905//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000906void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000907 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000908 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000909 if (Symbol->isTemporary())
910 return;
911 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000912 // We won't create dwarf labels for symbols in sections that we are not
913 // generating debug info for.
914 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000915 return;
916
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000917 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000918 // underbar if any.
919 StringRef Name = Symbol->getName();
920 if (Name.startswith("_"))
921 Name = Name.substr(1, Name.size()-1);
922
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000923 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000924 unsigned FileNumber = context.getGenDwarfFileNumber();
925
926 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000927 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000928 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000929 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
930
931 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
932 // values so that they don't have things like an ARM thumb bit from the
933 // original symbol. So when used they won't get a low bit set after
934 // relocation.
935 MCSymbol *Label = context.CreateTempSymbol();
936 MCOS->EmitLabel(Label);
937
938 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000939 MCOS->getContext().addMCGenDwarfLabelEntry(
940 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000941}
942
Rafael Espindola0a017a62010-12-10 07:39:47 +0000943static int getDataAlignmentFactor(MCStreamer &streamer) {
944 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000945 const MCAsmInfo *asmInfo = context.getAsmInfo();
946 int size = asmInfo->getCalleeSaveStackSlotSize();
947 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000948 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000949 else
950 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000951}
952
Rafael Espindola5395f442011-04-22 00:08:43 +0000953static unsigned getSizeForEncoding(MCStreamer &streamer,
954 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000955 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000956 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000957 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000958 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000959 case dwarf::DW_EH_PE_absptr:
960 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000961 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000962 case dwarf::DW_EH_PE_udata2:
963 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000964 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000965 case dwarf::DW_EH_PE_udata4:
966 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000967 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000968 case dwarf::DW_EH_PE_udata8:
969 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000970 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000971 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000972}
973
Iain Sandoe618def62014-01-08 10:22:54 +0000974static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
975 unsigned symbolEncoding, bool isEH,
Craig Topperbb694de2014-04-13 04:57:38 +0000976 const char *comment = nullptr) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000977 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000978 const MCAsmInfo *asmInfo = context.getAsmInfo();
979 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
980 symbolEncoding,
981 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000982 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000983 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000984 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
985 streamer.EmitAbsValue(v, size);
986 else
987 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000988}
989
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000990static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
991 unsigned symbolEncoding) {
992 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000993 const MCAsmInfo *asmInfo = context.getAsmInfo();
994 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
995 symbolEncoding,
996 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000997 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000998 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000999}
1000
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001001namespace {
1002 class FrameEmitterImpl {
1003 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001004 int CIENum;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001005 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +00001006 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001007 public:
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001008 FrameEmitterImpl(bool isEH)
1009 : CFAOffset(0), CIENum(0), IsEH(isEH), SectionStart(nullptr) {}
Bill Wendling4d9aa512011-07-22 21:18:59 +00001010
1011 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001012
Bill Wendling3af441f2013-09-05 00:54:52 +00001013 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001014 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001015 const MCDwarfFrameInfo &frame);
1016
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001017 const MCSymbol &EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001018 const MCSymbol *personality,
1019 unsigned personalityEncoding,
1020 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001021 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001022 unsigned lsdaEncoding,
1023 bool IsSimple);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001024 MCSymbol *EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001025 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001026 const MCDwarfFrameInfo &frame);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001027 void EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001028 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001029 MCSymbol *BaseLabel);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001030 void EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001031 const MCCFIInstruction &Instr);
1032 };
Bill Wendling567a1ae2011-06-30 21:25:51 +00001033
1034} // end anonymous namespace
1035
1036static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
1037 StringRef Prefix) {
1038 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001039 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001040 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001041 default: EncStr = "<unknown encoding>"; break;
1042 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
1043 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
1044 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
1045 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
1046 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
1047 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
1048 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
1049 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
1050 EncStr = "pcrel udata4";
1051 break;
1052 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
1053 EncStr = "pcrel sdata4";
1054 break;
1055 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
1056 EncStr = "pcrel udata8";
1057 break;
1058 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
1059 EncStr = "screl sdata8";
1060 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001061 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
1062 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001063 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001064 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
1065 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001066 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001067 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
1068 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001069 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001070 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
1071 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001072 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001073 }
1074
1075 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
1076 }
1077
1078 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001079}
1080
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001081void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001082 const MCCFIInstruction &Instr) {
1083 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001084 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001085
1086 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001087 case MCCFIInstruction::OpRegister: {
1088 unsigned Reg1 = Instr.getRegister();
1089 unsigned Reg2 = Instr.getRegister2();
1090 if (VerboseAsm) {
1091 Streamer.AddComment("DW_CFA_register");
1092 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
1093 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
1094 }
1095 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1096 Streamer.EmitULEB128IntValue(Reg1);
1097 Streamer.EmitULEB128IntValue(Reg2);
1098 return;
1099 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001100 case MCCFIInstruction::OpWindowSave: {
1101 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1102 return;
1103 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001104 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001105 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001106 if (VerboseAsm) {
1107 Streamer.AddComment("DW_CFA_undefined");
1108 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1109 }
1110 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1111 Streamer.EmitULEB128IntValue(Reg);
1112 return;
1113 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001114 case MCCFIInstruction::OpAdjustCfaOffset:
1115 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001116 const bool IsRelative =
1117 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1118
1119 if (VerboseAsm)
1120 Streamer.AddComment("DW_CFA_def_cfa_offset");
1121 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1122
1123 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001124 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001125 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001126 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001127
1128 if (VerboseAsm)
1129 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1130 Streamer.EmitULEB128IntValue(CFAOffset);
1131
1132 return;
1133 }
1134 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001135 if (VerboseAsm)
1136 Streamer.AddComment("DW_CFA_def_cfa");
1137 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1138
1139 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001140 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1141 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001142
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001143 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001144
1145 if (VerboseAsm)
1146 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1147 Streamer.EmitULEB128IntValue(CFAOffset);
1148
1149 return;
1150 }
1151
1152 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001153 if (VerboseAsm)
1154 Streamer.AddComment("DW_CFA_def_cfa_register");
1155 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1156
1157 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001158 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1159 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001160
1161 return;
1162 }
1163
1164 case MCCFIInstruction::OpOffset:
1165 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001166 const bool IsRelative =
1167 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001168
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001169 unsigned Reg = Instr.getRegister();
1170 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001171 if (IsRelative)
1172 Offset -= CFAOffset;
1173 Offset = Offset / dataAlignmentFactor;
1174
1175 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001176 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001177 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001178 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001179 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001180 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001181 Streamer.EmitSLEB128IntValue(Offset);
1182 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001183 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1184 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001185 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001186 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001187 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001188 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001189 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001190 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001191 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001192 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001193 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001194 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001195 }
1196 return;
1197 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001198 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001199 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001200 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1201 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001202 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001203 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001204 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1205 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001206 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001207 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001208 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001209 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001210 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001211 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001212 return;
1213 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001214 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001215 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001216 if (VerboseAsm) {
1217 Streamer.AddComment("DW_CFA_restore");
1218 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1219 }
1220 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1221 return;
1222 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001223 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001224 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001225 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001226 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001227 }
1228 llvm_unreachable("Unhandled case in switch");
1229}
1230
1231/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1232/// frame.
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001233void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001234 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001235 MCSymbol *BaseLabel) {
1236 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1237 const MCCFIInstruction &Instr = Instrs[i];
1238 MCSymbol *Label = Instr.getLabel();
1239 // Throw out move if the label is invalid.
1240 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1241
1242 // Advance row if new location.
1243 if (BaseLabel && Label) {
1244 MCSymbol *ThisSym = Label;
1245 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001246 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001247 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1248 BaseLabel = ThisSym;
1249 }
1250 }
1251
1252 EmitCFIInstruction(streamer, Instr);
1253 }
1254}
1255
Bill Wendling3af441f2013-09-05 00:54:52 +00001256/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001257void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001258 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001259 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001260 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001261 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001262
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001263 // range-start range-length compact-unwind-enc personality-func lsda
1264 // _foo LfooEnd-_foo 0x00000023 0 0
1265 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1266 //
1267 // .section __LD,__compact_unwind,regular,debug
1268 //
1269 // # compact unwind for _foo
1270 // .quad _foo
1271 // .set L1,LfooEnd-_foo
1272 // .long L1
1273 // .long 0x01010001
1274 // .quad 0
1275 // .quad 0
1276 //
1277 // # compact unwind for _bar
1278 // .quad _bar
1279 // .set L2,LbarEnd-_bar
1280 // .long L2
1281 // .long 0x01020011
1282 // .quad __gxx_personality
1283 // .quad except_tab1
1284
Bill Wendling49bc6802011-07-19 00:06:12 +00001285 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001286 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001287 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001288
Bill Wendlingdafd5982011-07-14 23:34:45 +00001289 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001290 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001291 Encoding |= 0x40000000;
1292
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001293 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001294 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001295 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001296 if (VerboseAsm) Streamer.AddComment("Range Start");
Rafael Espindola88604822014-06-23 15:34:32 +00001297 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001298
1299 // Range Length
1300 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1301 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001302 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001303 Streamer.EmitAbsValue(Range, 4);
1304
Bill Wendling75174662011-06-30 00:30:52 +00001305 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001306 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001307 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1308 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001309 Streamer.EmitIntValue(Encoding, Size);
1310
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001311 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001312 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001313 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001314 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001315 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001316 else
1317 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001318
1319 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001320 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001321 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001322 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001323 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001324 else
1325 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001326}
1327
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001328const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001329 const MCSymbol *personality,
1330 unsigned personalityEncoding,
1331 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001332 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001333 unsigned lsdaEncoding,
1334 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001335 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001336 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001337 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001338 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001339
Rafael Espindolab4076b292014-06-20 23:54:32 +00001340 MCSymbol *sectionStart = context.CreateTempSymbol();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001341 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001342 CIENum++;
1343
Bill Wendlingb6adf462011-07-07 00:54:13 +00001344 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001345
1346 // Length
1347 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1348 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001349 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001350 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001351
1352 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001353 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001354 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001355 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001356
1357 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001358 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Oliver Stannardf7693f42014-06-19 15:39:33 +00001359 // For DWARF2, we use CIE version 1
1360 // For DWARF3+, we use CIE version 3
1361 uint8_t CIEVersion = context.getDwarfVersion() <= 2 ? 1 : 3;
1362 streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001363
1364 // Augmentation String
1365 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001366 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001367 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001368 Augmentation += "z";
1369 if (personality)
1370 Augmentation += "P";
1371 if (lsda)
1372 Augmentation += "L";
1373 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001374 if (IsSignalFrame)
1375 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001376 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001377 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001378 streamer.EmitIntValue(0, 1);
1379
1380 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001381 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001382 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001383
1384 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001385 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001386 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1387
1388 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001389 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Oliver Stannardf7693f42014-06-19 15:39:33 +00001390 if (CIEVersion == 1) {
1391 assert(MRI->getRARegister() <= 255 &&
1392 "DWARF 2 encodes return_address_register in one byte");
1393 streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true), 1);
1394 } else {
1395 streamer.EmitULEB128IntValue(
1396 MRI->getDwarfRegNum(MRI->getRARegister(), true));
1397 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001398
1399 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001400
1401 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001402 if (IsEH) {
1403 if (personality) {
1404 // Personality Encoding
1405 augmentationLength += 1;
1406 // Personality
1407 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1408 }
1409 if (lsda)
1410 augmentationLength += 1;
1411 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001412 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001413
Bill Wendling567a1ae2011-06-30 21:25:51 +00001414 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001415 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001416
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001417 // Augmentation Data (optional)
1418 if (personality) {
1419 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001420 EmitEncodingByte(streamer, personalityEncoding,
1421 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001422 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001423 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001424 EmitPersonality(streamer, *personality, personalityEncoding);
1425 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001426
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001427 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001428 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1429
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001430 // Encoding of the FDE pointers
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001431 EmitEncodingByte(streamer, MOFI->getFDEEncoding(), "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001432 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001433
1434 // Initial Instructions
1435
Bill Wendlingbc07a892013-06-18 07:20:20 +00001436 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001437 if (!IsSimple) {
1438 const std::vector<MCCFIInstruction> &Instructions =
1439 MAI->getInitialFrameState();
Craig Topperbb694de2014-04-13 04:57:38 +00001440 EmitCFIInstructions(streamer, Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001441 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001442
1443 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001444 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001445
1446 streamer.EmitLabel(sectionEnd);
1447 return *sectionStart;
1448}
1449
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001450MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001451 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001452 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001453 MCContext &context = streamer.getContext();
1454 MCSymbol *fdeStart = context.CreateTempSymbol();
1455 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001456 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001457 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001458
1459 // Length
1460 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001461 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001462 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001463
1464 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001465
Rafael Espindola0a017a62010-12-10 07:39:47 +00001466 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001467 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001468 if (IsEH) {
1469 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1470 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001471 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001472 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001473 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001474 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1475 cieStart, 0);
1476 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001477 } else {
1478 streamer.EmitSymbolValue(&cieStart, 4);
1479 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001480
Rafael Espindola0a017a62010-12-10 07:39:47 +00001481 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001482 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001483 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Nick Lewycky333449c2012-08-12 08:09:45 +00001484 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001485 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001486
1487 // PC Range
1488 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1489 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001490 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001491 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001492
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001493 if (IsEH) {
1494 // Augmentation Data Length
1495 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001496
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001497 if (frame.Lsda)
1498 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001499
Bill Wendlingf166ab42011-06-30 22:02:20 +00001500 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001501 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001502
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001503 // Augmentation Data
1504 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001505 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1506 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001507 }
Rafael Espindola68067662011-04-29 03:06:29 +00001508
Rafael Espindola0a017a62010-12-10 07:39:47 +00001509 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001510 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001511
Rafael Espindola0a017a62010-12-10 07:39:47 +00001512 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001513 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001514
1515 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001516}
1517
Benjamin Kramer570dd782010-12-30 22:34:44 +00001518namespace {
1519 struct CIEKey {
Craig Topperbb694de2014-04-13 04:57:38 +00001520 static const CIEKey getEmptyKey() {
1521 return CIEKey(nullptr, 0, -1, false, false);
1522 }
1523 static const CIEKey getTombstoneKey() {
1524 return CIEKey(nullptr, -1, 0, false, false);
1525 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001526
Eric Christopherd29430d2014-06-19 20:00:09 +00001527 CIEKey(const MCSymbol *Personality_, unsigned PersonalityEncoding_,
1528 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_)
1529 : Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1530 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1531 IsSimple(IsSimple_) {}
1532 const MCSymbol *Personality;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001533 unsigned PersonalityEncoding;
1534 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001535 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001536 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001537 };
1538}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001539
1540namespace llvm {
1541 template <>
1542 struct DenseMapInfo<CIEKey> {
1543 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001544 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001545 }
1546 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001547 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001548 }
1549 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001550 return static_cast<unsigned>(hash_combine(Key.Personality,
1551 Key.PersonalityEncoding,
1552 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001553 Key.IsSignalFrame,
1554 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001555 }
1556 static bool isEqual(const CIEKey &LHS,
1557 const CIEKey &RHS) {
1558 return LHS.Personality == RHS.Personality &&
1559 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001560 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001561 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1562 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001563 }
1564 };
1565}
1566
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001567void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001568 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001569 Streamer.generateCompactUnwindEncodings(MAB);
1570
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001571 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001572 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001573 FrameEmitterImpl Emitter(IsEH);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001574 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001575
Bill Wendling9803abb2011-09-06 18:37:11 +00001576 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001577 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001578 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001579 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001580 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1581 const MCDwarfFrameInfo &Frame = FrameArray[i];
1582 if (Frame.CompactUnwindEncoding == 0) continue;
1583 if (!SectionEmitted) {
1584 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001585 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001586 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001587 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001588 NeedsEHFrameSection |=
1589 Frame.CompactUnwindEncoding ==
1590 MOFI->getCompactUnwindDwarfEHFrameOnly();
Bob Wilson0e180f22013-05-07 20:56:33 +00001591 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001592 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001593 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001594
Tim Northoverd1c6f512014-03-29 09:03:13 +00001595 if (!NeedsEHFrameSection) return;
1596
Bill Wendlinga800f952013-05-30 18:52:57 +00001597 const MCSection &Section =
1598 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1599 *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001600
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001601 Streamer.SwitchSection(&Section);
1602 MCSymbol *SectionStart = Context.CreateTempSymbol();
1603 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001604 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001605
Craig Topperbb694de2014-04-13 04:57:38 +00001606 MCSymbol *FDEEnd = nullptr;
Eric Christopherd29430d2014-06-19 20:00:09 +00001607 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001608
Craig Topperbb694de2014-04-13 04:57:38 +00001609 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001610 NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001611 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1612 const MCDwarfFrameInfo &Frame = FrameArray[i];
Tim Northoverd1c6f512014-03-29 09:03:13 +00001613
1614 // Emit the label from the previous iteration
1615 if (FDEEnd) {
1616 Streamer.EmitLabel(FDEEnd);
Craig Topperbb694de2014-04-13 04:57:38 +00001617 FDEEnd = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001618 }
1619
1620 if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding !=
1621 MOFI->getCompactUnwindDwarfEHFrameOnly())
1622 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1623 // of by the compact unwind encoding.
1624 continue;
1625
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001626 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001627 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001628 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1629 if (!CIEStart)
1630 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1631 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001632 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001633 Frame.LsdaEncoding,
1634 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001635
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001636 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001637 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001638
Bill Wendlingbc07a892013-06-18 07:20:20 +00001639 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001640 if (FDEEnd)
1641 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001642}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001643
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001644void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001645 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001646 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001647 SmallString<256> Tmp;
1648 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001649 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001650 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001651}
1652
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001653void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1654 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001655 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001656 // Scale the address delta by the minimum instruction length.
1657 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1658
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001659 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001660 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001661 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1662 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001663 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001664 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1665 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001666 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001667 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001668 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001669 OS << uint8_t( AddrDelta & 0xff);
1670 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001671 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001672 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001673 assert(isUInt<32>(AddrDelta));
1674 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001675 OS << uint8_t( AddrDelta & 0xff);
1676 OS << uint8_t((AddrDelta >> 8) & 0xff);
1677 OS << uint8_t((AddrDelta >> 16) & 0xff);
1678 OS << uint8_t((AddrDelta >> 24) & 0xff);
1679
Rafael Espindola736a35d2010-12-28 05:39:27 +00001680 }
1681}