blob: d481ecf89bc73df02d4b63c7987b3916d5bde6f8 [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"
Kevin Enderbyc0957932010-09-30 16:52:03 +000011#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCSymbol.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/ADT/SmallString.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000019#include "llvm/Target/TargetAsmBackend.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000020using namespace llvm;
21
Kevin Enderbyc0957932010-09-30 16:52:03 +000022// Given a special op, return the address skip amount (in units of
23// DWARF2_LINE_MIN_INSN_LENGTH.
24#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
25
26// The maximum address skip amount that can be encoded with a special op.
27#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
28
29// First special line opcode - leave room for the standard opcodes.
30// Note: If you want to change this, you'll have to update the
31// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
32#define DWARF2_LINE_OPCODE_BASE 13
33
34// Minimum line offset in a special line info. opcode. This value
35// was chosen to give a reasonable range of values.
36#define DWARF2_LINE_BASE -5
37
38// Range of line offsets in a special line info. opcode.
39# define DWARF2_LINE_RANGE 14
40
41// Define the architecture-dependent minimum instruction length (in bytes).
42// This value should be rather too small than too big.
43# define DWARF2_LINE_MIN_INSN_LENGTH 1
44
45// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
46// this routine is a nop and will be optimized away.
47static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
48{
49 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
50 return AddrDelta;
51 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
52 // TODO: report this error, but really only once.
53 ;
54 }
55 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
56}
57
58//
59// This is called when an instruction is assembled into the specified section
60// and if there is information from the last .loc directive that has yet to have
61// a line entry made for it is made.
62//
63void MCLineEntry::Make(MCObjectStreamer *MCOS, const MCSection *Section) {
64 if (!MCOS->getContext().getDwarfLocSeen())
65 return;
66
67 // Create a symbol at in the current section for use in the line entry.
68 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
69 // Set the value of the symbol to use for the MCLineEntry.
70 MCOS->EmitLabel(LineSym);
71
72 // Get the current .loc info saved in the context.
73 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
74
75 // Create a (local) line entry with the symbol and the current .loc info.
76 MCLineEntry LineEntry(LineSym, DwarfLoc);
77
78 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000079 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000080
81 // Get the MCLineSection for this section, if one does not exist for this
82 // section create it.
83 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
84 MCOS->getContext().getMCLineSections();
85 MCLineSection *LineSection = MCLineSections[Section];
86 if (!LineSection) {
87 // Create a new MCLineSection. This will be deleted after the dwarf line
88 // table is created using it by iterating through the MCLineSections
89 // DenseMap.
90 LineSection = new MCLineSection;
91 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
92 MCLineSections[Section] = LineSection;
93 }
94
95 // Add the line entry to this section's entries.
96 LineSection->addLineEntry(LineEntry);
97}
98
99//
100// This helper routine returns an expression of End - Start + IntVal .
101//
102static inline const MCExpr *MakeStartMinusEndExpr(MCObjectStreamer *MCOS,
103 MCSymbol *Start,
104 MCSymbol *End, int IntVal) {
105 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
106 const MCExpr *Res =
107 MCSymbolRefExpr::Create(End, Variant, MCOS->getContext());
108 const MCExpr *RHS =
109 MCSymbolRefExpr::Create(Start, Variant, MCOS->getContext());
110 const MCExpr *Res1 =
111 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS->getContext());
112 const MCExpr *Res2 =
113 MCConstantExpr::Create(IntVal, MCOS->getContext());
114 const MCExpr *Res3 =
115 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS->getContext());
116 return Res3;
117}
118
119//
120// This emits an "absolute" address used in the start of a dwarf line number
121// table. This will result in a relocatation entry for the address.
122//
123static inline void EmitDwarfSetAddress(MCObjectStreamer *MCOS,
124 MCSymbol *Symbol) {
125 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
126
127 int sizeof_address = MCOS->getAssembler().getBackend().getPointerSize();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000128 MCOS->EmitULEB128IntValue(sizeof_address + 1);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000129
130 MCOS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
131 MCOS->EmitSymbolValue(Symbol, sizeof_address);
132}
133
134//
135// This emits the Dwarf line table for the specified section from the entries
136// in the LineSection.
137//
138static inline bool EmitDwarfLineTable(MCObjectStreamer *MCOS,
139 const MCSection *Section,
140 MCLineSection *LineSection,
141 const MCSection *DwarfLineSection) {
142 unsigned FileNum = 1;
143 unsigned LastLine = 1;
144 unsigned Column = 0;
145 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
146 unsigned Isa = 0;
147 bool EmittedLineTable = false;
148 MCSymbol *LastLabel = NULL;
149 MCSectionData &DLS =
150 MCOS->getAssembler().getOrCreateSectionData(*DwarfLineSection);
151
152 // Loop through each MCLineEntry and encode the dwarf line number table.
153 for (MCLineSection::iterator
154 it = LineSection->getMCLineEntries()->begin(),
155 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
156
157 if (FileNum != it->getFileNum()) {
158 FileNum = it->getFileNum();
159 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000160 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000161 }
162 if (Column != it->getColumn()) {
163 Column = it->getColumn();
164 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000165 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000166 }
167 if (Isa != it->getIsa()) {
168 Isa = it->getIsa();
169 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000170 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000171 }
172 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
173 Flags = it->getFlags();
174 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
175 }
176 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
177 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
178 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
179 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
180 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
181 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
182
183 int64_t LineDelta = it->getLine() - LastLine;
184 MCSymbol *Label = it->getLabel();
185
186 // At this point we want to emit/create the sequence to encode the delta in
187 // line numbers and the increment of the address from the previous Label
188 // and the current Label.
189 if (LastLabel == NULL) {
190 // emit the sequence to set the address
191 EmitDwarfSetAddress(MCOS, Label);
192 // emit the sequence for the LineDelta (from 1) and a zero address delta.
193 MCDwarfLineAddr::Emit(MCOS, LineDelta, 0);
194 }
195 else {
196 // Create an expression for the address delta from the LastLabel and
197 // this Label (plus 0).
198 const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, Label,0);
199 // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
200 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, &DLS);
201 }
202
203 LastLine = it->getLine();
204 LastLabel = Label;
205 EmittedLineTable = true;
206 }
207
208 // Emit a DW_LNE_end_sequence for the end of the section.
209 // Using the pointer Section create a temporary label at the end of the
210 // section and use that and the LastLabel to compute the address delta
211 // and use INT64_MAX as the line delta which is the signal that this is
212 // actually a DW_LNE_end_sequence.
213
214 // Switch to the section to be able to create a symbol at its end.
215 MCOS->SwitchSection(Section);
216 // Create a symbol at the end of the section.
217 MCSymbol *SectionEnd = MCOS->getContext().CreateTempSymbol();
218 // Set the value of the symbol, as we are at the end of the section.
219 MCOS->EmitLabel(SectionEnd);
220
221 // Switch back the the dwarf line section.
222 MCOS->SwitchSection(DwarfLineSection);
223 // Create an expression for the address delta from the LastLabel and this
224 // SectionEnd label.
225 const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, SectionEnd,
226 0);
227 // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
228 new MCDwarfLineAddrFragment(INT64_MAX, *AddrDelta, &DLS);
229
230 return EmittedLineTable;
231}
232
233//
234// This emits the Dwarf file and the line tables.
235//
236void MCDwarfFileTable::Emit(MCObjectStreamer *MCOS,
237 const MCSection *DwarfLineSection) {
238 // Switch to the section where the table will be emitted into.
239 MCOS->SwitchSection(DwarfLineSection);
240
241 // Create a symbol at the beginning of this section.
242 MCSymbol *LineStartSym = MCOS->getContext().CreateTempSymbol();
243 // Set the value of the symbol, as we are at the start of the section.
244 MCOS->EmitLabel(LineStartSym);
245
246 // Create a symbol for the end of the section (to be set when we get there).
247 MCSymbol *LineEndSym = MCOS->getContext().CreateTempSymbol();
248
249 // The first 4 bytes is the total length of the information for this
250 // compilation unit (not including these 4 bytes for the length).
251 MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym, 4),
252 4, 0);
253
254 // Next 2 bytes is the Version, which is Dwarf 2.
255 MCOS->EmitIntValue(2, 2);
256
257 // Create a symbol for the end of the prologue (to be set when we get there).
258 MCSymbol *ProEndSym = MCOS->getContext().CreateTempSymbol(); // Lprologue_end
259
260 // Length of the prologue, is the next 4 bytes. Which is the start of the
261 // section to the end of the prologue. Not including the 4 bytes for the
262 // total length, the 2 bytes for the version, and these 4 bytes for the
263 // length of the prologue.
264 MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym,
265 (4 + 2 + 4)),
266 4, 0);
267
268 // Parameters of the state machine, are next.
269 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
270 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
271 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
272 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
273 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
274
275 // Standard opcode lengths
276 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
277 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
278 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
279 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
280 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
281 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
282 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
283 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
284 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
285 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
286 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
287 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
288
289 // Put out the directory and file tables.
290
291 // First the directory table.
292 const std::vector<StringRef> &MCDwarfDirs =
293 MCOS->getContext().getMCDwarfDirs();
294 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
295 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
296 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
297 }
298 MCOS->EmitIntValue(0, 1); // Terminate the directory list
299
300 // Second the file table.
301 const std::vector<MCDwarfFile *> &MCDwarfFiles =
302 MCOS->getContext().getMCDwarfFiles();
303 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
304 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
305 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000306 // the Directory num
307 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000308 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
309 MCOS->EmitIntValue(0, 1); // filesize (always 0)
310 }
311 MCOS->EmitIntValue(0, 1); // Terminate the file list
312
313 // This is the end of the prologue, so set the value of the symbol at the
314 // end of the prologue (that was used in a previous expression).
315 MCOS->EmitLabel(ProEndSym);
316
317 // Put out the line tables.
318 bool EmittedLineTable = false;
319 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
320 MCOS->getContext().getMCLineSections();
321 for (DenseMap<const MCSection *, MCLineSection *>::iterator it =
322 MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) {
323 EmittedLineTable = EmitDwarfLineTable(MCOS, it->first, it->second,
324 DwarfLineSection);
325
326 // Now delete the MCLineSections that were created in MCLineEntry::Make()
327 // and used to emit the line table.
328 delete it->second;
329 }
330
331 // If there are no line tables emited then we emit:
332 // The following DW_LNE_set_address sequence to set the address to zero and
333 // the DW_LNE_end_sequence.
334 if (EmittedLineTable == false) {
335 if (MCOS->getAssembler().getBackend().getPointerSize() == 8) {
336 // This is the DW_LNE_set_address sequence for 64-bit code.
337 MCOS->EmitIntValue(0, 1);
338 MCOS->EmitIntValue(9, 1);
339 MCOS->EmitIntValue(2, 1);
340 MCOS->EmitIntValue(0, 1);
341 MCOS->EmitIntValue(0, 1);
342 MCOS->EmitIntValue(0, 1);
343 MCOS->EmitIntValue(0, 1);
344 MCOS->EmitIntValue(0, 1);
345 MCOS->EmitIntValue(0, 1);
346 MCOS->EmitIntValue(0, 1);
347 MCOS->EmitIntValue(0, 1);
348 }
349 else {
350 // This is the DW_LNE_set_address sequence for 32-bit code.
351 MCOS->EmitIntValue(0, 1);
352 MCOS->EmitIntValue(5, 1);
353 MCOS->EmitIntValue(2, 1);
354 MCOS->EmitIntValue(0, 1);
355 MCOS->EmitIntValue(0, 1);
356 MCOS->EmitIntValue(0, 1);
357 MCOS->EmitIntValue(0, 1);
358 }
359
360 // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01'
361 // (00 is the code for extended opcodes, followed by a ULEB128 length of the
362 // extended opcode (01), and the DW_LNE_end_sequence (01).
363 MCOS->EmitIntValue(0, 1); // DW_LNS_extended_op
364 MCOS->EmitIntValue(1, 1); // ULEB128 length of the extended opcode
365 MCOS->EmitIntValue(1, 1); // DW_LNE_end_sequence
366 }
367
368 // This is the end of the section, so set the value of the symbol at the end
369 // of this section (that was used in a previous expression).
370 MCOS->EmitLabel(LineEndSym);
371}
372
373/// Utility function to compute the size of the encoding.
374uint64_t MCDwarfLineAddr::ComputeSize(int64_t LineDelta, uint64_t AddrDelta) {
375 SmallString<256> Tmp;
376 raw_svector_ostream OS(Tmp);
377 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
378 return OS.GetNumBytesInBuffer();
379}
380
381/// Utility function to write the encoding to an object writer.
382void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
383 uint64_t AddrDelta) {
384 SmallString<256> Tmp;
385 raw_svector_ostream OS(Tmp);
386 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
387 OW->WriteBytes(OS.str());
388}
389
390/// Utility function to emit the encoding to a streamer.
391void MCDwarfLineAddr::Emit(MCObjectStreamer *MCOS, int64_t LineDelta,
392 uint64_t AddrDelta) {
393 SmallString<256> Tmp;
394 raw_svector_ostream OS(Tmp);
395 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
396 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
397}
398
399/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
400void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
401 raw_ostream &OS) {
402 uint64_t Temp, Opcode;
403 bool NeedCopy = false;
404
405 // Scale the address delta by the minimum instruction length.
406 AddrDelta = ScaleAddrDelta(AddrDelta);
407
408 // A LineDelta of INT64_MAX is a signal that this is actually a
409 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
410 // end_sequence to emit the matrix entry.
411 if (LineDelta == INT64_MAX) {
412 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
413 OS << char(dwarf::DW_LNS_const_add_pc);
414 else {
415 OS << char(dwarf::DW_LNS_advance_pc);
416 SmallString<32> Tmp;
417 raw_svector_ostream OSE(Tmp);
418 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
419 OS << OSE.str();
420 }
421 OS << char(dwarf::DW_LNS_extended_op);
422 OS << char(1);
423 OS << char(dwarf::DW_LNE_end_sequence);
424 return;
425 }
426
427 // Bias the line delta by the base.
428 Temp = LineDelta - DWARF2_LINE_BASE;
429
430 // If the line increment is out of range of a special opcode, we must encode
431 // it with DW_LNS_advance_line.
432 if (Temp >= DWARF2_LINE_RANGE) {
433 OS << char(dwarf::DW_LNS_advance_line);
434 SmallString<32> Tmp;
435 raw_svector_ostream OSE(Tmp);
436 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
437 OS << OSE.str();
438
439 LineDelta = 0;
440 Temp = 0 - DWARF2_LINE_BASE;
441 NeedCopy = true;
442 }
443
444 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
445 if (LineDelta == 0 && AddrDelta == 0) {
446 OS << char(dwarf::DW_LNS_copy);
447 return;
448 }
449
450 // Bias the opcode by the special opcode base.
451 Temp += DWARF2_LINE_OPCODE_BASE;
452
453 // Avoid overflow when addr_delta is large.
454 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
455 // Try using a special opcode.
456 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
457 if (Opcode <= 255) {
458 OS << char(Opcode);
459 return;
460 }
461
462 // Try using DW_LNS_const_add_pc followed by special op.
463 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
464 if (Opcode <= 255) {
465 OS << char(dwarf::DW_LNS_const_add_pc);
466 OS << char(Opcode);
467 return;
468 }
469 }
470
471 // Otherwise use DW_LNS_advance_pc.
472 OS << char(dwarf::DW_LNS_advance_pc);
473 SmallString<32> Tmp;
474 raw_svector_ostream OSE(Tmp);
475 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
476 OS << OSE.str();
477
478 if (NeedCopy)
479 OS << char(dwarf::DW_LNS_copy);
480 else
481 OS << char(Temp);
482}
483
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000484void MCDwarfFile::print(raw_ostream &OS) const {
485 OS << '"' << getName() << '"';
486}
487
488void MCDwarfFile::dump() const {
489 print(dbgs());
490}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000491