blob: 1dff71a495d9be448d8a73a97e0f5ac5e1439de3 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000011#include "llvm/ADT/Hashing.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/Twine.h"
14#include "llvm/Config/config.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/MC/MCExpr.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000018#include "llvm/MC/MCObjectFileInfo.h"
19#include "llvm/MC/MCObjectWriter.h"
20#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000021#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000022#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000023#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000024#include "llvm/Support/ErrorHandling.h"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000025#include "llvm/Support/LEB128.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000026#include "llvm/Support/Path.h"
27#include "llvm/Support/SourceMgr.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Support/raw_ostream.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.
Manman Ren43213cf2013-02-05 21:52:47 +0000104 LineSection->addLineEntry(LineEntry,
105 MCOS->getContext().getDwarfCompileUnitID());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000106}
107
108//
109// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000110//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000111static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
112 const MCSymbol &Start,
113 const MCSymbol &End,
114 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000115 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
116 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000117 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000118 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000119 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000120 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000121 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000122 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000123 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000124 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000125 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000126 return Res3;
127}
128
Kevin Enderbyc0957932010-09-30 16:52:03 +0000129//
130// This emits the Dwarf line table for the specified section from the entries
131// in the LineSection.
132//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000133static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000134 const MCSection *Section,
Manman Ren43213cf2013-02-05 21:52:47 +0000135 const MCLineSection *LineSection,
136 unsigned CUID) {
137 // This LineSection does not contain any LineEntry for the given Compile Unit.
138 if (!LineSection->containEntriesForID(CUID))
139 return;
140
Kevin Enderbyc0957932010-09-30 16:52:03 +0000141 unsigned FileNum = 1;
142 unsigned LastLine = 1;
143 unsigned Column = 0;
144 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
145 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000146 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000147
148 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000149 for (MCLineSection::const_iterator
Manman Ren43213cf2013-02-05 21:52:47 +0000150 it = LineSection->getMCLineEntries(CUID).begin(),
151 ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000152
153 if (FileNum != it->getFileNum()) {
154 FileNum = it->getFileNum();
155 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000156 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000157 }
158 if (Column != it->getColumn()) {
159 Column = it->getColumn();
160 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000161 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000162 }
163 if (Isa != it->getIsa()) {
164 Isa = it->getIsa();
165 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000166 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000167 }
168 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
169 Flags = it->getFlags();
170 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
171 }
172 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
173 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
174 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
175 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
176 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
177 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
178
Rafael Espindola64185cc2010-11-13 01:06:27 +0000179 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000180 MCSymbol *Label = it->getLabel();
181
182 // At this point we want to emit/create the sequence to encode the delta in
183 // line numbers and the increment of the address from the previous Label
184 // and the current Label.
Evan Cheng1be0e272011-07-15 02:09:41 +0000185 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000186 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
187 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000188
189 LastLine = it->getLine();
190 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000191 }
192
193 // Emit a DW_LNE_end_sequence for the end of the section.
194 // Using the pointer Section create a temporary label at the end of the
195 // section and use that and the LastLabel to compute the address delta
196 // and use INT64_MAX as the line delta which is the signal that this is
197 // actually a DW_LNE_end_sequence.
198
199 // Switch to the section to be able to create a symbol at its end.
Peter Collingbournedf39be62013-04-17 21:18:16 +0000200 // TODO: keep track of the last subsection so that this symbol appears in the
201 // correct place.
Kevin Enderbyc0957932010-09-30 16:52:03 +0000202 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000203
204 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000205 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000206 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000207 // Set the value of the symbol, as we are at the end of the section.
208 MCOS->EmitLabel(SectionEnd);
209
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000210 // Switch back the dwarf line section.
Evan Chenge76a33b2011-07-20 05:58:47 +0000211 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000212
Evan Cheng1be0e272011-07-15 02:09:41 +0000213 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000214 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
215 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000216}
217
218//
219// This emits the Dwarf file and the line tables.
220//
Rafael Espindola489d6792012-02-28 21:13:05 +0000221const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000222 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000223 // Switch to the section where the table will be emitted into.
Evan Chenge76a33b2011-07-20 05:58:47 +0000224 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000225
Manman Ren43213cf2013-02-05 21:52:47 +0000226 const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
227 MCOS->getContext().getMCLineTableSymbols();
228 // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
229 // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
230 // Handle Compile Unit 0, the line table start symbol is the section symbol.
231 const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
232 // Handle the rest of the Compile Units.
233 for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
234 EmitCU(MCOS, Is);
235
236 // Now delete the MCLineSections that were created in MCLineEntry::Make()
237 // and used to emit the line table.
238 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
239 MCOS->getContext().getMCLineSections();
240 for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
241 MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
242 ++it)
243 delete it->second;
244
245 return LineStartSym;
246}
247
248const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
249 MCContext &context = MCOS->getContext();
250
251 // Create a symbol at the beginning of the line table.
252 MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
253 if (!LineStartSym)
254 LineStartSym = context.CreateTempSymbol();
255 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbyc0957932010-09-30 16:52:03 +0000256 MCOS->EmitLabel(LineStartSym);
257
258 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000259 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000260
261 // The first 4 bytes is the total length of the information for this
262 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000263 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000264 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000265
266 // Next 2 bytes is the Version, which is Dwarf 2.
267 MCOS->EmitIntValue(2, 2);
268
269 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000270 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000271
272 // Length of the prologue, is the next 4 bytes. Which is the start of the
273 // section to the end of the prologue. Not including the 4 bytes for the
274 // total length, the 2 bytes for the version, and these 4 bytes for the
275 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000276 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Eric Christophere0501e82013-02-07 21:19:56 +0000277 (4 + 2 + 4)), 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000278
279 // Parameters of the state machine, are next.
280 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
281 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
282 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
283 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
284 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
285
286 // Standard opcode lengths
287 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
288 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
289 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
290 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
291 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
292 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
293 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
294 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
295 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
296 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
297 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
298 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
299
300 // Put out the directory and file tables.
301
302 // First the directory table.
Manman Ren9e999ad2013-03-12 20:17:00 +0000303 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Manman Ren3de61b42013-03-07 01:42:00 +0000304 context.getMCDwarfDirs(CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000305 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christopher68ca5622013-01-09 01:57:54 +0000306 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
307 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbyc0957932010-09-30 16:52:03 +0000308 }
309 MCOS->EmitIntValue(0, 1); // Terminate the directory list
310
311 // Second the file table.
Manman Ren9e999ad2013-03-12 20:17:00 +0000312 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Manman Ren3de61b42013-03-07 01:42:00 +0000313 MCOS->getContext().getMCDwarfFiles(CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000314 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Eric Christopher68ca5622013-01-09 01:57:54 +0000315 MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
316 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000317 // the Directory num
318 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000319 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
320 MCOS->EmitIntValue(0, 1); // filesize (always 0)
321 }
322 MCOS->EmitIntValue(0, 1); // Terminate the file list
323
324 // This is the end of the prologue, so set the value of the symbol at the
325 // end of the prologue (that was used in a previous expression).
326 MCOS->EmitLabel(ProEndSym);
327
328 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000329 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000330 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000331 const std::vector<const MCSection *> &MCLineSectionOrder =
332 MCOS->getContext().getMCLineSectionOrder();
333 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc3882162011-06-30 23:59:38 +0000334 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000335 ++it) {
336 const MCSection *Sec = *it;
337 const MCLineSection *Line = MCLineSections.lookup(Sec);
Manman Ren43213cf2013-02-05 21:52:47 +0000338 EmitDwarfLineTable(MCOS, Sec, Line, CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000339 }
340
Rafael Espindola767b1be2010-12-04 00:31:13 +0000341 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
342 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000343 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000344 // it requires:
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000345 // total_length >= prologue_length + 10
346 // We are 4 bytes short, since we have total_length = 51 and
347 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000348
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000349 // The regular end_sequence should be sufficient.
350 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000351 }
352
Kevin Enderbyc0957932010-09-30 16:52:03 +0000353 // This is the end of the section, so set the value of the symbol at the end
354 // of this section (that was used in a previous expression).
355 MCOS->EmitLabel(LineEndSym);
Rafael Espindola489d6792012-02-28 21:13:05 +0000356
357 return LineStartSym;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000358}
359
Kevin Enderbyc0957932010-09-30 16:52:03 +0000360/// Utility function to write the encoding to an object writer.
361void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
362 uint64_t AddrDelta) {
363 SmallString<256> Tmp;
364 raw_svector_ostream OS(Tmp);
365 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
366 OW->WriteBytes(OS.str());
367}
368
369/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000370void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000371 uint64_t AddrDelta) {
372 SmallString<256> Tmp;
373 raw_svector_ostream OS(Tmp);
374 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
Eric Christopher68ca5622013-01-09 01:57:54 +0000375 MCOS->EmitBytes(OS.str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000376}
377
378/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
379void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
380 raw_ostream &OS) {
381 uint64_t Temp, Opcode;
382 bool NeedCopy = false;
383
384 // Scale the address delta by the minimum instruction length.
385 AddrDelta = ScaleAddrDelta(AddrDelta);
386
387 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000388 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbyc0957932010-09-30 16:52:03 +0000389 // end_sequence to emit the matrix entry.
390 if (LineDelta == INT64_MAX) {
391 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
392 OS << char(dwarf::DW_LNS_const_add_pc);
393 else {
394 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000395 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000396 }
397 OS << char(dwarf::DW_LNS_extended_op);
398 OS << char(1);
399 OS << char(dwarf::DW_LNE_end_sequence);
400 return;
401 }
402
403 // Bias the line delta by the base.
404 Temp = LineDelta - DWARF2_LINE_BASE;
405
406 // If the line increment is out of range of a special opcode, we must encode
407 // it with DW_LNS_advance_line.
408 if (Temp >= DWARF2_LINE_RANGE) {
409 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000410 encodeSLEB128(LineDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000411
412 LineDelta = 0;
413 Temp = 0 - DWARF2_LINE_BASE;
414 NeedCopy = true;
415 }
416
417 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
418 if (LineDelta == 0 && AddrDelta == 0) {
419 OS << char(dwarf::DW_LNS_copy);
420 return;
421 }
422
423 // Bias the opcode by the special opcode base.
424 Temp += DWARF2_LINE_OPCODE_BASE;
425
426 // Avoid overflow when addr_delta is large.
427 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
428 // Try using a special opcode.
429 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
430 if (Opcode <= 255) {
431 OS << char(Opcode);
432 return;
433 }
434
435 // Try using DW_LNS_const_add_pc followed by special op.
436 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
437 if (Opcode <= 255) {
438 OS << char(dwarf::DW_LNS_const_add_pc);
439 OS << char(Opcode);
440 return;
441 }
442 }
443
444 // Otherwise use DW_LNS_advance_pc.
445 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000446 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000447
448 if (NeedCopy)
449 OS << char(dwarf::DW_LNS_copy);
450 else
451 OS << char(Temp);
452}
453
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000454void MCDwarfFile::print(raw_ostream &OS) const {
455 OS << '"' << getName() << '"';
456}
457
Manman Ren286c4dc2012-09-12 05:06:18 +0000458#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000459void MCDwarfFile::dump() const {
460 print(dbgs());
461}
Manman Rencc77eec2012-09-06 19:55:56 +0000462#endif
Kevin Enderbyc0957932010-09-30 16:52:03 +0000463
Kevin Enderby94c2e852011-12-09 18:09:40 +0000464// Utility function to write a tuple for .debug_abbrev.
465static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
466 MCOS->EmitULEB128IntValue(Name);
467 MCOS->EmitULEB128IntValue(Form);
468}
469
470// When generating dwarf for assembly source files this emits
471// the data for .debug_abbrev section which contains three DIEs.
472static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
473 MCContext &context = MCOS->getContext();
474 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
475
476 // DW_TAG_compile_unit DIE abbrev (1).
477 MCOS->EmitULEB128IntValue(1);
478 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
479 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
480 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
481 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
482 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
483 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
484 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
485 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
486 if (!DwarfDebugFlags.empty())
487 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
488 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
489 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
490 EmitAbbrev(MCOS, 0, 0);
491
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000492 // DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000493 MCOS->EmitULEB128IntValue(2);
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000494 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000495 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
496 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
497 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
498 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
499 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000500 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
501 EmitAbbrev(MCOS, 0, 0);
502
503 // DW_TAG_unspecified_parameters DIE abbrev (3).
504 MCOS->EmitULEB128IntValue(3);
505 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
506 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
507 EmitAbbrev(MCOS, 0, 0);
508
509 // Terminate the abbreviations for this compilation unit.
510 MCOS->EmitIntValue(0, 1);
511}
512
513// When generating dwarf for assembly source files this emits the data for
514// .debug_aranges section. Which contains a header and a table of pairs of
515// PointerSize'ed values for the address and size of section(s) with line table
516// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000517static void EmitGenDwarfAranges(MCStreamer *MCOS,
518 const MCSymbol *InfoSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000519 MCContext &context = MCOS->getContext();
520
521 // Create a symbol at the end of the section that we are creating the dwarf
522 // debugging info to use later in here as part of the expression to calculate
523 // the size of the section for the table.
524 MCOS->SwitchSection(context.getGenDwarfSection());
525 MCSymbol *SectionEndSym = context.CreateTempSymbol();
526 MCOS->EmitLabel(SectionEndSym);
527 context.setGenDwarfSectionEndSym(SectionEndSym);
528
529 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
530
531 // This will be the length of the .debug_aranges section, first account for
532 // the size of each item in the header (see below where we emit these items).
533 int Length = 4 + 2 + 4 + 1 + 1;
534
535 // Figure the padding after the header before the table of address and size
536 // pairs who's values are PointerSize'ed.
537 const MCAsmInfo &asmInfo = context.getAsmInfo();
538 int AddrSize = asmInfo.getPointerSize();
539 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
540 if (Pad == 2 * AddrSize)
541 Pad = 0;
542 Length += Pad;
543
544 // Add the size of the pair of PointerSize'ed values for the address and size
545 // of the one default .text section we have in the table.
546 Length += 2 * AddrSize;
547 // And the pair of terminating zeros.
548 Length += 2 * AddrSize;
549
550
551 // Emit the header for this section.
552 // The 4 byte length not including the 4 byte value for the length.
553 MCOS->EmitIntValue(Length - 4, 4);
554 // The 2 byte version, which is 2.
555 MCOS->EmitIntValue(2, 2);
556 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000557 // of the .debug_info.
558 if (InfoSectionSymbol)
559 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
560 else
561 MCOS->EmitIntValue(0, 4);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000562 // The 1 byte size of an address.
563 MCOS->EmitIntValue(AddrSize, 1);
564 // The 1 byte size of a segment descriptor, we use a value of zero.
565 MCOS->EmitIntValue(0, 1);
566 // Align the header with the padding if needed, before we put out the table.
567 for(int i = 0; i < Pad; i++)
568 MCOS->EmitIntValue(0, 1);
569
570 // Now emit the table of pairs of PointerSize'ed values for the section(s)
571 // address and size, in our case just the one default .text section.
572 const MCExpr *Addr = MCSymbolRefExpr::Create(
573 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
574 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
575 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
576 MCOS->EmitAbsValue(Addr, AddrSize);
577 MCOS->EmitAbsValue(Size, AddrSize);
578
579 // And finally the pair of terminating zeros.
580 MCOS->EmitIntValue(0, AddrSize);
581 MCOS->EmitIntValue(0, AddrSize);
582}
583
584// When generating dwarf for assembly source files this emits the data for
585// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000586// DIE and a list of label DIEs.
Rafael Espindola489d6792012-02-28 21:13:05 +0000587static void EmitGenDwarfInfo(MCStreamer *MCOS,
588 const MCSymbol *AbbrevSectionSymbol,
589 const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000590 MCContext &context = MCOS->getContext();
591
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000592 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000593
594 // Create a symbol at the start and end of this section used in here for the
595 // expression to calculate the length in the header.
596 MCSymbol *InfoStart = context.CreateTempSymbol();
597 MCOS->EmitLabel(InfoStart);
598 MCSymbol *InfoEnd = context.CreateTempSymbol();
599
600 // First part: the header.
601
602 // The 4 byte total length of the information for this compilation unit, not
603 // including these 4 bytes.
604 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
605 MCOS->EmitAbsValue(Length, 4);
606
607 // The 2 byte DWARF version, which is 2.
608 MCOS->EmitIntValue(2, 2);
609
610 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
611 // it is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000612 if (AbbrevSectionSymbol) {
613 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
614 } else {
615 MCOS->EmitIntValue(0, 4);
616 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000617
618 const MCAsmInfo &asmInfo = context.getAsmInfo();
619 int AddrSize = asmInfo.getPointerSize();
620 // The 1 byte size of an address.
621 MCOS->EmitIntValue(AddrSize, 1);
622
623 // Second part: the compile_unit DIE.
624
625 // The DW_TAG_compile_unit DIE abbrev (1).
626 MCOS->EmitULEB128IntValue(1);
627
628 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
629 // which is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000630 if (LineSectionSymbol) {
631 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
632 } else {
633 MCOS->EmitIntValue(0, 4);
634 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000635
636 // AT_low_pc, the first address of the default .text section.
637 const MCExpr *Start = MCSymbolRefExpr::Create(
638 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
639 MCOS->EmitAbsValue(Start, AddrSize);
640
641 // AT_high_pc, the last address of the default .text section.
642 const MCExpr *End = MCSymbolRefExpr::Create(
643 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
644 MCOS->EmitAbsValue(End, AddrSize);
645
646 // AT_name, the name of the source file. Reconstruct from the first directory
647 // and file table entries.
Manman Ren9e999ad2013-03-12 20:17:00 +0000648 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000649 context.getMCDwarfDirs();
650 if (MCDwarfDirs.size() > 0) {
Eric Christopher68ca5622013-01-09 01:57:54 +0000651 MCOS->EmitBytes(MCDwarfDirs[0]);
652 MCOS->EmitBytes("/");
Kevin Enderby94c2e852011-12-09 18:09:40 +0000653 }
Manman Ren9e999ad2013-03-12 20:17:00 +0000654 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000655 MCOS->getContext().getMCDwarfFiles();
Eric Christopher68ca5622013-01-09 01:57:54 +0000656 MCOS->EmitBytes(MCDwarfFiles[1]->getName());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000657 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
658
659 // AT_comp_dir, the working directory the assembly was done in.
Eric Christopher68ca5622013-01-09 01:57:54 +0000660 MCOS->EmitBytes(context.getCompilationDir());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000661 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
662
663 // AT_APPLE_flags, the command line arguments of the assembler tool.
664 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
665 if (!DwarfDebugFlags.empty()){
Eric Christopher68ca5622013-01-09 01:57:54 +0000666 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000667 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
668 }
669
670 // AT_producer, the version of the assembler tool.
Kevin Enderby75c9b932013-01-16 17:46:23 +0000671 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
672 if (!DwarfDebugProducer.empty()){
673 MCOS->EmitBytes(DwarfDebugProducer);
674 }
675 else {
676 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
677 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
678 MCOS->EmitBytes(StringRef(")"));
679 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000680 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
681
682 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
683 // draft has no standard code for assembler.
684 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
685
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000686 // Third part: the list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000687
Kevin Enderby11c2def2012-01-10 21:12:34 +0000688 // Loop on saved info for dwarf labels and create the DIEs for them.
689 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
690 MCOS->getContext().getMCGenDwarfLabelEntries();
691 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000692 Entries.begin(), ie = Entries.end(); it != ie;
693 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000694 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000695
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000696 // The DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000697 MCOS->EmitULEB128IntValue(2);
698
699 // AT_name, of the label without any leading underbar.
Eric Christopher68ca5622013-01-09 01:57:54 +0000700 MCOS->EmitBytes(Entry->getName());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000701 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
702
703 // AT_decl_file, index into the file table.
704 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
705
706 // AT_decl_line, source line number.
707 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
708
709 // AT_low_pc, start address of the label.
710 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
711 MCSymbolRefExpr::VK_None, context);
712 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
713
Kevin Enderby94c2e852011-12-09 18:09:40 +0000714 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
715 MCOS->EmitIntValue(0, 1);
716
717 // The DW_TAG_unspecified_parameters DIE abbrev (3).
718 MCOS->EmitULEB128IntValue(3);
719
720 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
721 MCOS->EmitIntValue(0, 1);
722 }
Kevin Enderby11c2def2012-01-10 21:12:34 +0000723 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
724 // for the dwarf labels.
725 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000726 Entries.begin(), ie = Entries.end(); it != ie;
727 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000728 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000729 delete Entry;
730 }
731
732 // Add the NULL DIE terminating the Compile Unit DIE's.
733 MCOS->EmitIntValue(0, 1);
734
735 // Now set the value of the symbol at the end of the info section.
736 MCOS->EmitLabel(InfoEnd);
737}
738
739//
740// When generating dwarf for assembly source files this emits the Dwarf
741// sections.
742//
Rafael Espindola489d6792012-02-28 21:13:05 +0000743void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000744 // Create the dwarf sections in this order (.debug_line already created).
745 MCContext &context = MCOS->getContext();
Rafael Espindola489d6792012-02-28 21:13:05 +0000746 const MCAsmInfo &AsmInfo = context.getAsmInfo();
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000747 bool CreateDwarfSectionSymbols =
748 AsmInfo.doesDwarfUseRelocationsAcrossSections();
749 if (!CreateDwarfSectionSymbols)
750 LineSectionSymbol = NULL;
751 MCSymbol *AbbrevSectionSymbol = NULL;
752 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000753 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000754 if (CreateDwarfSectionSymbols) {
755 InfoSectionSymbol = context.CreateTempSymbol();
756 MCOS->EmitLabel(InfoSectionSymbol);
757 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000758 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000759 if (CreateDwarfSectionSymbols) {
Rafael Espindola489d6792012-02-28 21:13:05 +0000760 AbbrevSectionSymbol = context.CreateTempSymbol();
761 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindola489d6792012-02-28 21:13:05 +0000762 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000763 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
764
765 // If there are no line table entries then do not emit any section contents.
766 if (context.getMCLineSections().empty())
767 return;
768
769 // Output the data for .debug_aranges section.
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000770 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000771
772 // Output the data for .debug_abbrev section.
773 EmitGenDwarfAbbrev(MCOS);
774
775 // Output the data for .debug_info section.
Rafael Espindola489d6792012-02-28 21:13:05 +0000776 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000777}
778
779//
780// When generating dwarf for assembly source files this is called when symbol
781// for a label is created. If this symbol is not a temporary and is in the
782// section that dwarf is being generated for, save the needed info to create
Kevin Enderby11c2def2012-01-10 21:12:34 +0000783// a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000784//
Kevin Enderby11c2def2012-01-10 21:12:34 +0000785void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderby94c2e852011-12-09 18:09:40 +0000786 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher3c3cdcc2012-02-25 01:02:44 +0000787 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderby94c2e852011-12-09 18:09:40 +0000788 // the default text.
789 if (Symbol->isTemporary())
790 return;
791 MCContext &context = MCOS->getContext();
Peter Collingbournedf39be62013-04-17 21:18:16 +0000792 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderby94c2e852011-12-09 18:09:40 +0000793 return;
794
Kevin Enderby11c2def2012-01-10 21:12:34 +0000795 // The dwarf label's name does not have the symbol name's leading
Kevin Enderby94c2e852011-12-09 18:09:40 +0000796 // underbar if any.
797 StringRef Name = Symbol->getName();
798 if (Name.startswith("_"))
799 Name = Name.substr(1, Name.size()-1);
800
Kevin Enderby11c2def2012-01-10 21:12:34 +0000801 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000802 unsigned FileNumber = context.getGenDwarfFileNumber();
803
804 // Finding the line number is the expensive part which is why we just don't
Kevin Enderby11c2def2012-01-10 21:12:34 +0000805 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000806 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
807 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
808
809 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
810 // values so that they don't have things like an ARM thumb bit from the
811 // original symbol. So when used they won't get a low bit set after
812 // relocation.
813 MCSymbol *Label = context.CreateTempSymbol();
814 MCOS->EmitLabel(Label);
815
816 // Create and entry for the info and add it to the other entries.
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000817 MCGenDwarfLabelEntry *Entry =
Kevin Enderby11c2def2012-01-10 21:12:34 +0000818 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
819 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000820}
821
Rafael Espindola89b93722010-12-10 07:39:47 +0000822static int getDataAlignmentFactor(MCStreamer &streamer) {
823 MCContext &context = streamer.getContext();
Evan Cheng1be0e272011-07-15 02:09:41 +0000824 const MCAsmInfo &asmInfo = context.getAsmInfo();
Eli Bendersky9dd2a3b2013-01-22 18:02:49 +0000825 int size = asmInfo.getCalleeSaveStackSlotSize();
Evan Cheng1be0e272011-07-15 02:09:41 +0000826 if (asmInfo.isStackGrowthDirectionUp())
Rafael Espindola89b93722010-12-10 07:39:47 +0000827 return size;
Evan Cheng1be0e272011-07-15 02:09:41 +0000828 else
829 return -size;
Rafael Espindola89b93722010-12-10 07:39:47 +0000830}
831
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000832static unsigned getSizeForEncoding(MCStreamer &streamer,
833 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000834 MCContext &context = streamer.getContext();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000835 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000836 switch (format) {
Craig Topper85814382012-02-07 05:05:23 +0000837 default: llvm_unreachable("Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000838 case dwarf::DW_EH_PE_absptr:
839 case dwarf::DW_EH_PE_signed:
Evan Cheng1be0e272011-07-15 02:09:41 +0000840 return context.getAsmInfo().getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000841 case dwarf::DW_EH_PE_udata2:
842 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000843 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000844 case dwarf::DW_EH_PE_udata4:
845 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000846 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000847 case dwarf::DW_EH_PE_udata8:
848 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000849 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000850 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000851}
852
853static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000854 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000855 MCContext &context = streamer.getContext();
856 const MCAsmInfo &asmInfo = context.getAsmInfo();
857 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000858 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000859 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000860 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000861 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000862 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000863}
864
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000865static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
866 unsigned symbolEncoding) {
867 MCContext &context = streamer.getContext();
868 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000869 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
870 symbolEncoding,
871 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000872 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000873 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000874}
875
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000876static const MachineLocation TranslateMachineLocation(
Evan Cheng0e6a0522011-07-18 20:57:22 +0000877 const MCRegisterInfo &MRI,
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000878 const MachineLocation &Loc) {
879 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
880 MachineLocation::VirtualFP :
Evan Cheng0e6a0522011-07-18 20:57:22 +0000881 unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000882 const MachineLocation &NewLoc = Loc.isReg() ?
883 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
884 return NewLoc;
885}
886
Rafael Espindola25f492e2011-04-12 16:12:03 +0000887namespace {
888 class FrameEmitterImpl {
889 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000890 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000891 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000892 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000893 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000894 public:
Bill Wendling1424c3c2011-07-22 21:18:59 +0000895 FrameEmitterImpl(bool usingCFI, bool isEH)
896 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
897 SectionStart(0) {}
898
899 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola25f492e2011-04-12 16:12:03 +0000900
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000901 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
902 /// we're successful, return 'true'. Otherwise, return 'false' and it will
903 /// emit the normal CIE and FDE.
Bill Wendling3c08eef2013-04-10 22:03:59 +0000904 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000905 const MCDwarfFrameInfo &frame);
906
Rafael Espindola25f492e2011-04-12 16:12:03 +0000907 const MCSymbol &EmitCIE(MCStreamer &streamer,
908 const MCSymbol *personality,
909 unsigned personalityEncoding,
910 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +0000911 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000912 unsigned lsdaEncoding);
913 MCSymbol *EmitFDE(MCStreamer &streamer,
914 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000915 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000916 void EmitCFIInstructions(MCStreamer &streamer,
917 const std::vector<MCCFIInstruction> &Instrs,
918 MCSymbol *BaseLabel);
919 void EmitCFIInstruction(MCStreamer &Streamer,
920 const MCCFIInstruction &Instr);
921 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000922
923} // end anonymous namespace
924
925static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
926 StringRef Prefix) {
927 if (Streamer.isVerboseAsm()) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000928 const char *EncStr;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000929 switch (Encoding) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000930 default: EncStr = "<unknown encoding>"; break;
931 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
932 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
933 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
934 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
935 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
936 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
937 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
938 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
939 EncStr = "pcrel udata4";
940 break;
941 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
942 EncStr = "pcrel sdata4";
943 break;
944 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
945 EncStr = "pcrel udata8";
946 break;
947 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
948 EncStr = "screl sdata8";
949 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000950 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
951 EncStr = "indirect pcrel udata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000952 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000953 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
954 EncStr = "indirect pcrel sdata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000955 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000956 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
957 EncStr = "indirect pcrel udata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000958 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000959 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
960 EncStr = "indirect pcrel sdata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000961 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000962 }
963
964 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
965 }
966
967 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000968}
969
970void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
971 const MCCFIInstruction &Instr) {
972 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000973 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000974
975 switch (Instr.getOperation()) {
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000976 case MCCFIInstruction::OpRegister: {
977 unsigned Reg1 = Instr.getRegister();
978 unsigned Reg2 = Instr.getRegister2();
979 if (VerboseAsm) {
980 Streamer.AddComment("DW_CFA_register");
981 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
982 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
983 }
984 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
985 Streamer.EmitULEB128IntValue(Reg1);
986 Streamer.EmitULEB128IntValue(Reg2);
987 return;
988 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000989 case MCCFIInstruction::OpUndefined: {
Rafael Espindolaff233c92012-11-24 04:33:48 +0000990 unsigned Reg = Instr.getRegister();
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000991 if (VerboseAsm) {
992 Streamer.AddComment("DW_CFA_undefined");
993 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
994 }
995 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
996 Streamer.EmitULEB128IntValue(Reg);
997 return;
998 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000999 case MCCFIInstruction::OpAdjustCfaOffset:
1000 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001001 const bool IsRelative =
1002 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1003
1004 if (VerboseAsm)
1005 Streamer.AddComment("DW_CFA_def_cfa_offset");
1006 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1007
1008 if (IsRelative)
Rafael Espindolaff233c92012-11-24 04:33:48 +00001009 CFAOffset += Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001010 else
Rafael Espindolaff233c92012-11-24 04:33:48 +00001011 CFAOffset = -Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001012
1013 if (VerboseAsm)
1014 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1015 Streamer.EmitULEB128IntValue(CFAOffset);
1016
1017 return;
1018 }
1019 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001020 if (VerboseAsm)
1021 Streamer.AddComment("DW_CFA_def_cfa");
1022 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1023
1024 if (VerboseAsm)
Rafael Espindolaff233c92012-11-24 04:33:48 +00001025 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1026 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001027
Rafael Espindolaff233c92012-11-24 04:33:48 +00001028 CFAOffset = -Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001029
1030 if (VerboseAsm)
1031 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1032 Streamer.EmitULEB128IntValue(CFAOffset);
1033
1034 return;
1035 }
1036
1037 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001038 if (VerboseAsm)
1039 Streamer.AddComment("DW_CFA_def_cfa_register");
1040 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1041
1042 if (VerboseAsm)
Rafael Espindolaff233c92012-11-24 04:33:48 +00001043 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1044 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001045
1046 return;
1047 }
1048
1049 case MCCFIInstruction::OpOffset:
1050 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001051 const bool IsRelative =
1052 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001053
Rafael Espindolaff233c92012-11-24 04:33:48 +00001054 unsigned Reg = Instr.getRegister();
1055 int Offset = Instr.getOffset();
Rafael Espindola25f492e2011-04-12 16:12:03 +00001056 if (IsRelative)
1057 Offset -= CFAOffset;
1058 Offset = Offset / dataAlignmentFactor;
1059
1060 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001061 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001062 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001063 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +00001064 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001065 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +00001066 Streamer.EmitSLEB128IntValue(Offset);
1067 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +00001068 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1069 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001071 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001072 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001073 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001074 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001075 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001076 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001077 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001078 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001079 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001080 }
1081 return;
1082 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001083 case MCCFIInstruction::OpRememberState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001084 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001085 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1086 return;
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001087 case MCCFIInstruction::OpRestoreState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001088 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001089 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1090 return;
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001091 case MCCFIInstruction::OpSameValue: {
Rafael Espindolaff233c92012-11-24 04:33:48 +00001092 unsigned Reg = Instr.getRegister();
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001093 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001094 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001095 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +00001096 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001097 return;
1098 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001099 case MCCFIInstruction::OpRestore: {
Rafael Espindolaff233c92012-11-24 04:33:48 +00001100 unsigned Reg = Instr.getRegister();
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00001101 if (VerboseAsm) {
1102 Streamer.AddComment("DW_CFA_restore");
1103 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1104 }
1105 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1106 return;
1107 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001108 case MCCFIInstruction::OpEscape:
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001109 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christopher68ca5622013-01-09 01:57:54 +00001110 Streamer.EmitBytes(Instr.getValues());
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001111 return;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001112 }
1113 llvm_unreachable("Unhandled case in switch");
1114}
1115
1116/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1117/// frame.
1118void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
1119 const std::vector<MCCFIInstruction> &Instrs,
1120 MCSymbol *BaseLabel) {
1121 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1122 const MCCFIInstruction &Instr = Instrs[i];
1123 MCSymbol *Label = Instr.getLabel();
1124 // Throw out move if the label is invalid.
1125 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1126
1127 // Advance row if new location.
1128 if (BaseLabel && Label) {
1129 MCSymbol *ThisSym = Label;
1130 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +00001131 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001132 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1133 BaseLabel = ThisSym;
1134 }
1135 }
1136
1137 EmitCFIInstruction(streamer, Instr);
1138 }
1139}
1140
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001141/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1142/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1143/// normal CIE and FDE.
Bill Wendling3c08eef2013-04-10 22:03:59 +00001144void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001145 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001146 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001147 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001148 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001149
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001150 // range-start range-length compact-unwind-enc personality-func lsda
1151 // _foo LfooEnd-_foo 0x00000023 0 0
1152 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1153 //
1154 // .section __LD,__compact_unwind,regular,debug
1155 //
1156 // # compact unwind for _foo
1157 // .quad _foo
1158 // .set L1,LfooEnd-_foo
1159 // .long L1
1160 // .long 0x01010001
1161 // .quad 0
1162 // .quad 0
1163 //
1164 // # compact unwind for _bar
1165 // .quad _bar
1166 // .set L2,LbarEnd-_bar
1167 // .long L2
1168 // .long 0x01020011
1169 // .quad __gxx_personality
1170 // .quad except_tab1
1171
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001172 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling3c08eef2013-04-10 22:03:59 +00001173 if (!Encoding) return;
1174
Bill Wendling62c75ad2013-04-10 21:42:06 +00001175 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001176
Bill Wendling4bb5b032011-07-14 23:34:45 +00001177 // The encoding needs to know we have an LSDA.
Bill Wendling62c75ad2013-04-10 21:42:06 +00001178 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling4bb5b032011-07-14 23:34:45 +00001179 Encoding |= 0x40000000;
1180
Evan Chenge76a33b2011-07-20 05:58:47 +00001181 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendling3c163cf2011-06-30 21:25:51 +00001182
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001183 // Range Start
Evan Cheng203576a2011-07-20 19:50:42 +00001184 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling9056e902011-06-29 23:49:12 +00001185 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001186 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +00001187 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001188
1189 // Range Length
1190 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1191 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001192 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +00001193 Streamer.EmitAbsValue(Range, 4);
1194
Bill Wendling9287a6e2011-06-30 00:30:52 +00001195 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +00001196 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer70be28a2011-11-07 21:00:59 +00001197 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1198 Twine::utohexstr(Encoding));
Bill Wendling9287a6e2011-06-30 00:30:52 +00001199 Streamer.EmitIntValue(Encoding, Size);
1200
Bill Wendling9056e902011-06-29 23:49:12 +00001201 // Personality Function
Bill Wendling4bb5b032011-07-14 23:34:45 +00001202 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001203 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling62c75ad2013-04-10 21:42:06 +00001204 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +00001205 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001206 else
1207 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +00001208
1209 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +00001210 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001211 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling62c75ad2013-04-10 21:42:06 +00001212 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +00001213 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001214 else
1215 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001216}
1217
Rafael Espindola25f492e2011-04-12 16:12:03 +00001218const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1219 const MCSymbol *personality,
1220 unsigned personalityEncoding,
1221 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001222 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001223 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +00001224 MCContext &context = streamer.getContext();
Evan Cheng0e6a0522011-07-18 20:57:22 +00001225 const MCRegisterInfo &MRI = context.getRegisterInfo();
Evan Chenge76a33b2011-07-20 05:58:47 +00001226 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001227 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +00001228
1229 MCSymbol *sectionStart;
Evan Chenge76a33b2011-07-20 05:58:47 +00001230 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +00001231 sectionStart = context.CreateTempSymbol();
1232 else
1233 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1234
Bill Wendling3c163cf2011-06-30 21:25:51 +00001235 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +00001236 CIENum++;
1237
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001238 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +00001239
1240 // Length
1241 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1242 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001243 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001244 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001245
1246 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001247 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +00001248 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001249 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001250
1251 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +00001252 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +00001253 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1254
1255 // Augmentation String
1256 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001257 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +00001258 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001259 Augmentation += "z";
1260 if (personality)
1261 Augmentation += "P";
1262 if (lsda)
1263 Augmentation += "L";
1264 Augmentation += "R";
Rafael Espindola16d7d432012-01-23 21:51:52 +00001265 if (IsSignalFrame)
1266 Augmentation += "S";
Eric Christopher68ca5622013-01-09 01:57:54 +00001267 streamer.EmitBytes(Augmentation.str());
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001268 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001269 streamer.EmitIntValue(0, 1);
1270
1271 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001272 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001273 streamer.EmitULEB128IntValue(1);
1274
1275 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001276 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001277 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1278
1279 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +00001280 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Evan Cheng0e6a0522011-07-18 20:57:22 +00001281 streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
Rafael Espindola89b93722010-12-10 07:39:47 +00001282
1283 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001284
1285 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001286 if (IsEH) {
1287 if (personality) {
1288 // Personality Encoding
1289 augmentationLength += 1;
1290 // Personality
1291 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1292 }
1293 if (lsda)
1294 augmentationLength += 1;
1295 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001296 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001297
Bill Wendling3c163cf2011-06-30 21:25:51 +00001298 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001299 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001300
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001301 // Augmentation Data (optional)
1302 if (personality) {
1303 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +00001304 EmitEncodingByte(streamer, personalityEncoding,
1305 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001306 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +00001307 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001308 EmitPersonality(streamer, *personality, personalityEncoding);
1309 }
Bill Wendling3c163cf2011-06-30 21:25:51 +00001310
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001311 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +00001312 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1313
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001314 // Encoding of the FDE pointers
Evan Cheng203576a2011-07-20 19:50:42 +00001315 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling3c163cf2011-06-30 21:25:51 +00001316 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +00001317 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001318
1319 // Initial Instructions
1320
Evan Cheng2d286172011-07-18 22:29:13 +00001321 const MCAsmInfo &MAI = context.getAsmInfo();
1322 const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +00001323 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +00001324
Rafael Espindolafe024d02010-12-28 18:36:23 +00001325 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001326 MCSymbol *Label = Moves[i].getLabel();
1327 const MachineLocation &Dst =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001328 TranslateMachineLocation(MRI, Moves[i].getDestination());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001329 const MachineLocation &Src =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001330 TranslateMachineLocation(MRI, Moves[i].getSource());
Rafael Espindola28c9ea32012-11-24 02:01:08 +00001331
1332 if (Dst.isReg()) {
1333 assert(Dst.getReg() == MachineLocation::VirtualFP);
1334 assert(!Src.isReg());
1335 MCCFIInstruction Inst =
1336 MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset());
1337 Instructions.push_back(Inst);
1338 } else {
1339 assert(Src.isReg());
1340 unsigned Reg = Src.getReg();
1341 int Offset = Dst.getOffset();
1342 MCCFIInstruction Inst =
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001343 MCCFIInstruction::createOffset(Label, Reg, Offset);
Rafael Espindola28c9ea32012-11-24 02:01:08 +00001344 Instructions.push_back(Inst);
1345 }
Rafael Espindolafe024d02010-12-28 18:36:23 +00001346 }
1347
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001348 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +00001349
1350 // Padding
Evan Cheng1be0e272011-07-15 02:09:41 +00001351 streamer.EmitValueToAlignment(IsEH
1352 ? 4 : context.getAsmInfo().getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +00001353
1354 streamer.EmitLabel(sectionEnd);
1355 return *sectionStart;
1356}
1357
Rafael Espindola25f492e2011-04-12 16:12:03 +00001358MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1359 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +00001360 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +00001361 MCContext &context = streamer.getContext();
1362 MCSymbol *fdeStart = context.CreateTempSymbol();
1363 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Chenge76a33b2011-07-20 05:58:47 +00001364 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling2541c412011-06-30 22:02:20 +00001365 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +00001366
Evan Cheng5fbe5e72011-08-24 22:31:37 +00001367 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendling2541c412011-06-30 22:02:20 +00001368 MCSymbol *EHSym =
1369 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +00001370 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +00001371 streamer.EmitLabel(EHSym);
1372 }
1373
Rafael Espindola89b93722010-12-10 07:39:47 +00001374 // Length
1375 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001376 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001377 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001378
1379 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001380
Rafael Espindola89b93722010-12-10 07:39:47 +00001381 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001382 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001383 if (IsEH) {
1384 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1385 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001386 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001387 streamer.EmitAbsValue(offset, 4);
Rafael Espindola2241e512012-06-22 13:24:07 +00001388 } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001389 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1390 cieStart, 0);
1391 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001392 } else {
1393 streamer.EmitSymbolValue(&cieStart, 4);
1394 }
Bill Wendling2541c412011-06-30 22:02:20 +00001395
Rafael Espindola89b93722010-12-10 07:39:47 +00001396 // PC Begin
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001397 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1398 : (unsigned)dwarf::DW_EH_PE_absptr;
1399 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
1400 EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +00001401
1402 // PC Range
1403 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1404 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001405 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001406 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola89b93722010-12-10 07:39:47 +00001407
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001408 if (IsEH) {
1409 // Augmentation Data Length
1410 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001411
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001412 if (frame.Lsda)
1413 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001414
Bill Wendling2541c412011-06-30 22:02:20 +00001415 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001416 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001417
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001418 // Augmentation Data
1419 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +00001420 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1421 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001422 }
Rafael Espindola4892dff2011-04-29 03:06:29 +00001423
Rafael Espindola89b93722010-12-10 07:39:47 +00001424 // Call Frame Instructions
1425
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001426 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +00001427
Rafael Espindola89b93722010-12-10 07:39:47 +00001428 // Padding
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001429 streamer.EmitValueToAlignment(PCSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001430
1431 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +00001432}
1433
Benjamin Kramer19282362010-12-30 22:34:44 +00001434namespace {
1435 struct CIEKey {
Rafael Espindola16d7d432012-01-23 21:51:52 +00001436 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1437 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindolabdc31672010-12-27 15:56:22 +00001438
Benjamin Kramer19282362010-12-30 22:34:44 +00001439 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001440 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1441 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1442 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer19282362010-12-30 22:34:44 +00001443 }
1444 const MCSymbol* Personality;
1445 unsigned PersonalityEncoding;
1446 unsigned LsdaEncoding;
Rafael Espindola16d7d432012-01-23 21:51:52 +00001447 bool IsSignalFrame;
Benjamin Kramer19282362010-12-30 22:34:44 +00001448 };
1449}
Rafael Espindolabdc31672010-12-27 15:56:22 +00001450
1451namespace llvm {
1452 template <>
1453 struct DenseMapInfo<CIEKey> {
1454 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001455 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001456 }
1457 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001458 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001459 }
1460 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer74849202012-04-11 14:06:39 +00001461 return static_cast<unsigned>(hash_combine(Key.Personality,
1462 Key.PersonalityEncoding,
1463 Key.LsdaEncoding,
1464 Key.IsSignalFrame));
Rafael Espindolabdc31672010-12-27 15:56:22 +00001465 }
1466 static bool isEqual(const CIEKey &LHS,
1467 const CIEKey &RHS) {
1468 return LHS.Personality == RHS.Personality &&
1469 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola16d7d432012-01-23 21:51:52 +00001470 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1471 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001472 }
1473 };
1474}
1475
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001476void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1477 bool UsingCFI,
1478 bool IsEH) {
1479 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001480 MCObjectFileInfo *MOFI =
1481 const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
Bill Wendling1424c3c2011-07-22 21:18:59 +00001482 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001483 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001484
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001485 // Emit the compact unwind info if available.
Bill Wendling7a13b722011-12-15 00:14:24 +00001486 if (IsEH && MOFI->getCompactUnwindSection())
Bill Wendling1424c3c2011-07-22 21:18:59 +00001487 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1488 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
Bill Wendlinga2ff3e22011-11-08 22:23:43 +00001489 if (Frame.CompactUnwindEncoding)
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001490 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001491 }
1492
Evan Chenge76a33b2011-07-20 05:58:47 +00001493 const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1494 *MOFI->getDwarfFrameSection();
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001495 Streamer.SwitchSection(&Section);
1496 MCSymbol *SectionStart = Context.CreateTempSymbol();
1497 Streamer.EmitLabel(SectionStart);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001498 Emitter.setSectionStart(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001499
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001500 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001501 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001502
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001503 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling1424c3c2011-07-22 21:18:59 +00001504 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1505 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001506 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001507 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001508 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1509 if (!CIEStart)
1510 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1511 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001512 Frame.IsSignalFrame,
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001513 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001514
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001515 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001516
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001517 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001518 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001519 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001520
Evan Cheng1be0e272011-07-15 02:09:41 +00001521 Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001522 if (FDEEnd)
1523 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001524}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001525
1526void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1527 uint64_t AddrDelta) {
1528 SmallString<256> Tmp;
1529 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001530 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Eric Christopher68ca5622013-01-09 01:57:54 +00001531 Streamer.EmitBytes(OS.str());
Rafael Espindola245a1e22010-12-28 05:39:27 +00001532}
1533
1534void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001535 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001536 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001537 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001538 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001539 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1540 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001541 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001542 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1543 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001544 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001545 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001546 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001547 OS << uint8_t( AddrDelta & 0xff);
1548 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001549 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001550 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001551 assert(isUInt<32>(AddrDelta));
1552 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001553 OS << uint8_t( AddrDelta & 0xff);
1554 OS << uint8_t((AddrDelta >> 8) & 0xff);
1555 OS << uint8_t((AddrDelta >> 16) & 0xff);
1556 OS << uint8_t((AddrDelta >> 24) & 0xff);
1557
Rafael Espindola245a1e22010-12-28 05:39:27 +00001558 }
1559}