Update documentation for old api changes.
[oota-llvm.git] / docs / SourceLevelDebugging.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6   <title>Source Level Debugging with LLVM</title>
7   <link rel="stylesheet" href="llvm.css" type="text/css">
8 </head>
9 <body>
10
11 <h1>Source Level Debugging with LLVM</h1>
12
13 <table class="layout" style="width:100%">
14   <tr class="layout">
15     <td class="left">
16 <ul>
17   <li><a href="#introduction">Introduction</a>
18   <ol>
19     <li><a href="#phil">Philosophy behind LLVM debugging information</a></li>
20     <li><a href="#consumers">Debug information consumers</a></li>
21     <li><a href="#debugopt">Debugging optimized code</a></li>
22   </ol></li>
23   <li><a href="#format">Debugging information format</a>
24   <ol>
25     <li><a href="#debug_info_descriptors">Debug information descriptors</a>
26     <ul>
27       <li><a href="#format_compile_units">Compile unit descriptors</a></li>
28       <li><a href="#format_files">File descriptors</a></li>
29       <li><a href="#format_global_variables">Global variable descriptors</a></li>
30       <li><a href="#format_subprograms">Subprogram descriptors</a></li>
31       <li><a href="#format_blocks">Block descriptors</a></li>
32       <li><a href="#format_basic_type">Basic type descriptors</a></li>
33       <li><a href="#format_derived_type">Derived type descriptors</a></li>
34       <li><a href="#format_composite_type">Composite type descriptors</a></li>
35       <li><a href="#format_subrange">Subrange descriptors</a></li>
36       <li><a href="#format_enumeration">Enumerator descriptors</a></li>
37       <li><a href="#format_variables">Local variables</a></li>
38     </ul></li>
39     <li><a href="#format_common_intrinsics">Debugger intrinsic functions</a>
40       <ul>
41       <li><a href="#format_common_declare">llvm.dbg.declare</a></li>
42       <li><a href="#format_common_value">llvm.dbg.value</a></li>
43     </ul></li>
44   </ol></li>
45   <li><a href="#format_common_lifetime">Object lifetimes and scoping</a></li>
46   <li><a href="#ccxx_frontend">C/C++ front-end specific debug information</a>
47   <ol>
48     <li><a href="#ccxx_compile_units">C/C++ source file information</a></li>
49     <li><a href="#ccxx_global_variable">C/C++ global variable information</a></li>
50     <li><a href="#ccxx_subprogram">C/C++ function information</a></li>
51     <li><a href="#ccxx_basic_types">C/C++ basic types</a></li>
52     <li><a href="#ccxx_derived_types">C/C++ derived types</a></li>
53     <li><a href="#ccxx_composite_types">C/C++ struct/union types</a></li>
54     <li><a href="#ccxx_enumeration_types">C/C++ enumeration types</a></li>
55   </ol></li>
56   <li><a href="#llvmdwarfextension">LLVM Dwarf Extensions</a>
57     <ol>
58       <li><a href="#objcproperty">Debugging Information Extension
59           for Objective C Properties</a>
60         <ul>
61           <li><a href="#objcpropertyintroduction">Introduction</a></li>
62           <li><a href="#objcpropertyproposal">Proposal</a></li>
63           <li><a href="#objcpropertynewattributes">New DWARF Attributes</a></li>
64           <li><a href="#objcpropertynewconstants">New DWARF Constants</a></li>
65         </ul>
66       </li>
67       <li><a href="#acceltable">Name Accelerator Tables</a>
68         <ul>
69           <li><a href="#acceltableintroduction">Introduction</a></li>
70           <li><a href="#acceltablehashes">Hash Tables</a></li>
71           <li><a href="#acceltabledetails">Details</a></li>
72           <li><a href="#acceltablecontents">Contents</a></li>
73           <li><a href="#acceltableextensions">Language Extensions and File Format Changes</a></li>
74         </ul>
75       </li>
76     </ol>
77   </li>
78 </ul>
79 </td>
80 <td class="right">
81 <img src="img/venusflytrap.jpg" alt="A leafy and green bug eater" width="247"
82 height="369">
83 </td>
84 </tr></table>
85
86 <div class="doc_author">
87   <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
88             and <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
89 </div>
90
91
92 <!-- *********************************************************************** -->
93 <h2><a name="introduction">Introduction</a></h2>
94 <!-- *********************************************************************** -->
95
96 <div>
97
98 <p>This document is the central repository for all information pertaining to
99    debug information in LLVM.  It describes the <a href="#format">actual format
100    that the LLVM debug information</a> takes, which is useful for those
101    interested in creating front-ends or dealing directly with the information.
102    Further, this document provides specific examples of what debug information
103    for C/C++ looks like.</p>
104
105 <!-- ======================================================================= -->
106 <h3>
107   <a name="phil">Philosophy behind LLVM debugging information</a>
108 </h3>
109
110 <div>
111
112 <p>The idea of the LLVM debugging information is to capture how the important
113    pieces of the source-language's Abstract Syntax Tree map onto LLVM code.
114    Several design aspects have shaped the solution that appears here.  The
115    important ones are:</p>
116
117 <ul>
118   <li>Debugging information should have very little impact on the rest of the
119       compiler.  No transformations, analyses, or code generators should need to
120       be modified because of debugging information.</li>
121
122   <li>LLVM optimizations should interact in <a href="#debugopt">well-defined and
123       easily described ways</a> with the debugging information.</li>
124
125   <li>Because LLVM is designed to support arbitrary programming languages,
126       LLVM-to-LLVM tools should not need to know anything about the semantics of
127       the source-level-language.</li>
128
129   <li>Source-level languages are often <b>widely</b> different from one another.
130       LLVM should not put any restrictions of the flavor of the source-language,
131       and the debugging information should work with any language.</li>
132
133   <li>With code generator support, it should be possible to use an LLVM compiler
134       to compile a program to native machine code and standard debugging
135       formats.  This allows compatibility with traditional machine-code level
136       debuggers, like GDB or DBX.</li>
137 </ul>
138
139 <p>The approach used by the LLVM implementation is to use a small set
140    of <a href="#format_common_intrinsics">intrinsic functions</a> to define a
141    mapping between LLVM program objects and the source-level objects.  The
142    description of the source-level program is maintained in LLVM metadata
143    in an <a href="#ccxx_frontend">implementation-defined format</a>
144    (the C/C++ front-end currently uses working draft 7 of
145    the <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3
146    standard</a>).</p>
147
148 <p>When a program is being debugged, a debugger interacts with the user and
149    turns the stored debug information into source-language specific information.
150    As such, a debugger must be aware of the source-language, and is thus tied to
151    a specific language or family of languages.</p>
152
153 </div>
154
155 <!-- ======================================================================= -->
156 <h3>
157   <a name="consumers">Debug information consumers</a>
158 </h3>
159
160 <div>
161
162 <p>The role of debug information is to provide meta information normally
163    stripped away during the compilation process.  This meta information provides
164    an LLVM user a relationship between generated code and the original program
165    source code.</p>
166
167 <p>Currently, debug information is consumed by DwarfDebug to produce dwarf
168    information used by the gdb debugger.  Other targets could use the same
169    information to produce stabs or other debug forms.</p>
170
171 <p>It would also be reasonable to use debug information to feed profiling tools
172    for analysis of generated code, or, tools for reconstructing the original
173    source from generated code.</p>
174
175 <p>TODO - expound a bit more.</p>
176
177 </div>
178
179 <!-- ======================================================================= -->
180 <h3>
181   <a name="debugopt">Debugging optimized code</a>
182 </h3>
183
184 <div>
185
186 <p>An extremely high priority of LLVM debugging information is to make it
187    interact well with optimizations and analysis.  In particular, the LLVM debug
188    information provides the following guarantees:</p>
189
190 <ul>
191   <li>LLVM debug information <b>always provides information to accurately read
192       the source-level state of the program</b>, regardless of which LLVM
193       optimizations have been run, and without any modification to the
194       optimizations themselves.  However, some optimizations may impact the
195       ability to modify the current state of the program with a debugger, such
196       as setting program variables, or calling functions that have been
197       deleted.</li>
198
199   <li>As desired, LLVM optimizations can be upgraded to be aware of the LLVM
200       debugging information, allowing them to update the debugging information
201       as they perform aggressive optimizations.  This means that, with effort,
202       the LLVM optimizers could optimize debug code just as well as non-debug
203       code.</li>
204
205   <li>LLVM debug information does not prevent optimizations from
206       happening (for example inlining, basic block reordering/merging/cleanup,
207       tail duplication, etc).</li>
208
209   <li>LLVM debug information is automatically optimized along with the rest of
210       the program, using existing facilities.  For example, duplicate
211       information is automatically merged by the linker, and unused information
212       is automatically removed.</li>
213 </ul>
214
215 <p>Basically, the debug information allows you to compile a program with
216    "<tt>-O0 -g</tt>" and get full debug information, allowing you to arbitrarily
217    modify the program as it executes from a debugger.  Compiling a program with
218    "<tt>-O3 -g</tt>" gives you full debug information that is always available
219    and accurate for reading (e.g., you get accurate stack traces despite tail
220    call elimination and inlining), but you might lose the ability to modify the
221    program and call functions where were optimized out of the program, or
222    inlined away completely.</p>
223
224 <p><a href="TestingGuide.html#quicktestsuite">LLVM test suite</a> provides a
225    framework to test optimizer's handling of debugging information. It can be
226    run like this:</p>
227
228 <div class="doc_code">
229 <pre>
230 % cd llvm/projects/test-suite/MultiSource/Benchmarks  # or some other level
231 % make TEST=dbgopt
232 </pre>
233 </div>
234
235 <p>This will test impact of debugging information on optimization passes. If
236    debugging information influences optimization passes then it will be reported
237    as a failure. See <a href="TestingGuide.html">TestingGuide</a> for more
238    information on LLVM test infrastructure and how to run various tests.</p>
239
240 </div>
241
242 </div>
243
244 <!-- *********************************************************************** -->
245 <h2>
246   <a name="format">Debugging information format</a>
247 </h2>
248 <!-- *********************************************************************** -->
249
250 <div>
251
252 <p>LLVM debugging information has been carefully designed to make it possible
253    for the optimizer to optimize the program and debugging information without
254    necessarily having to know anything about debugging information.  In
255    particular, the use of metadata avoids duplicated debugging information from
256    the beginning, and the global dead code elimination pass automatically
257    deletes debugging information for a function if it decides to delete the
258    function. </p>
259
260 <p>To do this, most of the debugging information (descriptors for types,
261    variables, functions, source files, etc) is inserted by the language
262    front-end in the form of LLVM metadata. </p>
263
264 <p>Debug information is designed to be agnostic about the target debugger and
265    debugging information representation (e.g. DWARF/Stabs/etc).  It uses a
266    generic pass to decode the information that represents variables, types,
267    functions, namespaces, etc: this allows for arbitrary source-language
268    semantics and type-systems to be used, as long as there is a module
269    written for the target debugger to interpret the information. </p>
270
271 <p>To provide basic functionality, the LLVM debugger does have to make some
272    assumptions about the source-level language being debugged, though it keeps
273    these to a minimum.  The only common features that the LLVM debugger assumes
274    exist are <a href="#format_files">source files</a>,
275    and <a href="#format_global_variables">program objects</a>.  These abstract
276    objects are used by a debugger to form stack traces, show information about
277    local variables, etc.</p>
278
279 <p>This section of the documentation first describes the representation aspects
280    common to any source-language.  The <a href="#ccxx_frontend">next section</a>
281    describes the data layout conventions used by the C and C++ front-ends.</p>
282
283 <!-- ======================================================================= -->
284 <h3>
285   <a name="debug_info_descriptors">Debug information descriptors</a>
286 </h3>
287
288 <div>
289
290 <p>In consideration of the complexity and volume of debug information, LLVM
291    provides a specification for well formed debug descriptors. </p>
292
293 <p>Consumers of LLVM debug information expect the descriptors for program
294    objects to start in a canonical format, but the descriptors can include
295    additional information appended at the end that is source-language
296    specific. All LLVM debugging information is versioned, allowing backwards
297    compatibility in the case that the core structures need to change in some
298    way.  Also, all debugging information objects start with a tag to indicate
299    what type of object it is.  The source-language is allowed to define its own
300    objects, by using unreserved tag numbers.  We recommend using with tags in
301    the range 0x1000 through 0x2000 (there is a defined enum DW_TAG_user_base =
302    0x1000.)</p>
303
304 <p>The fields of debug descriptors used internally by LLVM
305    are restricted to only the simple data types <tt>i32</tt>, <tt>i1</tt>,
306    <tt>float</tt>, <tt>double</tt>, <tt>mdstring</tt> and <tt>mdnode</tt>. </p>
307
308 <div class="doc_code">
309 <pre>
310 !1 = metadata !{
311   i32,   ;; A tag
312   ...
313 }
314 </pre>
315 </div>
316
317 <p><a name="LLVMDebugVersion">The first field of a descriptor is always an
318    <tt>i32</tt> containing a tag value identifying the content of the
319    descriptor.  The remaining fields are specific to the descriptor.  The values
320    of tags are loosely bound to the tag values of DWARF information entries.
321    However, that does not restrict the use of the information supplied to DWARF
322    targets.  To facilitate versioning of debug information, the tag is augmented
323    with the current debug version (LLVMDebugVersion = 8 &lt;&lt; 16 or
324    0x80000 or 524288.)</a></p>
325
326 <p>The details of the various descriptors follow.</p>
327
328 <!-- ======================================================================= -->
329 <h4>
330   <a name="format_compile_units">Compile unit descriptors</a>
331 </h4>
332
333 <div>
334
335 <div class="doc_code">
336 <pre>
337 !0 = metadata !{
338   i32,       ;; Tag = 17 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
339              ;; (DW_TAG_compile_unit)
340   i32,       ;; Unused field.
341   i32,       ;; DWARF language identifier (ex. DW_LANG_C89)
342   metadata,  ;; Source file name
343   metadata,  ;; Source file directory (includes trailing slash)
344   metadata   ;; Producer (ex. "4.0.1 LLVM (LLVM research group)")
345   i1,        ;; True if this is a main compile unit.
346   i1,        ;; True if this is optimized.
347   metadata,  ;; Flags
348   i32        ;; Runtime version
349   metadata   ;; List of enums types
350   metadata   ;; List of retained types
351   metadata   ;; List of subprograms
352   metadata   ;; List of global variables
353 }
354 </pre>
355 </div>
356
357 <p>These descriptors contain a source language ID for the file (we use the DWARF
358    3.0 ID numbers, such as <tt>DW_LANG_C89</tt>, <tt>DW_LANG_C_plus_plus</tt>,
359    <tt>DW_LANG_Cobol74</tt>, etc), three strings describing the filename,
360    working directory of the compiler, and an identifier string for the compiler
361    that produced it.</p>
362
363 <p>Compile unit descriptors provide the root context for objects declared in a
364    specific compilation unit. File descriptors are defined using this context.
365    These descriptors are collected by a named metadata
366    <tt>!llvm.dbg.cu</tt>. Compile unit descriptor keeps track of subprograms,
367    global variables and type information.
368
369 </div>
370
371 <!-- ======================================================================= -->
372 <h4>
373   <a name="format_files">File descriptors</a>
374 </h4>
375
376 <div>
377
378 <div class="doc_code">
379 <pre>
380 !0 = metadata !{
381   i32,       ;; Tag = 41 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
382              ;; (DW_TAG_file_type)
383   metadata,  ;; Source file name
384   metadata,  ;; Source file directory (includes trailing slash)
385   metadata   ;; Unused
386 }
387 </pre>
388 </div>
389
390 <p>These descriptors contain information for a file. Global variables and top
391    level functions would be defined using this context.k File descriptors also
392    provide context for source line correspondence. </p>
393
394 <p>Each input file is encoded as a separate file descriptor in LLVM debugging
395    information output. </p>
396
397 </div>
398
399 <!-- ======================================================================= -->
400 <h4>
401   <a name="format_global_variables">Global variable descriptors</a>
402 </h4>
403
404 <div>
405
406 <div class="doc_code">
407 <pre>
408 !1 = metadata !{
409   i32,      ;; Tag = 52 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
410             ;; (DW_TAG_variable)
411   i32,      ;; Unused field.
412   metadata, ;; Reference to context descriptor
413   metadata, ;; Name
414   metadata, ;; Display name (fully qualified C++ name)
415   metadata, ;; MIPS linkage name (for C++)
416   metadata, ;; Reference to file where defined
417   i32,      ;; Line number where defined
418   metadata, ;; Reference to type descriptor
419   i1,       ;; True if the global is local to compile unit (static)
420   i1,       ;; True if the global is defined in the compile unit (not extern)
421   {}*       ;; Reference to the global variable
422 }
423 </pre>
424 </div>
425
426 <p>These descriptors provide debug information about globals variables.  The
427 provide details such as name, type and where the variable is defined. All
428 global variables are collected inside the named metadata
429 <tt>!llvm.dbg.cu</tt>.</p>
430
431 </div>
432
433 <!-- ======================================================================= -->
434 <h4>
435   <a name="format_subprograms">Subprogram descriptors</a>
436 </h4>
437
438 <div>
439
440 <div class="doc_code">
441 <pre>
442 !2 = metadata !{
443   i32,      ;; Tag = 46 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
444             ;; (DW_TAG_subprogram)
445   i32,      ;; Unused field.
446   metadata, ;; Reference to context descriptor
447   metadata, ;; Name
448   metadata, ;; Display name (fully qualified C++ name)
449   metadata, ;; MIPS linkage name (for C++)
450   metadata, ;; Reference to file where defined
451   i32,      ;; Line number where defined
452   metadata, ;; Reference to type descriptor
453   i1,       ;; True if the global is local to compile unit (static)
454   i1,       ;; True if the global is defined in the compile unit (not extern)
455   i32,      ;; Virtuality, e.g. dwarf::DW_VIRTUALITY__virtual
456   i32,      ;; Index into a virtual function
457   metadata, ;; indicates which base type contains the vtable pointer for the
458             ;; derived class
459   i32,      ;; Flags - Artifical, Private, Protected, Explicit, Prototyped.
460   i1,       ;; isOptimized
461   Function *,;; Pointer to LLVM function
462   metadata, ;; Lists function template parameters
463   metadata  ;; Function declaration descriptor
464   metadata  ;; List of function variables
465 }
466 </pre>
467 </div>
468
469 <p>These descriptors provide debug information about functions, methods and
470    subprograms.  They provide details such as name, return types and the source
471    location where the subprogram is defined.
472 </p>
473
474 </div>
475
476 <!-- ======================================================================= -->
477 <h4>
478   <a name="format_blocks">Block descriptors</a>
479 </h4>
480
481 <div>
482
483 <div class="doc_code">
484 <pre>
485 !3 = metadata !{
486   i32,     ;; Tag = 11 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a> (DW_TAG_lexical_block)
487   metadata,;; Reference to context descriptor
488   i32,     ;; Line number
489   i32,     ;; Column number
490   metadata,;; Reference to source file
491   i32      ;; Unique ID to identify blocks from a template function
492 }
493 </pre>
494 </div>
495
496 <p>This descriptor provides debug information about nested blocks within a
497    subprogram. The line number and column numbers are used to dinstinguish
498    two lexical blocks at same depth. </p>
499
500 <div class="doc_code">
501 <pre>
502 !3 = metadata !{
503   i32,     ;; Tag = 11 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a> (DW_TAG_lexical_block)
504   metadata ;; Reference to the scope we're annotating with a file change
505   metadata,;; Reference to the file the scope is enclosed in.
506 }
507 </pre>
508 </div>
509
510 <p>This descriptor provides a wrapper around a lexical scope to handle file
511    changes in the middle of a lexical block.</p>
512
513 </div>
514
515 <!-- ======================================================================= -->
516 <h4>
517   <a name="format_basic_type">Basic type descriptors</a>
518 </h4>
519
520 <div>
521
522 <div class="doc_code">
523 <pre>
524 !4 = metadata !{
525   i32,      ;; Tag = 36 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
526             ;; (DW_TAG_base_type)
527   metadata, ;; Reference to context
528   metadata, ;; Name (may be "" for anonymous types)
529   metadata, ;; Reference to file where defined (may be NULL)
530   i32,      ;; Line number where defined (may be 0)
531   i64,      ;; Size in bits
532   i64,      ;; Alignment in bits
533   i64,      ;; Offset in bits
534   i32,      ;; Flags
535   i32       ;; DWARF type encoding
536 }
537 </pre>
538 </div>
539
540 <p>These descriptors define primitive types used in the code. Example int, bool
541    and float.  The context provides the scope of the type, which is usually the
542    top level.  Since basic types are not usually user defined the context
543    and line number can be left as NULL and 0.  The size, alignment and offset
544    are expressed in bits and can be 64 bit values.  The alignment is used to
545    round the offset when embedded in a
546    <a href="#format_composite_type">composite type</a> (example to keep float
547    doubles on 64 bit boundaries.) The offset is the bit offset if embedded in
548    a <a href="#format_composite_type">composite type</a>.</p>
549
550 <p>The type encoding provides the details of the type.  The values are typically
551    one of the following:</p>
552
553 <div class="doc_code">
554 <pre>
555 DW_ATE_address       = 1
556 DW_ATE_boolean       = 2
557 DW_ATE_float         = 4
558 DW_ATE_signed        = 5
559 DW_ATE_signed_char   = 6
560 DW_ATE_unsigned      = 7
561 DW_ATE_unsigned_char = 8
562 </pre>
563 </div>
564
565 </div>
566
567 <!-- ======================================================================= -->
568 <h4>
569   <a name="format_derived_type">Derived type descriptors</a>
570 </h4>
571
572 <div>
573
574 <div class="doc_code">
575 <pre>
576 !5 = metadata !{
577   i32,      ;; Tag (see below)
578   metadata, ;; Reference to context
579   metadata, ;; Name (may be "" for anonymous types)
580   metadata, ;; Reference to file where defined (may be NULL)
581   i32,      ;; Line number where defined (may be 0)
582   i64,      ;; Size in bits
583   i64,      ;; Alignment in bits
584   i64,      ;; Offset in bits
585   i32,      ;; Flags to encode attributes, e.g. private
586   metadata, ;; Reference to type derived from
587   metadata, ;; (optional) Name of the Objective C property associated with
588             ;; Objective-C an ivar
589   metadata, ;; (optional) Name of the Objective C property getter selector.
590   metadata, ;; (optional) Name of the Objective C property setter selector.
591   i32       ;; (optional) Objective C property attributes.
592 }
593 </pre>
594 </div>
595
596 <p>These descriptors are used to define types derived from other types.  The
597 value of the tag varies depending on the meaning.  The following are possible
598 tag values:</p>
599
600 <div class="doc_code">
601 <pre>
602 DW_TAG_formal_parameter = 5
603 DW_TAG_member           = 13
604 DW_TAG_pointer_type     = 15
605 DW_TAG_reference_type   = 16
606 DW_TAG_typedef          = 22
607 DW_TAG_const_type       = 38
608 DW_TAG_volatile_type    = 53
609 DW_TAG_restrict_type    = 55
610 </pre>
611 </div>
612
613 <p><tt>DW_TAG_member</tt> is used to define a member of
614    a <a href="#format_composite_type">composite type</a>
615    or <a href="#format_subprograms">subprogram</a>.  The type of the member is
616    the <a href="#format_derived_type">derived
617    type</a>. <tt>DW_TAG_formal_parameter</tt> is used to define a member which
618    is a formal argument of a subprogram.</p>
619
620 <p><tt>DW_TAG_typedef</tt> is used to provide a name for the derived type.</p>
621
622 <p><tt>DW_TAG_pointer_type</tt>, <tt>DW_TAG_reference_type</tt>,
623    <tt>DW_TAG_const_type</tt>, <tt>DW_TAG_volatile_type</tt> and
624    <tt>DW_TAG_restrict_type</tt> are used to qualify
625    the <a href="#format_derived_type">derived type</a>. </p>
626
627 <p><a href="#format_derived_type">Derived type</a> location can be determined
628    from the context and line number.  The size, alignment and offset are
629    expressed in bits and can be 64 bit values.  The alignment is used to round
630    the offset when embedded in a <a href="#format_composite_type">composite
631    type</a> (example to keep float doubles on 64 bit boundaries.) The offset is
632    the bit offset if embedded in a <a href="#format_composite_type">composite
633    type</a>.</p>
634
635 <p>Note that the <tt>void *</tt> type is expressed as a type derived from NULL.
636 </p>
637
638 </div>
639
640 <!-- ======================================================================= -->
641 <h4>
642   <a name="format_composite_type">Composite type descriptors</a>
643 </h4>
644
645 <div>
646
647 <div class="doc_code">
648 <pre>
649 !6 = metadata !{
650   i32,      ;; Tag (see below)
651   metadata, ;; Reference to context
652   metadata, ;; Name (may be "" for anonymous types)
653   metadata, ;; Reference to file where defined (may be NULL)
654   i32,      ;; Line number where defined (may be 0)
655   i64,      ;; Size in bits
656   i64,      ;; Alignment in bits
657   i64,      ;; Offset in bits
658   i32,      ;; Flags
659   metadata, ;; Reference to type derived from
660   metadata, ;; Reference to array of member descriptors
661   i32       ;; Runtime languages
662 }
663 </pre>
664 </div>
665
666 <p>These descriptors are used to define types that are composed of 0 or more
667 elements.  The value of the tag varies depending on the meaning.  The following
668 are possible tag values:</p>
669
670 <div class="doc_code">
671 <pre>
672 DW_TAG_array_type       = 1
673 DW_TAG_enumeration_type = 4
674 DW_TAG_structure_type   = 19
675 DW_TAG_union_type       = 23
676 DW_TAG_vector_type      = 259
677 DW_TAG_subroutine_type  = 21
678 DW_TAG_inheritance      = 28
679 </pre>
680 </div>
681
682 <p>The vector flag indicates that an array type is a native packed vector.</p>
683
684 <p>The members of array types (tag = <tt>DW_TAG_array_type</tt>) or vector types
685    (tag = <tt>DW_TAG_vector_type</tt>) are <a href="#format_subrange">subrange
686    descriptors</a>, each representing the range of subscripts at that level of
687    indexing.</p>
688
689 <p>The members of enumeration types (tag = <tt>DW_TAG_enumeration_type</tt>) are
690    <a href="#format_enumeration">enumerator descriptors</a>, each representing
691    the definition of enumeration value for the set. All enumeration type
692    descriptors are collected inside the named metadata
693    <tt>!llvm.dbg.cu</tt>.</p>
694
695 <p>The members of structure (tag = <tt>DW_TAG_structure_type</tt>) or union (tag
696    = <tt>DW_TAG_union_type</tt>) types are any one of
697    the <a href="#format_basic_type">basic</a>,
698    <a href="#format_derived_type">derived</a>
699    or <a href="#format_composite_type">composite</a> type descriptors, each
700    representing a field member of the structure or union.</p>
701
702 <p>For C++ classes (tag = <tt>DW_TAG_structure_type</tt>), member descriptors
703    provide information about base classes, static members and member
704    functions. If a member is a <a href="#format_derived_type">derived type
705    descriptor</a> and has a tag of <tt>DW_TAG_inheritance</tt>, then the type
706    represents a base class. If the member of is
707    a <a href="#format_global_variables">global variable descriptor</a> then it
708    represents a static member.  And, if the member is
709    a <a href="#format_subprograms">subprogram descriptor</a> then it represents
710    a member function.  For static members and member
711    functions, <tt>getName()</tt> returns the members link or the C++ mangled
712    name.  <tt>getDisplayName()</tt> the simplied version of the name.</p>
713
714 <p>The first member of subroutine (tag = <tt>DW_TAG_subroutine_type</tt>) type
715    elements is the return type for the subroutine.  The remaining elements are
716    the formal arguments to the subroutine.</p>
717
718 <p><a href="#format_composite_type">Composite type</a> location can be
719    determined from the context and line number.  The size, alignment and
720    offset are expressed in bits and can be 64 bit values.  The alignment is used
721    to round the offset when embedded in
722    a <a href="#format_composite_type">composite type</a> (as an example, to keep
723    float doubles on 64 bit boundaries.) The offset is the bit offset if embedded
724    in a <a href="#format_composite_type">composite type</a>.</p>
725
726 </div>
727
728 <!-- ======================================================================= -->
729 <h4>
730   <a name="format_subrange">Subrange descriptors</a>
731 </h4>
732
733 <div>
734
735 <div class="doc_code">
736 <pre>
737 !42 = metadata !{
738   i32,    ;; Tag = 33 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a> (DW_TAG_subrange_type)
739   i64,    ;; Low value
740   i64     ;; High value
741 }
742 </pre>
743 </div>
744
745 <p>These descriptors are used to define ranges of array subscripts for an array
746    <a href="#format_composite_type">composite type</a>.  The low value defines
747    the lower bounds typically zero for C/C++.  The high value is the upper
748    bounds.  Values are 64 bit.  High - low + 1 is the size of the array.  If low
749    > high the array bounds are not included in generated debugging information.
750 </p>
751
752 </div>
753
754 <!-- ======================================================================= -->
755 <h4>
756   <a name="format_enumeration">Enumerator descriptors</a>
757 </h4>
758
759 <div>
760
761 <div class="doc_code">
762 <pre>
763 !6 = metadata !{
764   i32,      ;; Tag = 40 + <a href="#LLVMDebugVersion">LLVMDebugVersion</a>
765             ;; (DW_TAG_enumerator)
766   metadata, ;; Name
767   i64       ;; Value
768 }
769 </pre>
770 </div>
771
772 <p>These descriptors are used to define members of an
773    enumeration <a href="#format_composite_type">composite type</a>, it
774    associates the name to the value.</p>
775
776 </div>
777
778 <!-- ======================================================================= -->
779 <h4>
780   <a name="format_variables">Local variables</a>
781 </h4>
782
783 <div>
784
785 <div class="doc_code">
786 <pre>
787 !7 = metadata !{
788   i32,      ;; Tag (see below)
789   metadata, ;; Context
790   metadata, ;; Name
791   metadata, ;; Reference to file where defined
792   i32,      ;; 24 bit - Line number where defined
793             ;; 8 bit - Argument number. 1 indicates 1st argument.
794   metadata, ;; Type descriptor
795   i32,      ;; flags
796   metadata  ;; (optional) Reference to inline location
797 }
798 </pre>
799 </div>
800
801 <p>These descriptors are used to define variables local to a sub program.  The
802    value of the tag depends on the usage of the variable:</p>
803
804 <div class="doc_code">
805 <pre>
806 DW_TAG_auto_variable   = 256
807 DW_TAG_arg_variable    = 257
808 DW_TAG_return_variable = 258
809 </pre>
810 </div>
811
812 <p>An auto variable is any variable declared in the body of the function.  An
813    argument variable is any variable that appears as a formal argument to the
814    function.  A return variable is used to track the result of a function and
815    has no source correspondent.</p>
816
817 <p>The context is either the subprogram or block where the variable is defined.
818    Name the source variable name.  Context and line indicate where the
819    variable was defined. Type descriptor defines the declared type of the
820    variable.</p>
821
822 </div>
823
824 </div>
825
826 <!-- ======================================================================= -->
827 <h3>
828   <a name="format_common_intrinsics">Debugger intrinsic functions</a>
829 </h3>
830
831 <div>
832
833 <p>LLVM uses several intrinsic functions (name prefixed with "llvm.dbg") to
834    provide debug information at various points in generated code.</p>
835
836 <!-- ======================================================================= -->
837 <h4>
838   <a name="format_common_declare">llvm.dbg.declare</a>
839 </h4>
840
841 <div>
842 <pre>
843   void %<a href="#format_common_declare">llvm.dbg.declare</a>(metadata, metadata)
844 </pre>
845
846 <p>This intrinsic provides information about a local element (e.g., variable). The
847    first argument is metadata holding the alloca for the variable. The
848    second argument is metadata containing a description of the variable.</p>
849 </div>
850
851 <!-- ======================================================================= -->
852 <h4>
853   <a name="format_common_value">llvm.dbg.value</a>
854 </h4>
855
856 <div>
857 <pre>
858   void %<a href="#format_common_value">llvm.dbg.value</a>(metadata, i64, metadata)
859 </pre>
860
861 <p>This intrinsic provides information when a user source variable is set to a
862    new value.  The first argument is the new value (wrapped as metadata).  The
863    second argument is the offset in the user source variable where the new value
864    is written.  The third argument is metadata containing a description of the
865    user source variable.</p>
866 </div>
867
868 </div>
869
870 <!-- ======================================================================= -->
871 <h3>
872   <a name="format_common_lifetime">Object lifetimes and scoping</a>
873 </h3>
874
875 <div>
876 <p>In many languages, the local variables in functions can have their lifetimes
877    or scopes limited to a subset of a function.  In the C family of languages,
878    for example, variables are only live (readable and writable) within the
879    source block that they are defined in.  In functional languages, values are
880    only readable after they have been defined.  Though this is a very obvious
881    concept, it is non-trivial to model in LLVM, because it has no notion of
882    scoping in this sense, and does not want to be tied to a language's scoping
883    rules.</p>
884
885 <p>In order to handle this, the LLVM debug format uses the metadata attached to
886    llvm instructions to encode line number and scoping information. Consider
887    the following C fragment, for example:</p>
888
889 <div class="doc_code">
890 <pre>
891 1.  void foo() {
892 2.    int X = 21;
893 3.    int Y = 22;
894 4.    {
895 5.      int Z = 23;
896 6.      Z = X;
897 7.    }
898 8.    X = Y;
899 9.  }
900 </pre>
901 </div>
902
903 <p>Compiled to LLVM, this function would be represented like this:</p>
904
905 <div class="doc_code">
906 <pre>
907 define void @foo() nounwind ssp {
908 entry:
909   %X = alloca i32, align 4                        ; &lt;i32*&gt; [#uses=4]
910   %Y = alloca i32, align 4                        ; &lt;i32*&gt; [#uses=4]
911   %Z = alloca i32, align 4                        ; &lt;i32*&gt; [#uses=3]
912   %0 = bitcast i32* %X to {}*                     ; &lt;{}*&gt; [#uses=1]
913   call void @llvm.dbg.declare(metadata !{i32 * %X}, metadata !0), !dbg !7
914   store i32 21, i32* %X, !dbg !8
915   %1 = bitcast i32* %Y to {}*                     ; &lt;{}*&gt; [#uses=1]
916   call void @llvm.dbg.declare(metadata !{i32 * %Y}, metadata !9), !dbg !10
917   store i32 22, i32* %Y, !dbg !11
918   %2 = bitcast i32* %Z to {}*                     ; &lt;{}*&gt; [#uses=1]
919   call void @llvm.dbg.declare(metadata !{i32 * %Z}, metadata !12), !dbg !14
920   store i32 23, i32* %Z, !dbg !15
921   %tmp = load i32* %X, !dbg !16                   ; &lt;i32&gt; [#uses=1]
922   %tmp1 = load i32* %Y, !dbg !16                  ; &lt;i32&gt; [#uses=1]
923   %add = add nsw i32 %tmp, %tmp1, !dbg !16        ; &lt;i32&gt; [#uses=1]
924   store i32 %add, i32* %Z, !dbg !16
925   %tmp2 = load i32* %Y, !dbg !17                  ; &lt;i32&gt; [#uses=1]
926   store i32 %tmp2, i32* %X, !dbg !17
927   ret void, !dbg !18
928 }
929
930 declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone
931
932 !0 = metadata !{i32 459008, metadata !1, metadata !"X",
933                 metadata !3, i32 2, metadata !6}; [ DW_TAG_auto_variable ]
934 !1 = metadata !{i32 458763, metadata !2}; [DW_TAG_lexical_block ]
935 !2 = metadata !{i32 458798, i32 0, metadata !3, metadata !"foo", metadata !"foo",
936                metadata !"foo", metadata !3, i32 1, metadata !4,
937                i1 false, i1 true}; [DW_TAG_subprogram ]
938 !3 = metadata !{i32 458769, i32 0, i32 12, metadata !"foo.c",
939                 metadata !"/private/tmp", metadata !"clang 1.1", i1 true,
940                 i1 false, metadata !"", i32 0}; [DW_TAG_compile_unit ]
941 !4 = metadata !{i32 458773, metadata !3, metadata !"", null, i32 0, i64 0, i64 0,
942                 i64 0, i32 0, null, metadata !5, i32 0}; [DW_TAG_subroutine_type ]
943 !5 = metadata !{null}
944 !6 = metadata !{i32 458788, metadata !3, metadata !"int", metadata !3, i32 0,
945                 i64 32, i64 32, i64 0, i32 0, i32 5}; [DW_TAG_base_type ]
946 !7 = metadata !{i32 2, i32 7, metadata !1, null}
947 !8 = metadata !{i32 2, i32 3, metadata !1, null}
948 !9 = metadata !{i32 459008, metadata !1, metadata !"Y", metadata !3, i32 3,
949                 metadata !6}; [ DW_TAG_auto_variable ]
950 !10 = metadata !{i32 3, i32 7, metadata !1, null}
951 !11 = metadata !{i32 3, i32 3, metadata !1, null}
952 !12 = metadata !{i32 459008, metadata !13, metadata !"Z", metadata !3, i32 5,
953                  metadata !6}; [ DW_TAG_auto_variable ]
954 !13 = metadata !{i32 458763, metadata !1}; [DW_TAG_lexical_block ]
955 !14 = metadata !{i32 5, i32 9, metadata !13, null}
956 !15 = metadata !{i32 5, i32 5, metadata !13, null}
957 !16 = metadata !{i32 6, i32 5, metadata !13, null}
958 !17 = metadata !{i32 8, i32 3, metadata !1, null}
959 !18 = metadata !{i32 9, i32 1, metadata !2, null}
960 </pre>
961 </div>
962
963 <p>This example illustrates a few important details about LLVM debugging
964    information. In particular, it shows how the <tt>llvm.dbg.declare</tt>
965    intrinsic and location information, which are attached to an instruction,
966    are applied together to allow a debugger to analyze the relationship between
967    statements, variable definitions, and the code used to implement the
968    function.</p>
969
970 <div class="doc_code">
971 <pre>
972 call void @llvm.dbg.declare(metadata, metadata !0), !dbg !7
973 </pre>
974 </div>
975
976 <p>The first intrinsic
977    <tt>%<a href="#format_common_declare">llvm.dbg.declare</a></tt>
978    encodes debugging information for the variable <tt>X</tt>. The metadata
979    <tt>!dbg !7</tt> attached to the intrinsic provides scope information for the
980    variable <tt>X</tt>.</p>
981
982 <div class="doc_code">
983 <pre>
984 !7 = metadata !{i32 2, i32 7, metadata !1, null}
985 !1 = metadata !{i32 458763, metadata !2}; [DW_TAG_lexical_block ]
986 !2 = metadata !{i32 458798, i32 0, metadata !3, metadata !"foo",
987                 metadata !"foo", metadata !"foo", metadata !3, i32 1,
988                 metadata !4, i1 false, i1 true}; [DW_TAG_subprogram ]
989 </pre>
990 </div>
991
992 <p>Here <tt>!7</tt> is metadata providing location information. It has four
993    fields: line number, column number, scope, and original scope. The original
994    scope represents inline location if this instruction is inlined inside a
995    caller, and is null otherwise. In this example, scope is encoded by
996    <tt>!1</tt>. <tt>!1</tt> represents a lexical block inside the scope
997    <tt>!2</tt>, where <tt>!2</tt> is a
998    <a href="#format_subprograms">subprogram descriptor</a>. This way the
999    location information attached to the intrinsics indicates that the
1000    variable <tt>X</tt> is declared at line number 2 at a function level scope in
1001    function <tt>foo</tt>.</p>
1002
1003 <p>Now lets take another example.</p>
1004
1005 <div class="doc_code">
1006 <pre>
1007 call void @llvm.dbg.declare(metadata, metadata !12), !dbg !14
1008 </pre>
1009 </div>
1010
1011 <p>The second intrinsic
1012    <tt>%<a href="#format_common_declare">llvm.dbg.declare</a></tt>
1013    encodes debugging information for variable <tt>Z</tt>. The metadata
1014    <tt>!dbg !14</tt> attached to the intrinsic provides scope information for
1015    the variable <tt>Z</tt>.</p>
1016
1017 <div class="doc_code">
1018 <pre>
1019 !13 = metadata !{i32 458763, metadata !1}; [DW_TAG_lexical_block ]
1020 !14 = metadata !{i32 5, i32 9, metadata !13, null}
1021 </pre>
1022 </div>
1023
1024 <p>Here <tt>!14</tt> indicates that <tt>Z</tt> is declared at line number 5 and
1025    column number 9 inside of lexical scope <tt>!13</tt>. The lexical scope
1026    itself resides inside of lexical scope <tt>!1</tt> described above.</p>
1027
1028 <p>The scope information attached with each instruction provides a
1029    straightforward way to find instructions covered by a scope.</p>
1030
1031 </div>
1032
1033 </div>
1034
1035 <!-- *********************************************************************** -->
1036 <h2>
1037   <a name="ccxx_frontend">C/C++ front-end specific debug information</a>
1038 </h2>
1039 <!-- *********************************************************************** -->
1040
1041 <div>
1042
1043 <p>The C and C++ front-ends represent information about the program in a format
1044    that is effectively identical
1045    to <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3.0</a> in
1046    terms of information content.  This allows code generators to trivially
1047    support native debuggers by generating standard dwarf information, and
1048    contains enough information for non-dwarf targets to translate it as
1049    needed.</p>
1050
1051 <p>This section describes the forms used to represent C and C++ programs. Other
1052    languages could pattern themselves after this (which itself is tuned to
1053    representing programs in the same way that DWARF 3 does), or they could
1054    choose to provide completely different forms if they don't fit into the DWARF
1055    model.  As support for debugging information gets added to the various LLVM
1056    source-language front-ends, the information used should be documented
1057    here.</p>
1058
1059 <p>The following sections provide examples of various C/C++ constructs and the
1060    debug information that would best describe those constructs.</p>
1061
1062 <!-- ======================================================================= -->
1063 <h3>
1064   <a name="ccxx_compile_units">C/C++ source file information</a>
1065 </h3>
1066
1067 <div>
1068
1069 <p>Given the source files <tt>MySource.cpp</tt> and <tt>MyHeader.h</tt> located
1070    in the directory <tt>/Users/mine/sources</tt>, the following code:</p>
1071
1072 <div class="doc_code">
1073 <pre>
1074 #include "MyHeader.h"
1075
1076 int main(int argc, char *argv[]) {
1077   return 0;
1078 }
1079 </pre>
1080 </div>
1081
1082 <p>a C/C++ front-end would generate the following descriptors:</p>
1083
1084 <div class="doc_code">
1085 <pre>
1086 ...
1087 ;;
1088 ;; Define the compile unit for the main source file "/Users/mine/sources/MySource.cpp".
1089 ;;
1090 !2 = metadata !{
1091   i32 524305,    ;; Tag
1092   i32 0,         ;; Unused
1093   i32 4,         ;; Language Id
1094   metadata !"MySource.cpp",
1095   metadata !"/Users/mine/sources",
1096   metadata !"4.2.1 (Based on Apple Inc. build 5649) (LLVM build 00)",
1097   i1 true,       ;; Main Compile Unit
1098   i1 false,      ;; Optimized compile unit
1099   metadata !"",  ;; Compiler flags
1100   i32 0}         ;; Runtime version
1101
1102 ;;
1103 ;; Define the file for the file "/Users/mine/sources/MySource.cpp".
1104 ;;
1105 !1 = metadata !{
1106   i32 524329,    ;; Tag
1107   metadata !"MySource.cpp",
1108   metadata !"/Users/mine/sources",
1109   metadata !2    ;; Compile unit
1110 }
1111
1112 ;;
1113 ;; Define the file for the file "/Users/mine/sources/Myheader.h"
1114 ;;
1115 !3 = metadata !{
1116   i32 524329,    ;; Tag
1117   metadata !"Myheader.h"
1118   metadata !"/Users/mine/sources",
1119   metadata !2    ;; Compile unit
1120 }
1121
1122 ...
1123 </pre>
1124 </div>
1125
1126 <p>llvm::Instruction provides easy access to metadata attached with an
1127 instruction. One can extract line number information encoded in LLVM IR
1128 using <tt>Instruction::getMetadata()</tt> and
1129 <tt>DILocation::getLineNumber()</tt>.
1130 <pre>
1131  if (MDNode *N = I->getMetadata("dbg")) {  // Here I is an LLVM instruction
1132    DILocation Loc(N);                      // DILocation is in DebugInfo.h
1133    unsigned Line = Loc.getLineNumber();
1134    StringRef File = Loc.getFilename();
1135    StringRef Dir = Loc.getDirectory();
1136  }
1137 </pre>
1138 </div>
1139
1140 <!-- ======================================================================= -->
1141 <h3>
1142   <a name="ccxx_global_variable">C/C++ global variable information</a>
1143 </h3>
1144
1145 <div>
1146
1147 <p>Given an integer global variable declared as follows:</p>
1148
1149 <div class="doc_code">
1150 <pre>
1151 int MyGlobal = 100;
1152 </pre>
1153 </div>
1154
1155 <p>a C/C++ front-end would generate the following descriptors:</p>
1156
1157 <div class="doc_code">
1158 <pre>
1159 ;;
1160 ;; Define the global itself.
1161 ;;
1162 %MyGlobal = global int 100
1163 ...
1164 ;;
1165 ;; List of debug info of globals
1166 ;;
1167 !llvm.dbg.cu = !{!0}
1168
1169 ;; Define the compile unit.
1170 !0 = metadata !{
1171   i32 786449,                       ;; Tag
1172   i32 0,                            ;; Context
1173   i32 4,                            ;; Language
1174   metadata !"foo.cpp",              ;; File
1175   metadata !"/Volumes/Data/tmp",    ;; Directory
1176   metadata !"clang version 3.1 ",   ;; Producer
1177   i1 true,                          ;; Deprecated field
1178   i1 false,                         ;; "isOptimized"?
1179   metadata !"",                     ;; Flags
1180   i32 0,                            ;; Runtime Version
1181   metadata !1,                      ;; Enum Types
1182   metadata !1,                      ;; Retained Types
1183   metadata !1,                      ;; Subprograms
1184   metadata !3                       ;; Global Variables
1185 } ; [ DW_TAG_compile_unit ]
1186
1187 ;; The Array of Global Variables
1188 !3 = metadata !{
1189   metadata !4
1190 }
1191
1192 !4 = metadata !{
1193   metadata !5
1194 }
1195
1196 ;;
1197 ;; Define the global variable itself.
1198 ;;
1199 !5 = metadata !{
1200   i32 786484,                        ;; Tag
1201   i32 0,                             ;; Unused
1202   null,                              ;; Unused
1203   metadata !"MyGlobal",              ;; Name
1204   metadata !"MyGlobal",              ;; Display Name
1205   metadata !"",                      ;; Linkage Name
1206   metadata !6,                       ;; File
1207   i32 1,                             ;; Line
1208   metadata !7,                       ;; Type
1209   i32 0,                             ;; IsLocalToUnit
1210   i32 1,                             ;; IsDefinition
1211   i32* @MyGlobal                     ;; LLVM-IR Value
1212 } ; [ DW_TAG_variable ]
1213
1214 ;;
1215 ;; Define the file
1216 ;;
1217 !6 = metadata !{
1218   i32 786473,                        ;; Tag
1219   metadata !"foo.cpp",               ;; File
1220   metadata !"/Volumes/Data/tmp",     ;; Directory
1221   null                               ;; Unused
1222 } ; [ DW_TAG_file_type ]
1223
1224 ;;
1225 ;; Define the type
1226 ;;
1227 !7 = metadata !{
1228   i32 786468,                         ;; Tag
1229   null,                               ;; Unused
1230   metadata !"int",                    ;; Name
1231   null,                               ;; Unused
1232   i32 0,                              ;; Line
1233   i64 32,                             ;; Size in Bits
1234   i64 32,                             ;; Align in Bits
1235   i64 0,                              ;; Offset
1236   i32 0,                              ;; Flags
1237   i32 5                               ;; Encoding
1238 } ; [ DW_TAG_base_type ]
1239
1240 </pre>
1241 </div>
1242
1243 </div>
1244
1245 <!-- ======================================================================= -->
1246 <h3>
1247   <a name="ccxx_subprogram">C/C++ function information</a>
1248 </h3>
1249
1250 <div>
1251
1252 <p>Given a function declared as follows:</p>
1253
1254 <div class="doc_code">
1255 <pre>
1256 int main(int argc, char *argv[]) {
1257   return 0;
1258 }
1259 </pre>
1260 </div>
1261
1262 <p>a C/C++ front-end would generate the following descriptors:</p>
1263
1264 <div class="doc_code">
1265 <pre>
1266 ;;
1267 ;; Define the anchor for subprograms.  Note that the second field of the
1268 ;; anchor is 46, which is the same as the tag for subprograms
1269 ;; (46 = DW_TAG_subprogram.)
1270 ;;
1271 !6 = metadata !{
1272   i32 524334,        ;; Tag
1273   i32 0,             ;; Unused
1274   metadata !1,       ;; Context
1275   metadata !"main",  ;; Name
1276   metadata !"main",  ;; Display name
1277   metadata !"main",  ;; Linkage name
1278   metadata !1,       ;; File
1279   i32 1,             ;; Line number
1280   metadata !4,       ;; Type
1281   i1 false,          ;; Is local
1282   i1 true,           ;; Is definition
1283   i32 0,             ;; Virtuality attribute, e.g. pure virtual function
1284   i32 0,             ;; Index into virtual table for C++ methods
1285   i32 0,             ;; Type that holds virtual table.
1286   i32 0,             ;; Flags
1287   i1 false,          ;; True if this function is optimized
1288   Function *,        ;; Pointer to llvm::Function
1289   null               ;; Function template parameters
1290 }
1291 ;;
1292 ;; Define the subprogram itself.
1293 ;;
1294 define i32 @main(i32 %argc, i8** %argv) {
1295 ...
1296 }
1297 </pre>
1298 </div>
1299
1300 </div>
1301
1302 <!-- ======================================================================= -->
1303 <h3>
1304   <a name="ccxx_basic_types">C/C++ basic types</a>
1305 </h3>
1306
1307 <div>
1308
1309 <p>The following are the basic type descriptors for C/C++ core types:</p>
1310
1311 <!-- ======================================================================= -->
1312 <h4>
1313   <a name="ccxx_basic_type_bool">bool</a>
1314 </h4>
1315
1316 <div>
1317
1318 <div class="doc_code">
1319 <pre>
1320 !2 = metadata !{
1321   i32 524324,        ;; Tag
1322   metadata !1,       ;; Context
1323   metadata !"bool",  ;; Name
1324   metadata !1,       ;; File
1325   i32 0,             ;; Line number
1326   i64 8,             ;; Size in Bits
1327   i64 8,             ;; Align in Bits
1328   i64 0,             ;; Offset in Bits
1329   i32 0,             ;; Flags
1330   i32 2              ;; Encoding
1331 }
1332 </pre>
1333 </div>
1334
1335 </div>
1336
1337 <!-- ======================================================================= -->
1338 <h4>
1339   <a name="ccxx_basic_char">char</a>
1340 </h4>
1341
1342 <div>
1343
1344 <div class="doc_code">
1345 <pre>
1346 !2 = metadata !{
1347   i32 524324,        ;; Tag
1348   metadata !1,       ;; Context
1349   metadata !"char",  ;; Name
1350   metadata !1,       ;; File
1351   i32 0,             ;; Line number
1352   i64 8,             ;; Size in Bits
1353   i64 8,             ;; Align in Bits
1354   i64 0,             ;; Offset in Bits
1355   i32 0,             ;; Flags
1356   i32 6              ;; Encoding
1357 }
1358 </pre>
1359 </div>
1360
1361 </div>
1362
1363 <!-- ======================================================================= -->
1364 <h4>
1365   <a name="ccxx_basic_unsigned_char">unsigned char</a>
1366 </h4>
1367
1368 <div>
1369
1370 <div class="doc_code">
1371 <pre>
1372 !2 = metadata !{
1373   i32 524324,        ;; Tag
1374   metadata !1,       ;; Context
1375   metadata !"unsigned char",
1376   metadata !1,       ;; File
1377   i32 0,             ;; Line number
1378   i64 8,             ;; Size in Bits
1379   i64 8,             ;; Align in Bits
1380   i64 0,             ;; Offset in Bits
1381   i32 0,             ;; Flags
1382   i32 8              ;; Encoding
1383 }
1384 </pre>
1385 </div>
1386
1387 </div>
1388
1389 <!-- ======================================================================= -->
1390 <h4>
1391   <a name="ccxx_basic_short">short</a>
1392 </h4>
1393
1394 <div>
1395
1396 <div class="doc_code">
1397 <pre>
1398 !2 = metadata !{
1399   i32 524324,        ;; Tag
1400   metadata !1,       ;; Context
1401   metadata !"short int",
1402   metadata !1,       ;; File
1403   i32 0,             ;; Line number
1404   i64 16,            ;; Size in Bits
1405   i64 16,            ;; Align in Bits
1406   i64 0,             ;; Offset in Bits
1407   i32 0,             ;; Flags
1408   i32 5              ;; Encoding
1409 }
1410 </pre>
1411 </div>
1412
1413 </div>
1414
1415 <!-- ======================================================================= -->
1416 <h4>
1417   <a name="ccxx_basic_unsigned_short">unsigned short</a>
1418 </h4>
1419
1420 <div>
1421
1422 <div class="doc_code">
1423 <pre>
1424 !2 = metadata !{
1425   i32 524324,        ;; Tag
1426   metadata !1,       ;; Context
1427   metadata !"short unsigned int",
1428   metadata !1,       ;; File
1429   i32 0,             ;; Line number
1430   i64 16,            ;; Size in Bits
1431   i64 16,            ;; Align in Bits
1432   i64 0,             ;; Offset in Bits
1433   i32 0,             ;; Flags
1434   i32 7              ;; Encoding
1435 }
1436 </pre>
1437 </div>
1438
1439 </div>
1440
1441 <!-- ======================================================================= -->
1442 <h4>
1443   <a name="ccxx_basic_int">int</a>
1444 </h4>
1445
1446 <div>
1447
1448 <div class="doc_code">
1449 <pre>
1450 !2 = metadata !{
1451   i32 524324,        ;; Tag
1452   metadata !1,       ;; Context
1453   metadata !"int",   ;; Name
1454   metadata !1,       ;; File
1455   i32 0,             ;; Line number
1456   i64 32,            ;; Size in Bits
1457   i64 32,            ;; Align in Bits
1458   i64 0,             ;; Offset in Bits
1459   i32 0,             ;; Flags
1460   i32 5              ;; Encoding
1461 }
1462 </pre></div>
1463
1464 </div>
1465
1466 <!-- ======================================================================= -->
1467 <h4>
1468   <a name="ccxx_basic_unsigned_int">unsigned int</a>
1469 </h4>
1470
1471 <div>
1472
1473 <div class="doc_code">
1474 <pre>
1475 !2 = metadata !{
1476   i32 524324,        ;; Tag
1477   metadata !1,       ;; Context
1478   metadata !"unsigned int",
1479   metadata !1,       ;; File
1480   i32 0,             ;; Line number
1481   i64 32,            ;; Size in Bits
1482   i64 32,            ;; Align in Bits
1483   i64 0,             ;; Offset in Bits
1484   i32 0,             ;; Flags
1485   i32 7              ;; Encoding
1486 }
1487 </pre>
1488 </div>
1489
1490 </div>
1491
1492 <!-- ======================================================================= -->
1493 <h4>
1494   <a name="ccxx_basic_long_long">long long</a>
1495 </h4>
1496
1497 <div>
1498
1499 <div class="doc_code">
1500 <pre>
1501 !2 = metadata !{
1502   i32 524324,        ;; Tag
1503   metadata !1,       ;; Context
1504   metadata !"long long int",
1505   metadata !1,       ;; File
1506   i32 0,             ;; Line number
1507   i64 64,            ;; Size in Bits
1508   i64 64,            ;; Align in Bits
1509   i64 0,             ;; Offset in Bits
1510   i32 0,             ;; Flags
1511   i32 5              ;; Encoding
1512 }
1513 </pre>
1514 </div>
1515
1516 </div>
1517
1518 <!-- ======================================================================= -->
1519 <h4>
1520   <a name="ccxx_basic_unsigned_long_long">unsigned long long</a>
1521 </h4>
1522
1523 <div>
1524
1525 <div class="doc_code">
1526 <pre>
1527 !2 = metadata !{
1528   i32 524324,        ;; Tag
1529   metadata !1,       ;; Context
1530   metadata !"long long unsigned int",
1531   metadata !1,       ;; File
1532   i32 0,             ;; Line number
1533   i64 64,            ;; Size in Bits
1534   i64 64,            ;; Align in Bits
1535   i64 0,             ;; Offset in Bits
1536   i32 0,             ;; Flags
1537   i32 7              ;; Encoding
1538 }
1539 </pre>
1540 </div>
1541
1542 </div>
1543
1544 <!-- ======================================================================= -->
1545 <h4>
1546   <a name="ccxx_basic_float">float</a>
1547 </h4>
1548
1549 <div>
1550
1551 <div class="doc_code">
1552 <pre>
1553 !2 = metadata !{
1554   i32 524324,        ;; Tag
1555   metadata !1,       ;; Context
1556   metadata !"float",
1557   metadata !1,       ;; File
1558   i32 0,             ;; Line number
1559   i64 32,            ;; Size in Bits
1560   i64 32,            ;; Align in Bits
1561   i64 0,             ;; Offset in Bits
1562   i32 0,             ;; Flags
1563   i32 4              ;; Encoding
1564 }
1565 </pre>
1566 </div>
1567
1568 </div>
1569
1570 <!-- ======================================================================= -->
1571 <h4>
1572   <a name="ccxx_basic_double">double</a>
1573 </h4>
1574
1575 <div>
1576
1577 <div class="doc_code">
1578 <pre>
1579 !2 = metadata !{
1580   i32 524324,        ;; Tag
1581   metadata !1,       ;; Context
1582   metadata !"double",;; Name
1583   metadata !1,       ;; File
1584   i32 0,             ;; Line number
1585   i64 64,            ;; Size in Bits
1586   i64 64,            ;; Align in Bits
1587   i64 0,             ;; Offset in Bits
1588   i32 0,             ;; Flags
1589   i32 4              ;; Encoding
1590 }
1591 </pre>
1592 </div>
1593
1594 </div>
1595
1596 </div>
1597
1598 <!-- ======================================================================= -->
1599 <h3>
1600   <a name="ccxx_derived_types">C/C++ derived types</a>
1601 </h3>
1602
1603 <div>
1604
1605 <p>Given the following as an example of C/C++ derived type:</p>
1606
1607 <div class="doc_code">
1608 <pre>
1609 typedef const int *IntPtr;
1610 </pre>
1611 </div>
1612
1613 <p>a C/C++ front-end would generate the following descriptors:</p>
1614
1615 <div class="doc_code">
1616 <pre>
1617 ;;
1618 ;; Define the typedef "IntPtr".
1619 ;;
1620 !2 = metadata !{
1621   i32 524310,          ;; Tag
1622   metadata !1,         ;; Context
1623   metadata !"IntPtr",  ;; Name
1624   metadata !3,         ;; File
1625   i32 0,               ;; Line number
1626   i64 0,               ;; Size in bits
1627   i64 0,               ;; Align in bits
1628   i64 0,               ;; Offset in bits
1629   i32 0,               ;; Flags
1630   metadata !4          ;; Derived From type
1631 }
1632
1633 ;;
1634 ;; Define the pointer type.
1635 ;;
1636 !4 = metadata !{
1637   i32 524303,          ;; Tag
1638   metadata !1,         ;; Context
1639   metadata !"",        ;; Name
1640   metadata !1,         ;; File
1641   i32 0,               ;; Line number
1642   i64 64,              ;; Size in bits
1643   i64 64,              ;; Align in bits
1644   i64 0,               ;; Offset in bits
1645   i32 0,               ;; Flags
1646   metadata !5          ;; Derived From type
1647 }
1648 ;;
1649 ;; Define the const type.
1650 ;;
1651 !5 = metadata !{
1652   i32 524326,          ;; Tag
1653   metadata !1,         ;; Context
1654   metadata !"",        ;; Name
1655   metadata !1,         ;; File
1656   i32 0,               ;; Line number
1657   i64 32,              ;; Size in bits
1658   i64 32,              ;; Align in bits
1659   i64 0,               ;; Offset in bits
1660   i32 0,               ;; Flags
1661   metadata !6          ;; Derived From type
1662 }
1663 ;;
1664 ;; Define the int type.
1665 ;;
1666 !6 = metadata !{
1667   i32 524324,          ;; Tag
1668   metadata !1,         ;; Context
1669   metadata !"int",     ;; Name
1670   metadata !1,         ;; File
1671   i32 0,               ;; Line number
1672   i64 32,              ;; Size in bits
1673   i64 32,              ;; Align in bits
1674   i64 0,               ;; Offset in bits
1675   i32 0,               ;; Flags
1676   5                    ;; Encoding
1677 }
1678 </pre>
1679 </div>
1680
1681 </div>
1682
1683 <!-- ======================================================================= -->
1684 <h3>
1685   <a name="ccxx_composite_types">C/C++ struct/union types</a>
1686 </h3>
1687
1688 <div>
1689
1690 <p>Given the following as an example of C/C++ struct type:</p>
1691
1692 <div class="doc_code">
1693 <pre>
1694 struct Color {
1695   unsigned Red;
1696   unsigned Green;
1697   unsigned Blue;
1698 };
1699 </pre>
1700 </div>
1701
1702 <p>a C/C++ front-end would generate the following descriptors:</p>
1703
1704 <div class="doc_code">
1705 <pre>
1706 ;;
1707 ;; Define basic type for unsigned int.
1708 ;;
1709 !5 = metadata !{
1710   i32 524324,        ;; Tag
1711   metadata !1,       ;; Context
1712   metadata !"unsigned int",
1713   metadata !1,       ;; File
1714   i32 0,             ;; Line number
1715   i64 32,            ;; Size in Bits
1716   i64 32,            ;; Align in Bits
1717   i64 0,             ;; Offset in Bits
1718   i32 0,             ;; Flags
1719   i32 7              ;; Encoding
1720 }
1721 ;;
1722 ;; Define composite type for struct Color.
1723 ;;
1724 !2 = metadata !{
1725   i32 524307,        ;; Tag
1726   metadata !1,       ;; Context
1727   metadata !"Color", ;; Name
1728   metadata !1,       ;; Compile unit
1729   i32 1,             ;; Line number
1730   i64 96,            ;; Size in bits
1731   i64 32,            ;; Align in bits
1732   i64 0,             ;; Offset in bits
1733   i32 0,             ;; Flags
1734   null,              ;; Derived From
1735   metadata !3,       ;; Elements
1736   i32 0              ;; Runtime Language
1737 }
1738
1739 ;;
1740 ;; Define the Red field.
1741 ;;
1742 !4 = metadata !{
1743   i32 524301,        ;; Tag
1744   metadata !1,       ;; Context
1745   metadata !"Red",   ;; Name
1746   metadata !1,       ;; File
1747   i32 2,             ;; Line number
1748   i64 32,            ;; Size in bits
1749   i64 32,            ;; Align in bits
1750   i64 0,             ;; Offset in bits
1751   i32 0,             ;; Flags
1752   metadata !5        ;; Derived From type
1753 }
1754
1755 ;;
1756 ;; Define the Green field.
1757 ;;
1758 !6 = metadata !{
1759   i32 524301,        ;; Tag
1760   metadata !1,       ;; Context
1761   metadata !"Green", ;; Name
1762   metadata !1,       ;; File
1763   i32 3,             ;; Line number
1764   i64 32,            ;; Size in bits
1765   i64 32,            ;; Align in bits
1766   i64 32,             ;; Offset in bits
1767   i32 0,             ;; Flags
1768   metadata !5        ;; Derived From type
1769 }
1770
1771 ;;
1772 ;; Define the Blue field.
1773 ;;
1774 !7 = metadata !{
1775   i32 524301,        ;; Tag
1776   metadata !1,       ;; Context
1777   metadata !"Blue",  ;; Name
1778   metadata !1,       ;; File
1779   i32 4,             ;; Line number
1780   i64 32,            ;; Size in bits
1781   i64 32,            ;; Align in bits
1782   i64 64,             ;; Offset in bits
1783   i32 0,             ;; Flags
1784   metadata !5        ;; Derived From type
1785 }
1786
1787 ;;
1788 ;; Define the array of fields used by the composite type Color.
1789 ;;
1790 !3 = metadata !{metadata !4, metadata !6, metadata !7}
1791 </pre>
1792 </div>
1793
1794 </div>
1795
1796 <!-- ======================================================================= -->
1797 <h3>
1798   <a name="ccxx_enumeration_types">C/C++ enumeration types</a>
1799 </h3>
1800
1801 <div>
1802
1803 <p>Given the following as an example of C/C++ enumeration type:</p>
1804
1805 <div class="doc_code">
1806 <pre>
1807 enum Trees {
1808   Spruce = 100,
1809   Oak = 200,
1810   Maple = 300
1811 };
1812 </pre>
1813 </div>
1814
1815 <p>a C/C++ front-end would generate the following descriptors:</p>
1816
1817 <div class="doc_code">
1818 <pre>
1819 ;;
1820 ;; Define composite type for enum Trees
1821 ;;
1822 !2 = metadata !{
1823   i32 524292,        ;; Tag
1824   metadata !1,       ;; Context
1825   metadata !"Trees", ;; Name
1826   metadata !1,       ;; File
1827   i32 1,             ;; Line number
1828   i64 32,            ;; Size in bits
1829   i64 32,            ;; Align in bits
1830   i64 0,             ;; Offset in bits
1831   i32 0,             ;; Flags
1832   null,              ;; Derived From type
1833   metadata !3,       ;; Elements
1834   i32 0              ;; Runtime language
1835 }
1836
1837 ;;
1838 ;; Define the array of enumerators used by composite type Trees.
1839 ;;
1840 !3 = metadata !{metadata !4, metadata !5, metadata !6}
1841
1842 ;;
1843 ;; Define Spruce enumerator.
1844 ;;
1845 !4 = metadata !{i32 524328, metadata !"Spruce", i64 100}
1846
1847 ;;
1848 ;; Define Oak enumerator.
1849 ;;
1850 !5 = metadata !{i32 524328, metadata !"Oak", i64 200}
1851
1852 ;;
1853 ;; Define Maple enumerator.
1854 ;;
1855 !6 = metadata !{i32 524328, metadata !"Maple", i64 300}
1856
1857 </pre>
1858 </div>
1859
1860 </div>
1861
1862 </div>
1863
1864
1865 <!-- *********************************************************************** -->
1866 <h2>
1867   <a name="llvmdwarfextension">Debugging information format</a>
1868 </h2>
1869 <!-- *********************************************************************** -->
1870 <div>
1871 <!-- ======================================================================= -->
1872 <h3>
1873   <a name="objcproperty">Debugging Information Extension for Objective C Properties</a>
1874 </h3>
1875 <div>
1876 <!-- *********************************************************************** -->
1877 <h4>
1878   <a name="objcpropertyintroduction">Introduction</a>
1879 </h4>
1880 <!-- *********************************************************************** -->
1881
1882 <div>
1883 <p>Objective C provides a simpler way to declare and define accessor methods
1884 using declared properties. The language provides features to declare a
1885 property and to let compiler synthesize accessor methods.
1886 </p>
1887
1888 <p>The debugger lets developer inspect Objective C interfaces and their
1889 instance variables and class variables. However, the debugger does not know
1890 anything about the properties defined in Objective C interfaces. The debugger
1891 consumes information generated by compiler in DWARF format. The format does
1892 not support encoding of Objective C properties. This proposal describes DWARF
1893 extensions to encode Objective C properties, which the debugger can use to let
1894 developers inspect Objective C properties.
1895 </p>
1896
1897 </div>
1898
1899
1900 <!-- *********************************************************************** -->
1901 <h4>
1902   <a name="objcpropertyproposal">Proposal</a>
1903 </h4>
1904 <!-- *********************************************************************** -->
1905
1906 <div>
1907 <p>Objective C properties exist separately from class members. A property
1908 can be defined only by &quot;setter&quot; and &quot;getter&quot; selectors, and
1909 be calculated anew on each access.  Or a property can just be a direct access
1910 to some declared ivar.  Finally it can have an ivar &quot;automatically
1911 synthesized&quot; for it by the compiler, in which case the property can be
1912 referred to in user code directly using the standard C dereference syntax as
1913 well as through the property &quot;dot&quot; syntax, but there is no entry in
1914 the @interface declaration corresponding to this ivar.
1915 </p>
1916 <p>
1917 To facilitate debugging, these properties we will add a new DWARF TAG into the
1918 DW_TAG_structure_type definition for the class to hold the description of a
1919 given property, and a set of DWARF attributes that provide said description.
1920 The property tag will also contain the name and declared type of the property.
1921 </p>
1922 <p>
1923 If there is a related ivar, there will also be a DWARF property attribute placed
1924 in the DW_TAG_member DIE for that ivar referring back to the property TAG for
1925 that property. And in the case where the compiler synthesizes the ivar directly,
1926 the compiler is expected to generate a DW_TAG_member for that ivar (with the
1927 DW_AT_artificial set to 1), whose name will be the name used to access this
1928 ivar directly in code, and with the property attribute pointing back to the
1929 property it is backing.
1930 </p>
1931 <p>
1932 The following examples will serve as illustration for our discussion:
1933 </p>
1934
1935 <div class="doc_code">
1936 <pre>
1937 @interface I1 {
1938   int n2;
1939 }
1940
1941 @property int p1;
1942 @property int p2;
1943 @end
1944
1945 @implementation I1
1946 @synthesize p1;
1947 @synthesize p2 = n2;
1948 @end
1949 </pre>
1950 </div>
1951
1952 <p>
1953 This produces the following DWARF (this is a &quot;pseudo dwarfdump&quot; output):
1954 </p>
1955 <div class="doc_code">
1956 <pre>
1957 0x00000100:  TAG_structure_type [7] *
1958                AT_APPLE_runtime_class( 0x10 )
1959                AT_name( "I1" )
1960                AT_decl_file( "Objc_Property.m" )
1961                AT_decl_line( 3 )
1962
1963 0x00000110    TAG_APPLE_property
1964                 AT_name ( "p1" )
1965                 AT_type ( {0x00000150} ( int ) )
1966
1967 0x00000120:   TAG_APPLE_property
1968                 AT_name ( "p2" )
1969                 AT_type ( {0x00000150} ( int ) )
1970
1971 0x00000130:   TAG_member [8]
1972                 AT_name( "_p1" )
1973                 AT_APPLE_property ( {0x00000110} "p1" )
1974                 AT_type( {0x00000150} ( int ) )
1975                 AT_artificial ( 0x1 )
1976
1977 0x00000140:    TAG_member [8]
1978                  AT_name( "n2" )
1979                  AT_APPLE_property ( {0x00000120} "p2" )
1980                  AT_type( {0x00000150} ( int ) )
1981
1982 0x00000150:  AT_type( ( int ) )
1983 </pre>
1984 </div>
1985
1986 <p> Note, the current convention is that the name of the ivar for an
1987 auto-synthesized property is the name of the property from which it derives with
1988 an underscore prepended, as is shown in the example.
1989 But we actually don't need to know this convention, since we are given the name
1990 of the ivar directly.
1991 </p>
1992
1993 <p>
1994 Also, it is common practice in ObjC to have different property declarations in
1995 the @interface and @implementation - e.g. to provide a read-only property in
1996 the interface,and a read-write interface in the implementation.  In that case,
1997 the compiler should emit whichever property declaration will be in force in the
1998 current translation unit.
1999 </p>
2000
2001 <p> Developers can decorate a property with attributes which are encoded using
2002 DW_AT_APPLE_property_attribute.
2003 </p>
2004
2005 <div class="doc_code">
2006 <pre>
2007 @property (readonly, nonatomic) int pr;
2008 </pre>
2009 </div>
2010 <p>
2011 Which produces a property tag:
2012 <p>
2013 <div class="doc_code">
2014 <pre>
2015 TAG_APPLE_property [8]
2016   AT_name( "pr" )
2017   AT_type ( {0x00000147} (int) )
2018   AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic)
2019 </pre>
2020 </div>
2021
2022 <p> The setter and getter method names are attached to the property using
2023 DW_AT_APPLE_property_setter and DW_AT_APPLE_property_getter attributes.
2024 </p>
2025 <div class="doc_code">
2026 <pre>
2027 @interface I1
2028 @property (setter=myOwnP3Setter:) int p3;
2029 -(void)myOwnP3Setter:(int)a;
2030 @end
2031
2032 @implementation I1
2033 @synthesize p3;
2034 -(void)myOwnP3Setter:(int)a{ }
2035 @end
2036 </pre>
2037 </div>
2038
2039 <p>
2040 The DWARF for this would be:
2041 </p>
2042 <div class="doc_code">
2043 <pre>
2044 0x000003bd: TAG_structure_type [7] *
2045               AT_APPLE_runtime_class( 0x10 )
2046               AT_name( "I1" )
2047               AT_decl_file( "Objc_Property.m" )
2048               AT_decl_line( 3 )
2049
2050 0x000003cd      TAG_APPLE_property
2051                   AT_name ( "p3" )
2052                   AT_APPLE_property_setter ( "myOwnP3Setter:" )
2053                   AT_type( {0x00000147} ( int ) )
2054
2055 0x000003f3:     TAG_member [8]
2056                   AT_name( "_p3" )
2057                   AT_type ( {0x00000147} ( int ) )
2058                   AT_APPLE_property ( {0x000003cd} )
2059                   AT_artificial ( 0x1 )
2060 </pre>
2061 </div>
2062
2063 </div>
2064
2065 <!-- *********************************************************************** -->
2066 <h4>
2067   <a name="objcpropertynewtags">New DWARF Tags</a>
2068 </h4>
2069 <!-- *********************************************************************** -->
2070
2071 <div>
2072 <table border="1" cellspacing="0">
2073   <col width="200">
2074   <col width="200">
2075   <tr>
2076     <th>TAG</th>
2077     <th>Value</th>
2078   </tr>
2079   <tr>
2080     <td>DW_TAG_APPLE_property</td>
2081     <td>0x4200</td>
2082   </tr>
2083 </table>
2084
2085 </div>
2086
2087 <!-- *********************************************************************** -->
2088 <h4>
2089   <a name="objcpropertynewattributes">New DWARF Attributes</a>
2090 </h4>
2091 <!-- *********************************************************************** -->
2092
2093 <div>
2094 <table border="1" cellspacing="0">
2095   <col width="200">
2096   <col width="200">
2097   <col width="200">
2098   <tr>
2099     <th>Attribute</th>
2100     <th>Value</th>
2101     <th>Classes</th>
2102   </tr>
2103   <tr>
2104     <td>DW_AT_APPLE_property</td>
2105     <td>0x3fed</td>
2106     <td>Reference</td>
2107   </tr>
2108   <tr>
2109     <td>DW_AT_APPLE_property_getter</td>
2110     <td>0x3fe9</td>
2111     <td>String</td>
2112   </tr>
2113   <tr>
2114     <td>DW_AT_APPLE_property_setter</td>
2115     <td>0x3fea</td>
2116     <td>String</td>
2117   </tr>
2118   <tr>
2119     <td>DW_AT_APPLE_property_attribute</td>
2120     <td>0x3feb</td>
2121     <td>Constant</td>
2122   </tr>
2123 </table>
2124
2125 </div>
2126
2127 <!-- *********************************************************************** -->
2128 <h4>
2129   <a name="objcpropertynewconstants">New DWARF Constants</a>
2130 </h4>
2131 <!-- *********************************************************************** -->
2132
2133 <div>
2134 <table border="1" cellspacing="0">
2135   <col width="200">
2136   <col width="200">
2137   <tr>
2138     <th>Name</th>
2139     <th>Value</th>
2140   </tr>
2141   <tr>
2142     <td>DW_AT_APPLE_PROPERTY_readonly</td>
2143     <td>0x1</td>
2144   </tr>
2145   <tr>
2146     <td>DW_AT_APPLE_PROPERTY_readwrite</td>
2147     <td>0x2</td>
2148   </tr>
2149   <tr>
2150     <td>DW_AT_APPLE_PROPERTY_assign</td>
2151     <td>0x4</td>
2152   </tr>
2153   <tr>
2154     <td>DW_AT_APPLE_PROPERTY_retain</td>
2155     <td>0x8</td>
2156   </tr>
2157   <tr>
2158     <td>DW_AT_APPLE_PROPERTY_copy</td>
2159     <td>0x10</td>
2160   </tr>
2161   <tr>
2162     <td>DW_AT_APPLE_PROPERTY_nonatomic</td>
2163     <td>0x20</td>
2164   </tr>
2165 </table>
2166
2167 </div>
2168 </div>
2169 </div>
2170
2171 <div>
2172 <!-- ======================================================================= -->
2173 <h3>
2174   <a name="acceltable">Name Accelerator Tables</a>
2175 </h3>
2176 <!-- ======================================================================= -->
2177 <!-- ======================================================================= -->
2178 <h4>
2179   <a name="acceltableintroduction">Introduction</a>
2180 </h4>
2181 <!-- ======================================================================= -->
2182 <div>
2183 <p>The .debug_pubnames and .debug_pubtypes formats are not what a debugger
2184   needs. The "pub" in the section name indicates that the entries in the
2185   table are publicly visible names only. This means no static or hidden
2186   functions show up in the .debug_pubnames. No static variables or private class
2187   variables are in the .debug_pubtypes. Many compilers add different things to
2188   these tables, so we can't rely upon the contents between gcc, icc, or clang.</p>
2189
2190 <p>The typical query given by users tends not to match up with the contents of
2191   these tables. For example, the DWARF spec states that "In the case of the
2192   name of a function member or static data member of a C++ structure, class or
2193   union, the name presented in the .debug_pubnames section is not the simple
2194   name given by the DW_AT_name attribute of the referenced debugging information
2195   entry, but rather the fully qualified name of the data or function member."
2196   So the only names in these tables for complex C++ entries is a fully
2197   qualified name.  Debugger users tend not to enter their search strings as
2198   "a::b::c(int,const Foo&) const", but rather as "c", "b::c" , or "a::b::c".  So
2199   the name entered in the name table must be demangled in order to chop it up
2200   appropriately and additional names must be manually entered into the table
2201   to make it effective as a name lookup table for debuggers to use.</p>
2202
2203 <p>All debuggers currently ignore the .debug_pubnames table as a result of
2204   its inconsistent and useless public-only name content making it a waste of
2205   space in the object file. These tables, when they are written to disk, are
2206   not sorted in any way, leaving every debugger to do its own parsing
2207   and sorting. These tables also include an inlined copy of the string values
2208   in the table itself making the tables much larger than they need to be on
2209   disk, especially for large C++ programs.</p>
2210
2211 <p>Can't we just fix the sections by adding all of the names we need to this
2212   table? No, because that is not what the tables are defined to contain and we
2213   won't know the difference between the old bad tables and the new good tables.
2214   At best we could make our own renamed sections that contain all of the data
2215   we need.</p>
2216
2217 <p>These tables are also insufficient for what a debugger like LLDB needs.
2218   LLDB uses clang for its expression parsing where LLDB acts as a PCH. LLDB is
2219   then often asked to look for type "foo" or namespace "bar", or list items in
2220   namespace "baz". Namespaces are not included in the pubnames or pubtypes
2221   tables. Since clang asks a lot of questions when it is parsing an expression,
2222   we need to be very fast when looking up names, as it happens a lot. Having new
2223   accelerator tables that are optimized for very quick lookups will benefit
2224   this type of debugging experience greatly.</p>
2225
2226 <p>We would like to generate name lookup tables that can be mapped into
2227   memory from disk, and used as is, with little or no up-front parsing. We would
2228   also be able to control the exact content of these different tables so they
2229   contain exactly what we need. The Name Accelerator Tables were designed
2230   to fix these issues. In order to solve these issues we need to:</p>
2231
2232 <ul>
2233   <li>Have a format that can be mapped into memory from disk and used as is</li>
2234   <li>Lookups should be very fast</li>
2235   <li>Extensible table format so these tables can be made by many producers</li>
2236   <li>Contain all of the names needed for typical lookups out of the box</li>
2237   <li>Strict rules for the contents of tables</li>
2238 </ul>
2239
2240 <p>Table size is important and the accelerator table format should allow the
2241   reuse of strings from common string tables so the strings for the names are
2242   not duplicated. We also want to make sure the table is ready to be used as-is
2243   by simply mapping the table into memory with minimal header parsing.</p>
2244
2245 <p>The name lookups need to be fast and optimized for the kinds of lookups
2246   that debuggers tend to do. Optimally we would like to touch as few parts of
2247   the mapped table as possible when doing a name lookup and be able to quickly
2248   find the name entry we are looking for, or discover there are no matches. In
2249   the case of debuggers we optimized for lookups that fail most of the time.</p>
2250
2251 <p>Each table that is defined should have strict rules on exactly what is in
2252   the accelerator tables and documented so clients can rely on the content.</p>
2253
2254 </div>
2255
2256 <!-- ======================================================================= -->
2257 <h4>
2258   <a name="acceltablehashes">Hash Tables</a>
2259 </h4>
2260 <!-- ======================================================================= -->
2261
2262 <div>
2263 <h5>Standard Hash Tables</h5>
2264
2265 <p>Typical hash tables have a header, buckets, and each bucket points to the
2266 bucket contents:
2267 </p>
2268
2269 <div class="doc_code">
2270 <pre>
2271 .------------.
2272 |  HEADER    |
2273 |------------|
2274 |  BUCKETS   |
2275 |------------|
2276 |  DATA      |
2277 `------------'
2278 </pre>
2279 </div>
2280
2281 <p>The BUCKETS are an array of offsets to DATA for each hash:</p>
2282
2283 <div class="doc_code">
2284 <pre>
2285 .------------.
2286 | 0x00001000 | BUCKETS[0]
2287 | 0x00002000 | BUCKETS[1]
2288 | 0x00002200 | BUCKETS[2]
2289 | 0x000034f0 | BUCKETS[3]
2290 |            | ...
2291 | 0xXXXXXXXX | BUCKETS[n_buckets]
2292 '------------'
2293 </pre>
2294 </div>
2295
2296 <p>So for bucket[3] in the example above, we have an offset into the table
2297   0x000034f0 which points to a chain of entries for the bucket. Each bucket
2298   must contain a next pointer, full 32 bit hash value, the string itself,
2299   and the data for the current string value.</p>
2300
2301 <div class="doc_code">
2302 <pre>
2303             .------------.
2304 0x000034f0: | 0x00003500 | next pointer
2305             | 0x12345678 | 32 bit hash
2306             | "erase"    | string value
2307             | data[n]    | HashData for this bucket
2308             |------------|
2309 0x00003500: | 0x00003550 | next pointer
2310             | 0x29273623 | 32 bit hash
2311             | "dump"     | string value
2312             | data[n]    | HashData for this bucket
2313             |------------|
2314 0x00003550: | 0x00000000 | next pointer
2315             | 0x82638293 | 32 bit hash
2316             | "main"     | string value
2317             | data[n]    | HashData for this bucket
2318             `------------'
2319 </pre>
2320 </div>
2321
2322 <p>The problem with this layout for debuggers is that we need to optimize for
2323   the negative lookup case where the symbol we're searching for is not present.
2324   So if we were to lookup "printf" in the table above, we would make a 32 hash
2325   for "printf", it might match bucket[3]. We would need to go to the offset
2326   0x000034f0 and start looking to see if our 32 bit hash matches. To do so, we
2327   need to read the next pointer, then read the hash, compare it, and skip to
2328   the next bucket. Each time we are skipping many bytes in memory and touching
2329   new cache pages just to do the compare on the full 32 bit hash. All of these
2330   accesses then tell us that we didn't have a match.</p>
2331
2332 <h5>Name Hash Tables</h5>
2333
2334 <p>To solve the issues mentioned above we have structured the hash tables
2335   a bit differently: a header, buckets, an array of all unique 32 bit hash
2336   values, followed by an array of hash value data offsets, one for each hash
2337   value, then the data for all hash values:</p>
2338
2339 <div class="doc_code">
2340 <pre>
2341 .-------------.
2342 |  HEADER     |
2343 |-------------|
2344 |  BUCKETS    |
2345 |-------------|
2346 |  HASHES     |
2347 |-------------|
2348 |  OFFSETS    |
2349 |-------------|
2350 |  DATA       |
2351 `-------------'
2352 </pre>
2353 </div>
2354
2355 <p>The BUCKETS in the name tables are an index into the HASHES array. By
2356   making all of the full 32 bit hash values contiguous in memory, we allow
2357   ourselves to efficiently check for a match while touching as little
2358   memory as possible. Most often checking the 32 bit hash values is as far as
2359   the lookup goes. If it does match, it usually is a match with no collisions.
2360   So for a table with "n_buckets" buckets, and "n_hashes" unique 32 bit hash
2361   values, we can clarify the contents of the BUCKETS, HASHES and OFFSETS as:</p>
2362
2363 <div class="doc_code">
2364 <pre>
2365 .-------------------------.
2366 |  HEADER.magic           | uint32_t
2367 |  HEADER.version         | uint16_t
2368 |  HEADER.hash_function   | uint16_t
2369 |  HEADER.bucket_count    | uint32_t
2370 |  HEADER.hashes_count    | uint32_t
2371 |  HEADER.header_data_len | uint32_t
2372 |  HEADER_DATA            | HeaderData
2373 |-------------------------|
2374 |  BUCKETS                | uint32_t[n_buckets] // 32 bit hash indexes
2375 |-------------------------|
2376 |  HASHES                 | uint32_t[n_buckets] // 32 bit hash values
2377 |-------------------------|
2378 |  OFFSETS                | uint32_t[n_buckets] // 32 bit offsets to hash value data
2379 |-------------------------|
2380 |  ALL HASH DATA          |
2381 `-------------------------'
2382 </pre>
2383 </div>
2384
2385 <p>So taking the exact same data from the standard hash example above we end up
2386   with:</p>
2387
2388 <div class="doc_code">
2389 <pre>
2390             .------------.
2391             | HEADER     |
2392             |------------|
2393             |          0 | BUCKETS[0]
2394             |          2 | BUCKETS[1]
2395             |          5 | BUCKETS[2]
2396             |          6 | BUCKETS[3]
2397             |            | ...
2398             |        ... | BUCKETS[n_buckets]
2399             |------------|
2400             | 0x........ | HASHES[0]
2401             | 0x........ | HASHES[1]
2402             | 0x........ | HASHES[2]
2403             | 0x........ | HASHES[3]
2404             | 0x........ | HASHES[4]
2405             | 0x........ | HASHES[5]
2406             | 0x12345678 | HASHES[6]    hash for BUCKETS[3]
2407             | 0x29273623 | HASHES[7]    hash for BUCKETS[3]
2408             | 0x82638293 | HASHES[8]    hash for BUCKETS[3]
2409             | 0x........ | HASHES[9]
2410             | 0x........ | HASHES[10]
2411             | 0x........ | HASHES[11]
2412             | 0x........ | HASHES[12]
2413             | 0x........ | HASHES[13]
2414             | 0x........ | HASHES[n_hashes]
2415             |------------|
2416             | 0x........ | OFFSETS[0]
2417             | 0x........ | OFFSETS[1]
2418             | 0x........ | OFFSETS[2]
2419             | 0x........ | OFFSETS[3]
2420             | 0x........ | OFFSETS[4]
2421             | 0x........ | OFFSETS[5]
2422             | 0x000034f0 | OFFSETS[6]   offset for BUCKETS[3]
2423             | 0x00003500 | OFFSETS[7]   offset for BUCKETS[3]
2424             | 0x00003550 | OFFSETS[8]   offset for BUCKETS[3]
2425             | 0x........ | OFFSETS[9]
2426             | 0x........ | OFFSETS[10]
2427             | 0x........ | OFFSETS[11]
2428             | 0x........ | OFFSETS[12]
2429             | 0x........ | OFFSETS[13]
2430             | 0x........ | OFFSETS[n_hashes]
2431             |------------|
2432             |            |
2433             |            |
2434             |            |
2435             |            |
2436             |            |
2437             |------------|
2438 0x000034f0: | 0x00001203 | .debug_str ("erase")
2439             | 0x00000004 | A 32 bit array count - number of HashData with name "erase"
2440             | 0x........ | HashData[0]
2441             | 0x........ | HashData[1]
2442             | 0x........ | HashData[2]
2443             | 0x........ | HashData[3]
2444             | 0x00000000 | String offset into .debug_str (terminate data for hash)
2445             |------------|
2446 0x00003500: | 0x00001203 | String offset into .debug_str ("collision")
2447             | 0x00000002 | A 32 bit array count - number of HashData with name "collision"
2448             | 0x........ | HashData[0]
2449             | 0x........ | HashData[1]
2450             | 0x00001203 | String offset into .debug_str ("dump")
2451             | 0x00000003 | A 32 bit array count - number of HashData with name "dump"
2452             | 0x........ | HashData[0]
2453             | 0x........ | HashData[1]
2454             | 0x........ | HashData[2]
2455             | 0x00000000 | String offset into .debug_str (terminate data for hash)
2456             |------------|
2457 0x00003550: | 0x00001203 | String offset into .debug_str ("main")
2458             | 0x00000009 | A 32 bit array count - number of HashData with name "main"
2459             | 0x........ | HashData[0]
2460             | 0x........ | HashData[1]
2461             | 0x........ | HashData[2]
2462             | 0x........ | HashData[3]
2463             | 0x........ | HashData[4]
2464             | 0x........ | HashData[5]
2465             | 0x........ | HashData[6]
2466             | 0x........ | HashData[7]
2467             | 0x........ | HashData[8]
2468             | 0x00000000 | String offset into .debug_str (terminate data for hash)
2469             `------------'
2470 </pre>
2471 </div>
2472
2473 <p>So we still have all of the same data, we just organize it more efficiently
2474   for debugger lookup. If we repeat the same "printf" lookup from above, we
2475   would hash "printf" and find it matches BUCKETS[3] by taking the 32 bit hash
2476   value and modulo it by n_buckets. BUCKETS[3] contains "6" which is the index
2477   into the HASHES table. We would then compare any consecutive 32 bit hashes
2478   values in the HASHES array as long as the hashes would be in BUCKETS[3]. We
2479   do this by verifying that each subsequent hash value modulo n_buckets is still
2480   3. In the case of a failed lookup we would access the memory for BUCKETS[3], and
2481   then compare a few consecutive 32 bit hashes before we know that we have no match.
2482   We don't end up marching through multiple words of memory and we really keep the
2483   number of processor data cache lines being accessed as small as possible.</p>
2484
2485 <p>The string hash that is used for these lookup tables is the Daniel J.
2486   Bernstein hash which is also used in the ELF GNU_HASH sections. It is a very
2487   good hash for all kinds of names in programs with very few hash collisions.</p>
2488
2489 <p>Empty buckets are designated by using an invalid hash index of UINT32_MAX.</p>
2490 </div>
2491
2492 <!-- ======================================================================= -->
2493 <h4>
2494   <a name="acceltabledetails">Details</a>
2495 </h4>
2496 <!-- ======================================================================= -->
2497 <div>
2498 <p>These name hash tables are designed to be generic where specializations of
2499   the table get to define additional data that goes into the header
2500   ("HeaderData"), how the string value is stored ("KeyType") and the content
2501   of the data for each hash value.</p>
2502
2503 <h5>Header Layout</h5>
2504 <p>The header has a fixed part, and the specialized part. The exact format of
2505   the header is:</p>
2506 <div class="doc_code">
2507 <pre>
2508 struct Header
2509 {
2510   uint32_t   magic;           // 'HASH' magic value to allow endian detection
2511   uint16_t   version;         // Version number
2512   uint16_t   hash_function;   // The hash function enumeration that was used
2513   uint32_t   bucket_count;    // The number of buckets in this hash table
2514   uint32_t   hashes_count;    // The total number of unique hash values and hash data offsets in this table
2515   uint32_t   header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment
2516                               // Specifically the length of the following HeaderData field - this does not
2517                               // include the size of the preceding fields
2518   HeaderData header_data;     // Implementation specific header data
2519 };
2520 </pre>
2521 </div>
2522 <p>The header starts with a 32 bit "magic" value which must be 'HASH' encoded as
2523   an ASCII integer. This allows the detection of the start of the hash table and
2524   also allows the table's byte order to be determined so the table can be
2525   correctly extracted. The "magic" value is followed by a 16 bit version number
2526   which allows the table to be revised and modified in the future. The current
2527   version number is 1. "hash_function" is a uint16_t enumeration that specifies
2528   which hash function was used to produce this table. The current values for the
2529   hash function enumerations include:</p>
2530 <div class="doc_code">
2531 <pre>
2532 enum HashFunctionType
2533 {
2534   eHashFunctionDJB = 0u, // Daniel J Bernstein hash function
2535 };
2536 </pre>
2537 </div>
2538 <p>"bucket_count" is a 32 bit unsigned integer that represents how many buckets
2539   are in the BUCKETS array. "hashes_count" is the number of unique 32 bit hash
2540   values that are in the HASHES array, and is the same number of offsets are
2541   contained in the OFFSETS array. "header_data_len" specifies the size in
2542   bytes of the HeaderData that is filled in by specialized versions of this
2543   table.</p>
2544
2545 <h5>Fixed Lookup</h5>
2546 <p>The header is followed by the buckets, hashes, offsets, and hash value
2547   data.
2548 <div class="doc_code">
2549 <pre>
2550 struct FixedTable
2551 {
2552   uint32_t buckets[Header.bucket_count];  // An array of hash indexes into the "hashes[]" array below
2553   uint32_t hashes [Header.hashes_count];  // Every unique 32 bit hash for the entire table is in this table
2554   uint32_t offsets[Header.hashes_count];  // An offset that corresponds to each item in the "hashes[]" array above
2555 };
2556 </pre>
2557 </div>
2558 <p>"buckets" is an array of 32 bit indexes into the "hashes" array. The
2559   "hashes" array contains all of the 32 bit hash values for all names in the
2560   hash table. Each hash in the "hashes" table has an offset in the "offsets"
2561   array that points to the data for the hash value.</p>
2562
2563 <p>This table setup makes it very easy to repurpose these tables to contain
2564   different data, while keeping the lookup mechanism the same for all tables.
2565   This layout also makes it possible to save the table to disk and map it in
2566   later and do very efficient name lookups with little or no parsing.</p>
2567
2568 <p>DWARF lookup tables can be implemented in a variety of ways and can store
2569   a lot of information for each name. We want to make the DWARF tables
2570   extensible and able to store the data efficiently so we have used some of the
2571   DWARF features that enable efficient data storage to define exactly what kind
2572   of data we store for each name.</p>
2573
2574 <p>The "HeaderData" contains a definition of the contents of each HashData
2575   chunk. We might want to store an offset to all of the debug information
2576   entries (DIEs) for each name. To keep things extensible, we create a list of
2577   items, or Atoms, that are contained in the data for each name. First comes the
2578   type of the data in each atom:</p>
2579 <div class="doc_code">
2580 <pre>
2581 enum AtomType
2582 {
2583   eAtomTypeNULL       = 0u,
2584   eAtomTypeDIEOffset  = 1u,   // DIE offset, check form for encoding
2585   eAtomTypeCUOffset   = 2u,   // DIE offset of the compiler unit header that contains the item in question
2586   eAtomTypeTag        = 3u,   // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
2587   eAtomTypeNameFlags  = 4u,   // Flags from enum NameFlags
2588   eAtomTypeTypeFlags  = 5u,   // Flags from enum TypeFlags
2589 };
2590 </pre>
2591 </div>
2592 <p>The enumeration values and their meanings are:</p>
2593 <div class="doc_code">
2594 <pre>
2595   eAtomTypeNULL       - a termination atom that specifies the end of the atom list
2596   eAtomTypeDIEOffset  - an offset into the .debug_info section for the DWARF DIE for this name
2597   eAtomTypeCUOffset   - an offset into the .debug_info section for the CU that contains the DIE
2598   eAtomTypeDIETag     - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is
2599   eAtomTypeNameFlags  - Flags for functions and global variables (isFunction, isInlined, isExternal...)
2600   eAtomTypeTypeFlags  - Flags for types (isCXXClass, isObjCClass, ...)
2601 </pre>
2602 </div>
2603 <p>Then we allow each atom type to define the atom type and how the data for
2604   each atom type data is encoded:</p>
2605 <div class="doc_code">
2606 <pre>
2607 struct Atom
2608 {
2609   uint16_t type;  // AtomType enum value
2610   uint16_t form;  // DWARF DW_FORM_XXX defines
2611 };
2612 </pre>
2613 </div>
2614 <p>The "form" type above is from the DWARF specification and defines the
2615   exact encoding of the data for the Atom type. See the DWARF specification for
2616   the DW_FORM_ definitions.</p>
2617 <div class="doc_code">
2618 <pre>
2619 struct HeaderData
2620 {
2621   uint32_t die_offset_base;
2622   uint32_t atom_count;
2623   Atoms    atoms[atom_count0];
2624 };
2625 </pre>
2626 </div>
2627 <p>"HeaderData" defines the base DIE offset that should be added to any atoms
2628   that are encoded using the DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4,
2629   DW_FORM_ref8 or DW_FORM_ref_udata. It also defines what is contained in
2630   each "HashData" object -- Atom.form tells us how large each field will be in
2631   the HashData and the Atom.type tells us how this data should be interpreted.</p>
2632
2633 <p>For the current implementations of the ".apple_names" (all functions + globals),
2634   the ".apple_types" (names of all types that are defined), and the
2635   ".apple_namespaces" (all namespaces), we currently set the Atom array to be:</p>
2636 <div class="doc_code">
2637 <pre>
2638 HeaderData.atom_count = 1;
2639 HeaderData.atoms[0].type = eAtomTypeDIEOffset;
2640 HeaderData.atoms[0].form = DW_FORM_data4;
2641 </pre>
2642 </div>
2643 <p>This defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is
2644   encoded as a 32 bit value (DW_FORM_data4). This allows a single name to have
2645   multiple matching DIEs in a single file, which could come up with an inlined
2646   function for instance. Future tables could include more information about the
2647   DIE such as flags indicating if the DIE is a function, method, block,
2648   or inlined.</p>
2649
2650 <p>The KeyType for the DWARF table is a 32 bit string table offset into the
2651   ".debug_str" table. The ".debug_str" is the string table for the DWARF which
2652   may already contain copies of all of the strings. This helps make sure, with
2653   help from the compiler, that we reuse the strings between all of the DWARF
2654   sections and keeps the hash table size down. Another benefit to having the
2655   compiler generate all strings as DW_FORM_strp in the debug info, is that
2656   DWARF parsing can be made much faster.</p>
2657
2658 <p>After a lookup is made, we get an offset into the hash data. The hash data
2659   needs to be able to deal with 32 bit hash collisions, so the chunk of data
2660   at the offset in the hash data consists of a triple:</p>
2661 <div class="doc_code">
2662 <pre>
2663 uint32_t str_offset
2664 uint32_t hash_data_count
2665 HashData[hash_data_count]
2666 </pre>
2667 </div>
2668 <p>If "str_offset" is zero, then the bucket contents are done. 99.9% of the
2669   hash data chunks contain a single item (no 32 bit hash collision):</p>
2670 <div class="doc_code">
2671 <pre>
2672 .------------.
2673 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
2674 | 0x00000004 | uint32_t HashData count
2675 | 0x........ | uint32_t HashData[0] DIE offset
2676 | 0x........ | uint32_t HashData[1] DIE offset
2677 | 0x........ | uint32_t HashData[2] DIE offset
2678 | 0x........ | uint32_t HashData[3] DIE offset
2679 | 0x00000000 | uint32_t KeyType (end of hash chain)
2680 `------------'
2681 </pre>
2682 </div>
2683 <p>If there are collisions, you will have multiple valid string offsets:</p>
2684 <div class="doc_code">
2685 <pre>
2686 .------------.
2687 | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
2688 | 0x00000004 | uint32_t HashData count
2689 | 0x........ | uint32_t HashData[0] DIE offset
2690 | 0x........ | uint32_t HashData[1] DIE offset
2691 | 0x........ | uint32_t HashData[2] DIE offset
2692 | 0x........ | uint32_t HashData[3] DIE offset
2693 | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print")
2694 | 0x00000002 | uint32_t HashData count
2695 | 0x........ | uint32_t HashData[0] DIE offset
2696 | 0x........ | uint32_t HashData[1] DIE offset
2697 | 0x00000000 | uint32_t KeyType (end of hash chain)
2698 `------------'
2699 </pre>
2700 </div>
2701 <p>Current testing with real world C++ binaries has shown that there is around 1
2702   32 bit hash collision per 100,000 name entries.</p>
2703 </div>
2704 <!-- ======================================================================= -->
2705 <h4>
2706   <a name="acceltablecontents">Contents</a>
2707 </h4>
2708 <!-- ======================================================================= -->
2709 <div>
2710 <p>As we said, we want to strictly define exactly what is included in the
2711   different tables. For DWARF, we have 3 tables: ".apple_names", ".apple_types",
2712   and ".apple_namespaces".</p>
2713
2714 <p>".apple_names" sections should contain an entry for each DWARF DIE whose
2715   DW_TAG is a DW_TAG_label, DW_TAG_inlined_subroutine, or DW_TAG_subprogram that
2716   has address attributes: DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges or
2717   DW_AT_entry_pc. It also contains DW_TAG_variable DIEs that have a DW_OP_addr
2718   in the location (global and static variables). All global and static variables
2719   should be included, including those scoped withing functions and classes. For
2720   example using the following code:</p>
2721 <div class="doc_code">
2722 <pre>
2723 static int var = 0;
2724
2725 void f ()
2726 {
2727   static int var = 0;
2728 }
2729 </pre>
2730 </div>
2731 <p>Both of the static "var" variables would be included in the table. All
2732   functions should emit both their full names and their basenames. For C or C++,
2733   the full name is the mangled name (if available) which is usually in the
2734   DW_AT_MIPS_linkage_name attribute, and the DW_AT_name contains the function
2735   basename. If global or static variables have a mangled name in a
2736   DW_AT_MIPS_linkage_name attribute, this should be emitted along with the
2737   simple name found in the DW_AT_name attribute.</p>
2738
2739 <p>".apple_types" sections should contain an entry for each DWARF DIE whose
2740   tag is one of:</p>
2741 <ul>
2742   <li>DW_TAG_array_type</li>
2743   <li>DW_TAG_class_type</li>
2744   <li>DW_TAG_enumeration_type</li>
2745   <li>DW_TAG_pointer_type</li>
2746   <li>DW_TAG_reference_type</li>
2747   <li>DW_TAG_string_type</li>
2748   <li>DW_TAG_structure_type</li>
2749   <li>DW_TAG_subroutine_type</li>
2750   <li>DW_TAG_typedef</li>
2751   <li>DW_TAG_union_type</li>
2752   <li>DW_TAG_ptr_to_member_type</li>
2753   <li>DW_TAG_set_type</li>
2754   <li>DW_TAG_subrange_type</li>
2755   <li>DW_TAG_base_type</li>
2756   <li>DW_TAG_const_type</li>
2757   <li>DW_TAG_constant</li>
2758   <li>DW_TAG_file_type</li>
2759   <li>DW_TAG_namelist</li>
2760   <li>DW_TAG_packed_type</li>
2761   <li>DW_TAG_volatile_type</li>
2762   <li>DW_TAG_restrict_type</li>
2763   <li>DW_TAG_interface_type</li>
2764   <li>DW_TAG_unspecified_type</li>
2765   <li>DW_TAG_shared_type</li>
2766 </ul>
2767 <p>Only entries with a DW_AT_name attribute are included, and the entry must
2768   not be a forward declaration (DW_AT_declaration attribute with a non-zero value).
2769   For example, using the following code:</p>
2770 <div class="doc_code">
2771 <pre>
2772 int main ()
2773 {
2774   int *b = 0;
2775   return *b;
2776 }
2777 </pre>
2778 </div>
2779 <p>We get a few type DIEs:</p>
2780 <div class="doc_code">
2781 <pre>
2782 0x00000067:     TAG_base_type [5]
2783                 AT_encoding( DW_ATE_signed )
2784                 AT_name( "int" )
2785                 AT_byte_size( 0x04 )
2786
2787 0x0000006e:     TAG_pointer_type [6]
2788                 AT_type( {0x00000067} ( int ) )
2789                 AT_byte_size( 0x08 )
2790 </pre>
2791 </div>
2792 <p>The DW_TAG_pointer_type is not included because it does not have a DW_AT_name.</p>
2793
2794 <p>".apple_namespaces" section should contain all DW_TAG_namespace DIEs. If
2795   we run into a namespace that has no name this is an anonymous namespace,
2796   and the name should be output as "(anonymous namespace)" (without the quotes).
2797   Why? This matches the output of the abi::cxa_demangle() that is in the standard
2798   C++ library that demangles mangled names.</p>
2799 </div>
2800
2801 <!-- ======================================================================= -->
2802 <h4>
2803   <a name="acceltableextensions">Language Extensions and File Format Changes</a>
2804 </h4>
2805 <!-- ======================================================================= -->
2806 <div>
2807 <h5>Objective-C Extensions</h5>
2808 <p>".apple_objc" section should contain all DW_TAG_subprogram DIEs for an
2809   Objective-C class. The name used in the hash table is the name of the
2810   Objective-C class itself. If the Objective-C class has a category, then an
2811   entry is made for both the class name without the category, and for the class
2812   name with the category. So if we have a DIE at offset 0x1234 with a name
2813   of method "-[NSString(my_additions) stringWithSpecialString:]", we would add
2814   an entry for "NSString" that points to DIE 0x1234, and an entry for
2815   "NSString(my_additions)" that points to 0x1234. This allows us to quickly
2816   track down all Objective-C methods for an Objective-C class when doing
2817   expressions. It is needed because of the dynamic nature of Objective-C where
2818   anyone can add methods to a class. The DWARF for Objective-C methods is also
2819   emitted differently from C++ classes where the methods are not usually
2820   contained in the class definition, they are scattered about across one or more
2821   compile units. Categories can also be defined in different shared libraries.
2822   So we need to be able to quickly find all of the methods and class functions
2823   given the Objective-C class name, or quickly find all methods and class
2824   functions for a class + category name. This table does not contain any selector
2825   names, it just maps Objective-C class names (or class names + category) to all
2826   of the methods and class functions. The selectors are added as function
2827   basenames in the .debug_names section.</p>
2828
2829 <p>In the ".apple_names" section for Objective-C functions, the full name is the
2830   entire function name with the brackets ("-[NSString stringWithCString:]") and the
2831   basename is the selector only ("stringWithCString:").</p>
2832
2833 <h5>Mach-O Changes</h5>
2834 <p>The sections names for the apple hash tables are for non mach-o files. For
2835   mach-o files, the sections should be contained in the "__DWARF" segment with
2836   names as follows:</p>
2837 <ul>
2838   <li>".apple_names" -> "__apple_names"</li>
2839   <li>".apple_types" -> "__apple_types"</li>
2840   <li>".apple_namespaces" -> "__apple_namespac" (16 character limit)</li>
2841   <li> ".apple_objc" -> "__apple_objc"</li>
2842 </ul>
2843 </div>
2844 </div>
2845
2846 <!-- *********************************************************************** -->
2847
2848 <hr>
2849 <address>
2850   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2851   src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
2852   <a href="http://validator.w3.org/check/referer"><img
2853   src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
2854
2855   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
2856   <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
2857   Last modified: $Date$
2858 </address>
2859
2860 </body>
2861 </html>