SourceLevelDebugging.rst 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. ================================
  2. Source Level Debugging with LLVM
  3. ================================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. This document is the central repository for all information pertaining to debug
  9. information in LLVM. It describes the :ref:`actual format that the LLVM debug
  10. information takes <format>`, which is useful for those interested in creating
  11. front-ends or dealing directly with the information. Further, this document
  12. provides specific examples of what debug information for C/C++ looks like.
  13. HLSL and DXIL-specific information is available in the :doc:`Source Level
  14. Debugging with HLSL <SourceLevelDebuggingHLSL>` document.
  15. Philosophy behind LLVM debugging information
  16. --------------------------------------------
  17. The idea of the LLVM debugging information is to capture how the important
  18. pieces of the source-language's Abstract Syntax Tree map onto LLVM code.
  19. Several design aspects have shaped the solution that appears here. The
  20. important ones are:
  21. * Debugging information should have very little impact on the rest of the
  22. compiler. No transformations, analyses, or code generators should need to
  23. be modified because of debugging information.
  24. * LLVM optimizations should interact in :ref:`well-defined and easily described
  25. ways <intro_debugopt>` with the debugging information.
  26. * Because LLVM is designed to support arbitrary programming languages,
  27. LLVM-to-LLVM tools should not need to know anything about the semantics of
  28. the source-level-language.
  29. * Source-level languages are often **widely** different from one another.
  30. LLVM should not put any restrictions of the flavor of the source-language,
  31. and the debugging information should work with any language.
  32. * With code generator support, it should be possible to use an LLVM compiler
  33. to compile a program to native machine code and standard debugging
  34. formats. This allows compatibility with traditional machine-code level
  35. debuggers, like GDB or DBX.
  36. The approach used by the LLVM implementation is to use a small set of
  37. :ref:`intrinsic functions <format_common_intrinsics>` to define a mapping
  38. between LLVM program objects and the source-level objects. The description of
  39. the source-level program is maintained in LLVM metadata in an
  40. :ref:`implementation-defined format <ccxx_frontend>` (the C/C++ front-end
  41. currently uses working draft 7 of the `DWARF 3 standard
  42. <http://www.eagercon.com/dwarf/dwarf3std.htm>`_).
  43. When a program is being debugged, a debugger interacts with the user and turns
  44. the stored debug information into source-language specific information. As
  45. such, a debugger must be aware of the source-language, and is thus tied to a
  46. specific language or family of languages.
  47. Debug information consumers
  48. ---------------------------
  49. The role of debug information is to provide meta information normally stripped
  50. away during the compilation process. This meta information provides an LLVM
  51. user a relationship between generated code and the original program source
  52. code.
  53. Currently, debug information is consumed by DwarfDebug to produce dwarf
  54. information used by the gdb debugger. Other targets could use the same
  55. information to produce stabs or other debug forms.
  56. It would also be reasonable to use debug information to feed profiling tools
  57. for analysis of generated code, or, tools for reconstructing the original
  58. source from generated code.
  59. TODO - expound a bit more.
  60. .. _intro_debugopt:
  61. Debugging optimized code
  62. ------------------------
  63. An extremely high priority of LLVM debugging information is to make it interact
  64. well with optimizations and analysis. In particular, the LLVM debug
  65. information provides the following guarantees:
  66. * LLVM debug information **always provides information to accurately read
  67. the source-level state of the program**, regardless of which LLVM
  68. optimizations have been run, and without any modification to the
  69. optimizations themselves. However, some optimizations may impact the
  70. ability to modify the current state of the program with a debugger, such
  71. as setting program variables, or calling functions that have been
  72. deleted.
  73. * As desired, LLVM optimizations can be upgraded to be aware of the LLVM
  74. debugging information, allowing them to update the debugging information
  75. as they perform aggressive optimizations. This means that, with effort,
  76. the LLVM optimizers could optimize debug code just as well as non-debug
  77. code.
  78. * LLVM debug information does not prevent optimizations from
  79. happening (for example inlining, basic block reordering/merging/cleanup,
  80. tail duplication, etc).
  81. * LLVM debug information is automatically optimized along with the rest of
  82. the program, using existing facilities. For example, duplicate
  83. information is automatically merged by the linker, and unused information
  84. is automatically removed.
  85. Basically, the debug information allows you to compile a program with
  86. "``-O0 -g``" and get full debug information, allowing you to arbitrarily modify
  87. the program as it executes from a debugger. Compiling a program with
  88. "``-O3 -g``" gives you full debug information that is always available and
  89. accurate for reading (e.g., you get accurate stack traces despite tail call
  90. elimination and inlining), but you might lose the ability to modify the program
  91. and call functions where were optimized out of the program, or inlined away
  92. completely.
  93. .. _format:
  94. Debugging information format
  95. ============================
  96. LLVM debugging information has been carefully designed to make it possible for
  97. the optimizer to optimize the program and debugging information without
  98. necessarily having to know anything about debugging information. In
  99. particular, the use of metadata avoids duplicated debugging information from
  100. the beginning, and the global dead code elimination pass automatically deletes
  101. debugging information for a function if it decides to delete the function.
  102. To do this, most of the debugging information (descriptors for types,
  103. variables, functions, source files, etc) is inserted by the language front-end
  104. in the form of LLVM metadata.
  105. Debug information is designed to be agnostic about the target debugger and
  106. debugging information representation (e.g. DWARF/Stabs/etc). It uses a generic
  107. pass to decode the information that represents variables, types, functions,
  108. namespaces, etc: this allows for arbitrary source-language semantics and
  109. type-systems to be used, as long as there is a module written for the target
  110. debugger to interpret the information.
  111. To provide basic functionality, the LLVM debugger does have to make some
  112. assumptions about the source-level language being debugged, though it keeps
  113. these to a minimum. The only common features that the LLVM debugger assumes
  114. exist are `source files <LangRef.html#difile>`_, and `program objects
  115. <LangRef.html#diglobalvariable>`_. These abstract objects are used by a
  116. debugger to form stack traces, show information about local variables, etc.
  117. This section of the documentation first describes the representation aspects
  118. common to any source-language. :ref:`ccxx_frontend` describes the data layout
  119. conventions used by the C and C++ front-ends.
  120. Debug information descriptors are `specialized metadata nodes
  121. <LangRef.html#specialized-metadata>`_, first-class subclasses of ``Metadata``.
  122. .. _format_common_intrinsics:
  123. Debugger intrinsic functions
  124. ----------------------------
  125. LLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to
  126. provide debug information at various points in generated code.
  127. ``llvm.dbg.declare``
  128. ^^^^^^^^^^^^^^^^^^^^
  129. .. code-block:: llvm
  130. void @llvm.dbg.declare(metadata, metadata, metadata)
  131. This intrinsic provides information about a local element (e.g., variable).
  132. The first argument is metadata holding the alloca for the variable. The second
  133. argument is a `local variable <LangRef.html#dilocalvariable>`_ containing a
  134. description of the variable. The third argument is a `complex expression
  135. <LangRef.html#diexpression>`_.
  136. ``llvm.dbg.value``
  137. ^^^^^^^^^^^^^^^^^^
  138. .. code-block:: llvm
  139. void @llvm.dbg.value(metadata, i64, metadata, metadata)
  140. This intrinsic provides information when a user source variable is set to a new
  141. value. The first argument is the new value (wrapped as metadata). The second
  142. argument is the offset in the user source variable where the new value is
  143. written. The third argument is a `local variable
  144. <LangRef.html#dilocalvariable>`_ containing a description of the variable. The
  145. third argument is a `complex expression <LangRef.html#diexpression>`_.
  146. Object lifetimes and scoping
  147. ============================
  148. In many languages, the local variables in functions can have their lifetimes or
  149. scopes limited to a subset of a function. In the C family of languages, for
  150. example, variables are only live (readable and writable) within the source
  151. block that they are defined in. In functional languages, values are only
  152. readable after they have been defined. Though this is a very obvious concept,
  153. it is non-trivial to model in LLVM, because it has no notion of scoping in this
  154. sense, and does not want to be tied to a language's scoping rules.
  155. In order to handle this, the LLVM debug format uses the metadata attached to
  156. llvm instructions to encode line number and scoping information. Consider the
  157. following C fragment, for example:
  158. .. code-block:: c
  159. 1. void foo() {
  160. 2. int X = 21;
  161. 3. int Y = 22;
  162. 4. {
  163. 5. int Z = 23;
  164. 6. Z = X;
  165. 7. }
  166. 8. X = Y;
  167. 9. }
  168. Compiled to LLVM, this function would be represented like this:
  169. .. code-block:: llvm
  170. ; Function Attrs: nounwind ssp uwtable
  171. define void @foo() #0 {
  172. entry:
  173. %X = alloca i32, align 4
  174. %Y = alloca i32, align 4
  175. %Z = alloca i32, align 4
  176. call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
  177. store i32 21, i32* %X, align 4, !dbg !14
  178. call void @llvm.dbg.declare(metadata i32* %Y, metadata !15, metadata !13), !dbg !16
  179. store i32 22, i32* %Y, align 4, !dbg !16
  180. call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
  181. store i32 23, i32* %Z, align 4, !dbg !19
  182. %0 = load i32, i32* %X, align 4, !dbg !20
  183. store i32 %0, i32* %Z, align 4, !dbg !21
  184. %1 = load i32, i32* %Y, align 4, !dbg !22
  185. store i32 %1, i32* %X, align 4, !dbg !23
  186. ret void, !dbg !24
  187. }
  188. ; Function Attrs: nounwind readnone
  189. declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
  190. 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" }
  191. attributes #1 = { nounwind readnone }
  192. !llvm.dbg.cu = !{!0}
  193. !llvm.module.flags = !{!7, !8, !9}
  194. !llvm.ident = !{!10}
  195. !0 = !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
  196. !1 = !DIFile(filename: "/dev/stdin", directory: "/Users/dexonsmith/data/llvm/debug-info")
  197. !2 = !{}
  198. !3 = !{!4}
  199. !4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, function: void ()* @foo, variables: !2)
  200. !5 = !DISubroutineType(types: !6)
  201. !6 = !{null}
  202. !7 = !{i32 2, !"Dwarf Version", i32 2}
  203. !8 = !{i32 2, !"Debug Info Version", i32 3}
  204. !9 = !{i32 1, !"PIC Level", i32 2}
  205. !10 = !{!"clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)"}
  206. !11 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "X", scope: !4, file: !1, line: 2, type: !12)
  207. !12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
  208. !13 = !DIExpression()
  209. !14 = !DILocation(line: 2, column: 9, scope: !4)
  210. !15 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "Y", scope: !4, file: !1, line: 3, type: !12)
  211. !16 = !DILocation(line: 3, column: 9, scope: !4)
  212. !17 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "Z", scope: !18, file: !1, line: 5, type: !12)
  213. !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
  214. !19 = !DILocation(line: 5, column: 11, scope: !18)
  215. !20 = !DILocation(line: 6, column: 11, scope: !18)
  216. !21 = !DILocation(line: 6, column: 9, scope: !18)
  217. !22 = !DILocation(line: 8, column: 9, scope: !4)
  218. !23 = !DILocation(line: 8, column: 7, scope: !4)
  219. !24 = !DILocation(line: 9, column: 3, scope: !4)
  220. This example illustrates a few important details about LLVM debugging
  221. information. In particular, it shows how the ``llvm.dbg.declare`` intrinsic and
  222. location information, which are attached to an instruction, are applied
  223. together to allow a debugger to analyze the relationship between statements,
  224. variable definitions, and the code used to implement the function.
  225. .. code-block:: llvm
  226. call void @llvm.dbg.declare(metadata i32* %X, metadata !11, metadata !13), !dbg !14
  227. ; [debug line = 2:7] [debug variable = X]
  228. The first intrinsic ``%llvm.dbg.declare`` encodes debugging information for the
  229. variable ``X``. The metadata ``!dbg !14`` attached to the intrinsic provides
  230. scope information for the variable ``X``.
  231. .. code-block:: llvm
  232. !14 = !DILocation(line: 2, column: 9, scope: !4)
  233. !4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
  234. isLocal: false, isDefinition: true, scopeLine: 1,
  235. isOptimized: false, function: void ()* @foo,
  236. variables: !2)
  237. Here ``!14`` is metadata providing `location information
  238. <LangRef.html#dilocation>`_. In this example, scope is encoded by ``!4``, a
  239. `subprogram descriptor <LangRef.html#disubprogram>`_. This way the location
  240. information attached to the intrinsics indicates that the variable ``X`` is
  241. declared at line number 2 at a function level scope in function ``foo``.
  242. Now lets take another example.
  243. .. code-block:: llvm
  244. call void @llvm.dbg.declare(metadata i32* %Z, metadata !17, metadata !13), !dbg !19
  245. ; [debug line = 5:9] [debug variable = Z]
  246. The third intrinsic ``%llvm.dbg.declare`` encodes debugging information for
  247. variable ``Z``. The metadata ``!dbg !19`` attached to the intrinsic provides
  248. scope information for the variable ``Z``.
  249. .. code-block:: llvm
  250. !18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 5)
  251. !19 = !DILocation(line: 5, column: 11, scope: !18)
  252. Here ``!19`` indicates that ``Z`` is declared at line number 5 and column
  253. number 0 inside of lexical scope ``!18``. The lexical scope itself resides
  254. inside of subprogram ``!4`` described above.
  255. The scope information attached with each instruction provides a straightforward
  256. way to find instructions covered by a scope.
  257. .. _ccxx_frontend:
  258. C/C++ front-end specific debug information
  259. ==========================================
  260. The C and C++ front-ends represent information about the program in a format
  261. that is effectively identical to `DWARF 3.0
  262. <http://www.eagercon.com/dwarf/dwarf3std.htm>`_ in terms of information
  263. content. This allows code generators to trivially support native debuggers by
  264. generating standard dwarf information, and contains enough information for
  265. non-dwarf targets to translate it as needed.
  266. This section describes the forms used to represent C and C++ programs. Other
  267. languages could pattern themselves after this (which itself is tuned to
  268. representing programs in the same way that DWARF 3 does), or they could choose
  269. to provide completely different forms if they don't fit into the DWARF model.
  270. As support for debugging information gets added to the various LLVM
  271. source-language front-ends, the information used should be documented here.
  272. The following sections provide examples of a few C/C++ constructs and the debug
  273. information that would best describe those constructs. The canonical
  274. references are the ``DIDescriptor`` classes defined in
  275. ``include/llvm/IR/DebugInfo.h`` and the implementations of the helper functions
  276. in ``lib/IR/DIBuilder.cpp``.
  277. C/C++ source file information
  278. -----------------------------
  279. ``llvm::Instruction`` provides easy access to metadata attached with an
  280. instruction. One can extract line number information encoded in LLVM IR using
  281. ``Instruction::getMetadata()`` and ``DILocation::getLineNumber()``.
  282. .. code-block:: c++
  283. if (MDNode *N = I->getMetadata("dbg")) { // Here I is an LLVM instruction
  284. DILocation Loc(N); // DILocation is in DebugInfo.h
  285. unsigned Line = Loc.getLineNumber();
  286. StringRef File = Loc.getFilename();
  287. StringRef Dir = Loc.getDirectory();
  288. }
  289. C/C++ global variable information
  290. ---------------------------------
  291. Given an integer global variable declared as follows:
  292. .. code-block:: c
  293. int MyGlobal = 100;
  294. a C/C++ front-end would generate the following descriptors:
  295. .. code-block:: llvm
  296. ;;
  297. ;; Define the global itself.
  298. ;;
  299. @MyGlobal = global i32 100, align 4
  300. ;;
  301. ;; List of debug info of globals
  302. ;;
  303. !llvm.dbg.cu = !{!0}
  304. ;; Some unrelated metadata.
  305. !llvm.module.flags = !{!6, !7}
  306. ;; Define the compile unit.
  307. !0 = !DICompileUnit(language: DW_LANG_C99, file: !1,
  308. producer:
  309. "clang version 3.7.0 (trunk 231150) (llvm/trunk 231154)",
  310. isOptimized: false, runtimeVersion: 0, emissionKind: 1,
  311. enums: !2, retainedTypes: !2, subprograms: !2, globals:
  312. !3, imports: !2)
  313. ;;
  314. ;; Define the file
  315. ;;
  316. !1 = !DIFile(filename: "/dev/stdin",
  317. directory: "/Users/dexonsmith/data/llvm/debug-info")
  318. ;; An empty array.
  319. !2 = !{}
  320. ;; The Array of Global Variables
  321. !3 = !{!4}
  322. ;;
  323. ;; Define the global variable itself.
  324. ;;
  325. !4 = !DIGlobalVariable(name: "MyGlobal", scope: !0, file: !1, line: 1,
  326. type: !5, isLocal: false, isDefinition: true,
  327. variable: i32* @MyGlobal)
  328. ;;
  329. ;; Define the type
  330. ;;
  331. !5 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
  332. ;; Dwarf version to output.
  333. !6 = !{i32 2, !"Dwarf Version", i32 2}
  334. ;; Debug info schema version.
  335. !7 = !{i32 2, !"Debug Info Version", i32 3}
  336. C/C++ function information
  337. --------------------------
  338. Given a function declared as follows:
  339. .. code-block:: c
  340. int main(int argc, char *argv[]) {
  341. return 0;
  342. }
  343. a C/C++ front-end would generate the following descriptors:
  344. .. code-block:: llvm
  345. ;;
  346. ;; Define the anchor for subprograms.
  347. ;;
  348. !4 = !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !5,
  349. isLocal: false, isDefinition: true, scopeLine: 1,
  350. flags: DIFlagPrototyped, isOptimized: false,
  351. function: i32 (i32, i8**)* @main, variables: !2)
  352. ;;
  353. ;; Define the subprogram itself.
  354. ;;
  355. define i32 @main(i32 %argc, i8** %argv) {
  356. ...
  357. }
  358. Debugging information format
  359. ============================
  360. Debugging Information Extension for Objective C Properties
  361. ----------------------------------------------------------
  362. Introduction
  363. ^^^^^^^^^^^^
  364. Objective C provides a simpler way to declare and define accessor methods using
  365. declared properties. The language provides features to declare a property and
  366. to let compiler synthesize accessor methods.
  367. The debugger lets developer inspect Objective C interfaces and their instance
  368. variables and class variables. However, the debugger does not know anything
  369. about the properties defined in Objective C interfaces. The debugger consumes
  370. information generated by compiler in DWARF format. The format does not support
  371. encoding of Objective C properties. This proposal describes DWARF extensions to
  372. encode Objective C properties, which the debugger can use to let developers
  373. inspect Objective C properties.
  374. Proposal
  375. ^^^^^^^^
  376. Objective C properties exist separately from class members. A property can be
  377. defined only by "setter" and "getter" selectors, and be calculated anew on each
  378. access. Or a property can just be a direct access to some declared ivar.
  379. Finally it can have an ivar "automatically synthesized" for it by the compiler,
  380. in which case the property can be referred to in user code directly using the
  381. standard C dereference syntax as well as through the property "dot" syntax, but
  382. there is no entry in the ``@interface`` declaration corresponding to this ivar.
  383. To facilitate debugging, these properties we will add a new DWARF TAG into the
  384. ``DW_TAG_structure_type`` definition for the class to hold the description of a
  385. given property, and a set of DWARF attributes that provide said description.
  386. The property tag will also contain the name and declared type of the property.
  387. If there is a related ivar, there will also be a DWARF property attribute placed
  388. in the ``DW_TAG_member`` DIE for that ivar referring back to the property TAG
  389. for that property. And in the case where the compiler synthesizes the ivar
  390. directly, the compiler is expected to generate a ``DW_TAG_member`` for that
  391. ivar (with the ``DW_AT_artificial`` set to 1), whose name will be the name used
  392. to access this ivar directly in code, and with the property attribute pointing
  393. back to the property it is backing.
  394. The following examples will serve as illustration for our discussion:
  395. .. code-block:: objc
  396. @interface I1 {
  397. int n2;
  398. }
  399. @property int p1;
  400. @property int p2;
  401. @end
  402. @implementation I1
  403. @synthesize p1;
  404. @synthesize p2 = n2;
  405. @end
  406. This produces the following DWARF (this is a "pseudo dwarfdump" output):
  407. .. code-block:: none
  408. 0x00000100: TAG_structure_type [7] *
  409. AT_APPLE_runtime_class( 0x10 )
  410. AT_name( "I1" )
  411. AT_decl_file( "Objc_Property.m" )
  412. AT_decl_line( 3 )
  413. 0x00000110 TAG_APPLE_property
  414. AT_name ( "p1" )
  415. AT_type ( {0x00000150} ( int ) )
  416. 0x00000120: TAG_APPLE_property
  417. AT_name ( "p2" )
  418. AT_type ( {0x00000150} ( int ) )
  419. 0x00000130: TAG_member [8]
  420. AT_name( "_p1" )
  421. AT_APPLE_property ( {0x00000110} "p1" )
  422. AT_type( {0x00000150} ( int ) )
  423. AT_artificial ( 0x1 )
  424. 0x00000140: TAG_member [8]
  425. AT_name( "n2" )
  426. AT_APPLE_property ( {0x00000120} "p2" )
  427. AT_type( {0x00000150} ( int ) )
  428. 0x00000150: AT_type( ( int ) )
  429. Note, the current convention is that the name of the ivar for an
  430. auto-synthesized property is the name of the property from which it derives
  431. with an underscore prepended, as is shown in the example. But we actually
  432. don't need to know this convention, since we are given the name of the ivar
  433. directly.
  434. Also, it is common practice in ObjC to have different property declarations in
  435. the @interface and @implementation - e.g. to provide a read-only property in
  436. the interface,and a read-write interface in the implementation. In that case,
  437. the compiler should emit whichever property declaration will be in force in the
  438. current translation unit.
  439. Developers can decorate a property with attributes which are encoded using
  440. ``DW_AT_APPLE_property_attribute``.
  441. .. code-block:: objc
  442. @property (readonly, nonatomic) int pr;
  443. .. code-block:: none
  444. TAG_APPLE_property [8]
  445. AT_name( "pr" )
  446. AT_type ( {0x00000147} (int) )
  447. AT_APPLE_property_attribute (DW_APPLE_PROPERTY_readonly, DW_APPLE_PROPERTY_nonatomic)
  448. The setter and getter method names are attached to the property using
  449. ``DW_AT_APPLE_property_setter`` and ``DW_AT_APPLE_property_getter`` attributes.
  450. .. code-block:: objc
  451. @interface I1
  452. @property (setter=myOwnP3Setter:) int p3;
  453. -(void)myOwnP3Setter:(int)a;
  454. @end
  455. @implementation I1
  456. @synthesize p3;
  457. -(void)myOwnP3Setter:(int)a{ }
  458. @end
  459. The DWARF for this would be:
  460. .. code-block:: none
  461. 0x000003bd: TAG_structure_type [7] *
  462. AT_APPLE_runtime_class( 0x10 )
  463. AT_name( "I1" )
  464. AT_decl_file( "Objc_Property.m" )
  465. AT_decl_line( 3 )
  466. 0x000003cd TAG_APPLE_property
  467. AT_name ( "p3" )
  468. AT_APPLE_property_setter ( "myOwnP3Setter:" )
  469. AT_type( {0x00000147} ( int ) )
  470. 0x000003f3: TAG_member [8]
  471. AT_name( "_p3" )
  472. AT_type ( {0x00000147} ( int ) )
  473. AT_APPLE_property ( {0x000003cd} )
  474. AT_artificial ( 0x1 )
  475. New DWARF Tags
  476. ^^^^^^^^^^^^^^
  477. +-----------------------+--------+
  478. | TAG | Value |
  479. +=======================+========+
  480. | DW_TAG_APPLE_property | 0x4200 |
  481. +-----------------------+--------+
  482. New DWARF Attributes
  483. ^^^^^^^^^^^^^^^^^^^^
  484. +--------------------------------+--------+-----------+
  485. | Attribute | Value | Classes |
  486. +================================+========+===========+
  487. | DW_AT_APPLE_property | 0x3fed | Reference |
  488. +--------------------------------+--------+-----------+
  489. | DW_AT_APPLE_property_getter | 0x3fe9 | String |
  490. +--------------------------------+--------+-----------+
  491. | DW_AT_APPLE_property_setter | 0x3fea | String |
  492. +--------------------------------+--------+-----------+
  493. | DW_AT_APPLE_property_attribute | 0x3feb | Constant |
  494. +--------------------------------+--------+-----------+
  495. New DWARF Constants
  496. ^^^^^^^^^^^^^^^^^^^
  497. +--------------------------------------+-------+
  498. | Name | Value |
  499. +======================================+=======+
  500. | DW_APPLE_PROPERTY_readonly | 0x01 |
  501. +--------------------------------------+-------+
  502. | DW_APPLE_PROPERTY_getter | 0x02 |
  503. +--------------------------------------+-------+
  504. | DW_APPLE_PROPERTY_assign | 0x04 |
  505. +--------------------------------------+-------+
  506. | DW_APPLE_PROPERTY_readwrite | 0x08 |
  507. +--------------------------------------+-------+
  508. | DW_APPLE_PROPERTY_retain | 0x10 |
  509. +--------------------------------------+-------+
  510. | DW_APPLE_PROPERTY_copy | 0x20 |
  511. +--------------------------------------+-------+
  512. | DW_APPLE_PROPERTY_nonatomic | 0x40 |
  513. +--------------------------------------+-------+
  514. | DW_APPLE_PROPERTY_setter | 0x80 |
  515. +--------------------------------------+-------+
  516. | DW_APPLE_PROPERTY_atomic | 0x100 |
  517. +--------------------------------------+-------+
  518. | DW_APPLE_PROPERTY_weak | 0x200 |
  519. +--------------------------------------+-------+
  520. | DW_APPLE_PROPERTY_strong | 0x400 |
  521. +--------------------------------------+-------+
  522. | DW_APPLE_PROPERTY_unsafe_unretained | 0x800 |
  523. +--------------------------------+-----+-------+
  524. Name Accelerator Tables
  525. -----------------------
  526. Introduction
  527. ^^^^^^^^^^^^
  528. The "``.debug_pubnames``" and "``.debug_pubtypes``" formats are not what a
  529. debugger needs. The "``pub``" in the section name indicates that the entries
  530. in the table are publicly visible names only. This means no static or hidden
  531. functions show up in the "``.debug_pubnames``". No static variables or private
  532. class variables are in the "``.debug_pubtypes``". Many compilers add different
  533. things to these tables, so we can't rely upon the contents between gcc, icc, or
  534. clang.
  535. The typical query given by users tends not to match up with the contents of
  536. these tables. For example, the DWARF spec states that "In the case of the name
  537. of a function member or static data member of a C++ structure, class or union,
  538. the name presented in the "``.debug_pubnames``" section is not the simple name
  539. given by the ``DW_AT_name attribute`` of the referenced debugging information
  540. entry, but rather the fully qualified name of the data or function member."
  541. So the only names in these tables for complex C++ entries is a fully
  542. qualified name. Debugger users tend not to enter their search strings as
  543. "``a::b::c(int,const Foo&) const``", but rather as "``c``", "``b::c``" , or
  544. "``a::b::c``". So the name entered in the name table must be demangled in
  545. order to chop it up appropriately and additional names must be manually entered
  546. into the table to make it effective as a name lookup table for debuggers to
  547. se.
  548. All debuggers currently ignore the "``.debug_pubnames``" table as a result of
  549. its inconsistent and useless public-only name content making it a waste of
  550. space in the object file. These tables, when they are written to disk, are not
  551. sorted in any way, leaving every debugger to do its own parsing and sorting.
  552. These tables also include an inlined copy of the string values in the table
  553. itself making the tables much larger than they need to be on disk, especially
  554. for large C++ programs.
  555. Can't we just fix the sections by adding all of the names we need to this
  556. table? No, because that is not what the tables are defined to contain and we
  557. won't know the difference between the old bad tables and the new good tables.
  558. At best we could make our own renamed sections that contain all of the data we
  559. need.
  560. These tables are also insufficient for what a debugger like LLDB needs. LLDB
  561. uses clang for its expression parsing where LLDB acts as a PCH. LLDB is then
  562. often asked to look for type "``foo``" or namespace "``bar``", or list items in
  563. namespace "``baz``". Namespaces are not included in the pubnames or pubtypes
  564. tables. Since clang asks a lot of questions when it is parsing an expression,
  565. we need to be very fast when looking up names, as it happens a lot. Having new
  566. accelerator tables that are optimized for very quick lookups will benefit this
  567. type of debugging experience greatly.
  568. We would like to generate name lookup tables that can be mapped into memory
  569. from disk, and used as is, with little or no up-front parsing. We would also
  570. be able to control the exact content of these different tables so they contain
  571. exactly what we need. The Name Accelerator Tables were designed to fix these
  572. issues. In order to solve these issues we need to:
  573. * Have a format that can be mapped into memory from disk and used as is
  574. * Lookups should be very fast
  575. * Extensible table format so these tables can be made by many producers
  576. * Contain all of the names needed for typical lookups out of the box
  577. * Strict rules for the contents of tables
  578. Table size is important and the accelerator table format should allow the reuse
  579. of strings from common string tables so the strings for the names are not
  580. duplicated. We also want to make sure the table is ready to be used as-is by
  581. simply mapping the table into memory with minimal header parsing.
  582. The name lookups need to be fast and optimized for the kinds of lookups that
  583. debuggers tend to do. Optimally we would like to touch as few parts of the
  584. mapped table as possible when doing a name lookup and be able to quickly find
  585. the name entry we are looking for, or discover there are no matches. In the
  586. case of debuggers we optimized for lookups that fail most of the time.
  587. Each table that is defined should have strict rules on exactly what is in the
  588. accelerator tables and documented so clients can rely on the content.
  589. Hash Tables
  590. ^^^^^^^^^^^
  591. Standard Hash Tables
  592. """"""""""""""""""""
  593. Typical hash tables have a header, buckets, and each bucket points to the
  594. bucket contents:
  595. .. code-block:: none
  596. .------------.
  597. | HEADER |
  598. |------------|
  599. | BUCKETS |
  600. |------------|
  601. | DATA |
  602. `------------'
  603. The BUCKETS are an array of offsets to DATA for each hash:
  604. .. code-block:: none
  605. .------------.
  606. | 0x00001000 | BUCKETS[0]
  607. | 0x00002000 | BUCKETS[1]
  608. | 0x00002200 | BUCKETS[2]
  609. | 0x000034f0 | BUCKETS[3]
  610. | | ...
  611. | 0xXXXXXXXX | BUCKETS[n_buckets]
  612. '------------'
  613. So for ``bucket[3]`` in the example above, we have an offset into the table
  614. 0x000034f0 which points to a chain of entries for the bucket. Each bucket must
  615. contain a next pointer, full 32 bit hash value, the string itself, and the data
  616. for the current string value.
  617. .. code-block:: none
  618. .------------.
  619. 0x000034f0: | 0x00003500 | next pointer
  620. | 0x12345678 | 32 bit hash
  621. | "erase" | string value
  622. | data[n] | HashData for this bucket
  623. |------------|
  624. 0x00003500: | 0x00003550 | next pointer
  625. | 0x29273623 | 32 bit hash
  626. | "dump" | string value
  627. | data[n] | HashData for this bucket
  628. |------------|
  629. 0x00003550: | 0x00000000 | next pointer
  630. | 0x82638293 | 32 bit hash
  631. | "main" | string value
  632. | data[n] | HashData for this bucket
  633. `------------'
  634. The problem with this layout for debuggers is that we need to optimize for the
  635. negative lookup case where the symbol we're searching for is not present. So
  636. if we were to lookup "``printf``" in the table above, we would make a 32 hash
  637. for "``printf``", it might match ``bucket[3]``. We would need to go to the
  638. offset 0x000034f0 and start looking to see if our 32 bit hash matches. To do
  639. so, we need to read the next pointer, then read the hash, compare it, and skip
  640. to the next bucket. Each time we are skipping many bytes in memory and
  641. touching new cache pages just to do the compare on the full 32 bit hash. All
  642. of these accesses then tell us that we didn't have a match.
  643. Name Hash Tables
  644. """"""""""""""""
  645. To solve the issues mentioned above we have structured the hash tables a bit
  646. differently: a header, buckets, an array of all unique 32 bit hash values,
  647. followed by an array of hash value data offsets, one for each hash value, then
  648. the data for all hash values:
  649. .. code-block:: none
  650. .-------------.
  651. | HEADER |
  652. |-------------|
  653. | BUCKETS |
  654. |-------------|
  655. | HASHES |
  656. |-------------|
  657. | OFFSETS |
  658. |-------------|
  659. | DATA |
  660. `-------------'
  661. The ``BUCKETS`` in the name tables are an index into the ``HASHES`` array. By
  662. making all of the full 32 bit hash values contiguous in memory, we allow
  663. ourselves to efficiently check for a match while touching as little memory as
  664. possible. Most often checking the 32 bit hash values is as far as the lookup
  665. goes. If it does match, it usually is a match with no collisions. So for a
  666. table with "``n_buckets``" buckets, and "``n_hashes``" unique 32 bit hash
  667. values, we can clarify the contents of the ``BUCKETS``, ``HASHES`` and
  668. ``OFFSETS`` as:
  669. .. code-block:: none
  670. .-------------------------.
  671. | HEADER.magic | uint32_t
  672. | HEADER.version | uint16_t
  673. | HEADER.hash_function | uint16_t
  674. | HEADER.bucket_count | uint32_t
  675. | HEADER.hashes_count | uint32_t
  676. | HEADER.header_data_len | uint32_t
  677. | HEADER_DATA | HeaderData
  678. |-------------------------|
  679. | BUCKETS | uint32_t[n_buckets] // 32 bit hash indexes
  680. |-------------------------|
  681. | HASHES | uint32_t[n_hashes] // 32 bit hash values
  682. |-------------------------|
  683. | OFFSETS | uint32_t[n_hashes] // 32 bit offsets to hash value data
  684. |-------------------------|
  685. | ALL HASH DATA |
  686. `-------------------------'
  687. So taking the exact same data from the standard hash example above we end up
  688. with:
  689. .. code-block:: none
  690. .------------.
  691. | HEADER |
  692. |------------|
  693. | 0 | BUCKETS[0]
  694. | 2 | BUCKETS[1]
  695. | 5 | BUCKETS[2]
  696. | 6 | BUCKETS[3]
  697. | | ...
  698. | ... | BUCKETS[n_buckets]
  699. |------------|
  700. | 0x........ | HASHES[0]
  701. | 0x........ | HASHES[1]
  702. | 0x........ | HASHES[2]
  703. | 0x........ | HASHES[3]
  704. | 0x........ | HASHES[4]
  705. | 0x........ | HASHES[5]
  706. | 0x12345678 | HASHES[6] hash for BUCKETS[3]
  707. | 0x29273623 | HASHES[7] hash for BUCKETS[3]
  708. | 0x82638293 | HASHES[8] hash for BUCKETS[3]
  709. | 0x........ | HASHES[9]
  710. | 0x........ | HASHES[10]
  711. | 0x........ | HASHES[11]
  712. | 0x........ | HASHES[12]
  713. | 0x........ | HASHES[13]
  714. | 0x........ | HASHES[n_hashes]
  715. |------------|
  716. | 0x........ | OFFSETS[0]
  717. | 0x........ | OFFSETS[1]
  718. | 0x........ | OFFSETS[2]
  719. | 0x........ | OFFSETS[3]
  720. | 0x........ | OFFSETS[4]
  721. | 0x........ | OFFSETS[5]
  722. | 0x000034f0 | OFFSETS[6] offset for BUCKETS[3]
  723. | 0x00003500 | OFFSETS[7] offset for BUCKETS[3]
  724. | 0x00003550 | OFFSETS[8] offset for BUCKETS[3]
  725. | 0x........ | OFFSETS[9]
  726. | 0x........ | OFFSETS[10]
  727. | 0x........ | OFFSETS[11]
  728. | 0x........ | OFFSETS[12]
  729. | 0x........ | OFFSETS[13]
  730. | 0x........ | OFFSETS[n_hashes]
  731. |------------|
  732. | |
  733. | |
  734. | |
  735. | |
  736. | |
  737. |------------|
  738. 0x000034f0: | 0x00001203 | .debug_str ("erase")
  739. | 0x00000004 | A 32 bit array count - number of HashData with name "erase"
  740. | 0x........ | HashData[0]
  741. | 0x........ | HashData[1]
  742. | 0x........ | HashData[2]
  743. | 0x........ | HashData[3]
  744. | 0x00000000 | String offset into .debug_str (terminate data for hash)
  745. |------------|
  746. 0x00003500: | 0x00001203 | String offset into .debug_str ("collision")
  747. | 0x00000002 | A 32 bit array count - number of HashData with name "collision"
  748. | 0x........ | HashData[0]
  749. | 0x........ | HashData[1]
  750. | 0x00001203 | String offset into .debug_str ("dump")
  751. | 0x00000003 | A 32 bit array count - number of HashData with name "dump"
  752. | 0x........ | HashData[0]
  753. | 0x........ | HashData[1]
  754. | 0x........ | HashData[2]
  755. | 0x00000000 | String offset into .debug_str (terminate data for hash)
  756. |------------|
  757. 0x00003550: | 0x00001203 | String offset into .debug_str ("main")
  758. | 0x00000009 | A 32 bit array count - number of HashData with name "main"
  759. | 0x........ | HashData[0]
  760. | 0x........ | HashData[1]
  761. | 0x........ | HashData[2]
  762. | 0x........ | HashData[3]
  763. | 0x........ | HashData[4]
  764. | 0x........ | HashData[5]
  765. | 0x........ | HashData[6]
  766. | 0x........ | HashData[7]
  767. | 0x........ | HashData[8]
  768. | 0x00000000 | String offset into .debug_str (terminate data for hash)
  769. `------------'
  770. So we still have all of the same data, we just organize it more efficiently for
  771. debugger lookup. If we repeat the same "``printf``" lookup from above, we
  772. would hash "``printf``" and find it matches ``BUCKETS[3]`` by taking the 32 bit
  773. hash value and modulo it by ``n_buckets``. ``BUCKETS[3]`` contains "6" which
  774. is the index into the ``HASHES`` table. We would then compare any consecutive
  775. 32 bit hashes values in the ``HASHES`` array as long as the hashes would be in
  776. ``BUCKETS[3]``. We do this by verifying that each subsequent hash value modulo
  777. ``n_buckets`` is still 3. In the case of a failed lookup we would access the
  778. memory for ``BUCKETS[3]``, and then compare a few consecutive 32 bit hashes
  779. before we know that we have no match. We don't end up marching through
  780. multiple words of memory and we really keep the number of processor data cache
  781. lines being accessed as small as possible.
  782. The string hash that is used for these lookup tables is the Daniel J.
  783. Bernstein hash which is also used in the ELF ``GNU_HASH`` sections. It is a
  784. very good hash for all kinds of names in programs with very few hash
  785. collisions.
  786. Empty buckets are designated by using an invalid hash index of ``UINT32_MAX``.
  787. Details
  788. ^^^^^^^
  789. These name hash tables are designed to be generic where specializations of the
  790. table get to define additional data that goes into the header ("``HeaderData``"),
  791. how the string value is stored ("``KeyType``") and the content of the data for each
  792. hash value.
  793. Header Layout
  794. """""""""""""
  795. The header has a fixed part, and the specialized part. The exact format of the
  796. header is:
  797. .. code-block:: c
  798. struct Header
  799. {
  800. uint32_t magic; // 'HASH' magic value to allow endian detection
  801. uint16_t version; // Version number
  802. uint16_t hash_function; // The hash function enumeration that was used
  803. uint32_t bucket_count; // The number of buckets in this hash table
  804. uint32_t hashes_count; // The total number of unique hash values and hash data offsets in this table
  805. uint32_t header_data_len; // The bytes to skip to get to the hash indexes (buckets) for correct alignment
  806. // Specifically the length of the following HeaderData field - this does not
  807. // include the size of the preceding fields
  808. HeaderData header_data; // Implementation specific header data
  809. };
  810. The header starts with a 32 bit "``magic``" value which must be ``'HASH'``
  811. encoded as an ASCII integer. This allows the detection of the start of the
  812. hash table and also allows the table's byte order to be determined so the table
  813. can be correctly extracted. The "``magic``" value is followed by a 16 bit
  814. ``version`` number which allows the table to be revised and modified in the
  815. future. The current version number is 1. ``hash_function`` is a ``uint16_t``
  816. enumeration that specifies which hash function was used to produce this table.
  817. The current values for the hash function enumerations include:
  818. .. code-block:: c
  819. enum HashFunctionType
  820. {
  821. eHashFunctionDJB = 0u, // Daniel J Bernstein hash function
  822. };
  823. ``bucket_count`` is a 32 bit unsigned integer that represents how many buckets
  824. are in the ``BUCKETS`` array. ``hashes_count`` is the number of unique 32 bit
  825. hash values that are in the ``HASHES`` array, and is the same number of offsets
  826. are contained in the ``OFFSETS`` array. ``header_data_len`` specifies the size
  827. in bytes of the ``HeaderData`` that is filled in by specialized versions of
  828. this table.
  829. Fixed Lookup
  830. """"""""""""
  831. The header is followed by the buckets, hashes, offsets, and hash value data.
  832. .. code-block:: c
  833. struct FixedTable
  834. {
  835. uint32_t buckets[Header.bucket_count]; // An array of hash indexes into the "hashes[]" array below
  836. uint32_t hashes [Header.hashes_count]; // Every unique 32 bit hash for the entire table is in this table
  837. uint32_t offsets[Header.hashes_count]; // An offset that corresponds to each item in the "hashes[]" array above
  838. };
  839. ``buckets`` is an array of 32 bit indexes into the ``hashes`` array. The
  840. ``hashes`` array contains all of the 32 bit hash values for all names in the
  841. hash table. Each hash in the ``hashes`` table has an offset in the ``offsets``
  842. array that points to the data for the hash value.
  843. This table setup makes it very easy to repurpose these tables to contain
  844. different data, while keeping the lookup mechanism the same for all tables.
  845. This layout also makes it possible to save the table to disk and map it in
  846. later and do very efficient name lookups with little or no parsing.
  847. DWARF lookup tables can be implemented in a variety of ways and can store a lot
  848. of information for each name. We want to make the DWARF tables extensible and
  849. able to store the data efficiently so we have used some of the DWARF features
  850. that enable efficient data storage to define exactly what kind of data we store
  851. for each name.
  852. The ``HeaderData`` contains a definition of the contents of each HashData chunk.
  853. We might want to store an offset to all of the debug information entries (DIEs)
  854. for each name. To keep things extensible, we create a list of items, or
  855. Atoms, that are contained in the data for each name. First comes the type of
  856. the data in each atom:
  857. .. code-block:: c
  858. enum AtomType
  859. {
  860. eAtomTypeNULL = 0u,
  861. eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
  862. eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that contains the item in question
  863. eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
  864. eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
  865. eAtomTypeTypeFlags = 5u, // Flags from enum TypeFlags
  866. };
  867. The enumeration values and their meanings are:
  868. .. code-block:: none
  869. eAtomTypeNULL - a termination atom that specifies the end of the atom list
  870. eAtomTypeDIEOffset - an offset into the .debug_info section for the DWARF DIE for this name
  871. eAtomTypeCUOffset - an offset into the .debug_info section for the CU that contains the DIE
  872. eAtomTypeDIETag - The DW_TAG_XXX enumeration value so you don't have to parse the DWARF to see what it is
  873. eAtomTypeNameFlags - Flags for functions and global variables (isFunction, isInlined, isExternal...)
  874. eAtomTypeTypeFlags - Flags for types (isCXXClass, isObjCClass, ...)
  875. Then we allow each atom type to define the atom type and how the data for each
  876. atom type data is encoded:
  877. .. code-block:: c
  878. struct Atom
  879. {
  880. uint16_t type; // AtomType enum value
  881. uint16_t form; // DWARF DW_FORM_XXX defines
  882. };
  883. The ``form`` type above is from the DWARF specification and defines the exact
  884. encoding of the data for the Atom type. See the DWARF specification for the
  885. ``DW_FORM_`` definitions.
  886. .. code-block:: c
  887. struct HeaderData
  888. {
  889. uint32_t die_offset_base;
  890. uint32_t atom_count;
  891. Atoms atoms[atom_count0];
  892. };
  893. ``HeaderData`` defines the base DIE offset that should be added to any atoms
  894. that are encoded using the ``DW_FORM_ref1``, ``DW_FORM_ref2``,
  895. ``DW_FORM_ref4``, ``DW_FORM_ref8`` or ``DW_FORM_ref_udata``. It also defines
  896. what is contained in each ``HashData`` object -- ``Atom.form`` tells us how large
  897. each field will be in the ``HashData`` and the ``Atom.type`` tells us how this data
  898. should be interpreted.
  899. For the current implementations of the "``.apple_names``" (all functions +
  900. globals), the "``.apple_types``" (names of all types that are defined), and
  901. the "``.apple_namespaces``" (all namespaces), we currently set the ``Atom``
  902. array to be:
  903. .. code-block:: c
  904. HeaderData.atom_count = 1;
  905. HeaderData.atoms[0].type = eAtomTypeDIEOffset;
  906. HeaderData.atoms[0].form = DW_FORM_data4;
  907. This defines the contents to be the DIE offset (eAtomTypeDIEOffset) that is
  908. encoded as a 32 bit value (DW_FORM_data4). This allows a single name to have
  909. multiple matching DIEs in a single file, which could come up with an inlined
  910. function for instance. Future tables could include more information about the
  911. DIE such as flags indicating if the DIE is a function, method, block,
  912. or inlined.
  913. The KeyType for the DWARF table is a 32 bit string table offset into the
  914. ".debug_str" table. The ".debug_str" is the string table for the DWARF which
  915. may already contain copies of all of the strings. This helps make sure, with
  916. help from the compiler, that we reuse the strings between all of the DWARF
  917. sections and keeps the hash table size down. Another benefit to having the
  918. compiler generate all strings as DW_FORM_strp in the debug info, is that
  919. DWARF parsing can be made much faster.
  920. After a lookup is made, we get an offset into the hash data. The hash data
  921. needs to be able to deal with 32 bit hash collisions, so the chunk of data
  922. at the offset in the hash data consists of a triple:
  923. .. code-block:: c
  924. uint32_t str_offset
  925. uint32_t hash_data_count
  926. HashData[hash_data_count]
  927. If "str_offset" is zero, then the bucket contents are done. 99.9% of the
  928. hash data chunks contain a single item (no 32 bit hash collision):
  929. .. code-block:: none
  930. .------------.
  931. | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
  932. | 0x00000004 | uint32_t HashData count
  933. | 0x........ | uint32_t HashData[0] DIE offset
  934. | 0x........ | uint32_t HashData[1] DIE offset
  935. | 0x........ | uint32_t HashData[2] DIE offset
  936. | 0x........ | uint32_t HashData[3] DIE offset
  937. | 0x00000000 | uint32_t KeyType (end of hash chain)
  938. `------------'
  939. If there are collisions, you will have multiple valid string offsets:
  940. .. code-block:: none
  941. .------------.
  942. | 0x00001023 | uint32_t KeyType (.debug_str[0x0001023] => "main")
  943. | 0x00000004 | uint32_t HashData count
  944. | 0x........ | uint32_t HashData[0] DIE offset
  945. | 0x........ | uint32_t HashData[1] DIE offset
  946. | 0x........ | uint32_t HashData[2] DIE offset
  947. | 0x........ | uint32_t HashData[3] DIE offset
  948. | 0x00002023 | uint32_t KeyType (.debug_str[0x0002023] => "print")
  949. | 0x00000002 | uint32_t HashData count
  950. | 0x........ | uint32_t HashData[0] DIE offset
  951. | 0x........ | uint32_t HashData[1] DIE offset
  952. | 0x00000000 | uint32_t KeyType (end of hash chain)
  953. `------------'
  954. Current testing with real world C++ binaries has shown that there is around 1
  955. 32 bit hash collision per 100,000 name entries.
  956. Contents
  957. ^^^^^^^^
  958. As we said, we want to strictly define exactly what is included in the
  959. different tables. For DWARF, we have 3 tables: "``.apple_names``",
  960. "``.apple_types``", and "``.apple_namespaces``".
  961. "``.apple_names``" sections should contain an entry for each DWARF DIE whose
  962. ``DW_TAG`` is a ``DW_TAG_label``, ``DW_TAG_inlined_subroutine``, or
  963. ``DW_TAG_subprogram`` that has address attributes: ``DW_AT_low_pc``,
  964. ``DW_AT_high_pc``, ``DW_AT_ranges`` or ``DW_AT_entry_pc``. It also contains
  965. ``DW_TAG_variable`` DIEs that have a ``DW_OP_addr`` in the location (global and
  966. static variables). All global and static variables should be included,
  967. including those scoped within functions and classes. For example using the
  968. following code:
  969. .. code-block:: c
  970. static int var = 0;
  971. void f ()
  972. {
  973. static int var = 0;
  974. }
  975. Both of the static ``var`` variables would be included in the table. All
  976. functions should emit both their full names and their basenames. For C or C++,
  977. the full name is the mangled name (if available) which is usually in the
  978. ``DW_AT_MIPS_linkage_name`` attribute, and the ``DW_AT_name`` contains the
  979. function basename. If global or static variables have a mangled name in a
  980. ``DW_AT_MIPS_linkage_name`` attribute, this should be emitted along with the
  981. simple name found in the ``DW_AT_name`` attribute.
  982. "``.apple_types``" sections should contain an entry for each DWARF DIE whose
  983. tag is one of:
  984. * DW_TAG_array_type
  985. * DW_TAG_class_type
  986. * DW_TAG_enumeration_type
  987. * DW_TAG_pointer_type
  988. * DW_TAG_reference_type
  989. * DW_TAG_string_type
  990. * DW_TAG_structure_type
  991. * DW_TAG_subroutine_type
  992. * DW_TAG_typedef
  993. * DW_TAG_union_type
  994. * DW_TAG_ptr_to_member_type
  995. * DW_TAG_set_type
  996. * DW_TAG_subrange_type
  997. * DW_TAG_base_type
  998. * DW_TAG_const_type
  999. * DW_TAG_file_type
  1000. * DW_TAG_namelist
  1001. * DW_TAG_packed_type
  1002. * DW_TAG_volatile_type
  1003. * DW_TAG_restrict_type
  1004. * DW_TAG_interface_type
  1005. * DW_TAG_unspecified_type
  1006. * DW_TAG_shared_type
  1007. Only entries with a ``DW_AT_name`` attribute are included, and the entry must
  1008. not be a forward declaration (``DW_AT_declaration`` attribute with a non-zero
  1009. value). For example, using the following code:
  1010. .. code-block:: c
  1011. int main ()
  1012. {
  1013. int *b = 0;
  1014. return *b;
  1015. }
  1016. We get a few type DIEs:
  1017. .. code-block:: none
  1018. 0x00000067: TAG_base_type [5]
  1019. AT_encoding( DW_ATE_signed )
  1020. AT_name( "int" )
  1021. AT_byte_size( 0x04 )
  1022. 0x0000006e: TAG_pointer_type [6]
  1023. AT_type( {0x00000067} ( int ) )
  1024. AT_byte_size( 0x08 )
  1025. The DW_TAG_pointer_type is not included because it does not have a ``DW_AT_name``.
  1026. "``.apple_namespaces``" section should contain all ``DW_TAG_namespace`` DIEs.
  1027. If we run into a namespace that has no name this is an anonymous namespace, and
  1028. the name should be output as "``(anonymous namespace)``" (without the quotes).
  1029. Why? This matches the output of the ``abi::cxa_demangle()`` that is in the
  1030. standard C++ library that demangles mangled names.
  1031. Language Extensions and File Format Changes
  1032. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1033. Objective-C Extensions
  1034. """"""""""""""""""""""
  1035. "``.apple_objc``" section should contain all ``DW_TAG_subprogram`` DIEs for an
  1036. Objective-C class. The name used in the hash table is the name of the
  1037. Objective-C class itself. If the Objective-C class has a category, then an
  1038. entry is made for both the class name without the category, and for the class
  1039. name with the category. So if we have a DIE at offset 0x1234 with a name of
  1040. method "``-[NSString(my_additions) stringWithSpecialString:]``", we would add
  1041. an entry for "``NSString``" that points to DIE 0x1234, and an entry for
  1042. "``NSString(my_additions)``" that points to 0x1234. This allows us to quickly
  1043. track down all Objective-C methods for an Objective-C class when doing
  1044. expressions. It is needed because of the dynamic nature of Objective-C where
  1045. anyone can add methods to a class. The DWARF for Objective-C methods is also
  1046. emitted differently from C++ classes where the methods are not usually
  1047. contained in the class definition, they are scattered about across one or more
  1048. compile units. Categories can also be defined in different shared libraries.
  1049. So we need to be able to quickly find all of the methods and class functions
  1050. given the Objective-C class name, or quickly find all methods and class
  1051. functions for a class + category name. This table does not contain any
  1052. selector names, it just maps Objective-C class names (or class names +
  1053. category) to all of the methods and class functions. The selectors are added
  1054. as function basenames in the "``.debug_names``" section.
  1055. In the "``.apple_names``" section for Objective-C functions, the full name is
  1056. the entire function name with the brackets ("``-[NSString
  1057. stringWithCString:]``") and the basename is the selector only
  1058. ("``stringWithCString:``").
  1059. Mach-O Changes
  1060. """"""""""""""
  1061. The sections names for the apple hash tables are for non-mach-o files. For
  1062. mach-o files, the sections should be contained in the ``__DWARF`` segment with
  1063. names as follows:
  1064. * "``.apple_names``" -> "``__apple_names``"
  1065. * "``.apple_types``" -> "``__apple_types``"
  1066. * "``.apple_namespaces``" -> "``__apple_namespac``" (16 character limit)
  1067. * "``.apple_objc``" -> "``__apple_objc``"