xref: /aosp_15_r20/external/llvm/docs/SourceLevelDebugging.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker================================
2*9880d681SAndroid Build Coastguard WorkerSource Level Debugging with LLVM
3*9880d681SAndroid Build Coastguard Worker================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerIntroduction
9*9880d681SAndroid Build Coastguard Worker============
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerThis document is the central repository for all information pertaining to debug
12*9880d681SAndroid Build Coastguard Workerinformation in LLVM.  It describes the :ref:`actual format that the LLVM debug
13*9880d681SAndroid Build Coastguard Workerinformation takes <format>`, which is useful for those interested in creating
14*9880d681SAndroid Build Coastguard Workerfront-ends or dealing directly with the information.  Further, this document
15*9880d681SAndroid Build Coastguard Workerprovides specific examples of what debug information for C/C++ looks like.
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard WorkerPhilosophy behind LLVM debugging information
18*9880d681SAndroid Build Coastguard Worker--------------------------------------------
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard WorkerThe idea of the LLVM debugging information is to capture how the important
21*9880d681SAndroid Build Coastguard Workerpieces of the source-language's Abstract Syntax Tree map onto LLVM code.
22*9880d681SAndroid Build Coastguard WorkerSeveral design aspects have shaped the solution that appears here.  The
23*9880d681SAndroid Build Coastguard Workerimportant ones are:
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker* Debugging information should have very little impact on the rest of the
26*9880d681SAndroid Build Coastguard Worker  compiler.  No transformations, analyses, or code generators should need to
27*9880d681SAndroid Build Coastguard Worker  be modified because of debugging information.
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker* LLVM optimizations should interact in :ref:`well-defined and easily described
30*9880d681SAndroid Build Coastguard Worker  ways <intro_debugopt>` with the debugging information.
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker* Because LLVM is designed to support arbitrary programming languages,
33*9880d681SAndroid Build Coastguard Worker  LLVM-to-LLVM tools should not need to know anything about the semantics of
34*9880d681SAndroid Build Coastguard Worker  the source-level-language.
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker* Source-level languages are often **widely** different from one another.
37*9880d681SAndroid Build Coastguard Worker  LLVM should not put any restrictions of the flavor of the source-language,
38*9880d681SAndroid Build Coastguard Worker  and the debugging information should work with any language.
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker* With code generator support, it should be possible to use an LLVM compiler
41*9880d681SAndroid Build Coastguard Worker  to compile a program to native machine code and standard debugging
42*9880d681SAndroid Build Coastguard Worker  formats.  This allows compatibility with traditional machine-code level
43*9880d681SAndroid Build Coastguard Worker  debuggers, like GDB or DBX.
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard WorkerThe approach used by the LLVM implementation is to use a small set of
46*9880d681SAndroid Build Coastguard Worker:ref:`intrinsic functions <format_common_intrinsics>` to define a mapping
47*9880d681SAndroid Build Coastguard Workerbetween LLVM program objects and the source-level objects.  The description of
48*9880d681SAndroid Build Coastguard Workerthe source-level program is maintained in LLVM metadata in an
49*9880d681SAndroid Build Coastguard Worker:ref:`implementation-defined format <ccxx_frontend>` (the C/C++ front-end
50*9880d681SAndroid Build Coastguard Workercurrently uses working draft 7 of the `DWARF 3 standard
51*9880d681SAndroid Build Coastguard Worker<http://www.eagercon.com/dwarf/dwarf3std.htm>`_).
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard WorkerWhen a program is being debugged, a debugger interacts with the user and turns
54*9880d681SAndroid Build Coastguard Workerthe stored debug information into source-language specific information.  As
55*9880d681SAndroid Build Coastguard Workersuch, a debugger must be aware of the source-language, and is thus tied to a
56*9880d681SAndroid Build Coastguard Workerspecific language or family of languages.
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard WorkerDebug information consumers
59*9880d681SAndroid Build Coastguard Worker---------------------------
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard WorkerThe role of debug information is to provide meta information normally stripped
62*9880d681SAndroid Build Coastguard Workeraway during the compilation process.  This meta information provides an LLVM
63*9880d681SAndroid Build Coastguard Workeruser a relationship between generated code and the original program source
64*9880d681SAndroid Build Coastguard Workercode.
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard WorkerCurrently, there are two backend consumers of debug info: DwarfDebug and
67*9880d681SAndroid Build Coastguard WorkerCodeViewDebug. DwarfDebug produces DWARF sutable for use with GDB, LLDB, and
68*9880d681SAndroid Build Coastguard Workerother DWARF-based debuggers. :ref:`CodeViewDebug <codeview>` produces CodeView,
69*9880d681SAndroid Build Coastguard Workerthe Microsoft debug info format, which is usable with Microsoft debuggers such
70*9880d681SAndroid Build Coastguard Workeras Visual Studio and WinDBG. LLVM's debug information format is mostly derived
71*9880d681SAndroid Build Coastguard Workerfrom and inspired by DWARF, but it is feasible to translate into other target
72*9880d681SAndroid Build Coastguard Workerdebug info formats such as STABS.
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard WorkerIt would also be reasonable to use debug information to feed profiling tools
75*9880d681SAndroid Build Coastguard Workerfor analysis of generated code, or, tools for reconstructing the original
76*9880d681SAndroid Build Coastguard Workersource from generated code.
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker.. _intro_debugopt:
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard WorkerDebugging optimized code
81*9880d681SAndroid Build Coastguard Worker------------------------
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard WorkerAn extremely high priority of LLVM debugging information is to make it interact
84*9880d681SAndroid Build Coastguard Workerwell with optimizations and analysis.  In particular, the LLVM debug
85*9880d681SAndroid Build Coastguard Workerinformation provides the following guarantees:
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker* LLVM debug information **always provides information to accurately read
88*9880d681SAndroid Build Coastguard Worker  the source-level state of the program**, regardless of which LLVM
89*9880d681SAndroid Build Coastguard Worker  optimizations have been run, and without any modification to the
90*9880d681SAndroid Build Coastguard Worker  optimizations themselves.  However, some optimizations may impact the
91*9880d681SAndroid Build Coastguard Worker  ability to modify the current state of the program with a debugger, such
92*9880d681SAndroid Build Coastguard Worker  as setting program variables, or calling functions that have been
93*9880d681SAndroid Build Coastguard Worker  deleted.
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker* As desired, LLVM optimizations can be upgraded to be aware of the LLVM
96*9880d681SAndroid Build Coastguard Worker  debugging information, allowing them to update the debugging information
97*9880d681SAndroid Build Coastguard Worker  as they perform aggressive optimizations.  This means that, with effort,
98*9880d681SAndroid Build Coastguard Worker  the LLVM optimizers could optimize debug code just as well as non-debug
99*9880d681SAndroid Build Coastguard Worker  code.
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker* LLVM debug information does not prevent optimizations from
102*9880d681SAndroid Build Coastguard Worker  happening (for example inlining, basic block reordering/merging/cleanup,
103*9880d681SAndroid Build Coastguard Worker  tail duplication, etc).
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker* LLVM debug information is automatically optimized along with the rest of
106*9880d681SAndroid Build Coastguard Worker  the program, using existing facilities.  For example, duplicate
107*9880d681SAndroid Build Coastguard Worker  information is automatically merged by the linker, and unused information
108*9880d681SAndroid Build Coastguard Worker  is automatically removed.
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard WorkerBasically, the debug information allows you to compile a program with
111*9880d681SAndroid Build Coastguard Worker"``-O0 -g``" and get full debug information, allowing you to arbitrarily modify
112*9880d681SAndroid Build Coastguard Workerthe program as it executes from a debugger.  Compiling a program with
113*9880d681SAndroid Build Coastguard Worker"``-O3 -g``" gives you full debug information that is always available and
114*9880d681SAndroid Build Coastguard Workeraccurate for reading (e.g., you get accurate stack traces despite tail call
115*9880d681SAndroid Build Coastguard Workerelimination and inlining), but you might lose the ability to modify the program
116*9880d681SAndroid Build Coastguard Workerand call functions where were optimized out of the program, or inlined away
117*9880d681SAndroid Build Coastguard Workercompletely.
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker:ref:`LLVM test suite <test-suite-quickstart>` provides a framework to test
120*9880d681SAndroid Build Coastguard Workeroptimizer's handling of debugging information.  It can be run like this:
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker  % cd llvm/projects/test-suite/MultiSource/Benchmarks  # or some other level
125*9880d681SAndroid Build Coastguard Worker  % make TEST=dbgopt
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard WorkerThis will test impact of debugging information on optimization passes.  If
128*9880d681SAndroid Build Coastguard Workerdebugging information influences optimization passes then it will be reported
129*9880d681SAndroid Build Coastguard Workeras a failure.  See :doc:`TestingGuide` for more information on LLVM test
130*9880d681SAndroid Build Coastguard Workerinfrastructure and how to run various tests.
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker.. _format:
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard WorkerDebugging information format
135*9880d681SAndroid Build Coastguard Worker============================
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard WorkerLLVM debugging information has been carefully designed to make it possible for
138*9880d681SAndroid Build Coastguard Workerthe optimizer to optimize the program and debugging information without
139*9880d681SAndroid Build Coastguard Workernecessarily having to know anything about debugging information.  In
140*9880d681SAndroid Build Coastguard Workerparticular, the use of metadata avoids duplicated debugging information from
141*9880d681SAndroid Build Coastguard Workerthe beginning, and the global dead code elimination pass automatically deletes
142*9880d681SAndroid Build Coastguard Workerdebugging information for a function if it decides to delete the function.
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard WorkerTo do this, most of the debugging information (descriptors for types,
145*9880d681SAndroid Build Coastguard Workervariables, functions, source files, etc) is inserted by the language front-end
146*9880d681SAndroid Build Coastguard Workerin the form of LLVM metadata.
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard WorkerDebug information is designed to be agnostic about the target debugger and
149*9880d681SAndroid Build Coastguard Workerdebugging information representation (e.g. DWARF/Stabs/etc).  It uses a generic
150*9880d681SAndroid Build Coastguard Workerpass to decode the information that represents variables, types, functions,
151*9880d681SAndroid Build Coastguard Workernamespaces, etc: this allows for arbitrary source-language semantics and
152*9880d681SAndroid Build Coastguard Workertype-systems to be used, as long as there is a module written for the target
153*9880d681SAndroid Build Coastguard Workerdebugger to interpret the information.
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard WorkerTo provide basic functionality, the LLVM debugger does have to make some
156*9880d681SAndroid Build Coastguard Workerassumptions about the source-level language being debugged, though it keeps
157*9880d681SAndroid Build Coastguard Workerthese to a minimum.  The only common features that the LLVM debugger assumes
158*9880d681SAndroid Build Coastguard Workerexist are `source files <LangRef.html#difile>`_, and `program objects
159*9880d681SAndroid Build Coastguard Worker<LangRef.html#diglobalvariable>`_.  These abstract objects are used by a
160*9880d681SAndroid Build Coastguard Workerdebugger to form stack traces, show information about local variables, etc.
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard WorkerThis section of the documentation first describes the representation aspects
163*9880d681SAndroid Build Coastguard Workercommon to any source-language.  :ref:`ccxx_frontend` describes the data layout
164*9880d681SAndroid Build Coastguard Workerconventions used by the C and C++ front-ends.
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard WorkerDebug information descriptors are `specialized metadata nodes
167*9880d681SAndroid Build Coastguard Worker<LangRef.html#specialized-metadata>`_, first-class subclasses of ``Metadata``.
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker.. _format_common_intrinsics:
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard WorkerDebugger intrinsic functions
172*9880d681SAndroid Build Coastguard Worker----------------------------
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard WorkerLLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to
175*9880d681SAndroid Build Coastguard Workerprovide debug information at various points in generated code.
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker``llvm.dbg.declare``
178*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
181*9880d681SAndroid Build Coastguard Worker
182*9880d681SAndroid Build Coastguard Worker  void @llvm.dbg.declare(metadata, metadata, metadata)
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard WorkerThis intrinsic provides information about a local element (e.g., variable).
185*9880d681SAndroid Build Coastguard WorkerThe first argument is metadata holding the alloca for the variable.  The second
186*9880d681SAndroid Build Coastguard Workerargument is a `local variable <LangRef.html#dilocalvariable>`_ containing a
187*9880d681SAndroid Build Coastguard Workerdescription of the variable.  The third argument is a `complex expression
188*9880d681SAndroid Build Coastguard Worker<LangRef.html#diexpression>`_.
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker``llvm.dbg.value``
191*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker  void @llvm.dbg.value(metadata, i64, metadata, metadata)
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard WorkerThis intrinsic provides information when a user source variable is set to a new
198*9880d681SAndroid Build Coastguard Workervalue.  The first argument is the new value (wrapped as metadata).  The second
199*9880d681SAndroid Build Coastguard Workerargument is the offset in the user source variable where the new value is
200*9880d681SAndroid Build Coastguard Workerwritten.  The third argument is a `local variable
201*9880d681SAndroid Build Coastguard Worker<LangRef.html#dilocalvariable>`_ containing a description of the variable.  The
202*9880d681SAndroid Build Coastguard Workerfourth argument is a `complex expression <LangRef.html#diexpression>`_.
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard WorkerObject lifetimes and scoping
205*9880d681SAndroid Build Coastguard Worker============================
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard WorkerIn many languages, the local variables in functions can have their lifetimes or
208*9880d681SAndroid Build Coastguard Workerscopes limited to a subset of a function.  In the C family of languages, for
209*9880d681SAndroid Build Coastguard Workerexample, variables are only live (readable and writable) within the source
210*9880d681SAndroid Build Coastguard Workerblock that they are defined in.  In functional languages, values are only
211*9880d681SAndroid Build Coastguard Workerreadable after they have been defined.  Though this is a very obvious concept,
212*9880d681SAndroid Build Coastguard Workerit is non-trivial to model in LLVM, because it has no notion of scoping in this
213*9880d681SAndroid Build Coastguard Workersense, and does not want to be tied to a language's scoping rules.
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard WorkerIn order to handle this, the LLVM debug format uses the metadata attached to
216*9880d681SAndroid Build Coastguard Workerllvm instructions to encode line number and scoping information.  Consider the
217*9880d681SAndroid Build Coastguard Workerfollowing C fragment, for example:
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker.. code-block:: c
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker  1.  void foo() {
222*9880d681SAndroid Build Coastguard Worker  2.    int X = 21;
223*9880d681SAndroid Build Coastguard Worker  3.    int Y = 22;
224*9880d681SAndroid Build Coastguard Worker  4.    {
225*9880d681SAndroid Build Coastguard Worker  5.      int Z = 23;
226*9880d681SAndroid Build Coastguard Worker  6.      Z = X;
227*9880d681SAndroid Build Coastguard Worker  7.    }
228*9880d681SAndroid Build Coastguard Worker  8.    X = Y;
229*9880d681SAndroid Build Coastguard Worker  9.  }
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard WorkerCompiled to LLVM, this function would be represented like this:
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker  ; Function Attrs: nounwind ssp uwtable
236*9880d681SAndroid Build Coastguard Worker  define void @foo() #0 !dbg !4 {
237*9880d681SAndroid Build Coastguard Worker  entry:
238*9880d681SAndroid Build Coastguard Worker    %X = alloca i32, align 4
239*9880d681SAndroid Build Coastguard Worker    %Y = alloca i32, align 4
240*9880d681SAndroid Build Coastguard Worker    %Z = alloca i32, align 4
241*9880d681SAndroid Build Coastguard Worker    call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
242*9880d681SAndroid Build Coastguard Worker    store i32 21, i32* %X, align 4, !dbg !14
243*9880d681SAndroid Build Coastguard Worker    call void @llvm.dbg.declare(metadata i32* %Y, metadata !15, metadata !13), !dbg !16
244*9880d681SAndroid Build Coastguard Worker    store i32 22, i32* %Y, align 4, !dbg !16
245*9880d681SAndroid Build Coastguard Worker    call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
246*9880d681SAndroid Build Coastguard Worker    store i32 23, i32* %Z, align 4, !dbg !19
247*9880d681SAndroid Build Coastguard Worker    %0 = load i32, i32* %X, align 4, !dbg !20
248*9880d681SAndroid Build Coastguard Worker    store i32 %0, i32* %Z, align 4, !dbg !21
249*9880d681SAndroid Build Coastguard Worker    %1 = load i32, i32* %Y, align 4, !dbg !22
250*9880d681SAndroid Build Coastguard Worker    store i32 %1, i32* %X, align 4, !dbg !23
251*9880d681SAndroid Build Coastguard Worker    ret void, !dbg !24
252*9880d681SAndroid Build Coastguard Worker  }
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard Worker  ; Function Attrs: nounwind readnone
255*9880d681SAndroid Build Coastguard Worker  declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker  attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
258*9880d681SAndroid Build Coastguard Worker  attributes #1 = { nounwind readnone }
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker  !llvm.dbg.cu = !{!0}
261*9880d681SAndroid Build Coastguard Worker  !llvm.module.flags = !{!7, !8, !9}
262*9880d681SAndroid Build Coastguard Worker  !llvm.ident = !{!10}
263*9880d681SAndroid Build Coastguard Worker
264*9880d681SAndroid Build Coastguard Worker  !0 = !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
265*9880d681SAndroid Build Coastguard Worker  !1 = !DIFile(filename: "/dev/stdin", directory: "/Users/dexonsmith/data/llvm/debug-info")
266*9880d681SAndroid Build Coastguard Worker  !2 = !{}
267*9880d681SAndroid Build Coastguard Worker  !3 = !{!4}
268*9880d681SAndroid Build Coastguard Worker  !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, variables: !2)
269*9880d681SAndroid Build Coastguard Worker  !5 = !DISubroutineType(types: !6)
270*9880d681SAndroid Build Coastguard Worker  !6 = !{null}
271*9880d681SAndroid Build Coastguard Worker  !7 = !{i32 2, !"Dwarf Version", i32 2}
272*9880d681SAndroid Build Coastguard Worker  !8 = !{i32 2, !"Debug Info Version", i32 3}
273*9880d681SAndroid Build Coastguard Worker  !9 = !{i32 1, !"PIC Level", i32 2}
274*9880d681SAndroid Build Coastguard Worker  !10 = !{!"clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)"}
275*9880d681SAndroid Build Coastguard Worker  !11 = !DILocalVariable(name: "X", scope: !4, file: !1, line: 2, type: !12)
276*9880d681SAndroid Build Coastguard Worker  !12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
277*9880d681SAndroid Build Coastguard Worker  !13 = !DIExpression()
278*9880d681SAndroid Build Coastguard Worker  !14 = !DILocation(line: 2, column: 9, scope: !4)
279*9880d681SAndroid Build Coastguard Worker  !15 = !DILocalVariable(name: "Y", scope: !4, file: !1, line: 3, type: !12)
280*9880d681SAndroid Build Coastguard Worker  !16 = !DILocation(line: 3, column: 9, scope: !4)
281*9880d681SAndroid Build Coastguard Worker  !17 = !DILocalVariable(name: "Z", scope: !18, file: !1, line: 5, type: !12)
282*9880d681SAndroid Build Coastguard Worker  !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
283*9880d681SAndroid Build Coastguard Worker  !19 = !DILocation(line: 5, column: 11, scope: !18)
284*9880d681SAndroid Build Coastguard Worker  !20 = !DILocation(line: 6, column: 11, scope: !18)
285*9880d681SAndroid Build Coastguard Worker  !21 = !DILocation(line: 6, column: 9, scope: !18)
286*9880d681SAndroid Build Coastguard Worker  !22 = !DILocation(line: 8, column: 9, scope: !4)
287*9880d681SAndroid Build Coastguard Worker  !23 = !DILocation(line: 8, column: 7, scope: !4)
288*9880d681SAndroid Build Coastguard Worker  !24 = !DILocation(line: 9, column: 3, scope: !4)
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker
291*9880d681SAndroid Build Coastguard WorkerThis example illustrates a few important details about LLVM debugging
292*9880d681SAndroid Build Coastguard Workerinformation.  In particular, it shows how the ``llvm.dbg.declare`` intrinsic and
293*9880d681SAndroid Build Coastguard Workerlocation information, which are attached to an instruction, are applied
294*9880d681SAndroid Build Coastguard Workertogether to allow a debugger to analyze the relationship between statements,
295*9880d681SAndroid Build Coastguard Workervariable definitions, and the code used to implement the function.
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker  call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
300*9880d681SAndroid Build Coastguard Worker    ; [debug line = 2:7] [debug variable = X]
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard WorkerThe first intrinsic ``%llvm.dbg.declare`` encodes debugging information for the
303*9880d681SAndroid Build Coastguard Workervariable ``X``.  The metadata ``!dbg !14`` attached to the intrinsic provides
304*9880d681SAndroid Build Coastguard Workerscope information for the variable ``X``.
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker  !14 = !DILocation(line: 2, column: 9, scope: !4)
309*9880d681SAndroid Build Coastguard Worker  !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
310*9880d681SAndroid Build Coastguard Worker                              isLocal: false, isDefinition: true, scopeLine: 1,
311*9880d681SAndroid Build Coastguard Worker                              isOptimized: false, variables: !2)
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard WorkerHere ``!14`` is metadata providing `location information
314*9880d681SAndroid Build Coastguard Worker<LangRef.html#dilocation>`_.  In this example, scope is encoded by ``!4``, a
315*9880d681SAndroid Build Coastguard Worker`subprogram descriptor <LangRef.html#disubprogram>`_.  This way the location
316*9880d681SAndroid Build Coastguard Workerinformation attached to the intrinsics indicates that the variable ``X`` is
317*9880d681SAndroid Build Coastguard Workerdeclared at line number 2 at a function level scope in function ``foo``.
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard WorkerNow lets take another example.
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker  call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
324*9880d681SAndroid Build Coastguard Worker    ; [debug line = 5:9] [debug variable = Z]
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard WorkerThe third intrinsic ``%llvm.dbg.declare`` encodes debugging information for
327*9880d681SAndroid Build Coastguard Workervariable ``Z``.  The metadata ``!dbg !19`` attached to the intrinsic provides
328*9880d681SAndroid Build Coastguard Workerscope information for the variable ``Z``.
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker  !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
333*9880d681SAndroid Build Coastguard Worker  !19 = !DILocation(line: 5, column: 11, scope: !18)
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard WorkerHere ``!19`` indicates that ``Z`` is declared at line number 5 and column
336*9880d681SAndroid Build Coastguard Workernumber 0 inside of lexical scope ``!18``.  The lexical scope itself resides
337*9880d681SAndroid Build Coastguard Workerinside of subprogram ``!4`` described above.
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard WorkerThe scope information attached with each instruction provides a straightforward
340*9880d681SAndroid Build Coastguard Workerway to find instructions covered by a scope.
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard Worker.. _ccxx_frontend:
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard WorkerC/C++ front-end specific debug information
345*9880d681SAndroid Build Coastguard Worker==========================================
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard WorkerThe C and C++ front-ends represent information about the program in a format
348*9880d681SAndroid Build Coastguard Workerthat is effectively identical to `DWARF 3.0
349*9880d681SAndroid Build Coastguard Worker<http://www.eagercon.com/dwarf/dwarf3std.htm>`_ in terms of information
350*9880d681SAndroid Build Coastguard Workercontent.  This allows code generators to trivially support native debuggers by
351*9880d681SAndroid Build Coastguard Workergenerating standard dwarf information, and contains enough information for
352*9880d681SAndroid Build Coastguard Workernon-dwarf targets to translate it as needed.
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard WorkerThis section describes the forms used to represent C and C++ programs.  Other
355*9880d681SAndroid Build Coastguard Workerlanguages could pattern themselves after this (which itself is tuned to
356*9880d681SAndroid Build Coastguard Workerrepresenting programs in the same way that DWARF 3 does), or they could choose
357*9880d681SAndroid Build Coastguard Workerto provide completely different forms if they don't fit into the DWARF model.
358*9880d681SAndroid Build Coastguard WorkerAs support for debugging information gets added to the various LLVM
359*9880d681SAndroid Build Coastguard Workersource-language front-ends, the information used should be documented here.
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard WorkerThe following sections provide examples of a few C/C++ constructs and the debug
362*9880d681SAndroid Build Coastguard Workerinformation that would best describe those constructs.  The canonical
363*9880d681SAndroid Build Coastguard Workerreferences are the ``DIDescriptor`` classes defined in
364*9880d681SAndroid Build Coastguard Worker``include/llvm/IR/DebugInfo.h`` and the implementations of the helper functions
365*9880d681SAndroid Build Coastguard Workerin ``lib/IR/DIBuilder.cpp``.
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard WorkerC/C++ source file information
368*9880d681SAndroid Build Coastguard Worker-----------------------------
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker``llvm::Instruction`` provides easy access to metadata attached with an
371*9880d681SAndroid Build Coastguard Workerinstruction.  One can extract line number information encoded in LLVM IR using
372*9880d681SAndroid Build Coastguard Worker``Instruction::getDebugLoc()`` and ``DILocation::getLine()``.
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker  if (DILocation *Loc = I->getDebugLoc()) { // Here I is an LLVM instruction
377*9880d681SAndroid Build Coastguard Worker    unsigned Line = Loc->getLine();
378*9880d681SAndroid Build Coastguard Worker    StringRef File = Loc->getFilename();
379*9880d681SAndroid Build Coastguard Worker    StringRef Dir = Loc->getDirectory();
380*9880d681SAndroid Build Coastguard Worker  }
381*9880d681SAndroid Build Coastguard Worker
382*9880d681SAndroid Build Coastguard WorkerC/C++ global variable information
383*9880d681SAndroid Build Coastguard Worker---------------------------------
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard WorkerGiven an integer global variable declared as follows:
386*9880d681SAndroid Build Coastguard Worker
387*9880d681SAndroid Build Coastguard Worker.. code-block:: c
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker  int MyGlobal = 100;
390*9880d681SAndroid Build Coastguard Worker
391*9880d681SAndroid Build Coastguard Workera C/C++ front-end would generate the following descriptors:
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker  ;;
396*9880d681SAndroid Build Coastguard Worker  ;; Define the global itself.
397*9880d681SAndroid Build Coastguard Worker  ;;
398*9880d681SAndroid Build Coastguard Worker  @MyGlobal = global i32 100, align 4
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker  ;;
401*9880d681SAndroid Build Coastguard Worker  ;; List of debug info of globals
402*9880d681SAndroid Build Coastguard Worker  ;;
403*9880d681SAndroid Build Coastguard Worker  !llvm.dbg.cu = !{!0}
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker  ;; Some unrelated metadata.
406*9880d681SAndroid Build Coastguard Worker  !llvm.module.flags = !{!6, !7}
407*9880d681SAndroid Build Coastguard Worker
408*9880d681SAndroid Build Coastguard Worker  ;; Define the compile unit.
409*9880d681SAndroid Build Coastguard Worker  !0 = !DICompileUnit(language: DW_LANG_C99, file: !1,
410*9880d681SAndroid Build Coastguard Worker                      producer:
411*9880d681SAndroid Build Coastguard Worker                      "clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)",
412*9880d681SAndroid Build Coastguard Worker                      isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug,
413*9880d681SAndroid Build Coastguard Worker                      enums: !2, retainedTypes: !2, subprograms: !2, globals:
414*9880d681SAndroid Build Coastguard Worker                      !3, imports: !2)
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard Worker  ;;
417*9880d681SAndroid Build Coastguard Worker  ;; Define the file
418*9880d681SAndroid Build Coastguard Worker  ;;
419*9880d681SAndroid Build Coastguard Worker  !1 = !DIFile(filename: "/dev/stdin",
420*9880d681SAndroid Build Coastguard Worker               directory: "/Users/dexonsmith/data/llvm/debug-info")
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker  ;; An empty array.
423*9880d681SAndroid Build Coastguard Worker  !2 = !{}
424*9880d681SAndroid Build Coastguard Worker
425*9880d681SAndroid Build Coastguard Worker  ;; The Array of Global Variables
426*9880d681SAndroid Build Coastguard Worker  !3 = !{!4}
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker  ;;
429*9880d681SAndroid Build Coastguard Worker  ;; Define the global variable itself.
430*9880d681SAndroid Build Coastguard Worker  ;;
431*9880d681SAndroid Build Coastguard Worker  !4 = !DIGlobalVariable(name: "MyGlobal", scope: !0, file: !1, line: 1,
432*9880d681SAndroid Build Coastguard Worker                         type: !5, isLocal: false, isDefinition: true,
433*9880d681SAndroid Build Coastguard Worker                         variable: i32* @MyGlobal)
434*9880d681SAndroid Build Coastguard Worker
435*9880d681SAndroid Build Coastguard Worker  ;;
436*9880d681SAndroid Build Coastguard Worker  ;; Define the type
437*9880d681SAndroid Build Coastguard Worker  ;;
438*9880d681SAndroid Build Coastguard Worker  !5 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
439*9880d681SAndroid Build Coastguard Worker
440*9880d681SAndroid Build Coastguard Worker  ;; Dwarf version to output.
441*9880d681SAndroid Build Coastguard Worker  !6 = !{i32 2, !"Dwarf Version", i32 2}
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard Worker  ;; Debug info schema version.
444*9880d681SAndroid Build Coastguard Worker  !7 = !{i32 2, !"Debug Info Version", i32 3}
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard WorkerC/C++ function information
447*9880d681SAndroid Build Coastguard Worker--------------------------
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard WorkerGiven a function declared as follows:
450*9880d681SAndroid Build Coastguard Worker
451*9880d681SAndroid Build Coastguard Worker.. code-block:: c
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker  int main(int argc, char *argv[]) {
454*9880d681SAndroid Build Coastguard Worker    return 0;
455*9880d681SAndroid Build Coastguard Worker  }
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Workera C/C++ front-end would generate the following descriptors:
458*9880d681SAndroid Build Coastguard Worker
459*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker  ;;
462*9880d681SAndroid Build Coastguard Worker  ;; Define the anchor for subprograms.
463*9880d681SAndroid Build Coastguard Worker  ;;
464*9880d681SAndroid Build Coastguard Worker  !4 = !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !5,
465*9880d681SAndroid Build Coastguard Worker                     isLocal: false, isDefinition: true, scopeLine: 1,
466*9880d681SAndroid Build Coastguard Worker                     flags: DIFlagPrototyped, isOptimized: false,
467*9880d681SAndroid Build Coastguard Worker                     variables: !2)
468*9880d681SAndroid Build Coastguard Worker
469*9880d681SAndroid Build Coastguard Worker  ;;
470*9880d681SAndroid Build Coastguard Worker  ;; Define the subprogram itself.
471*9880d681SAndroid Build Coastguard Worker  ;;
472*9880d681SAndroid Build Coastguard Worker  define i32 @main(i32 %argc, i8** %argv) !dbg !4 {
473*9880d681SAndroid Build Coastguard Worker  ...
474*9880d681SAndroid Build Coastguard Worker  }
475*9880d681SAndroid Build Coastguard Worker
476*9880d681SAndroid Build Coastguard WorkerDebugging information format
477*9880d681SAndroid Build Coastguard Worker============================
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard WorkerDebugging Information Extension for Objective C Properties
480*9880d681SAndroid Build Coastguard Worker----------------------------------------------------------
481*9880d681SAndroid Build Coastguard Worker
482*9880d681SAndroid Build Coastguard WorkerIntroduction
483*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^
484*9880d681SAndroid Build Coastguard Worker
485*9880d681SAndroid Build Coastguard WorkerObjective C provides a simpler way to declare and define accessor methods using
486*9880d681SAndroid Build Coastguard Workerdeclared properties.  The language provides features to declare a property and
487*9880d681SAndroid Build Coastguard Workerto let compiler synthesize accessor methods.
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard WorkerThe debugger lets developer inspect Objective C interfaces and their instance
490*9880d681SAndroid Build Coastguard Workervariables and class variables.  However, the debugger does not know anything
491*9880d681SAndroid Build Coastguard Workerabout the properties defined in Objective C interfaces.  The debugger consumes
492*9880d681SAndroid Build Coastguard Workerinformation generated by compiler in DWARF format.  The format does not support
493*9880d681SAndroid Build Coastguard Workerencoding of Objective C properties.  This proposal describes DWARF extensions to
494*9880d681SAndroid Build Coastguard Workerencode Objective C properties, which the debugger can use to let developers
495*9880d681SAndroid Build Coastguard Workerinspect Objective C properties.
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard WorkerProposal
498*9880d681SAndroid Build Coastguard Worker^^^^^^^^
499*9880d681SAndroid Build Coastguard Worker
500*9880d681SAndroid Build Coastguard WorkerObjective C properties exist separately from class members.  A property can be
501*9880d681SAndroid Build Coastguard Workerdefined only by "setter" and "getter" selectors, and be calculated anew on each
502*9880d681SAndroid Build Coastguard Workeraccess.  Or a property can just be a direct access to some declared ivar.
503*9880d681SAndroid Build Coastguard WorkerFinally it can have an ivar "automatically synthesized" for it by the compiler,
504*9880d681SAndroid Build Coastguard Workerin which case the property can be referred to in user code directly using the
505*9880d681SAndroid Build Coastguard Workerstandard C dereference syntax as well as through the property "dot" syntax, but
506*9880d681SAndroid Build Coastguard Workerthere is no entry in the ``@interface`` declaration corresponding to this ivar.
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard WorkerTo facilitate debugging, these properties we will add a new DWARF TAG into the
509*9880d681SAndroid Build Coastguard Worker``DW_TAG_structure_type`` definition for the class to hold the description of a
510*9880d681SAndroid Build Coastguard Workergiven property, and a set of DWARF attributes that provide said description.
511*9880d681SAndroid Build Coastguard WorkerThe property tag will also contain the name and declared type of the property.
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard WorkerIf there is a related ivar, there will also be a DWARF property attribute placed
514*9880d681SAndroid Build Coastguard Workerin the ``DW_TAG_member`` DIE for that ivar referring back to the property TAG
515*9880d681SAndroid Build Coastguard Workerfor that property.  And in the case where the compiler synthesizes the ivar
516*9880d681SAndroid Build Coastguard Workerdirectly, the compiler is expected to generate a ``DW_TAG_member`` for that
517*9880d681SAndroid Build Coastguard Workerivar (with the ``DW_AT_artificial`` set to 1), whose name will be the name used
518*9880d681SAndroid Build Coastguard Workerto access this ivar directly in code, and with the property attribute pointing
519*9880d681SAndroid Build Coastguard Workerback to the property it is backing.
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard WorkerThe following examples will serve as illustration for our discussion:
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker.. code-block:: objc
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard Worker  @interface I1 {
526*9880d681SAndroid Build Coastguard Worker    int n2;
527*9880d681SAndroid Build Coastguard Worker  }
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker  @property int p1;
530*9880d681SAndroid Build Coastguard Worker  @property int p2;
531*9880d681SAndroid Build Coastguard Worker  @end
532*9880d681SAndroid Build Coastguard Worker
533*9880d681SAndroid Build Coastguard Worker  @implementation I1
534*9880d681SAndroid Build Coastguard Worker  @synthesize p1;
535*9880d681SAndroid Build Coastguard Worker  @synthesize p2 = n2;
536*9880d681SAndroid Build Coastguard Worker  @end
537*9880d681SAndroid Build Coastguard Worker
538*9880d681SAndroid Build Coastguard WorkerThis produces the following DWARF (this is a "pseudo dwarfdump" output):
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker.. code-block:: none
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker  0x00000100:  TAG_structure_type [7] *
543*9880d681SAndroid Build Coastguard Worker                 AT_APPLE_runtime_class( 0x10 )
544*9880d681SAndroid Build Coastguard Worker                 AT_name( "I1" )
545*9880d681SAndroid Build Coastguard Worker                 AT_decl_file( "Objc_Property.m" )
546*9880d681SAndroid Build Coastguard Worker                 AT_decl_line( 3 )
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker  0x00000110    TAG_APPLE_property
549*9880d681SAndroid Build Coastguard Worker                  AT_name ( "p1" )
550*9880d681SAndroid Build Coastguard Worker                  AT_type ( {0x00000150} ( int ) )
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard Worker  0x00000120:   TAG_APPLE_property
553*9880d681SAndroid Build Coastguard Worker                  AT_name ( "p2" )
554*9880d681SAndroid Build Coastguard Worker                  AT_type ( {0x00000150} ( int ) )
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker  0x00000130:   TAG_member [8]
557*9880d681SAndroid Build Coastguard Worker                  AT_name( "_p1" )
558*9880d681SAndroid Build Coastguard Worker                  AT_APPLE_property ( {0x00000110} "p1" )
559*9880d681SAndroid Build Coastguard Worker                  AT_type( {0x00000150} ( int ) )
560*9880d681SAndroid Build Coastguard Worker                  AT_artificial ( 0x1 )
561*9880d681SAndroid Build Coastguard Worker
562*9880d681SAndroid Build Coastguard Worker  0x00000140:    TAG_member [8]
563*9880d681SAndroid Build Coastguard Worker                   AT_name( "n2" )
564*9880d681SAndroid Build Coastguard Worker                   AT_APPLE_property ( {0x00000120} "p2" )
565*9880d681SAndroid Build Coastguard Worker                   AT_type( {0x00000150} ( int ) )
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard Worker  0x00000150:  AT_type( ( int ) )
568*9880d681SAndroid Build Coastguard Worker
569*9880d681SAndroid Build Coastguard WorkerNote, the current convention is that the name of the ivar for an
570*9880d681SAndroid Build Coastguard Workerauto-synthesized property is the name of the property from which it derives
571*9880d681SAndroid Build Coastguard Workerwith an underscore prepended, as is shown in the example.  But we actually
572*9880d681SAndroid Build Coastguard Workerdon't need to know this convention, since we are given the name of the ivar
573*9880d681SAndroid Build Coastguard Workerdirectly.
574*9880d681SAndroid Build Coastguard Worker
575*9880d681SAndroid Build Coastguard WorkerAlso, it is common practice in ObjC to have different property declarations in
576*9880d681SAndroid Build Coastguard Workerthe @interface and @implementation - e.g. to provide a read-only property in
577*9880d681SAndroid Build Coastguard Workerthe interface,and a read-write interface in the implementation.  In that case,
578*9880d681SAndroid Build Coastguard Workerthe compiler should emit whichever property declaration will be in force in the
579*9880d681SAndroid Build Coastguard Workercurrent translation unit.
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard WorkerDevelopers can decorate a property with attributes which are encoded using
582*9880d681SAndroid Build Coastguard Worker``DW_AT_APPLE_property_attribute``.
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker.. code-block:: objc
585*9880d681SAndroid Build Coastguard Worker
586*9880d681SAndroid Build Coastguard Worker  @property (readonly, nonatomic) int pr;
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard Worker.. code-block:: none
589*9880d681SAndroid Build Coastguard Worker
590*9880d681SAndroid Build Coastguard Worker  TAG_APPLE_property [8]
591*9880d681SAndroid Build Coastguard Worker    AT_name( "pr" )
592*9880d681SAndroid Build Coastguard Worker    AT_type ( {0x00000147} (int) )
593*9880d681SAndroid Build Coastguard Worker    AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic)
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard WorkerThe setter and getter method names are attached to the property using
596*9880d681SAndroid Build Coastguard Worker``DW_AT_APPLE_property_setter`` and ``DW_AT_APPLE_property_getter`` attributes.
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard Worker.. code-block:: objc
599*9880d681SAndroid Build Coastguard Worker
600*9880d681SAndroid Build Coastguard Worker  @interface I1
601*9880d681SAndroid Build Coastguard Worker  @property (setter=myOwnP3Setter:) int p3;
602*9880d681SAndroid Build Coastguard Worker  -(void)myOwnP3Setter:(int)a;
603*9880d681SAndroid Build Coastguard Worker  @end
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard Worker  @implementation I1
606*9880d681SAndroid Build Coastguard Worker  @synthesize p3;
607*9880d681SAndroid Build Coastguard Worker  -(void)myOwnP3Setter:(int)a{ }
608*9880d681SAndroid Build Coastguard Worker  @end
609*9880d681SAndroid Build Coastguard Worker
610*9880d681SAndroid Build Coastguard WorkerThe DWARF for this would be:
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker.. code-block:: none
613*9880d681SAndroid Build Coastguard Worker
614*9880d681SAndroid Build Coastguard Worker  0x000003bd: TAG_structure_type [7] *
615*9880d681SAndroid Build Coastguard Worker                AT_APPLE_runtime_class( 0x10 )
616*9880d681SAndroid Build Coastguard Worker                AT_name( "I1" )
617*9880d681SAndroid Build Coastguard Worker                AT_decl_file( "Objc_Property.m" )
618*9880d681SAndroid Build Coastguard Worker                AT_decl_line( 3 )
619*9880d681SAndroid Build Coastguard Worker
620*9880d681SAndroid Build Coastguard Worker  0x000003cd      TAG_APPLE_property
621*9880d681SAndroid Build Coastguard Worker                    AT_name ( "p3" )
622*9880d681SAndroid Build Coastguard Worker                    AT_APPLE_property_setter ( "myOwnP3Setter:" )
623*9880d681SAndroid Build Coastguard Worker                    AT_type( {0x00000147} ( int ) )
624*9880d681SAndroid Build Coastguard Worker
625*9880d681SAndroid Build Coastguard Worker  0x000003f3:     TAG_member [8]
626*9880d681SAndroid Build Coastguard Worker                    AT_name( "_p3" )
627*9880d681SAndroid Build Coastguard Worker                    AT_type ( {0x00000147} ( int ) )
628*9880d681SAndroid Build Coastguard Worker                    AT_APPLE_property ( {0x000003cd} )
629*9880d681SAndroid Build Coastguard Worker                    AT_artificial ( 0x1 )
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard WorkerNew DWARF Tags
632*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker+-----------------------+--------+
635*9880d681SAndroid Build Coastguard Worker| TAG                   | Value  |
636*9880d681SAndroid Build Coastguard Worker+=======================+========+
637*9880d681SAndroid Build Coastguard Worker| DW_TAG_APPLE_property | 0x4200 |
638*9880d681SAndroid Build Coastguard Worker+-----------------------+--------+
639*9880d681SAndroid Build Coastguard Worker
640*9880d681SAndroid Build Coastguard WorkerNew DWARF Attributes
641*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^
642*9880d681SAndroid Build Coastguard Worker
643*9880d681SAndroid Build Coastguard Worker+--------------------------------+--------+-----------+
644*9880d681SAndroid Build Coastguard Worker| Attribute                      | Value  | Classes   |
645*9880d681SAndroid Build Coastguard Worker+================================+========+===========+
646*9880d681SAndroid Build Coastguard Worker| DW_AT_APPLE_property           | 0x3fed | Reference |
647*9880d681SAndroid Build Coastguard Worker+--------------------------------+--------+-----------+
648*9880d681SAndroid Build Coastguard Worker| DW_AT_APPLE_property_getter    | 0x3fe9 | String    |
649*9880d681SAndroid Build Coastguard Worker+--------------------------------+--------+-----------+
650*9880d681SAndroid Build Coastguard Worker| DW_AT_APPLE_property_setter    | 0x3fea | String    |
651*9880d681SAndroid Build Coastguard Worker+--------------------------------+--------+-----------+
652*9880d681SAndroid Build Coastguard Worker| DW_AT_APPLE_property_attribute | 0x3feb | Constant  |
653*9880d681SAndroid Build Coastguard Worker+--------------------------------+--------+-----------+
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard WorkerNew DWARF Constants
656*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
659*9880d681SAndroid Build Coastguard Worker| Name                                 | Value |
660*9880d681SAndroid Build Coastguard Worker+======================================+=======+
661*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_readonly           | 0x01  |
662*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
663*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_getter             | 0x02  |
664*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
665*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_assign             | 0x04  |
666*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
667*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_readwrite          | 0x08  |
668*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
669*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_retain             | 0x10  |
670*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
671*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_copy               | 0x20  |
672*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
673*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_nonatomic          | 0x40  |
674*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
675*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_setter             | 0x80  |
676*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
677*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_atomic             | 0x100 |
678*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
679*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_weak               | 0x200 |
680*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
681*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_strong             | 0x400 |
682*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
683*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_unsafe_unretained  | 0x800 |
684*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
685*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_nullability        | 0x1000|
686*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
687*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_null_resettable    | 0x2000|
688*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
689*9880d681SAndroid Build Coastguard Worker| DW_APPLE_PROPERTY_class              | 0x4000|
690*9880d681SAndroid Build Coastguard Worker+--------------------------------------+-------+
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard WorkerName Accelerator Tables
693*9880d681SAndroid Build Coastguard Worker-----------------------
694*9880d681SAndroid Build Coastguard Worker
695*9880d681SAndroid Build Coastguard WorkerIntroduction
696*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^
697*9880d681SAndroid Build Coastguard Worker
698*9880d681SAndroid Build Coastguard WorkerThe "``.debug_pubnames``" and "``.debug_pubtypes``" formats are not what a
699*9880d681SAndroid Build Coastguard Workerdebugger needs.  The "``pub``" in the section name indicates that the entries
700*9880d681SAndroid Build Coastguard Workerin the table are publicly visible names only.  This means no static or hidden
701*9880d681SAndroid Build Coastguard Workerfunctions show up in the "``.debug_pubnames``".  No static variables or private
702*9880d681SAndroid Build Coastguard Workerclass variables are in the "``.debug_pubtypes``".  Many compilers add different
703*9880d681SAndroid Build Coastguard Workerthings to these tables, so we can't rely upon the contents between gcc, icc, or
704*9880d681SAndroid Build Coastguard Workerclang.
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard WorkerThe typical query given by users tends not to match up with the contents of
707*9880d681SAndroid Build Coastguard Workerthese tables.  For example, the DWARF spec states that "In the case of the name
708*9880d681SAndroid Build Coastguard Workerof a function member or static data member of a C++ structure, class or union,
709*9880d681SAndroid Build Coastguard Workerthe name presented in the "``.debug_pubnames``" section is not the simple name
710*9880d681SAndroid Build Coastguard Workergiven by the ``DW_AT_name attribute`` of the referenced debugging information
711*9880d681SAndroid Build Coastguard Workerentry, but rather the fully qualified name of the data or function member."
712*9880d681SAndroid Build Coastguard WorkerSo the only names in these tables for complex C++ entries is a fully
713*9880d681SAndroid Build Coastguard Workerqualified name.  Debugger users tend not to enter their search strings as
714*9880d681SAndroid Build Coastguard Worker"``a::b::c(int,const Foo&) const``", but rather as "``c``", "``b::c``" , or
715*9880d681SAndroid Build Coastguard Worker"``a::b::c``".  So the name entered in the name table must be demangled in
716*9880d681SAndroid Build Coastguard Workerorder to chop it up appropriately and additional names must be manually entered
717*9880d681SAndroid Build Coastguard Workerinto the table to make it effective as a name lookup table for debuggers to
718*9880d681SAndroid Build Coastguard Workeruse.
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard WorkerAll debuggers currently ignore the "``.debug_pubnames``" table as a result of
721*9880d681SAndroid Build Coastguard Workerits inconsistent and useless public-only name content making it a waste of
722*9880d681SAndroid Build Coastguard Workerspace in the object file.  These tables, when they are written to disk, are not
723*9880d681SAndroid Build Coastguard Workersorted in any way, leaving every debugger to do its own parsing and sorting.
724*9880d681SAndroid Build Coastguard WorkerThese tables also include an inlined copy of the string values in the table
725*9880d681SAndroid Build Coastguard Workeritself making the tables much larger than they need to be on disk, especially
726*9880d681SAndroid Build Coastguard Workerfor large C++ programs.
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard WorkerCan't we just fix the sections by adding all of the names we need to this
729*9880d681SAndroid Build Coastguard Workertable? No, because that is not what the tables are defined to contain and we
730*9880d681SAndroid Build Coastguard Workerwon't know the difference between the old bad tables and the new good tables.
731*9880d681SAndroid Build Coastguard WorkerAt best we could make our own renamed sections that contain all of the data we
732*9880d681SAndroid Build Coastguard Workerneed.
733*9880d681SAndroid Build Coastguard Worker
734*9880d681SAndroid Build Coastguard WorkerThese tables are also insufficient for what a debugger like LLDB needs.  LLDB
735*9880d681SAndroid Build Coastguard Workeruses clang for its expression parsing where LLDB acts as a PCH.  LLDB is then
736*9880d681SAndroid Build Coastguard Workeroften asked to look for type "``foo``" or namespace "``bar``", or list items in
737*9880d681SAndroid Build Coastguard Workernamespace "``baz``".  Namespaces are not included in the pubnames or pubtypes
738*9880d681SAndroid Build Coastguard Workertables.  Since clang asks a lot of questions when it is parsing an expression,
739*9880d681SAndroid Build Coastguard Workerwe need to be very fast when looking up names, as it happens a lot.  Having new
740*9880d681SAndroid Build Coastguard Workeraccelerator tables that are optimized for very quick lookups will benefit this
741*9880d681SAndroid Build Coastguard Workertype of debugging experience greatly.
742*9880d681SAndroid Build Coastguard Worker
743*9880d681SAndroid Build Coastguard WorkerWe would like to generate name lookup tables that can be mapped into memory
744*9880d681SAndroid Build Coastguard Workerfrom disk, and used as is, with little or no up-front parsing.  We would also
745*9880d681SAndroid Build Coastguard Workerbe able to control the exact content of these different tables so they contain
746*9880d681SAndroid Build Coastguard Workerexactly what we need.  The Name Accelerator Tables were designed to fix these
747*9880d681SAndroid Build Coastguard Workerissues.  In order to solve these issues we need to:
748*9880d681SAndroid Build Coastguard Worker
749*9880d681SAndroid Build Coastguard Worker* Have a format that can be mapped into memory from disk and used as is
750*9880d681SAndroid Build Coastguard Worker* Lookups should be very fast
751*9880d681SAndroid Build Coastguard Worker* Extensible table format so these tables can be made by many producers
752*9880d681SAndroid Build Coastguard Worker* Contain all of the names needed for typical lookups out of the box
753*9880d681SAndroid Build Coastguard Worker* Strict rules for the contents of tables
754*9880d681SAndroid Build Coastguard Worker
755*9880d681SAndroid Build Coastguard WorkerTable size is important and the accelerator table format should allow the reuse
756*9880d681SAndroid Build Coastguard Workerof strings from common string tables so the strings for the names are not
757*9880d681SAndroid Build Coastguard Workerduplicated.  We also want to make sure the table is ready to be used as-is by
758*9880d681SAndroid Build Coastguard Workersimply mapping the table into memory with minimal header parsing.
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard WorkerThe name lookups need to be fast and optimized for the kinds of lookups that
761*9880d681SAndroid Build Coastguard Workerdebuggers tend to do.  Optimally we would like to touch as few parts of the
762*9880d681SAndroid Build Coastguard Workermapped table as possible when doing a name lookup and be able to quickly find
763*9880d681SAndroid Build Coastguard Workerthe name entry we are looking for, or discover there are no matches.  In the
764*9880d681SAndroid Build Coastguard Workercase of debuggers we optimized for lookups that fail most of the time.
765*9880d681SAndroid Build Coastguard Worker
766*9880d681SAndroid Build Coastguard WorkerEach table that is defined should have strict rules on exactly what is in the
767*9880d681SAndroid Build Coastguard Workeraccelerator tables and documented so clients can rely on the content.
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard WorkerHash Tables
770*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard WorkerStandard Hash Tables
773*9880d681SAndroid Build Coastguard Worker""""""""""""""""""""
774*9880d681SAndroid Build Coastguard Worker
775*9880d681SAndroid Build Coastguard WorkerTypical hash tables have a header, buckets, and each bucket points to the
776*9880d681SAndroid Build Coastguard Workerbucket contents:
777*9880d681SAndroid Build Coastguard Worker
778*9880d681SAndroid Build Coastguard Worker.. code-block:: none
779*9880d681SAndroid Build Coastguard Worker
780*9880d681SAndroid Build Coastguard Worker  .------------.
781*9880d681SAndroid Build Coastguard Worker  |  HEADER    |
782*9880d681SAndroid Build Coastguard Worker  |------------|
783*9880d681SAndroid Build Coastguard Worker  |  BUCKETS   |
784*9880d681SAndroid Build Coastguard Worker  |------------|
785*9880d681SAndroid Build Coastguard Worker  |  DATA      |
786*9880d681SAndroid Build Coastguard Worker  `------------'
787*9880d681SAndroid Build Coastguard Worker
788*9880d681SAndroid Build Coastguard WorkerThe BUCKETS are an array of offsets to DATA for each hash:
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard Worker.. code-block:: none
791*9880d681SAndroid Build Coastguard Worker
792*9880d681SAndroid Build Coastguard Worker  .------------.
793*9880d681SAndroid Build Coastguard Worker  | 0x00001000 | BUCKETS[0]
794*9880d681SAndroid Build Coastguard Worker  | 0x00002000 | BUCKETS[1]
795*9880d681SAndroid Build Coastguard Worker  | 0x00002200 | BUCKETS[2]
796*9880d681SAndroid Build Coastguard Worker  | 0x000034f0 | BUCKETS[3]
797*9880d681SAndroid Build Coastguard Worker  |            | ...
798*9880d681SAndroid Build Coastguard Worker  | 0xXXXXXXXX | BUCKETS[n_buckets]
799*9880d681SAndroid Build Coastguard Worker  '------------'
800*9880d681SAndroid Build Coastguard Worker
801*9880d681SAndroid Build Coastguard WorkerSo for ``bucket[3]`` in the example above, we have an offset into the table
802*9880d681SAndroid Build Coastguard Worker0x000034f0 which points to a chain of entries for the bucket.  Each bucket must
803*9880d681SAndroid Build Coastguard Workercontain a next pointer, full 32 bit hash value, the string itself, and the data
804*9880d681SAndroid Build Coastguard Workerfor the current string value.
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker.. code-block:: none
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker              .------------.
809*9880d681SAndroid Build Coastguard Worker  0x000034f0: | 0x00003500 | next pointer
810*9880d681SAndroid Build Coastguard Worker              | 0x12345678 | 32 bit hash
811*9880d681SAndroid Build Coastguard Worker              | "erase"    | string value
812*9880d681SAndroid Build Coastguard Worker              | data[n]    | HashData for this bucket
813*9880d681SAndroid Build Coastguard Worker              |------------|
814*9880d681SAndroid Build Coastguard Worker  0x00003500: | 0x00003550 | next pointer
815*9880d681SAndroid Build Coastguard Worker              | 0x29273623 | 32 bit hash
816*9880d681SAndroid Build Coastguard Worker              | "dump"     | string value
817*9880d681SAndroid Build Coastguard Worker              | data[n]    | HashData for this bucket
818*9880d681SAndroid Build Coastguard Worker              |------------|
819*9880d681SAndroid Build Coastguard Worker  0x00003550: | 0x00000000 | next pointer
820*9880d681SAndroid Build Coastguard Worker              | 0x82638293 | 32 bit hash
821*9880d681SAndroid Build Coastguard Worker              | "main"     | string value
822*9880d681SAndroid Build Coastguard Worker              | data[n]    | HashData for this bucket
823*9880d681SAndroid Build Coastguard Worker              `------------'
824*9880d681SAndroid Build Coastguard Worker
825*9880d681SAndroid Build Coastguard WorkerThe problem with this layout for debuggers is that we need to optimize for the
826*9880d681SAndroid Build Coastguard Workernegative lookup case where the symbol we're searching for is not present.  So
827*9880d681SAndroid Build Coastguard Workerif we were to lookup "``printf``" in the table above, we would make a 32 hash
828*9880d681SAndroid Build Coastguard Workerfor "``printf``", it might match ``bucket[3]``.  We would need to go to the
829*9880d681SAndroid Build Coastguard Workeroffset 0x000034f0 and start looking to see if our 32 bit hash matches.  To do
830*9880d681SAndroid Build Coastguard Workerso, we need to read the next pointer, then read the hash, compare it, and skip
831*9880d681SAndroid Build Coastguard Workerto the next bucket.  Each time we are skipping many bytes in memory and
832*9880d681SAndroid Build Coastguard Workertouching new cache pages just to do the compare on the full 32 bit hash.  All
833*9880d681SAndroid Build Coastguard Workerof these accesses then tell us that we didn't have a match.
834*9880d681SAndroid Build Coastguard Worker
835*9880d681SAndroid Build Coastguard WorkerName Hash Tables
836*9880d681SAndroid Build Coastguard Worker""""""""""""""""
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard WorkerTo solve the issues mentioned above we have structured the hash tables a bit
839*9880d681SAndroid Build Coastguard Workerdifferently: a header, buckets, an array of all unique 32 bit hash values,
840*9880d681SAndroid Build Coastguard Workerfollowed by an array of hash value data offsets, one for each hash value, then
841*9880d681SAndroid Build Coastguard Workerthe data for all hash values:
842*9880d681SAndroid Build Coastguard Worker
843*9880d681SAndroid Build Coastguard Worker.. code-block:: none
844*9880d681SAndroid Build Coastguard Worker
845*9880d681SAndroid Build Coastguard Worker  .-------------.
846*9880d681SAndroid Build Coastguard Worker  |  HEADER     |
847*9880d681SAndroid Build Coastguard Worker  |-------------|
848*9880d681SAndroid Build Coastguard Worker  |  BUCKETS    |
849*9880d681SAndroid Build Coastguard Worker  |-------------|
850*9880d681SAndroid Build Coastguard Worker  |  HASHES     |
851*9880d681SAndroid Build Coastguard Worker  |-------------|
852*9880d681SAndroid Build Coastguard Worker  |  OFFSETS    |
853*9880d681SAndroid Build Coastguard Worker  |-------------|
854*9880d681SAndroid Build Coastguard Worker  |  DATA       |
855*9880d681SAndroid Build Coastguard Worker  `-------------'
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard WorkerThe ``BUCKETS`` in the name tables are an index into the ``HASHES`` array.  By
858*9880d681SAndroid Build Coastguard Workermaking all of the full 32 bit hash values contiguous in memory, we allow
859*9880d681SAndroid Build Coastguard Workerourselves to efficiently check for a match while touching as little memory as
860*9880d681SAndroid Build Coastguard Workerpossible.  Most often checking the 32 bit hash values is as far as the lookup
861*9880d681SAndroid Build Coastguard Workergoes.  If it does match, it usually is a match with no collisions.  So for a
862*9880d681SAndroid Build Coastguard Workertable with "``n_buckets``" buckets, and "``n_hashes``" unique 32 bit hash
863*9880d681SAndroid Build Coastguard Workervalues, we can clarify the contents of the ``BUCKETS``, ``HASHES`` and
864*9880d681SAndroid Build Coastguard Worker``OFFSETS`` as:
865*9880d681SAndroid Build Coastguard Worker
866*9880d681SAndroid Build Coastguard Worker.. code-block:: none
867*9880d681SAndroid Build Coastguard Worker
868*9880d681SAndroid Build Coastguard Worker  .-------------------------.
869*9880d681SAndroid Build Coastguard Worker  |  HEADER.magic           | uint32_t
870*9880d681SAndroid Build Coastguard Worker  |  HEADER.version         | uint16_t
871*9880d681SAndroid Build Coastguard Worker  |  HEADER.hash_function   | uint16_t
872*9880d681SAndroid Build Coastguard Worker  |  HEADER.bucket_count    | uint32_t
873*9880d681SAndroid Build Coastguard Worker  |  HEADER.hashes_count    | uint32_t
874*9880d681SAndroid Build Coastguard Worker  |  HEADER.header_data_len | uint32_t
875*9880d681SAndroid Build Coastguard Worker  |  HEADER_DATA            | HeaderData
876*9880d681SAndroid Build Coastguard Worker  |-------------------------|
877*9880d681SAndroid Build Coastguard Worker  |  BUCKETS                | uint32_t[n_buckets] // 32 bit hash indexes
878*9880d681SAndroid Build Coastguard Worker  |-------------------------|
879*9880d681SAndroid Build Coastguard Worker  |  HASHES                 | uint32_t[n_hashes] // 32 bit hash values
880*9880d681SAndroid Build Coastguard Worker  |-------------------------|
881*9880d681SAndroid Build Coastguard Worker  |  OFFSETS                | uint32_t[n_hashes] // 32 bit offsets to hash value data
882*9880d681SAndroid Build Coastguard Worker  |-------------------------|
883*9880d681SAndroid Build Coastguard Worker  |  ALL HASH DATA          |
884*9880d681SAndroid Build Coastguard Worker  `-------------------------'
885*9880d681SAndroid Build Coastguard Worker
886*9880d681SAndroid Build Coastguard WorkerSo taking the exact same data from the standard hash example above we end up
887*9880d681SAndroid Build Coastguard Workerwith:
888*9880d681SAndroid Build Coastguard Worker
889*9880d681SAndroid Build Coastguard Worker.. code-block:: none
890*9880d681SAndroid Build Coastguard Worker
891*9880d681SAndroid Build Coastguard Worker              .------------.
892*9880d681SAndroid Build Coastguard Worker              | HEADER     |
893*9880d681SAndroid Build Coastguard Worker              |------------|
894*9880d681SAndroid Build Coastguard Worker              |          0 | BUCKETS[0]
895*9880d681SAndroid Build Coastguard Worker              |          2 | BUCKETS[1]
896*9880d681SAndroid Build Coastguard Worker              |          5 | BUCKETS[2]
897*9880d681SAndroid Build Coastguard Worker              |          6 | BUCKETS[3]
898*9880d681SAndroid Build Coastguard Worker              |            | ...
899*9880d681SAndroid Build Coastguard Worker              |        ... | BUCKETS[n_buckets]
900*9880d681SAndroid Build Coastguard Worker              |------------|
901*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[0]
902*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[1]
903*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[2]
904*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[3]
905*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[4]
906*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[5]
907*9880d681SAndroid Build Coastguard Worker              | 0x12345678 | HASHES[6]    hash for BUCKETS[3]
908*9880d681SAndroid Build Coastguard Worker              | 0x29273623 | HASHES[7]    hash for BUCKETS[3]
909*9880d681SAndroid Build Coastguard Worker              | 0x82638293 | HASHES[8]    hash for BUCKETS[3]
910*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[9]
911*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[10]
912*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[11]
913*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[12]
914*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[13]
915*9880d681SAndroid Build Coastguard Worker              | 0x........ | HASHES[n_hashes]
916*9880d681SAndroid Build Coastguard Worker              |------------|
917*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[0]
918*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[1]
919*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[2]
920*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[3]
921*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[4]
922*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[5]
923*9880d681SAndroid Build Coastguard Worker              | 0x000034f0 | OFFSETS[6]   offset for BUCKETS[3]
924*9880d681SAndroid Build Coastguard Worker              | 0x00003500 | OFFSETS[7]   offset for BUCKETS[3]
925*9880d681SAndroid Build Coastguard Worker              | 0x00003550 | OFFSETS[8]   offset for BUCKETS[3]
926*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[9]
927*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[10]
928*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[11]
929*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[12]
930*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[13]
931*9880d681SAndroid Build Coastguard Worker              | 0x........ | OFFSETS[n_hashes]
932*9880d681SAndroid Build Coastguard Worker              |------------|
933*9880d681SAndroid Build Coastguard Worker              |            |
934*9880d681SAndroid Build Coastguard Worker              |            |
935*9880d681SAndroid Build Coastguard Worker              |            |
936*9880d681SAndroid Build Coastguard Worker              |            |
937*9880d681SAndroid Build Coastguard Worker              |            |
938*9880d681SAndroid Build Coastguard Worker              |------------|
939*9880d681SAndroid Build Coastguard Worker  0x000034f0: | 0x00001203 | .debug_str ("erase")
940*9880d681SAndroid Build Coastguard Worker              | 0x00000004 | A 32 bit array count - number of HashData with name "erase"
941*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[0]
942*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[1]
943*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[2]
944*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[3]
945*9880d681SAndroid Build Coastguard Worker              | 0x00000000 | String offset into .debug_str (terminate data for hash)
946*9880d681SAndroid Build Coastguard Worker              |------------|
947*9880d681SAndroid Build Coastguard Worker  0x00003500: | 0x00001203 | String offset into .debug_str ("collision")
948*9880d681SAndroid Build Coastguard Worker              | 0x00000002 | A 32 bit array count - number of HashData with name "collision"
949*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[0]
950*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[1]
951*9880d681SAndroid Build Coastguard Worker              | 0x00001203 | String offset into .debug_str ("dump")
952*9880d681SAndroid Build Coastguard Worker              | 0x00000003 | A 32 bit array count - number of HashData with name "dump"
953*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[0]
954*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[1]
955*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[2]
956*9880d681SAndroid Build Coastguard Worker              | 0x00000000 | String offset into .debug_str (terminate data for hash)
957*9880d681SAndroid Build Coastguard Worker              |------------|
958*9880d681SAndroid Build Coastguard Worker  0x00003550: | 0x00001203 | String offset into .debug_str ("main")
959*9880d681SAndroid Build Coastguard Worker              | 0x00000009 | A 32 bit array count - number of HashData with name "main"
960*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[0]
961*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[1]
962*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[2]
963*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[3]
964*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[4]
965*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[5]
966*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[6]
967*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[7]
968*9880d681SAndroid Build Coastguard Worker              | 0x........ | HashData[8]
969*9880d681SAndroid Build Coastguard Worker              | 0x00000000 | String offset into .debug_str (terminate data for hash)
970*9880d681SAndroid Build Coastguard Worker              `------------'
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard WorkerSo we still have all of the same data, we just organize it more efficiently for
973*9880d681SAndroid Build Coastguard Workerdebugger lookup.  If we repeat the same "``printf``" lookup from above, we
974*9880d681SAndroid Build Coastguard Workerwould hash "``printf``" and find it matches ``BUCKETS[3]`` by taking the 32 bit
975*9880d681SAndroid Build Coastguard Workerhash value and modulo it by ``n_buckets``.  ``BUCKETS[3]`` contains "6" which
976*9880d681SAndroid Build Coastguard Workeris the index into the ``HASHES`` table.  We would then compare any consecutive
977*9880d681SAndroid Build Coastguard Worker32 bit hashes values in the ``HASHES`` array as long as the hashes would be in
978*9880d681SAndroid Build Coastguard Worker``BUCKETS[3]``.  We do this by verifying that each subsequent hash value modulo
979*9880d681SAndroid Build Coastguard Worker``n_buckets`` is still 3.  In the case of a failed lookup we would access the
980*9880d681SAndroid Build Coastguard Workermemory for ``BUCKETS[3]``, and then compare a few consecutive 32 bit hashes
981*9880d681SAndroid Build Coastguard Workerbefore we know that we have no match.  We don't end up marching through
982*9880d681SAndroid Build Coastguard Workermultiple words of memory and we really keep the number of processor data cache
983*9880d681SAndroid Build Coastguard Workerlines being accessed as small as possible.
984*9880d681SAndroid Build Coastguard Worker
985*9880d681SAndroid Build Coastguard WorkerThe string hash that is used for these lookup tables is the Daniel J.
986*9880d681SAndroid Build Coastguard WorkerBernstein hash which is also used in the ELF ``GNU_HASH`` sections.  It is a
987*9880d681SAndroid Build Coastguard Workervery good hash for all kinds of names in programs with very few hash
988*9880d681SAndroid Build Coastguard Workercollisions.
989*9880d681SAndroid Build Coastguard Worker
990*9880d681SAndroid Build Coastguard WorkerEmpty buckets are designated by using an invalid hash index of ``UINT32_MAX``.
991*9880d681SAndroid Build Coastguard Worker
992*9880d681SAndroid Build Coastguard WorkerDetails
993*9880d681SAndroid Build Coastguard Worker^^^^^^^
994*9880d681SAndroid Build Coastguard Worker
995*9880d681SAndroid Build Coastguard WorkerThese name hash tables are designed to be generic where specializations of the
996*9880d681SAndroid Build Coastguard Workertable get to define additional data that goes into the header ("``HeaderData``"),
997*9880d681SAndroid Build Coastguard Workerhow the string value is stored ("``KeyType``") and the content of the data for each
998*9880d681SAndroid Build Coastguard Workerhash value.
999*9880d681SAndroid Build Coastguard Worker
1000*9880d681SAndroid Build Coastguard WorkerHeader Layout
1001*9880d681SAndroid Build Coastguard Worker"""""""""""""
1002*9880d681SAndroid Build Coastguard Worker
1003*9880d681SAndroid Build Coastguard WorkerThe header has a fixed part, and the specialized part.  The exact format of the
1004*9880d681SAndroid Build Coastguard Workerheader is:
1005*9880d681SAndroid Build Coastguard Worker
1006*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1007*9880d681SAndroid Build Coastguard Worker
1008*9880d681SAndroid Build Coastguard Worker  struct Header
1009*9880d681SAndroid Build Coastguard Worker  {
1010*9880d681SAndroid Build Coastguard Worker    uint32_t   magic;           // 'HASH' magic value to allow endian detection
1011*9880d681SAndroid Build Coastguard Worker    uint16_t   version;         // Version number
1012*9880d681SAndroid Build Coastguard Worker    uint16_t   hash_function;   // The hash function enumeration that was used
1013*9880d681SAndroid Build Coastguard Worker    uint32_t   bucket_count;    // The number of buckets in this hash table
1014*9880d681SAndroid Build Coastguard Worker    uint32_t   hashes_count;    // The total number of unique hash values and hash data offsets in this table
1015*9880d681SAndroid Build Coastguard Worker    uint32_t   header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment
1016*9880d681SAndroid Build Coastguard Worker                                // Specifically the length of the following HeaderData field - this does not
1017*9880d681SAndroid Build Coastguard Worker                                // include the size of the preceding fields
1018*9880d681SAndroid Build Coastguard Worker    HeaderData header_data;     // Implementation specific header data
1019*9880d681SAndroid Build Coastguard Worker  };
1020*9880d681SAndroid Build Coastguard Worker
1021*9880d681SAndroid Build Coastguard WorkerThe header starts with a 32 bit "``magic``" value which must be ``'HASH'``
1022*9880d681SAndroid Build Coastguard Workerencoded as an ASCII integer.  This allows the detection of the start of the
1023*9880d681SAndroid Build Coastguard Workerhash table and also allows the table's byte order to be determined so the table
1024*9880d681SAndroid Build Coastguard Workercan be correctly extracted.  The "``magic``" value is followed by a 16 bit
1025*9880d681SAndroid Build Coastguard Worker``version`` number which allows the table to be revised and modified in the
1026*9880d681SAndroid Build Coastguard Workerfuture.  The current version number is 1. ``hash_function`` is a ``uint16_t``
1027*9880d681SAndroid Build Coastguard Workerenumeration that specifies which hash function was used to produce this table.
1028*9880d681SAndroid Build Coastguard WorkerThe current values for the hash function enumerations include:
1029*9880d681SAndroid Build Coastguard Worker
1030*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard Worker  enum HashFunctionType
1033*9880d681SAndroid Build Coastguard Worker  {
1034*9880d681SAndroid Build Coastguard Worker    eHashFunctionDJB = 0u, // Daniel J Bernstein hash function
1035*9880d681SAndroid Build Coastguard Worker  };
1036*9880d681SAndroid Build Coastguard Worker
1037*9880d681SAndroid Build Coastguard Worker``bucket_count`` is a 32 bit unsigned integer that represents how many buckets
1038*9880d681SAndroid Build Coastguard Workerare in the ``BUCKETS`` array.  ``hashes_count`` is the number of unique 32 bit
1039*9880d681SAndroid Build Coastguard Workerhash values that are in the ``HASHES`` array, and is the same number of offsets
1040*9880d681SAndroid Build Coastguard Workerare contained in the ``OFFSETS`` array.  ``header_data_len`` specifies the size
1041*9880d681SAndroid Build Coastguard Workerin bytes of the ``HeaderData`` that is filled in by specialized versions of
1042*9880d681SAndroid Build Coastguard Workerthis table.
1043*9880d681SAndroid Build Coastguard Worker
1044*9880d681SAndroid Build Coastguard WorkerFixed Lookup
1045*9880d681SAndroid Build Coastguard Worker""""""""""""
1046*9880d681SAndroid Build Coastguard Worker
1047*9880d681SAndroid Build Coastguard WorkerThe header is followed by the buckets, hashes, offsets, and hash value data.
1048*9880d681SAndroid Build Coastguard Worker
1049*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1050*9880d681SAndroid Build Coastguard Worker
1051*9880d681SAndroid Build Coastguard Worker  struct FixedTable
1052*9880d681SAndroid Build Coastguard Worker  {
1053*9880d681SAndroid Build Coastguard Worker    uint32_t buckets[Header.bucket_count];  // An array of hash indexes into the "hashes[]" array below
1054*9880d681SAndroid Build Coastguard Worker    uint32_t hashes [Header.hashes_count];  // Every unique 32 bit hash for the entire table is in this table
1055*9880d681SAndroid Build Coastguard Worker    uint32_t offsets[Header.hashes_count];  // An offset that corresponds to each item in the "hashes[]" array above
1056*9880d681SAndroid Build Coastguard Worker  };
1057*9880d681SAndroid Build Coastguard Worker
1058*9880d681SAndroid Build Coastguard Worker``buckets`` is an array of 32 bit indexes into the ``hashes`` array.  The
1059*9880d681SAndroid Build Coastguard Worker``hashes`` array contains all of the 32 bit hash values for all names in the
1060*9880d681SAndroid Build Coastguard Workerhash table.  Each hash in the ``hashes`` table has an offset in the ``offsets``
1061*9880d681SAndroid Build Coastguard Workerarray that points to the data for the hash value.
1062*9880d681SAndroid Build Coastguard Worker
1063*9880d681SAndroid Build Coastguard WorkerThis table setup makes it very easy to repurpose these tables to contain
1064*9880d681SAndroid Build Coastguard Workerdifferent data, while keeping the lookup mechanism the same for all tables.
1065*9880d681SAndroid Build Coastguard WorkerThis layout also makes it possible to save the table to disk and map it in
1066*9880d681SAndroid Build Coastguard Workerlater and do very efficient name lookups with little or no parsing.
1067*9880d681SAndroid Build Coastguard Worker
1068*9880d681SAndroid Build Coastguard WorkerDWARF lookup tables can be implemented in a variety of ways and can store a lot
1069*9880d681SAndroid Build Coastguard Workerof information for each name.  We want to make the DWARF tables extensible and
1070*9880d681SAndroid Build Coastguard Workerable to store the data efficiently so we have used some of the DWARF features
1071*9880d681SAndroid Build Coastguard Workerthat enable efficient data storage to define exactly what kind of data we store
1072*9880d681SAndroid Build Coastguard Workerfor each name.
1073*9880d681SAndroid Build Coastguard Worker
1074*9880d681SAndroid Build Coastguard WorkerThe ``HeaderData`` contains a definition of the contents of each HashData chunk.
1075*9880d681SAndroid Build Coastguard WorkerWe might want to store an offset to all of the debug information entries (DIEs)
1076*9880d681SAndroid Build Coastguard Workerfor each name.  To keep things extensible, we create a list of items, or
1077*9880d681SAndroid Build Coastguard WorkerAtoms, that are contained in the data for each name.  First comes the type of
1078*9880d681SAndroid Build Coastguard Workerthe data in each atom:
1079*9880d681SAndroid Build Coastguard Worker
1080*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1081*9880d681SAndroid Build Coastguard Worker
1082*9880d681SAndroid Build Coastguard Worker  enum AtomType
1083*9880d681SAndroid Build Coastguard Worker  {
1084*9880d681SAndroid Build Coastguard Worker    eAtomTypeNULL       = 0u,
1085*9880d681SAndroid Build Coastguard Worker    eAtomTypeDIEOffset  = 1u,   // DIE offset, check form for encoding
1086*9880d681SAndroid Build Coastguard Worker    eAtomTypeCUOffset   = 2u,   // DIE offset of the compiler unit header that contains the item in question
1087*9880d681SAndroid Build Coastguard Worker    eAtomTypeTag        = 3u,   // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
1088*9880d681SAndroid Build Coastguard Worker    eAtomTypeNameFlags  = 4u,   // Flags from enum NameFlags
1089*9880d681SAndroid Build Coastguard Worker    eAtomTypeTypeFlags  = 5u,   // Flags from enum TypeFlags
1090*9880d681SAndroid Build Coastguard Worker  };
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard WorkerThe enumeration values and their meanings are:
1093*9880d681SAndroid Build Coastguard Worker
1094*9880d681SAndroid Build Coastguard Worker.. code-block:: none
1095*9880d681SAndroid Build Coastguard Worker
1096*9880d681SAndroid Build Coastguard Worker  eAtomTypeNULL       - a termination atom that specifies the end of the atom list
1097*9880d681SAndroid Build Coastguard Worker  eAtomTypeDIEOffset  - an offset into the .debug_info section for the DWARF DIE for this name
1098*9880d681SAndroid Build Coastguard Worker  eAtomTypeCUOffset   - an offset into the .debug_info section for the CU that contains the DIE
1099*9880d681SAndroid Build Coastguard Worker  eAtomTypeDIETag     - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is
1100*9880d681SAndroid Build Coastguard Worker  eAtomTypeNameFlags  - Flags for functions and global variables (isFunction, isInlined, isExternal...)
1101*9880d681SAndroid Build Coastguard Worker  eAtomTypeTypeFlags  - Flags for types (isCXXClass, isObjCClass, ...)
1102*9880d681SAndroid Build Coastguard Worker
1103*9880d681SAndroid Build Coastguard WorkerThen we allow each atom type to define the atom type and how the data for each
1104*9880d681SAndroid Build Coastguard Workeratom type data is encoded:
1105*9880d681SAndroid Build Coastguard Worker
1106*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard Worker  struct Atom
1109*9880d681SAndroid Build Coastguard Worker  {
1110*9880d681SAndroid Build Coastguard Worker    uint16_t type;  // AtomType enum value
1111*9880d681SAndroid Build Coastguard Worker    uint16_t form;  // DWARF DW_FORM_XXX defines
1112*9880d681SAndroid Build Coastguard Worker  };
1113*9880d681SAndroid Build Coastguard Worker
1114*9880d681SAndroid Build Coastguard WorkerThe ``form`` type above is from the DWARF specification and defines the exact
1115*9880d681SAndroid Build Coastguard Workerencoding of the data for the Atom type.  See the DWARF specification for the
1116*9880d681SAndroid Build Coastguard Worker``DW_FORM_`` definitions.
1117*9880d681SAndroid Build Coastguard Worker
1118*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1119*9880d681SAndroid Build Coastguard Worker
1120*9880d681SAndroid Build Coastguard Worker  struct HeaderData
1121*9880d681SAndroid Build Coastguard Worker  {
1122*9880d681SAndroid Build Coastguard Worker    uint32_t die_offset_base;
1123*9880d681SAndroid Build Coastguard Worker    uint32_t atom_count;
1124*9880d681SAndroid Build Coastguard Worker    Atoms    atoms[atom_count0];
1125*9880d681SAndroid Build Coastguard Worker  };
1126*9880d681SAndroid Build Coastguard Worker
1127*9880d681SAndroid Build Coastguard Worker``HeaderData`` defines the base DIE offset that should be added to any atoms
1128*9880d681SAndroid Build Coastguard Workerthat are encoded using the ``DW_FORM_ref1``, ``DW_FORM_ref2``,
1129*9880d681SAndroid Build Coastguard Worker``DW_FORM_ref4``, ``DW_FORM_ref8`` or ``DW_FORM_ref_udata``.  It also defines
1130*9880d681SAndroid Build Coastguard Workerwhat is contained in each ``HashData`` object -- ``Atom.form`` tells us how large
1131*9880d681SAndroid Build Coastguard Workereach field will be in the ``HashData`` and the ``Atom.type`` tells us how this data
1132*9880d681SAndroid Build Coastguard Workershould be interpreted.
1133*9880d681SAndroid Build Coastguard Worker
1134*9880d681SAndroid Build Coastguard WorkerFor the current implementations of the "``.apple_names``" (all functions +
1135*9880d681SAndroid Build Coastguard Workerglobals), the "``.apple_types``" (names of all types that are defined), and
1136*9880d681SAndroid Build Coastguard Workerthe "``.apple_namespaces``" (all namespaces), we currently set the ``Atom``
1137*9880d681SAndroid Build Coastguard Workerarray to be:
1138*9880d681SAndroid Build Coastguard Worker
1139*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1140*9880d681SAndroid Build Coastguard Worker
1141*9880d681SAndroid Build Coastguard Worker  HeaderData.atom_count = 1;
1142*9880d681SAndroid Build Coastguard Worker  HeaderData.atoms[0].type = eAtomTypeDIEOffset;
1143*9880d681SAndroid Build Coastguard Worker  HeaderData.atoms[0].form = DW_FORM_data4;
1144*9880d681SAndroid Build Coastguard Worker
1145*9880d681SAndroid Build Coastguard WorkerThis defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is
1146*9880d681SAndroid Build Coastguard Workerencoded as a 32 bit value (DW_FORM_data4).  This allows a single name to have
1147*9880d681SAndroid Build Coastguard Workermultiple matching DIEs in a single file, which could come up with an inlined
1148*9880d681SAndroid Build Coastguard Workerfunction for instance.  Future tables could include more information about the
1149*9880d681SAndroid Build Coastguard WorkerDIE such as flags indicating if the DIE is a function, method, block,
1150*9880d681SAndroid Build Coastguard Workeror inlined.
1151*9880d681SAndroid Build Coastguard Worker
1152*9880d681SAndroid Build Coastguard WorkerThe KeyType for the DWARF table is a 32 bit string table offset into the
1153*9880d681SAndroid Build Coastguard Worker".debug_str" table.  The ".debug_str" is the string table for the DWARF which
1154*9880d681SAndroid Build Coastguard Workermay already contain copies of all of the strings.  This helps make sure, with
1155*9880d681SAndroid Build Coastguard Workerhelp from the compiler, that we reuse the strings between all of the DWARF
1156*9880d681SAndroid Build Coastguard Workersections and keeps the hash table size down.  Another benefit to having the
1157*9880d681SAndroid Build Coastguard Workercompiler generate all strings as DW_FORM_strp in the debug info, is that
1158*9880d681SAndroid Build Coastguard WorkerDWARF parsing can be made much faster.
1159*9880d681SAndroid Build Coastguard Worker
1160*9880d681SAndroid Build Coastguard WorkerAfter a lookup is made, we get an offset into the hash data.  The hash data
1161*9880d681SAndroid Build Coastguard Workerneeds to be able to deal with 32 bit hash collisions, so the chunk of data
1162*9880d681SAndroid Build Coastguard Workerat the offset in the hash data consists of a triple:
1163*9880d681SAndroid Build Coastguard Worker
1164*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1165*9880d681SAndroid Build Coastguard Worker
1166*9880d681SAndroid Build Coastguard Worker  uint32_t str_offset
1167*9880d681SAndroid Build Coastguard Worker  uint32_t hash_data_count
1168*9880d681SAndroid Build Coastguard Worker  HashData[hash_data_count]
1169*9880d681SAndroid Build Coastguard Worker
1170*9880d681SAndroid Build Coastguard WorkerIf "str_offset" is zero, then the bucket contents are done. 99.9% of the
1171*9880d681SAndroid Build Coastguard Workerhash data chunks contain a single item (no 32 bit hash collision):
1172*9880d681SAndroid Build Coastguard Worker
1173*9880d681SAndroid Build Coastguard Worker.. code-block:: none
1174*9880d681SAndroid Build Coastguard Worker
1175*9880d681SAndroid Build Coastguard Worker  .------------.
1176*9880d681SAndroid Build Coastguard Worker  | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
1177*9880d681SAndroid Build Coastguard Worker  | 0x00000004 | uint32_t HashData count
1178*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[0] DIE offset
1179*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[1] DIE offset
1180*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[2] DIE offset
1181*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[3] DIE offset
1182*9880d681SAndroid Build Coastguard Worker  | 0x00000000 | uint32_t KeyType (end of hash chain)
1183*9880d681SAndroid Build Coastguard Worker  `------------'
1184*9880d681SAndroid Build Coastguard Worker
1185*9880d681SAndroid Build Coastguard WorkerIf there are collisions, you will have multiple valid string offsets:
1186*9880d681SAndroid Build Coastguard Worker
1187*9880d681SAndroid Build Coastguard Worker.. code-block:: none
1188*9880d681SAndroid Build Coastguard Worker
1189*9880d681SAndroid Build Coastguard Worker  .------------.
1190*9880d681SAndroid Build Coastguard Worker  | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
1191*9880d681SAndroid Build Coastguard Worker  | 0x00000004 | uint32_t HashData count
1192*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[0] DIE offset
1193*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[1] DIE offset
1194*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[2] DIE offset
1195*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[3] DIE offset
1196*9880d681SAndroid Build Coastguard Worker  | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print")
1197*9880d681SAndroid Build Coastguard Worker  | 0x00000002 | uint32_t HashData count
1198*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[0] DIE offset
1199*9880d681SAndroid Build Coastguard Worker  | 0x........ | uint32_t HashData[1] DIE offset
1200*9880d681SAndroid Build Coastguard Worker  | 0x00000000 | uint32_t KeyType (end of hash chain)
1201*9880d681SAndroid Build Coastguard Worker  `------------'
1202*9880d681SAndroid Build Coastguard Worker
1203*9880d681SAndroid Build Coastguard WorkerCurrent testing with real world C++ binaries has shown that there is around 1
1204*9880d681SAndroid Build Coastguard Worker32 bit hash collision per 100,000 name entries.
1205*9880d681SAndroid Build Coastguard Worker
1206*9880d681SAndroid Build Coastguard WorkerContents
1207*9880d681SAndroid Build Coastguard Worker^^^^^^^^
1208*9880d681SAndroid Build Coastguard Worker
1209*9880d681SAndroid Build Coastguard WorkerAs we said, we want to strictly define exactly what is included in the
1210*9880d681SAndroid Build Coastguard Workerdifferent tables.  For DWARF, we have 3 tables: "``.apple_names``",
1211*9880d681SAndroid Build Coastguard Worker"``.apple_types``", and "``.apple_namespaces``".
1212*9880d681SAndroid Build Coastguard Worker
1213*9880d681SAndroid Build Coastguard Worker"``.apple_names``" sections should contain an entry for each DWARF DIE whose
1214*9880d681SAndroid Build Coastguard Worker``DW_TAG`` is a ``DW_TAG_label``, ``DW_TAG_inlined_subroutine``, or
1215*9880d681SAndroid Build Coastguard Worker``DW_TAG_subprogram`` that has address attributes: ``DW_AT_low_pc``,
1216*9880d681SAndroid Build Coastguard Worker``DW_AT_high_pc``, ``DW_AT_ranges`` or ``DW_AT_entry_pc``.  It also contains
1217*9880d681SAndroid Build Coastguard Worker``DW_TAG_variable`` DIEs that have a ``DW_OP_addr`` in the location (global and
1218*9880d681SAndroid Build Coastguard Workerstatic variables).  All global and static variables should be included,
1219*9880d681SAndroid Build Coastguard Workerincluding those scoped within functions and classes.  For example using the
1220*9880d681SAndroid Build Coastguard Workerfollowing code:
1221*9880d681SAndroid Build Coastguard Worker
1222*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1223*9880d681SAndroid Build Coastguard Worker
1224*9880d681SAndroid Build Coastguard Worker  static int var = 0;
1225*9880d681SAndroid Build Coastguard Worker
1226*9880d681SAndroid Build Coastguard Worker  void f ()
1227*9880d681SAndroid Build Coastguard Worker  {
1228*9880d681SAndroid Build Coastguard Worker    static int var = 0;
1229*9880d681SAndroid Build Coastguard Worker  }
1230*9880d681SAndroid Build Coastguard Worker
1231*9880d681SAndroid Build Coastguard WorkerBoth of the static ``var`` variables would be included in the table.  All
1232*9880d681SAndroid Build Coastguard Workerfunctions should emit both their full names and their basenames.  For C or C++,
1233*9880d681SAndroid Build Coastguard Workerthe full name is the mangled name (if available) which is usually in the
1234*9880d681SAndroid Build Coastguard Worker``DW_AT_MIPS_linkage_name`` attribute, and the ``DW_AT_name`` contains the
1235*9880d681SAndroid Build Coastguard Workerfunction basename.  If global or static variables have a mangled name in a
1236*9880d681SAndroid Build Coastguard Worker``DW_AT_MIPS_linkage_name`` attribute, this should be emitted along with the
1237*9880d681SAndroid Build Coastguard Workersimple name found in the ``DW_AT_name`` attribute.
1238*9880d681SAndroid Build Coastguard Worker
1239*9880d681SAndroid Build Coastguard Worker"``.apple_types``" sections should contain an entry for each DWARF DIE whose
1240*9880d681SAndroid Build Coastguard Workertag is one of:
1241*9880d681SAndroid Build Coastguard Worker
1242*9880d681SAndroid Build Coastguard Worker* DW_TAG_array_type
1243*9880d681SAndroid Build Coastguard Worker* DW_TAG_class_type
1244*9880d681SAndroid Build Coastguard Worker* DW_TAG_enumeration_type
1245*9880d681SAndroid Build Coastguard Worker* DW_TAG_pointer_type
1246*9880d681SAndroid Build Coastguard Worker* DW_TAG_reference_type
1247*9880d681SAndroid Build Coastguard Worker* DW_TAG_string_type
1248*9880d681SAndroid Build Coastguard Worker* DW_TAG_structure_type
1249*9880d681SAndroid Build Coastguard Worker* DW_TAG_subroutine_type
1250*9880d681SAndroid Build Coastguard Worker* DW_TAG_typedef
1251*9880d681SAndroid Build Coastguard Worker* DW_TAG_union_type
1252*9880d681SAndroid Build Coastguard Worker* DW_TAG_ptr_to_member_type
1253*9880d681SAndroid Build Coastguard Worker* DW_TAG_set_type
1254*9880d681SAndroid Build Coastguard Worker* DW_TAG_subrange_type
1255*9880d681SAndroid Build Coastguard Worker* DW_TAG_base_type
1256*9880d681SAndroid Build Coastguard Worker* DW_TAG_const_type
1257*9880d681SAndroid Build Coastguard Worker* DW_TAG_file_type
1258*9880d681SAndroid Build Coastguard Worker* DW_TAG_namelist
1259*9880d681SAndroid Build Coastguard Worker* DW_TAG_packed_type
1260*9880d681SAndroid Build Coastguard Worker* DW_TAG_volatile_type
1261*9880d681SAndroid Build Coastguard Worker* DW_TAG_restrict_type
1262*9880d681SAndroid Build Coastguard Worker* DW_TAG_interface_type
1263*9880d681SAndroid Build Coastguard Worker* DW_TAG_unspecified_type
1264*9880d681SAndroid Build Coastguard Worker* DW_TAG_shared_type
1265*9880d681SAndroid Build Coastguard Worker
1266*9880d681SAndroid Build Coastguard WorkerOnly entries with a ``DW_AT_name`` attribute are included, and the entry must
1267*9880d681SAndroid Build Coastguard Workernot be a forward declaration (``DW_AT_declaration`` attribute with a non-zero
1268*9880d681SAndroid Build Coastguard Workervalue).  For example, using the following code:
1269*9880d681SAndroid Build Coastguard Worker
1270*9880d681SAndroid Build Coastguard Worker.. code-block:: c
1271*9880d681SAndroid Build Coastguard Worker
1272*9880d681SAndroid Build Coastguard Worker  int main ()
1273*9880d681SAndroid Build Coastguard Worker  {
1274*9880d681SAndroid Build Coastguard Worker    int *b = 0;
1275*9880d681SAndroid Build Coastguard Worker    return *b;
1276*9880d681SAndroid Build Coastguard Worker  }
1277*9880d681SAndroid Build Coastguard Worker
1278*9880d681SAndroid Build Coastguard WorkerWe get a few type DIEs:
1279*9880d681SAndroid Build Coastguard Worker
1280*9880d681SAndroid Build Coastguard Worker.. code-block:: none
1281*9880d681SAndroid Build Coastguard Worker
1282*9880d681SAndroid Build Coastguard Worker  0x00000067:     TAG_base_type [5]
1283*9880d681SAndroid Build Coastguard Worker                  AT_encoding( DW_ATE_signed )
1284*9880d681SAndroid Build Coastguard Worker                  AT_name( "int" )
1285*9880d681SAndroid Build Coastguard Worker                  AT_byte_size( 0x04 )
1286*9880d681SAndroid Build Coastguard Worker
1287*9880d681SAndroid Build Coastguard Worker  0x0000006e:     TAG_pointer_type [6]
1288*9880d681SAndroid Build Coastguard Worker                  AT_type( {0x00000067} ( int ) )
1289*9880d681SAndroid Build Coastguard Worker                  AT_byte_size( 0x08 )
1290*9880d681SAndroid Build Coastguard Worker
1291*9880d681SAndroid Build Coastguard WorkerThe DW_TAG_pointer_type is not included because it does not have a ``DW_AT_name``.
1292*9880d681SAndroid Build Coastguard Worker
1293*9880d681SAndroid Build Coastguard Worker"``.apple_namespaces``" section should contain all ``DW_TAG_namespace`` DIEs.
1294*9880d681SAndroid Build Coastguard WorkerIf we run into a namespace that has no name this is an anonymous namespace, and
1295*9880d681SAndroid Build Coastguard Workerthe name should be output as "``(anonymous namespace)``" (without the quotes).
1296*9880d681SAndroid Build Coastguard WorkerWhy?  This matches the output of the ``abi::cxa_demangle()`` that is in the
1297*9880d681SAndroid Build Coastguard Workerstandard C++ library that demangles mangled names.
1298*9880d681SAndroid Build Coastguard Worker
1299*9880d681SAndroid Build Coastguard Worker
1300*9880d681SAndroid Build Coastguard WorkerLanguage Extensions and File Format Changes
1301*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1302*9880d681SAndroid Build Coastguard Worker
1303*9880d681SAndroid Build Coastguard WorkerObjective-C Extensions
1304*9880d681SAndroid Build Coastguard Worker""""""""""""""""""""""
1305*9880d681SAndroid Build Coastguard Worker
1306*9880d681SAndroid Build Coastguard Worker"``.apple_objc``" section should contain all ``DW_TAG_subprogram`` DIEs for an
1307*9880d681SAndroid Build Coastguard WorkerObjective-C class.  The name used in the hash table is the name of the
1308*9880d681SAndroid Build Coastguard WorkerObjective-C class itself.  If the Objective-C class has a category, then an
1309*9880d681SAndroid Build Coastguard Workerentry is made for both the class name without the category, and for the class
1310*9880d681SAndroid Build Coastguard Workername with the category.  So if we have a DIE at offset 0x1234 with a name of
1311*9880d681SAndroid Build Coastguard Workermethod "``-[NSString(my_additions) stringWithSpecialString:]``", we would add
1312*9880d681SAndroid Build Coastguard Workeran entry for "``NSString``" that points to DIE 0x1234, and an entry for
1313*9880d681SAndroid Build Coastguard Worker"``NSString(my_additions)``" that points to 0x1234.  This allows us to quickly
1314*9880d681SAndroid Build Coastguard Workertrack down all Objective-C methods for an Objective-C class when doing
1315*9880d681SAndroid Build Coastguard Workerexpressions.  It is needed because of the dynamic nature of Objective-C where
1316*9880d681SAndroid Build Coastguard Workeranyone can add methods to a class.  The DWARF for Objective-C methods is also
1317*9880d681SAndroid Build Coastguard Workeremitted differently from C++ classes where the methods are not usually
1318*9880d681SAndroid Build Coastguard Workercontained in the class definition, they are scattered about across one or more
1319*9880d681SAndroid Build Coastguard Workercompile units.  Categories can also be defined in different shared libraries.
1320*9880d681SAndroid Build Coastguard WorkerSo we need to be able to quickly find all of the methods and class functions
1321*9880d681SAndroid Build Coastguard Workergiven the Objective-C class name, or quickly find all methods and class
1322*9880d681SAndroid Build Coastguard Workerfunctions for a class + category name.  This table does not contain any
1323*9880d681SAndroid Build Coastguard Workerselector names, it just maps Objective-C class names (or class names +
1324*9880d681SAndroid Build Coastguard Workercategory) to all of the methods and class functions.  The selectors are added
1325*9880d681SAndroid Build Coastguard Workeras function basenames in the "``.debug_names``" section.
1326*9880d681SAndroid Build Coastguard Worker
1327*9880d681SAndroid Build Coastguard WorkerIn the "``.apple_names``" section for Objective-C functions, the full name is
1328*9880d681SAndroid Build Coastguard Workerthe entire function name with the brackets ("``-[NSString
1329*9880d681SAndroid Build Coastguard WorkerstringWithCString:]``") and the basename is the selector only
1330*9880d681SAndroid Build Coastguard Worker("``stringWithCString:``").
1331*9880d681SAndroid Build Coastguard Worker
1332*9880d681SAndroid Build Coastguard WorkerMach-O Changes
1333*9880d681SAndroid Build Coastguard Worker""""""""""""""
1334*9880d681SAndroid Build Coastguard Worker
1335*9880d681SAndroid Build Coastguard WorkerThe sections names for the apple hash tables are for non-mach-o files.  For
1336*9880d681SAndroid Build Coastguard Workermach-o files, the sections should be contained in the ``__DWARF`` segment with
1337*9880d681SAndroid Build Coastguard Workernames as follows:
1338*9880d681SAndroid Build Coastguard Worker
1339*9880d681SAndroid Build Coastguard Worker* "``.apple_names``" -> "``__apple_names``"
1340*9880d681SAndroid Build Coastguard Worker* "``.apple_types``" -> "``__apple_types``"
1341*9880d681SAndroid Build Coastguard Worker* "``.apple_namespaces``" -> "``__apple_namespac``" (16 character limit)
1342*9880d681SAndroid Build Coastguard Worker* "``.apple_objc``" -> "``__apple_objc``"
1343*9880d681SAndroid Build Coastguard Worker
1344*9880d681SAndroid Build Coastguard Worker.. _codeview:
1345*9880d681SAndroid Build Coastguard Worker
1346*9880d681SAndroid Build Coastguard WorkerCodeView Debug Info Format
1347*9880d681SAndroid Build Coastguard Worker==========================
1348*9880d681SAndroid Build Coastguard Worker
1349*9880d681SAndroid Build Coastguard WorkerLLVM supports emitting CodeView, the Microsoft debug info format, and this
1350*9880d681SAndroid Build Coastguard Workersection describes the design and implementation of that support.
1351*9880d681SAndroid Build Coastguard Worker
1352*9880d681SAndroid Build Coastguard WorkerFormat Background
1353*9880d681SAndroid Build Coastguard Worker-----------------
1354*9880d681SAndroid Build Coastguard Worker
1355*9880d681SAndroid Build Coastguard WorkerCodeView as a format is clearly oriented around C++ debugging, and in C++, the
1356*9880d681SAndroid Build Coastguard Workermajority of debug information tends to be type information. Therefore, the
1357*9880d681SAndroid Build Coastguard Workeroverriding design constraint of CodeView is the separation of type information
1358*9880d681SAndroid Build Coastguard Workerfrom other "symbol" information so that type information can be efficiently
1359*9880d681SAndroid Build Coastguard Workermerged across translation units. Both type information and symbol information is
1360*9880d681SAndroid Build Coastguard Workergenerally stored as a sequence of records, where each record begins with a
1361*9880d681SAndroid Build Coastguard Worker16-bit record size and a 16-bit record kind.
1362*9880d681SAndroid Build Coastguard Worker
1363*9880d681SAndroid Build Coastguard WorkerType information is usually stored in the ``.debug$T`` section of the object
1364*9880d681SAndroid Build Coastguard Workerfile.  All other debug info, such as line info, string table, symbol info, and
1365*9880d681SAndroid Build Coastguard Workerinlinee info, is stored in one or more ``.debug$S`` sections. There may only be
1366*9880d681SAndroid Build Coastguard Workerone ``.debug$T`` section per object file, since all other debug info refers to
1367*9880d681SAndroid Build Coastguard Workerit. If a PDB (enabled by the ``/Zi`` MSVC option) was used during compilation,
1368*9880d681SAndroid Build Coastguard Workerthe ``.debug$T`` section will contain only an ``LF_TYPESERVER2`` record pointing
1369*9880d681SAndroid Build Coastguard Workerto the PDB. When using PDBs, symbol information appears to remain in the object
1370*9880d681SAndroid Build Coastguard Workerfile ``.debug$S`` sections.
1371*9880d681SAndroid Build Coastguard Worker
1372*9880d681SAndroid Build Coastguard WorkerType records are referred to by their index, which is the number of records in
1373*9880d681SAndroid Build Coastguard Workerthe stream before a given record plus ``0x1000``. Many common basic types, such
1374*9880d681SAndroid Build Coastguard Workeras the basic integral types and unqualified pointers to them, are represented
1375*9880d681SAndroid Build Coastguard Workerusing type indices less than ``0x1000``. Such basic types are built in to
1376*9880d681SAndroid Build Coastguard WorkerCodeView consumers and do not require type records.
1377*9880d681SAndroid Build Coastguard Worker
1378*9880d681SAndroid Build Coastguard WorkerEach type record may only contain type indices that are less than its own type
1379*9880d681SAndroid Build Coastguard Workerindex. This ensures that the graph of type stream references is acyclic. While
1380*9880d681SAndroid Build Coastguard Workerthe source-level type graph may contain cycles through pointer types (consider a
1381*9880d681SAndroid Build Coastguard Workerlinked list struct), these cycles are removed from the type stream by always
1382*9880d681SAndroid Build Coastguard Workerreferring to the forward declaration record of user-defined record types. Only
1383*9880d681SAndroid Build Coastguard Worker"symbol" records in the ``.debug$S`` streams may refer to complete,
1384*9880d681SAndroid Build Coastguard Workernon-forward-declaration type records.
1385*9880d681SAndroid Build Coastguard Worker
1386*9880d681SAndroid Build Coastguard WorkerWorking with CodeView
1387*9880d681SAndroid Build Coastguard Worker---------------------
1388*9880d681SAndroid Build Coastguard Worker
1389*9880d681SAndroid Build Coastguard WorkerThese are instructions for some common tasks for developers working to improve
1390*9880d681SAndroid Build Coastguard WorkerLLVM's CodeView support. Most of them revolve around using the CodeView dumper
1391*9880d681SAndroid Build Coastguard Workerembedded in ``llvm-readobj``.
1392*9880d681SAndroid Build Coastguard Worker
1393*9880d681SAndroid Build Coastguard Worker* Testing MSVC's output::
1394*9880d681SAndroid Build Coastguard Worker
1395*9880d681SAndroid Build Coastguard Worker    $ cl -c -Z7 foo.cpp # Use /Z7 to keep types in the object file
1396*9880d681SAndroid Build Coastguard Worker    $ llvm-readobj -codeview foo.obj
1397*9880d681SAndroid Build Coastguard Worker
1398*9880d681SAndroid Build Coastguard Worker* Getting LLVM IR debug info out of Clang::
1399*9880d681SAndroid Build Coastguard Worker
1400*9880d681SAndroid Build Coastguard Worker    $ clang -g -gcodeview --target=x86_64-windows-msvc foo.cpp -S -emit-llvm
1401*9880d681SAndroid Build Coastguard Worker
1402*9880d681SAndroid Build Coastguard Worker  Use this to generate LLVM IR for LLVM test cases.
1403*9880d681SAndroid Build Coastguard Worker
1404*9880d681SAndroid Build Coastguard Worker* Generate and dump CodeView from LLVM IR metadata::
1405*9880d681SAndroid Build Coastguard Worker
1406*9880d681SAndroid Build Coastguard Worker    $ llc foo.ll -filetype=obj -o foo.obj
1407*9880d681SAndroid Build Coastguard Worker    $ llvm-readobj -codeview foo.obj > foo.txt
1408*9880d681SAndroid Build Coastguard Worker
1409*9880d681SAndroid Build Coastguard Worker  Use this pattern in lit test cases and FileCheck the output of llvm-readobj
1410*9880d681SAndroid Build Coastguard Worker
1411*9880d681SAndroid Build Coastguard WorkerImproving LLVM's CodeView support is a process of finding interesting type
1412*9880d681SAndroid Build Coastguard Workerrecords, constructing a C++ test case that makes MSVC emit those records,
1413*9880d681SAndroid Build Coastguard Workerdumping the records, understanding them, and then generating equivalent records
1414*9880d681SAndroid Build Coastguard Workerin LLVM's backend.
1415