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