blob: f71b266ad632baeed2d7d74ce312d1ae21bf0a3c [file] [log] [blame]
Kevin Enderby7cbf73a2010-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"
Evan Chenge76a33b2011-07-20 05:58:47 +000011#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCObjectFileInfo.h"
14#include "llvm/MC/MCObjectWriter.h"
15#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000016#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000017#include "llvm/MC/MCSymbol.h"
18#include "llvm/MC/MCExpr.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000020#include "llvm/Support/ErrorHandling.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000021#include "llvm/Support/raw_ostream.h"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000022#include "llvm/Support/LEB128.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000023#include "llvm/Support/Path.h"
24#include "llvm/Support/SourceMgr.h"
Benjamin Kramer74849202012-04-11 14:06:39 +000025#include "llvm/ADT/Hashing.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000026#include "llvm/ADT/SmallString.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000027#include "llvm/ADT/Twine.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000028#include "llvm/Config/config.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029using namespace llvm;
30
Kevin Enderbyc0957932010-09-30 16:52:03 +000031// Given a special op, return the address skip amount (in units of
32// DWARF2_LINE_MIN_INSN_LENGTH.
33#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
34
35// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc3882162011-06-30 23:59:38 +000036#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbyc0957932010-09-30 16:52:03 +000037
38// First special line opcode - leave room for the standard opcodes.
39// Note: If you want to change this, you'll have to update the
Jim Grosbach2684d9e2012-05-11 01:41:30 +000040// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc3882162011-06-30 23:59:38 +000041#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbyc0957932010-09-30 16:52:03 +000042
43// Minimum line offset in a special line info. opcode. This value
44// was chosen to give a reasonable range of values.
Bill Wendlingc3882162011-06-30 23:59:38 +000045#define DWARF2_LINE_BASE -5
Kevin Enderbyc0957932010-09-30 16:52:03 +000046
47// Range of line offsets in a special line info. opcode.
Bill Wendlingc3882162011-06-30 23:59:38 +000048#define DWARF2_LINE_RANGE 14
Kevin Enderbyc0957932010-09-30 16:52:03 +000049
50// Define the architecture-dependent minimum instruction length (in bytes).
51// This value should be rather too small than too big.
Bill Wendlingc3882162011-06-30 23:59:38 +000052#define DWARF2_LINE_MIN_INSN_LENGTH 1
Kevin Enderbyc0957932010-09-30 16:52:03 +000053
54// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
55// this routine is a nop and will be optimized away.
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +000056static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000057 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
58 return AddrDelta;
59 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
60 // TODO: report this error, but really only once.
61 ;
62 }
63 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
64}
65
66//
67// This is called when an instruction is assembled into the specified section
68// and if there is information from the last .loc directive that has yet to have
69// a line entry made for it is made.
70//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000071void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000072 if (!MCOS->getContext().getDwarfLocSeen())
73 return;
74
75 // Create a symbol at in the current section for use in the line entry.
76 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
77 // Set the value of the symbol to use for the MCLineEntry.
78 MCOS->EmitLabel(LineSym);
79
80 // Get the current .loc info saved in the context.
81 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
82
83 // Create a (local) line entry with the symbol and the current .loc info.
84 MCLineEntry LineEntry(LineSym, DwarfLoc);
85
86 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000087 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000088
89 // Get the MCLineSection for this section, if one does not exist for this
90 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000091 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000092 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000093 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000094 if (!LineSection) {
95 // Create a new MCLineSection. This will be deleted after the dwarf line
96 // table is created using it by iterating through the MCLineSections
97 // DenseMap.
98 LineSection = new MCLineSection;
99 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000100 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000101 }
102
103 // Add the line entry to this section's entries.
104 LineSection->addLineEntry(LineEntry);
105}
106
107//
108// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000109//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000110static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
111 const MCSymbol &Start,
112 const MCSymbol &End,
113 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000114 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
115 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000116 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000117 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000118 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000119 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000120 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000121 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000122 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000123 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000124 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000125 return Res3;
126}
127
Kevin Enderbyc0957932010-09-30 16:52:03 +0000128//
129// This emits the Dwarf line table for the specified section from the entries
130// in the LineSection.
131//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000132static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000133 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000134 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000135 unsigned FileNum = 1;
136 unsigned LastLine = 1;
137 unsigned Column = 0;
138 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
139 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000140 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000141
142 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000143 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000144 it = LineSection->getMCLineEntries()->begin(),
145 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
146
147 if (FileNum != it->getFileNum()) {
148 FileNum = it->getFileNum();
149 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000150 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000151 }
152 if (Column != it->getColumn()) {
153 Column = it->getColumn();
154 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000155 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000156 }
157 if (Isa != it->getIsa()) {
158 Isa = it->getIsa();
159 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000160 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000161 }
162 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
163 Flags = it->getFlags();
164 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
165 }
166 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
167 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
168 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
169 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
170 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
171 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
172
Rafael Espindola64185cc2010-11-13 01:06:27 +0000173 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000174 MCSymbol *Label = it->getLabel();
175
176 // At this point we want to emit/create the sequence to encode the delta in
177 // line numbers and the increment of the address from the previous Label
178 // and the current Label.
Evan Cheng1be0e272011-07-15 02:09:41 +0000179 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000180 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
181 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000182
183 LastLine = it->getLine();
184 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000185 }
186
187 // Emit a DW_LNE_end_sequence for the end of the section.
188 // Using the pointer Section create a temporary label at the end of the
189 // section and use that and the LastLabel to compute the address delta
190 // and use INT64_MAX as the line delta which is the signal that this is
191 // actually a DW_LNE_end_sequence.
192
193 // Switch to the section to be able to create a symbol at its end.
194 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000195
196 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000197 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000198 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000199 // Set the value of the symbol, as we are at the end of the section.
200 MCOS->EmitLabel(SectionEnd);
201
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000202 // Switch back the dwarf line section.
Evan Chenge76a33b2011-07-20 05:58:47 +0000203 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000204
Evan Cheng1be0e272011-07-15 02:09:41 +0000205 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000206 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
207 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000208}
209
210//
211// This emits the Dwarf file and the line tables.
212//
Rafael Espindola489d6792012-02-28 21:13:05 +0000213const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000214 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000215 // Switch to the section where the table will be emitted into.
Evan Chenge76a33b2011-07-20 05:58:47 +0000216 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000217
218 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000219 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000220 // Set the value of the symbol, as we are at the start of the section.
221 MCOS->EmitLabel(LineStartSym);
222
223 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000224 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000225
226 // The first 4 bytes is the total length of the information for this
227 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000228 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000229 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000230
231 // Next 2 bytes is the Version, which is Dwarf 2.
232 MCOS->EmitIntValue(2, 2);
233
234 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000235 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000236
237 // Length of the prologue, is the next 4 bytes. Which is the start of the
238 // section to the end of the prologue. Not including the 4 bytes for the
239 // total length, the 2 bytes for the version, and these 4 bytes for the
240 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000241 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000242 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000243 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000244
245 // Parameters of the state machine, are next.
246 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
247 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
248 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
249 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
250 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
251
252 // Standard opcode lengths
253 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
254 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
255 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
256 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
257 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
258 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
259 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
260 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
261 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
262 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
263 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
264 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
265
266 // Put out the directory and file tables.
267
268 // First the directory table.
269 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000270 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000271 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
272 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
273 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
274 }
275 MCOS->EmitIntValue(0, 1); // Terminate the directory list
276
277 // Second the file table.
278 const std::vector<MCDwarfFile *> &MCDwarfFiles =
279 MCOS->getContext().getMCDwarfFiles();
280 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
281 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
282 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000283 // the Directory num
284 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000285 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
286 MCOS->EmitIntValue(0, 1); // filesize (always 0)
287 }
288 MCOS->EmitIntValue(0, 1); // Terminate the file list
289
290 // This is the end of the prologue, so set the value of the symbol at the
291 // end of the prologue (that was used in a previous expression).
292 MCOS->EmitLabel(ProEndSym);
293
294 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000295 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000296 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000297 const std::vector<const MCSection *> &MCLineSectionOrder =
298 MCOS->getContext().getMCLineSectionOrder();
299 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc3882162011-06-30 23:59:38 +0000300 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000301 ++it) {
302 const MCSection *Sec = *it;
303 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000304 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000305
306 // Now delete the MCLineSections that were created in MCLineEntry::Make()
307 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000308 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000309 }
310
Rafael Espindola767b1be2010-12-04 00:31:13 +0000311 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
312 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000313 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000314 // it requires:
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000315 // total_length >= prologue_length + 10
316 // We are 4 bytes short, since we have total_length = 51 and
317 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000318
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000319 // The regular end_sequence should be sufficient.
320 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000321 }
322
Kevin Enderbyc0957932010-09-30 16:52:03 +0000323 // This is the end of the section, so set the value of the symbol at the end
324 // of this section (that was used in a previous expression).
325 MCOS->EmitLabel(LineEndSym);
Rafael Espindola489d6792012-02-28 21:13:05 +0000326
327 return LineStartSym;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000328}
329
Kevin Enderbyc0957932010-09-30 16:52:03 +0000330/// Utility function to write the encoding to an object writer.
331void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
332 uint64_t AddrDelta) {
333 SmallString<256> Tmp;
334 raw_svector_ostream OS(Tmp);
335 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
336 OW->WriteBytes(OS.str());
337}
338
339/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000340void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000341 uint64_t AddrDelta) {
342 SmallString<256> Tmp;
343 raw_svector_ostream OS(Tmp);
344 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
345 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
346}
347
348/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
349void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
350 raw_ostream &OS) {
351 uint64_t Temp, Opcode;
352 bool NeedCopy = false;
353
354 // Scale the address delta by the minimum instruction length.
355 AddrDelta = ScaleAddrDelta(AddrDelta);
356
357 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000358 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbyc0957932010-09-30 16:52:03 +0000359 // end_sequence to emit the matrix entry.
360 if (LineDelta == INT64_MAX) {
361 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
362 OS << char(dwarf::DW_LNS_const_add_pc);
363 else {
364 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000365 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000366 }
367 OS << char(dwarf::DW_LNS_extended_op);
368 OS << char(1);
369 OS << char(dwarf::DW_LNE_end_sequence);
370 return;
371 }
372
373 // Bias the line delta by the base.
374 Temp = LineDelta - DWARF2_LINE_BASE;
375
376 // If the line increment is out of range of a special opcode, we must encode
377 // it with DW_LNS_advance_line.
378 if (Temp >= DWARF2_LINE_RANGE) {
379 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000380 encodeSLEB128(LineDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000381
382 LineDelta = 0;
383 Temp = 0 - DWARF2_LINE_BASE;
384 NeedCopy = true;
385 }
386
387 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
388 if (LineDelta == 0 && AddrDelta == 0) {
389 OS << char(dwarf::DW_LNS_copy);
390 return;
391 }
392
393 // Bias the opcode by the special opcode base.
394 Temp += DWARF2_LINE_OPCODE_BASE;
395
396 // Avoid overflow when addr_delta is large.
397 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
398 // Try using a special opcode.
399 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
400 if (Opcode <= 255) {
401 OS << char(Opcode);
402 return;
403 }
404
405 // Try using DW_LNS_const_add_pc followed by special op.
406 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
407 if (Opcode <= 255) {
408 OS << char(dwarf::DW_LNS_const_add_pc);
409 OS << char(Opcode);
410 return;
411 }
412 }
413
414 // Otherwise use DW_LNS_advance_pc.
415 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000416 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000417
418 if (NeedCopy)
419 OS << char(dwarf::DW_LNS_copy);
420 else
421 OS << char(Temp);
422}
423
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000424void MCDwarfFile::print(raw_ostream &OS) const {
425 OS << '"' << getName() << '"';
426}
427
Manman Ren286c4dc2012-09-12 05:06:18 +0000428#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000429void MCDwarfFile::dump() const {
430 print(dbgs());
431}
Manman Rencc77eec2012-09-06 19:55:56 +0000432#endif
Kevin Enderbyc0957932010-09-30 16:52:03 +0000433
Kevin Enderby94c2e852011-12-09 18:09:40 +0000434// Utility function to write a tuple for .debug_abbrev.
435static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
436 MCOS->EmitULEB128IntValue(Name);
437 MCOS->EmitULEB128IntValue(Form);
438}
439
440// When generating dwarf for assembly source files this emits
441// the data for .debug_abbrev section which contains three DIEs.
442static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
443 MCContext &context = MCOS->getContext();
444 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
445
446 // DW_TAG_compile_unit DIE abbrev (1).
447 MCOS->EmitULEB128IntValue(1);
448 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
449 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
450 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
451 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
452 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
453 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
454 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
455 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
456 if (!DwarfDebugFlags.empty())
457 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
458 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
459 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
460 EmitAbbrev(MCOS, 0, 0);
461
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000462 // DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000463 MCOS->EmitULEB128IntValue(2);
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000464 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000465 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
466 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
467 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
468 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
469 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000470 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
471 EmitAbbrev(MCOS, 0, 0);
472
473 // DW_TAG_unspecified_parameters DIE abbrev (3).
474 MCOS->EmitULEB128IntValue(3);
475 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
476 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
477 EmitAbbrev(MCOS, 0, 0);
478
479 // Terminate the abbreviations for this compilation unit.
480 MCOS->EmitIntValue(0, 1);
481}
482
483// When generating dwarf for assembly source files this emits the data for
484// .debug_aranges section. Which contains a header and a table of pairs of
485// PointerSize'ed values for the address and size of section(s) with line table
486// entries (just the default .text in our case) and a terminating pair of zeros.
487static void EmitGenDwarfAranges(MCStreamer *MCOS) {
488 MCContext &context = MCOS->getContext();
489
490 // Create a symbol at the end of the section that we are creating the dwarf
491 // debugging info to use later in here as part of the expression to calculate
492 // the size of the section for the table.
493 MCOS->SwitchSection(context.getGenDwarfSection());
494 MCSymbol *SectionEndSym = context.CreateTempSymbol();
495 MCOS->EmitLabel(SectionEndSym);
496 context.setGenDwarfSectionEndSym(SectionEndSym);
497
498 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
499
500 // This will be the length of the .debug_aranges section, first account for
501 // the size of each item in the header (see below where we emit these items).
502 int Length = 4 + 2 + 4 + 1 + 1;
503
504 // Figure the padding after the header before the table of address and size
505 // pairs who's values are PointerSize'ed.
506 const MCAsmInfo &asmInfo = context.getAsmInfo();
507 int AddrSize = asmInfo.getPointerSize();
508 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
509 if (Pad == 2 * AddrSize)
510 Pad = 0;
511 Length += Pad;
512
513 // Add the size of the pair of PointerSize'ed values for the address and size
514 // of the one default .text section we have in the table.
515 Length += 2 * AddrSize;
516 // And the pair of terminating zeros.
517 Length += 2 * AddrSize;
518
519
520 // Emit the header for this section.
521 // The 4 byte length not including the 4 byte value for the length.
522 MCOS->EmitIntValue(Length - 4, 4);
523 // The 2 byte version, which is 2.
524 MCOS->EmitIntValue(2, 2);
525 // The 4 byte offset to the compile unit in the .debug_info from the start
526 // of the .debug_info, it is at the start of that section so this is zero.
527 MCOS->EmitIntValue(0, 4);
528 // The 1 byte size of an address.
529 MCOS->EmitIntValue(AddrSize, 1);
530 // The 1 byte size of a segment descriptor, we use a value of zero.
531 MCOS->EmitIntValue(0, 1);
532 // Align the header with the padding if needed, before we put out the table.
533 for(int i = 0; i < Pad; i++)
534 MCOS->EmitIntValue(0, 1);
535
536 // Now emit the table of pairs of PointerSize'ed values for the section(s)
537 // address and size, in our case just the one default .text section.
538 const MCExpr *Addr = MCSymbolRefExpr::Create(
539 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
540 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
541 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
542 MCOS->EmitAbsValue(Addr, AddrSize);
543 MCOS->EmitAbsValue(Size, AddrSize);
544
545 // And finally the pair of terminating zeros.
546 MCOS->EmitIntValue(0, AddrSize);
547 MCOS->EmitIntValue(0, AddrSize);
548}
549
550// When generating dwarf for assembly source files this emits the data for
551// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000552// DIE and a list of label DIEs.
Rafael Espindola489d6792012-02-28 21:13:05 +0000553static void EmitGenDwarfInfo(MCStreamer *MCOS,
554 const MCSymbol *AbbrevSectionSymbol,
555 const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000556 MCContext &context = MCOS->getContext();
557
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000558 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000559
560 // Create a symbol at the start and end of this section used in here for the
561 // expression to calculate the length in the header.
562 MCSymbol *InfoStart = context.CreateTempSymbol();
563 MCOS->EmitLabel(InfoStart);
564 MCSymbol *InfoEnd = context.CreateTempSymbol();
565
566 // First part: the header.
567
568 // The 4 byte total length of the information for this compilation unit, not
569 // including these 4 bytes.
570 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
571 MCOS->EmitAbsValue(Length, 4);
572
573 // The 2 byte DWARF version, which is 2.
574 MCOS->EmitIntValue(2, 2);
575
576 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
577 // it is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000578 if (AbbrevSectionSymbol) {
579 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
580 } else {
581 MCOS->EmitIntValue(0, 4);
582 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000583
584 const MCAsmInfo &asmInfo = context.getAsmInfo();
585 int AddrSize = asmInfo.getPointerSize();
586 // The 1 byte size of an address.
587 MCOS->EmitIntValue(AddrSize, 1);
588
589 // Second part: the compile_unit DIE.
590
591 // The DW_TAG_compile_unit DIE abbrev (1).
592 MCOS->EmitULEB128IntValue(1);
593
594 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
595 // which is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000596 if (LineSectionSymbol) {
597 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
598 } else {
599 MCOS->EmitIntValue(0, 4);
600 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000601
602 // AT_low_pc, the first address of the default .text section.
603 const MCExpr *Start = MCSymbolRefExpr::Create(
604 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
605 MCOS->EmitAbsValue(Start, AddrSize);
606
607 // AT_high_pc, the last address of the default .text section.
608 const MCExpr *End = MCSymbolRefExpr::Create(
609 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
610 MCOS->EmitAbsValue(End, AddrSize);
611
612 // AT_name, the name of the source file. Reconstruct from the first directory
613 // and file table entries.
614 const std::vector<StringRef> &MCDwarfDirs =
615 context.getMCDwarfDirs();
616 if (MCDwarfDirs.size() > 0) {
617 MCOS->EmitBytes(MCDwarfDirs[0], 0);
618 MCOS->EmitBytes("/", 0);
619 }
620 const std::vector<MCDwarfFile *> &MCDwarfFiles =
621 MCOS->getContext().getMCDwarfFiles();
622 MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0);
623 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
624
625 // AT_comp_dir, the working directory the assembly was done in.
626 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
627 MCOS->EmitBytes(StringRef(CWD.c_str()), 0);
628 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
629
630 // AT_APPLE_flags, the command line arguments of the assembler tool.
631 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
632 if (!DwarfDebugFlags.empty()){
633 MCOS->EmitBytes(DwarfDebugFlags, 0);
634 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
635 }
636
637 // AT_producer, the version of the assembler tool.
638 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0);
639 MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0);
640 MCOS->EmitBytes(StringRef(")"), 0);
641 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
642
643 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
644 // draft has no standard code for assembler.
645 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
646
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000647 // Third part: the list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000648
Kevin Enderby11c2def2012-01-10 21:12:34 +0000649 // Loop on saved info for dwarf labels and create the DIEs for them.
650 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
651 MCOS->getContext().getMCGenDwarfLabelEntries();
652 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000653 Entries.begin(), ie = Entries.end(); it != ie;
654 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000655 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000656
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000657 // The DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000658 MCOS->EmitULEB128IntValue(2);
659
660 // AT_name, of the label without any leading underbar.
661 MCOS->EmitBytes(Entry->getName(), 0);
662 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
663
664 // AT_decl_file, index into the file table.
665 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
666
667 // AT_decl_line, source line number.
668 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
669
670 // AT_low_pc, start address of the label.
671 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
672 MCSymbolRefExpr::VK_None, context);
673 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
674
Kevin Enderby94c2e852011-12-09 18:09:40 +0000675 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
676 MCOS->EmitIntValue(0, 1);
677
678 // The DW_TAG_unspecified_parameters DIE abbrev (3).
679 MCOS->EmitULEB128IntValue(3);
680
681 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
682 MCOS->EmitIntValue(0, 1);
683 }
Kevin Enderby11c2def2012-01-10 21:12:34 +0000684 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
685 // for the dwarf labels.
686 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000687 Entries.begin(), ie = Entries.end(); it != ie;
688 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000689 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000690 delete Entry;
691 }
692
693 // Add the NULL DIE terminating the Compile Unit DIE's.
694 MCOS->EmitIntValue(0, 1);
695
696 // Now set the value of the symbol at the end of the info section.
697 MCOS->EmitLabel(InfoEnd);
698}
699
700//
701// When generating dwarf for assembly source files this emits the Dwarf
702// sections.
703//
Rafael Espindola489d6792012-02-28 21:13:05 +0000704void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000705 // Create the dwarf sections in this order (.debug_line already created).
706 MCContext &context = MCOS->getContext();
Rafael Espindola489d6792012-02-28 21:13:05 +0000707 const MCAsmInfo &AsmInfo = context.getAsmInfo();
Kevin Enderby94c2e852011-12-09 18:09:40 +0000708 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
709 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Rafael Espindola489d6792012-02-28 21:13:05 +0000710 MCSymbol *AbbrevSectionSymbol;
Rafael Espindola2241e512012-06-22 13:24:07 +0000711 if (AsmInfo.doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola489d6792012-02-28 21:13:05 +0000712 AbbrevSectionSymbol = context.CreateTempSymbol();
713 MCOS->EmitLabel(AbbrevSectionSymbol);
714 } else {
715 AbbrevSectionSymbol = NULL;
716 LineSectionSymbol = NULL;
717 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000718 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
719
720 // If there are no line table entries then do not emit any section contents.
721 if (context.getMCLineSections().empty())
722 return;
723
724 // Output the data for .debug_aranges section.
725 EmitGenDwarfAranges(MCOS);
726
727 // Output the data for .debug_abbrev section.
728 EmitGenDwarfAbbrev(MCOS);
729
730 // Output the data for .debug_info section.
Rafael Espindola489d6792012-02-28 21:13:05 +0000731 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000732}
733
734//
735// When generating dwarf for assembly source files this is called when symbol
736// for a label is created. If this symbol is not a temporary and is in the
737// section that dwarf is being generated for, save the needed info to create
Kevin Enderby11c2def2012-01-10 21:12:34 +0000738// a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000739//
Kevin Enderby11c2def2012-01-10 21:12:34 +0000740void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderby94c2e852011-12-09 18:09:40 +0000741 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher3c3cdcc2012-02-25 01:02:44 +0000742 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderby94c2e852011-12-09 18:09:40 +0000743 // the default text.
744 if (Symbol->isTemporary())
745 return;
746 MCContext &context = MCOS->getContext();
747 if (context.getGenDwarfSection() != MCOS->getCurrentSection())
748 return;
749
Kevin Enderby11c2def2012-01-10 21:12:34 +0000750 // The dwarf label's name does not have the symbol name's leading
Kevin Enderby94c2e852011-12-09 18:09:40 +0000751 // underbar if any.
752 StringRef Name = Symbol->getName();
753 if (Name.startswith("_"))
754 Name = Name.substr(1, Name.size()-1);
755
Kevin Enderby11c2def2012-01-10 21:12:34 +0000756 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000757 unsigned FileNumber = context.getGenDwarfFileNumber();
758
759 // Finding the line number is the expensive part which is why we just don't
Kevin Enderby11c2def2012-01-10 21:12:34 +0000760 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000761 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
762 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
763
764 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
765 // values so that they don't have things like an ARM thumb bit from the
766 // original symbol. So when used they won't get a low bit set after
767 // relocation.
768 MCSymbol *Label = context.CreateTempSymbol();
769 MCOS->EmitLabel(Label);
770
771 // Create and entry for the info and add it to the other entries.
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000772 MCGenDwarfLabelEntry *Entry =
Kevin Enderby11c2def2012-01-10 21:12:34 +0000773 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
774 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000775}
776
Rafael Espindola89b93722010-12-10 07:39:47 +0000777static int getDataAlignmentFactor(MCStreamer &streamer) {
778 MCContext &context = streamer.getContext();
Evan Cheng1be0e272011-07-15 02:09:41 +0000779 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola89b93722010-12-10 07:39:47 +0000780 int size = asmInfo.getPointerSize();
Evan Cheng1be0e272011-07-15 02:09:41 +0000781 if (asmInfo.isStackGrowthDirectionUp())
Rafael Espindola89b93722010-12-10 07:39:47 +0000782 return size;
Evan Cheng1be0e272011-07-15 02:09:41 +0000783 else
784 return -size;
Rafael Espindola89b93722010-12-10 07:39:47 +0000785}
786
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000787static unsigned getSizeForEncoding(MCStreamer &streamer,
788 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000789 MCContext &context = streamer.getContext();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000790 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000791 switch (format) {
Craig Topper85814382012-02-07 05:05:23 +0000792 default: llvm_unreachable("Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000793 case dwarf::DW_EH_PE_absptr:
794 case dwarf::DW_EH_PE_signed:
Evan Cheng1be0e272011-07-15 02:09:41 +0000795 return context.getAsmInfo().getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000796 case dwarf::DW_EH_PE_udata2:
797 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000798 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000799 case dwarf::DW_EH_PE_udata4:
800 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000801 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000802 case dwarf::DW_EH_PE_udata8:
803 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000804 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000805 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000806}
807
808static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000809 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000810 MCContext &context = streamer.getContext();
811 const MCAsmInfo &asmInfo = context.getAsmInfo();
812 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000813 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000814 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000815 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000816 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000817 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000818}
819
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000820static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
821 unsigned symbolEncoding) {
822 MCContext &context = streamer.getContext();
823 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000824 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
825 symbolEncoding,
826 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000827 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000828 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000829}
830
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000831static const MachineLocation TranslateMachineLocation(
Evan Cheng0e6a0522011-07-18 20:57:22 +0000832 const MCRegisterInfo &MRI,
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000833 const MachineLocation &Loc) {
834 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
835 MachineLocation::VirtualFP :
Evan Cheng0e6a0522011-07-18 20:57:22 +0000836 unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000837 const MachineLocation &NewLoc = Loc.isReg() ?
838 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
839 return NewLoc;
840}
841
Rafael Espindola25f492e2011-04-12 16:12:03 +0000842namespace {
843 class FrameEmitterImpl {
844 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000845 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000846 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000847 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000848 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000849 public:
Bill Wendling1424c3c2011-07-22 21:18:59 +0000850 FrameEmitterImpl(bool usingCFI, bool isEH)
851 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
852 SectionStart(0) {}
853
854 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola25f492e2011-04-12 16:12:03 +0000855
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000856 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
857 /// we're successful, return 'true'. Otherwise, return 'false' and it will
858 /// emit the normal CIE and FDE.
859 bool EmitCompactUnwind(MCStreamer &streamer,
860 const MCDwarfFrameInfo &frame);
861
Rafael Espindola25f492e2011-04-12 16:12:03 +0000862 const MCSymbol &EmitCIE(MCStreamer &streamer,
863 const MCSymbol *personality,
864 unsigned personalityEncoding,
865 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +0000866 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000867 unsigned lsdaEncoding);
868 MCSymbol *EmitFDE(MCStreamer &streamer,
869 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000870 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000871 void EmitCFIInstructions(MCStreamer &streamer,
872 const std::vector<MCCFIInstruction> &Instrs,
873 MCSymbol *BaseLabel);
874 void EmitCFIInstruction(MCStreamer &Streamer,
875 const MCCFIInstruction &Instr);
876 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000877
878} // end anonymous namespace
879
880static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
881 StringRef Prefix) {
882 if (Streamer.isVerboseAsm()) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000883 const char *EncStr;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000884 switch (Encoding) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000885 default: EncStr = "<unknown encoding>"; break;
886 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
887 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
888 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
889 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
890 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
891 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
892 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
893 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
894 EncStr = "pcrel udata4";
895 break;
896 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
897 EncStr = "pcrel sdata4";
898 break;
899 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
900 EncStr = "pcrel udata8";
901 break;
902 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
903 EncStr = "screl sdata8";
904 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000905 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
906 EncStr = "indirect pcrel udata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000907 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000908 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
909 EncStr = "indirect pcrel sdata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000910 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000911 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
912 EncStr = "indirect pcrel udata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000913 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000914 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
915 EncStr = "indirect pcrel sdata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000916 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000917 }
918
919 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
920 }
921
922 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000923}
924
925void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
926 const MCCFIInstruction &Instr) {
927 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000928 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000929
930 switch (Instr.getOperation()) {
931 case MCCFIInstruction::Move:
932 case MCCFIInstruction::RelMove: {
933 const MachineLocation &Dst = Instr.getDestination();
934 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000935 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000936
937 // If advancing cfa.
938 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000939 if (Src.getReg() == MachineLocation::VirtualFP) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000940 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000941 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
942 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000943 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000944 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000945 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
946 Twine(Src.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000947 Streamer.EmitULEB128IntValue(Src.getReg());
948 }
949
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000950 if (IsRelative)
951 CFAOffset += Src.getOffset();
952 else
953 CFAOffset = -Src.getOffset();
954
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000955 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000956 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000957 return;
958 }
959
960 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
961 assert(Dst.isReg() && "Machine move not supported yet.");
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000962 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000963 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000964 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000965 Streamer.EmitULEB128IntValue(Dst.getReg());
966 return;
967 }
968
969 unsigned Reg = Src.getReg();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000970 int Offset = Dst.getOffset();
971 if (IsRelative)
972 Offset -= CFAOffset;
973 Offset = Offset / dataAlignmentFactor;
974
975 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000976 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000977 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000978 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000979 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000980 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000981 Streamer.EmitSLEB128IntValue(Offset);
982 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +0000983 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
984 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000985 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000986 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000987 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000988 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000989 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000990 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000991 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000992 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000993 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000994 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000995 }
996 return;
997 }
Rafael Espindolac25680f2011-12-29 21:09:08 +0000998 case MCCFIInstruction::RememberState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000999 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001000 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1001 return;
Rafael Espindolac25680f2011-12-29 21:09:08 +00001002 case MCCFIInstruction::RestoreState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001003 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001004 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1005 return;
1006 case MCCFIInstruction::SameValue: {
1007 unsigned Reg = Instr.getDestination().getReg();
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001008 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001009 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001010 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +00001011 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001012 return;
1013 }
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00001014 case MCCFIInstruction::Restore: {
1015 unsigned Reg = Instr.getDestination().getReg();
1016 if (VerboseAsm) {
1017 Streamer.AddComment("DW_CFA_restore");
1018 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1019 }
1020 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1021 return;
1022 }
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001023 case MCCFIInstruction::Escape:
1024 if (VerboseAsm) Streamer.AddComment("Escape bytes");
1025 Streamer.EmitBytes(Instr.getValues(), 0);
1026 return;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001027 }
1028 llvm_unreachable("Unhandled case in switch");
1029}
1030
1031/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1032/// frame.
1033void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
1034 const std::vector<MCCFIInstruction> &Instrs,
1035 MCSymbol *BaseLabel) {
1036 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1037 const MCCFIInstruction &Instr = Instrs[i];
1038 MCSymbol *Label = Instr.getLabel();
1039 // Throw out move if the label is invalid.
1040 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1041
1042 // Advance row if new location.
1043 if (BaseLabel && Label) {
1044 MCSymbol *ThisSym = Label;
1045 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +00001046 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001047 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1048 BaseLabel = ThisSym;
1049 }
1050 }
1051
1052 EmitCFIInstruction(streamer, Instr);
1053 }
1054}
1055
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001056/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1057/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1058/// normal CIE and FDE.
1059bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1060 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001061 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001062 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001063 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001064
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001065 // range-start range-length compact-unwind-enc personality-func lsda
1066 // _foo LfooEnd-_foo 0x00000023 0 0
1067 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1068 //
1069 // .section __LD,__compact_unwind,regular,debug
1070 //
1071 // # compact unwind for _foo
1072 // .quad _foo
1073 // .set L1,LfooEnd-_foo
1074 // .long L1
1075 // .long 0x01010001
1076 // .quad 0
1077 // .quad 0
1078 //
1079 // # compact unwind for _bar
1080 // .quad _bar
1081 // .set L2,LbarEnd-_bar
1082 // .long L2
1083 // .long 0x01020011
1084 // .quad __gxx_personality
1085 // .quad except_tab1
1086
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001087 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001088 if (!Encoding) return false;
1089
Bill Wendling4bb5b032011-07-14 23:34:45 +00001090 // The encoding needs to know we have an LSDA.
1091 if (Frame.Lsda)
1092 Encoding |= 0x40000000;
1093
Evan Chenge76a33b2011-07-20 05:58:47 +00001094 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendling3c163cf2011-06-30 21:25:51 +00001095
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001096 // Range Start
Evan Cheng203576a2011-07-20 19:50:42 +00001097 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling9056e902011-06-29 23:49:12 +00001098 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001099 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +00001100 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001101
1102 // Range Length
1103 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1104 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001105 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +00001106 Streamer.EmitAbsValue(Range, 4);
1107
Bill Wendling9287a6e2011-06-30 00:30:52 +00001108 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +00001109 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer70be28a2011-11-07 21:00:59 +00001110 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1111 Twine::utohexstr(Encoding));
Bill Wendling9287a6e2011-06-30 00:30:52 +00001112 Streamer.EmitIntValue(Encoding, Size);
1113
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001114
Bill Wendling9056e902011-06-29 23:49:12 +00001115 // Personality Function
Bill Wendling4bb5b032011-07-14 23:34:45 +00001116 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001117 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling4498d392011-06-29 23:53:16 +00001118 if (Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +00001119 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001120 else
1121 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +00001122
1123 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +00001124 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001125 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling4498d392011-06-29 23:53:16 +00001126 if (Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +00001127 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001128 else
1129 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendling9056e902011-06-29 23:49:12 +00001130
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001131 return true;
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001132}
1133
Rafael Espindola25f492e2011-04-12 16:12:03 +00001134const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1135 const MCSymbol *personality,
1136 unsigned personalityEncoding,
1137 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001138 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001139 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +00001140 MCContext &context = streamer.getContext();
Evan Cheng0e6a0522011-07-18 20:57:22 +00001141 const MCRegisterInfo &MRI = context.getRegisterInfo();
Evan Chenge76a33b2011-07-20 05:58:47 +00001142 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001143 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +00001144
1145 MCSymbol *sectionStart;
Evan Chenge76a33b2011-07-20 05:58:47 +00001146 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +00001147 sectionStart = context.CreateTempSymbol();
1148 else
1149 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1150
Bill Wendling3c163cf2011-06-30 21:25:51 +00001151 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +00001152 CIENum++;
1153
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001154 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +00001155
1156 // Length
1157 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1158 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001159 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001160 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001161
1162 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001163 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +00001164 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001165 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001166
1167 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +00001168 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +00001169 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1170
1171 // Augmentation String
1172 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001173 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +00001174 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001175 Augmentation += "z";
1176 if (personality)
1177 Augmentation += "P";
1178 if (lsda)
1179 Augmentation += "L";
1180 Augmentation += "R";
Rafael Espindola16d7d432012-01-23 21:51:52 +00001181 if (IsSignalFrame)
1182 Augmentation += "S";
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001183 streamer.EmitBytes(Augmentation.str(), 0);
1184 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001185 streamer.EmitIntValue(0, 1);
1186
1187 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001188 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001189 streamer.EmitULEB128IntValue(1);
1190
1191 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001192 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001193 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1194
1195 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +00001196 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Evan Cheng0e6a0522011-07-18 20:57:22 +00001197 streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
Rafael Espindola89b93722010-12-10 07:39:47 +00001198
1199 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001200
1201 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001202 if (IsEH) {
1203 if (personality) {
1204 // Personality Encoding
1205 augmentationLength += 1;
1206 // Personality
1207 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1208 }
1209 if (lsda)
1210 augmentationLength += 1;
1211 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001212 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001213
Bill Wendling3c163cf2011-06-30 21:25:51 +00001214 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001215 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001216
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001217 // Augmentation Data (optional)
1218 if (personality) {
1219 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +00001220 EmitEncodingByte(streamer, personalityEncoding,
1221 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001222 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +00001223 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001224 EmitPersonality(streamer, *personality, personalityEncoding);
1225 }
Bill Wendling3c163cf2011-06-30 21:25:51 +00001226
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001227 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +00001228 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1229
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001230 // Encoding of the FDE pointers
Evan Cheng203576a2011-07-20 19:50:42 +00001231 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling3c163cf2011-06-30 21:25:51 +00001232 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +00001233 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001234
1235 // Initial Instructions
1236
Evan Cheng2d286172011-07-18 22:29:13 +00001237 const MCAsmInfo &MAI = context.getAsmInfo();
1238 const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +00001239 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +00001240
Rafael Espindolafe024d02010-12-28 18:36:23 +00001241 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001242 MCSymbol *Label = Moves[i].getLabel();
1243 const MachineLocation &Dst =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001244 TranslateMachineLocation(MRI, Moves[i].getDestination());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001245 const MachineLocation &Src =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001246 TranslateMachineLocation(MRI, Moves[i].getSource());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001247 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +00001248 Instructions.push_back(Inst);
1249 }
1250
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001251 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +00001252
1253 // Padding
Evan Cheng1be0e272011-07-15 02:09:41 +00001254 streamer.EmitValueToAlignment(IsEH
1255 ? 4 : context.getAsmInfo().getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +00001256
1257 streamer.EmitLabel(sectionEnd);
1258 return *sectionStart;
1259}
1260
Rafael Espindola25f492e2011-04-12 16:12:03 +00001261MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1262 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +00001263 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +00001264 MCContext &context = streamer.getContext();
1265 MCSymbol *fdeStart = context.CreateTempSymbol();
1266 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Chenge76a33b2011-07-20 05:58:47 +00001267 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling2541c412011-06-30 22:02:20 +00001268 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +00001269
Evan Cheng5fbe5e72011-08-24 22:31:37 +00001270 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendling2541c412011-06-30 22:02:20 +00001271 MCSymbol *EHSym =
1272 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +00001273 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +00001274 streamer.EmitLabel(EHSym);
1275 }
1276
Rafael Espindola89b93722010-12-10 07:39:47 +00001277 // Length
1278 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001279 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001280 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001281
1282 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001283
Rafael Espindola89b93722010-12-10 07:39:47 +00001284 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001285 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001286 if (IsEH) {
1287 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1288 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001289 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001290 streamer.EmitAbsValue(offset, 4);
Rafael Espindola2241e512012-06-22 13:24:07 +00001291 } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001292 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1293 cieStart, 0);
1294 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001295 } else {
1296 streamer.EmitSymbolValue(&cieStart, 4);
1297 }
Bill Wendling2541c412011-06-30 22:02:20 +00001298
Rafael Espindola89b93722010-12-10 07:39:47 +00001299 // PC Begin
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001300 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1301 : (unsigned)dwarf::DW_EH_PE_absptr;
1302 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
1303 EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +00001304
1305 // PC Range
1306 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1307 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001308 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001309 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola89b93722010-12-10 07:39:47 +00001310
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001311 if (IsEH) {
1312 // Augmentation Data Length
1313 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001314
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001315 if (frame.Lsda)
1316 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001317
Bill Wendling2541c412011-06-30 22:02:20 +00001318 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001319 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001320
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001321 // Augmentation Data
1322 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +00001323 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1324 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001325 }
Rafael Espindola4892dff2011-04-29 03:06:29 +00001326
Rafael Espindola89b93722010-12-10 07:39:47 +00001327 // Call Frame Instructions
1328
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001329 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +00001330
Rafael Espindola89b93722010-12-10 07:39:47 +00001331 // Padding
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001332 streamer.EmitValueToAlignment(PCSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001333
1334 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +00001335}
1336
Benjamin Kramer19282362010-12-30 22:34:44 +00001337namespace {
1338 struct CIEKey {
Rafael Espindola16d7d432012-01-23 21:51:52 +00001339 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1340 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindolabdc31672010-12-27 15:56:22 +00001341
Benjamin Kramer19282362010-12-30 22:34:44 +00001342 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001343 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1344 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1345 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer19282362010-12-30 22:34:44 +00001346 }
1347 const MCSymbol* Personality;
1348 unsigned PersonalityEncoding;
1349 unsigned LsdaEncoding;
Rafael Espindola16d7d432012-01-23 21:51:52 +00001350 bool IsSignalFrame;
Benjamin Kramer19282362010-12-30 22:34:44 +00001351 };
1352}
Rafael Espindolabdc31672010-12-27 15:56:22 +00001353
1354namespace llvm {
1355 template <>
1356 struct DenseMapInfo<CIEKey> {
1357 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001358 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001359 }
1360 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001361 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001362 }
1363 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer74849202012-04-11 14:06:39 +00001364 return static_cast<unsigned>(hash_combine(Key.Personality,
1365 Key.PersonalityEncoding,
1366 Key.LsdaEncoding,
1367 Key.IsSignalFrame));
Rafael Espindolabdc31672010-12-27 15:56:22 +00001368 }
1369 static bool isEqual(const CIEKey &LHS,
1370 const CIEKey &RHS) {
1371 return LHS.Personality == RHS.Personality &&
1372 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola16d7d432012-01-23 21:51:52 +00001373 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1374 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001375 }
1376 };
1377}
1378
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001379void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1380 bool UsingCFI,
1381 bool IsEH) {
1382 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001383 MCObjectFileInfo *MOFI =
1384 const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
Bill Wendling1424c3c2011-07-22 21:18:59 +00001385 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001386 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001387
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001388 // Emit the compact unwind info if available.
Bill Wendling7a13b722011-12-15 00:14:24 +00001389 if (IsEH && MOFI->getCompactUnwindSection())
Bill Wendling1424c3c2011-07-22 21:18:59 +00001390 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1391 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
Bill Wendlinga2ff3e22011-11-08 22:23:43 +00001392 if (Frame.CompactUnwindEncoding)
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001393 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001394 }
1395
Evan Chenge76a33b2011-07-20 05:58:47 +00001396 const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1397 *MOFI->getDwarfFrameSection();
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001398 Streamer.SwitchSection(&Section);
1399 MCSymbol *SectionStart = Context.CreateTempSymbol();
1400 Streamer.EmitLabel(SectionStart);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001401 Emitter.setSectionStart(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001402
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001403 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001404 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001405
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001406 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling1424c3c2011-07-22 21:18:59 +00001407 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1408 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001409 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001410 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001411 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1412 if (!CIEStart)
1413 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1414 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001415 Frame.IsSignalFrame,
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001416 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001417
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001418 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001419
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001420 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001421 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001422 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001423
Evan Cheng1be0e272011-07-15 02:09:41 +00001424 Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001425 if (FDEEnd)
1426 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001427}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001428
1429void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1430 uint64_t AddrDelta) {
1431 SmallString<256> Tmp;
1432 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001433 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001434 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1435}
1436
1437void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001438 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001439 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001440 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001441 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001442 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1443 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001444 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001445 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1446 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001447 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001448 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001449 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001450 OS << uint8_t( AddrDelta & 0xff);
1451 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001452 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001453 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001454 assert(isUInt<32>(AddrDelta));
1455 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001456 OS << uint8_t( AddrDelta & 0xff);
1457 OS << uint8_t((AddrDelta >> 8) & 0xff);
1458 OS << uint8_t((AddrDelta >> 16) & 0xff);
1459 OS << uint8_t((AddrDelta >> 24) & 0xff);
1460
Rafael Espindola245a1e22010-12-28 05:39:27 +00001461 }
1462}