/3rd_party/llvm/docs/ExceptionHandling.html

https://code.google.com/p/softart/ · HTML · 582 lines · 437 code · 121 blank · 24 comment · 0 complexity · 662c9827d2cb7798a7d92e537c602055 MD5 · raw file

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <title>Exception Handling in LLVM</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta name="description"
  8. content="Exception Handling in LLVM.">
  9. <link rel="stylesheet" href="llvm.css" type="text/css">
  10. </head>
  11. <body>
  12. <h1>Exception Handling in LLVM</h1>
  13. <table class="layout" style="width:100%">
  14. <tr class="layout">
  15. <td class="left">
  16. <ul>
  17. <li><a href="#introduction">Introduction</a>
  18. <ol>
  19. <li><a href="#itanium">Itanium ABI Zero-cost Exception Handling</a></li>
  20. <li><a href="#sjlj">Setjmp/Longjmp Exception Handling</a></li>
  21. <li><a href="#overview">Overview</a></li>
  22. </ol></li>
  23. <li><a href="#codegen">LLVM Code Generation</a>
  24. <ol>
  25. <li><a href="#throw">Throw</a></li>
  26. <li><a href="#try_catch">Try/Catch</a></li>
  27. <li><a href="#cleanups">Cleanups</a></li>
  28. <li><a href="#throw_filters">Throw Filters</a></li>
  29. <li><a href="#restrictions">Restrictions</a></li>
  30. </ol></li>
  31. <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
  32. <ol>
  33. <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
  34. <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li>
  35. <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li>
  36. <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
  37. <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
  38. <li><a href="#llvm_eh_sjlj_dispatchsetup"><tt>llvm.eh.sjlj.dispatchsetup</tt></a></li>
  39. </ol></li>
  40. <li><a href="#asm">Asm Table Formats</a>
  41. <ol>
  42. <li><a href="#unwind_tables">Exception Handling Frame</a></li>
  43. <li><a href="#exception_tables">Exception Tables</a></li>
  44. </ol></li>
  45. </ul>
  46. </td>
  47. </tr></table>
  48. <div class="doc_author">
  49. <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
  50. </div>
  51. <!-- *********************************************************************** -->
  52. <h2><a name="introduction">Introduction</a></h2>
  53. <!-- *********************************************************************** -->
  54. <div>
  55. <p>This document is the central repository for all information pertaining to
  56. exception handling in LLVM. It describes the format that LLVM exception
  57. handling information takes, which is useful for those interested in creating
  58. front-ends or dealing directly with the information. Further, this document
  59. provides specific examples of what exception handling information is used for
  60. in C and C++.</p>
  61. <!-- ======================================================================= -->
  62. <h3>
  63. <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
  64. </h3>
  65. <div>
  66. <p>Exception handling for most programming languages is designed to recover from
  67. conditions that rarely occur during general use of an application. To that
  68. end, exception handling should not interfere with the main flow of an
  69. application's algorithm by performing checkpointing tasks, such as saving the
  70. current pc or register state.</p>
  71. <p>The Itanium ABI Exception Handling Specification defines a methodology for
  72. providing outlying data in the form of exception tables without inlining
  73. speculative exception handling code in the flow of an application's main
  74. algorithm. Thus, the specification is said to add "zero-cost" to the normal
  75. execution of an application.</p>
  76. <p>A more complete description of the Itanium ABI exception handling runtime
  77. support of can be found at
  78. <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
  79. Exception Handling</a>. A description of the exception frame format can be
  80. found at
  81. <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception
  82. Frames</a>, with details of the DWARF 4 specification at
  83. <a href="http://dwarfstd.org/Dwarf4Std.php">DWARF 4 Standard</a>.
  84. A description for the C++ exception table formats can be found at
  85. <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
  86. Tables</a>.</p>
  87. </div>
  88. <!-- ======================================================================= -->
  89. <h3>
  90. <a name="sjlj">Setjmp/Longjmp Exception Handling</a>
  91. </h3>
  92. <div>
  93. <p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
  94. <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and
  95. <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to
  96. handle control flow for exception handling.</p>
  97. <p>For each function which does exception processing &mdash; be
  98. it <tt>try</tt>/<tt>catch</tt> blocks or cleanups &mdash; that function
  99. registers itself on a global frame list. When exceptions are unwinding, the
  100. runtime uses this list to identify which functions need processing.<p>
  101. <p>Landing pad selection is encoded in the call site entry of the function
  102. context. The runtime returns to the function via
  103. <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where
  104. a switch table transfers control to the appropriate landing pad based on
  105. the index stored in the function context.</p>
  106. <p>In contrast to DWARF exception handling, which encodes exception regions
  107. and frame information in out-of-line tables, SJLJ exception handling
  108. builds and removes the unwind frame context at runtime. This results in
  109. faster exception handling at the expense of slower execution when no
  110. exceptions are thrown. As exceptions are, by their nature, intended for
  111. uncommon code paths, DWARF exception handling is generally preferred to
  112. SJLJ.</p>
  113. </div>
  114. <!-- ======================================================================= -->
  115. <h3>
  116. <a name="overview">Overview</a>
  117. </h3>
  118. <div>
  119. <p>When an exception is thrown in LLVM code, the runtime does its best to find a
  120. handler suited to processing the circumstance.</p>
  121. <p>The runtime first attempts to find an <i>exception frame</i> corresponding to
  122. the function where the exception was thrown. If the programming language
  123. supports exception handling (e.g. C++), the exception frame contains a
  124. reference to an exception table describing how to process the exception. If
  125. the language does not support exception handling (e.g. C), or if the
  126. exception needs to be forwarded to a prior activation, the exception frame
  127. contains information about how to unwind the current activation and restore
  128. the state of the prior activation. This process is repeated until the
  129. exception is handled. If the exception is not handled and no activations
  130. remain, then the application is terminated with an appropriate error
  131. message.</p>
  132. <p>Because different programming languages have different behaviors when
  133. handling exceptions, the exception handling ABI provides a mechanism for
  134. supplying <i>personalities</i>. An exception handling personality is defined
  135. by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
  136. in C++), which receives the context of the exception, an <i>exception
  137. structure</i> containing the exception object type and value, and a reference
  138. to the exception table for the current function. The personality function
  139. for the current compile unit is specified in a <i>common exception
  140. frame</i>.</p>
  141. <p>The organization of an exception table is language dependent. For C++, an
  142. exception table is organized as a series of code ranges defining what to do
  143. if an exception occurs in that range. Typically, the information associated
  144. with a range defines which types of exception objects (using C++ <i>type
  145. info</i>) that are handled in that range, and an associated action that
  146. should take place. Actions typically pass control to a <i>landing
  147. pad</i>.</p>
  148. <p>A landing pad corresponds roughly to the code found in the <tt>catch</tt>
  149. portion of a <tt>try</tt>/<tt>catch</tt> sequence. When execution resumes at
  150. a landing pad, it receives an <i>exception structure</i> and a
  151. <i>selector value</i> corresponding to the <i>type</i> of exception
  152. thrown. The selector is then used to determine which <i>catch</i> should
  153. actually process the exception.</p>
  154. </div>
  155. </div>
  156. <!-- ======================================================================= -->
  157. <h2>
  158. <a name="codegen">LLVM Code Generation</a>
  159. </h2>
  160. <div>
  161. <p>From a C++ developer's perspective, exceptions are defined in terms of the
  162. <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
  163. we will describe the implementation of LLVM exception handling in terms of
  164. C++ examples.</p>
  165. <!-- ======================================================================= -->
  166. <h3>
  167. <a name="throw">Throw</a>
  168. </h3>
  169. <div>
  170. <p>Languages that support exception handling typically provide a <tt>throw</tt>
  171. operation to initiate the exception process. Internally, a <tt>throw</tt>
  172. operation breaks down into two steps.</p>
  173. <ol>
  174. <li>A request is made to allocate exception space for an exception structure.
  175. This structure needs to survive beyond the current activation. This
  176. structure will contain the type and value of the object being thrown.</li>
  177. <li>A call is made to the runtime to raise the exception, passing the
  178. exception structure as an argument.</li>
  179. </ol>
  180. <p>In C++, the allocation of the exception structure is done by the
  181. <tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
  182. handled by <tt>__cxa_throw</tt>. The type of the exception is represented
  183. using a C++ RTTI structure.</p>
  184. </div>
  185. <!-- ======================================================================= -->
  186. <h3>
  187. <a name="try_catch">Try/Catch</a>
  188. </h3>
  189. <div>
  190. <p>A call within the scope of a <i>try</i> statement can potentially raise an
  191. exception. In those circumstances, the LLVM C++ front-end replaces the call
  192. with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
  193. two potential continuation points:</p>
  194. <ol>
  195. <li>where to continue when the call succeeds as per normal, and</li>
  196. <li>where to continue if the call raises an exception, either by a throw or
  197. the unwinding of a throw</li>
  198. </ol>
  199. <p>The term used to define a the place where an <tt>invoke</tt> continues after
  200. an exception is called a <i>landing pad</i>. LLVM landing pads are
  201. conceptually alternative function entry points where an exception structure
  202. reference and a type info index are passed in as arguments. The landing pad
  203. saves the exception structure reference and then proceeds to select the catch
  204. block that corresponds to the type info of the exception object.</p>
  205. <p>The LLVM <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
  206. instruction</a> is used to convey information about the landing pad to the
  207. back end. For C++, the <tt>landingpad</tt> instruction returns a pointer and
  208. integer pair corresponding to the pointer to the <i>exception structure</i>
  209. and the <i>selector value</i> respectively.</p>
  210. <p>The <tt>landingpad</tt> instruction takes a reference to the personality
  211. function to be used for this <tt>try</tt>/<tt>catch</tt> sequence. The
  212. remainder of the instruction is a list of <i>cleanup</i>, <i>catch</i>,
  213. and <i>filter</i> clauses. The exception is tested against the clauses
  214. sequentially from first to last. The selector value is a positive number if
  215. the exception matched a type info, a negative number if it matched a filter,
  216. and zero if it matched a cleanup. If nothing is matched, the behavior of
  217. the program is <a href="#restrictions">undefined</a>. If a type info matched,
  218. then the selector value is the index of the type info in the exception table,
  219. which can be obtained using the
  220. <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
  221. <p>Once the landing pad has the type info selector, the code branches to the
  222. code for the first catch. The catch then checks the value of the type info
  223. selector against the index of type info for that catch. Since the type info
  224. index is not known until all the type infos have been gathered in the
  225. backend, the catch code must call the
  226. <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
  227. determine the index for a given type info. If the catch fails to match the
  228. selector then control is passed on to the next catch.</p>
  229. <p>Finally, the entry and exit of catch code is bracketed with calls to
  230. <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
  231. <ul>
  232. <li><tt>__cxa_begin_catch</tt> takes an exception structure reference as an
  233. argument and returns the value of the exception object.</li>
  234. <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
  235. <ol>
  236. <li>Locates the most recently caught exception and decrements its handler
  237. count,</li>
  238. <li>Removes the exception from the <i>caught</i> stack if the handler
  239. count goes to zero, and</li>
  240. <li>Destroys the exception if the handler count goes to zero and the
  241. exception was not re-thrown by throw.</li>
  242. </ol>
  243. <p><b>Note:</b> a rethrow from within the catch may replace this call with
  244. a <tt>__cxa_rethrow</tt>.</p></li>
  245. </ul>
  246. </div>
  247. <!-- ======================================================================= -->
  248. <h3>
  249. <a name="cleanups">Cleanups</a>
  250. </h3>
  251. <div>
  252. <p>A cleanup is extra code which needs to be run as part of unwinding a scope.
  253. C++ destructors are a typical example, but other languages and language
  254. extensions provide a variety of different kinds of cleanups. In general, a
  255. landing pad may need to run arbitrary amounts of cleanup code before actually
  256. entering a catch block. To indicate the presence of cleanups, a
  257. <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
  258. should have a <i>cleanup</i> clause. Otherwise, the unwinder will not stop at
  259. the landing pad if there are no catches or filters that require it to.</p>
  260. <p><b>Note:</b> Do not allow a new exception to propagate out of the execution
  261. of a cleanup. This can corrupt the internal state of the unwinder.
  262. Different languages describe different high-level semantics for these
  263. situations: for example, C++ requires that the process be terminated, whereas
  264. Ada cancels both exceptions and throws a third.</p>
  265. <p>When all cleanups are finished, if the exception is not handled by the
  266. current function, resume unwinding by calling the
  267. <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a>, passing in
  268. the result of the <tt>landingpad</tt> instruction for the original landing
  269. pad.</p>
  270. </div>
  271. <!-- ======================================================================= -->
  272. <h3>
  273. <a name="throw_filters">Throw Filters</a>
  274. </h3>
  275. <div>
  276. <p>C++ allows the specification of which exception types may be thrown from a
  277. function. To represent this, a top level landing pad may exist to filter out
  278. invalid types. To express this in LLVM code the
  279. <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> will
  280. have a filter clause. The clause consists of an array of type infos.
  281. <tt>landingpad</tt> will return a negative value if the exception does not
  282. match any of the type infos. If no match is found then a call
  283. to <tt>__cxa_call_unexpected</tt> should be made, otherwise
  284. <tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
  285. exception structure. Note that the most general form of a
  286. <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> can
  287. have any number of catch, cleanup, and filter clauses (though having more
  288. than one cleanup is pointless). The LLVM C++ front-end can generate such
  289. <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instructions</a> due
  290. to inlining creating nested exception handling scopes.</p>
  291. </div>
  292. <!-- ======================================================================= -->
  293. <h3>
  294. <a name="restrictions">Restrictions</a>
  295. </h3>
  296. <div>
  297. <p>The unwinder delegates the decision of whether to stop in a call frame to
  298. that call frame's language-specific personality function. Not all unwinders
  299. guarantee that they will stop to perform cleanups. For example, the GNU C++
  300. unwinder doesn't do so unless the exception is actually caught somewhere
  301. further up the stack.</p>
  302. <p>In order for inlining to behave correctly, landing pads must be prepared to
  303. handle selector results that they did not originally advertise. Suppose that
  304. a function catches exceptions of type <tt>A</tt>, and it's inlined into a
  305. function that catches exceptions of type <tt>B</tt>. The inliner will update
  306. the <tt>landingpad</tt> instruction for the inlined landing pad to include
  307. the fact that <tt>B</tt> is also caught. If that landing pad assumes that it
  308. will only be entered to catch an <tt>A</tt>, it's in for a rude awakening.
  309. Consequently, landing pads must test for the selector results they understand
  310. and then resume exception propagation with the
  311. <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a> if none of
  312. the conditions match.</p>
  313. </div>
  314. </div>
  315. <!-- ======================================================================= -->
  316. <h2>
  317. <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
  318. </h2>
  319. <div>
  320. <p>In addition to the
  321. <a href="LangRef.html#i_landingpad"><tt>landingpad</tt></a> and
  322. <a href="LangRef.html#i_resume"><tt>resume</tt></a> instructions, LLVM uses
  323. several intrinsic functions (name prefixed with <i><tt>llvm.eh</tt></i>) to
  324. provide exception handling information at various points in generated
  325. code.</p>
  326. <!-- ======================================================================= -->
  327. <h4>
  328. <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
  329. </h4>
  330. <div>
  331. <pre>
  332. i32 @llvm.eh.typeid.for(i8* %type_info)
  333. </pre>
  334. <p>This intrinsic returns the type info index in the exception table of the
  335. current function. This value can be used to compare against the result
  336. of <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>.
  337. The single argument is a reference to a type info.</p>
  338. </div>
  339. <!-- ======================================================================= -->
  340. <h4>
  341. <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
  342. </h4>
  343. <div>
  344. <pre>
  345. i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
  346. </pre>
  347. <p>For SJLJ based exception handling, this intrinsic forces register saving for
  348. the current function and stores the address of the following instruction for
  349. use as a destination address
  350. by <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>. The
  351. buffer format and the overall functioning of this intrinsic is compatible
  352. with the GCC <tt>__builtin_setjmp</tt> implementation allowing code built
  353. with the clang and GCC to interoperate.</p>
  354. <p>The single parameter is a pointer to a five word buffer in which the calling
  355. context is saved. The front end places the frame pointer in the first word,
  356. and the target implementation of this intrinsic should place the destination
  357. address for a
  358. <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
  359. second word. The following three words are available for use in a
  360. target-specific manner.</p>
  361. </div>
  362. <!-- ======================================================================= -->
  363. <h4>
  364. <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
  365. </h4>
  366. <div>
  367. <pre>
  368. void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
  369. </pre>
  370. <p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.longjmp</tt>
  371. intrinsic is used to implement <tt>__builtin_longjmp()</tt>. The single
  372. parameter is a pointer to a buffer populated
  373. by <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a>. The frame
  374. pointer and stack pointer are restored from the buffer, then control is
  375. transferred to the destination address.</p>
  376. </div>
  377. <!-- ======================================================================= -->
  378. <h4>
  379. <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
  380. </h4>
  381. <div>
  382. <pre>
  383. i8* @llvm.eh.sjlj.lsda()
  384. </pre>
  385. <p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.lsda</tt> intrinsic
  386. returns the address of the Language Specific Data Area (LSDA) for the current
  387. function. The SJLJ front-end code stores this address in the exception
  388. handling function context for use by the runtime.</p>
  389. </div>
  390. <!-- ======================================================================= -->
  391. <h4>
  392. <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
  393. </h4>
  394. <div>
  395. <pre>
  396. void @llvm.eh.sjlj.callsite(i32 %call_site_num)
  397. </pre>
  398. <p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.callsite</tt>
  399. intrinsic identifies the callsite value associated with the
  400. following <tt>invoke</tt> instruction. This is used to ensure that landing
  401. pad entries in the LSDA are generated in matching order.</p>
  402. </div>
  403. <!-- ======================================================================= -->
  404. <h4>
  405. <a name="llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>
  406. </h4>
  407. <div>
  408. <pre>
  409. void @llvm.eh.sjlj.dispatchsetup(i32 %dispatch_value)
  410. </pre>
  411. <p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.dispatchsetup</tt>
  412. intrinsic is used by targets to do any unwind edge setup they need. By
  413. default, no action is taken.</p>
  414. </div>
  415. </div>
  416. <!-- ======================================================================= -->
  417. <h2>
  418. <a name="asm">Asm Table Formats</a>
  419. </h2>
  420. <div>
  421. <p>There are two tables that are used by the exception handling runtime to
  422. determine which actions should be taken when an exception is thrown.</p>
  423. <!-- ======================================================================= -->
  424. <h3>
  425. <a name="unwind_tables">Exception Handling Frame</a>
  426. </h3>
  427. <div>
  428. <p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
  429. frame used by DWARF debug info. The frame contains all the information
  430. necessary to tear down the current frame and restore the state of the prior
  431. frame. There is an exception handling frame for each function in a compile
  432. unit, plus a common exception handling frame that defines information common
  433. to all functions in the unit.</p>
  434. <!-- Todo - Table details here. -->
  435. </div>
  436. <!-- ======================================================================= -->
  437. <h3>
  438. <a name="exception_tables">Exception Tables</a>
  439. </h3>
  440. <div>
  441. <p>An exception table contains information about what actions to take when an
  442. exception is thrown in a particular part of a function's code. There is one
  443. exception table per function, except leaf functions and functions that have
  444. calls only to non-throwing functions. They do not need an exception
  445. table.</p>
  446. <!-- Todo - Table details here. -->
  447. </div>
  448. </div>
  449. <!-- *********************************************************************** -->
  450. <hr>
  451. <address>
  452. <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
  453. src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
  454. <a href="http://validator.w3.org/check/referer"><img
  455. src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
  456. <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
  457. <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
  458. Last modified: $Date: 2011-09-28 04:16:57 +0800 (Wed, 28 Sep 2011) $
  459. </address>
  460. </body>
  461. </html>