Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

275 linhas
14KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Trampolines (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Trampolines (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Trampolines (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Target-Macros.html#Target-Macros" rel="up" title="Target Macros">
  30. <link href="Library-Calls.html#Library-Calls" rel="next" title="Library Calls">
  31. <link href="Varargs.html#Varargs" rel="prev" title="Varargs">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Trampolines"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Library-Calls.html#Library-Calls" accesskey="n" rel="next">Library Calls</a>, Previous: <a href="Varargs.html#Varargs" accesskey="p" rel="prev">Varargs</a>, Up: <a href="Target-Macros.html#Target-Macros" accesskey="u" rel="up">Target Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Support-for-Nested-Functions"></a>
  68. <h3 class="section">18.11 Support for Nested Functions</h3>
  69. <a name="index-support-for-nested-functions"></a>
  70. <a name="index-trampolines-for-nested-functions"></a>
  71. <a name="index-descriptors-for-nested-functions"></a>
  72. <a name="index-nested-functions_002c-support-for"></a>
  73. <p>Taking the address of a nested function requires special compiler
  74. handling to ensure that the static chain register is loaded when
  75. the function is invoked via an indirect call.
  76. </p>
  77. <p>GCC has traditionally supported nested functions by creating an
  78. executable <em>trampoline</em> at run time when the address of a nested
  79. function is taken. This is a small piece of code which normally
  80. resides on the stack, in the stack frame of the containing function.
  81. The trampoline loads the static chain register and then jumps to the
  82. real address of the nested function.
  83. </p>
  84. <p>The use of trampolines requires an executable stack, which is a
  85. security risk. To avoid this problem, GCC also supports another
  86. strategy: using descriptors for nested functions. Under this model,
  87. taking the address of a nested function results in a pointer to a
  88. non-executable function descriptor object. Initializing the static chain
  89. from the descriptor is handled at indirect call sites.
  90. </p>
  91. <p>On some targets, including HPPA and IA-64, function descriptors may be
  92. mandated by the ABI or be otherwise handled in a target-specific way
  93. by the back end in its code generation strategy for indirect calls.
  94. GCC also provides its own generic descriptor implementation to support the
  95. <samp>-fno-trampolines</samp> option. In this case runtime detection of
  96. function descriptors at indirect call sites relies on descriptor
  97. pointers being tagged with a bit that is never set in bare function
  98. addresses. Since GCC&rsquo;s generic function descriptors are
  99. not ABI-compliant, this option is typically used only on a
  100. per-language basis (notably by Ada) or when it can otherwise be
  101. applied to the whole program.
  102. </p>
  103. <p>Define the following hook if your backend either implements ABI-specified
  104. descriptor support, or can use GCC&rsquo;s generic descriptor implementation
  105. for nested functions.
  106. </p>
  107. <dl>
  108. <dt><a name="index-TARGET_005fCUSTOM_005fFUNCTION_005fDESCRIPTORS"></a>Target Hook: <em>int</em> <strong>TARGET_CUSTOM_FUNCTION_DESCRIPTORS</strong></dt>
  109. <dd><p>If the target can use GCC&rsquo;s generic descriptor mechanism for nested
  110. functions, define this hook to a power of 2 representing an unused bit
  111. in function pointers which can be used to differentiate descriptors at
  112. run time. This value gives the number of bytes by which descriptor
  113. pointers are misaligned compared to function pointers. For example, on
  114. targets that require functions to be aligned to a 4-byte boundary, a
  115. value of either 1 or 2 is appropriate unless the architecture already
  116. reserves the bit for another purpose, such as on ARM.
  117. </p>
  118. <p>Define this hook to 0 if the target implements ABI support for
  119. function descriptors in its standard calling sequence, like for example
  120. HPPA or IA-64.
  121. </p>
  122. <p>Using descriptors for nested functions
  123. eliminates the need for trampolines that reside on the stack and require
  124. it to be made executable.
  125. </p></dd></dl>
  126. <p>The following macros tell GCC how to generate code to allocate and
  127. initialize an executable trampoline. You can also use this interface
  128. if your back end needs to create ABI-specified non-executable descriptors; in
  129. this case the &quot;trampoline&quot; created is the descriptor containing data only.
  130. </p>
  131. <p>The instructions in an executable trampoline must do two things: load
  132. a constant address into the static chain register, and jump to the real
  133. address of the nested function. On CISC machines such as the m68k,
  134. this requires two instructions, a move immediate and a jump. Then the
  135. two addresses exist in the trampoline as word-long immediate operands.
  136. On RISC machines, it is often necessary to load each address into a
  137. register in two parts. Then pieces of each address form separate
  138. immediate operands.
  139. </p>
  140. <p>The code generated to initialize the trampoline must store the variable
  141. parts&mdash;the static chain value and the function address&mdash;into the
  142. immediate operands of the instructions. On a CISC machine, this is
  143. simply a matter of copying each address to a memory reference at the
  144. proper offset from the start of the trampoline. On a RISC machine, it
  145. may be necessary to take out pieces of the address and store them
  146. separately.
  147. </p>
  148. <dl>
  149. <dt><a name="index-TARGET_005fASM_005fTRAMPOLINE_005fTEMPLATE"></a>Target Hook: <em>void</em> <strong>TARGET_ASM_TRAMPOLINE_TEMPLATE</strong> <em>(FILE *<var>f</var>)</em></dt>
  150. <dd><p>This hook is called by <code>assemble_trampoline_template</code> to output,
  151. on the stream <var>f</var>, assembler code for a block of data that contains
  152. the constant parts of a trampoline. This code should not include a
  153. label&mdash;the label is taken care of automatically.
  154. </p>
  155. <p>If you do not define this hook, it means no template is needed
  156. for the target. Do not define this hook on systems where the block move
  157. code to copy the trampoline into place would be larger than the code
  158. to generate it on the spot.
  159. </p></dd></dl>
  160. <dl>
  161. <dt><a name="index-TRAMPOLINE_005fSECTION"></a>Macro: <strong>TRAMPOLINE_SECTION</strong></dt>
  162. <dd><p>Return the section into which the trampoline template is to be placed
  163. (see <a href="Sections.html#Sections">Sections</a>). The default value is <code>readonly_data_section</code>.
  164. </p></dd></dl>
  165. <dl>
  166. <dt><a name="index-TRAMPOLINE_005fSIZE"></a>Macro: <strong>TRAMPOLINE_SIZE</strong></dt>
  167. <dd><p>A C expression for the size in bytes of the trampoline, as an integer.
  168. </p></dd></dl>
  169. <dl>
  170. <dt><a name="index-TRAMPOLINE_005fALIGNMENT"></a>Macro: <strong>TRAMPOLINE_ALIGNMENT</strong></dt>
  171. <dd><p>Alignment required for trampolines, in bits.
  172. </p>
  173. <p>If you don&rsquo;t define this macro, the value of <code>FUNCTION_ALIGNMENT</code>
  174. is used for aligning trampolines.
  175. </p></dd></dl>
  176. <dl>
  177. <dt><a name="index-TARGET_005fTRAMPOLINE_005fINIT"></a>Target Hook: <em>void</em> <strong>TARGET_TRAMPOLINE_INIT</strong> <em>(rtx <var>m_tramp</var>, tree <var>fndecl</var>, rtx <var>static_chain</var>)</em></dt>
  178. <dd><p>This hook is called to initialize a trampoline.
  179. <var>m_tramp</var> is an RTX for the memory block for the trampoline; <var>fndecl</var>
  180. is the <code>FUNCTION_DECL</code> for the nested function; <var>static_chain</var> is an
  181. RTX for the static chain value that should be passed to the function
  182. when it is called.
  183. </p>
  184. <p>If the target defines <code>TARGET_ASM_TRAMPOLINE_TEMPLATE</code>, then the
  185. first thing this hook should do is emit a block move into <var>m_tramp</var>
  186. from the memory block returned by <code>assemble_trampoline_template</code>.
  187. Note that the block move need only cover the constant parts of the
  188. trampoline. If the target isolates the variable parts of the trampoline
  189. to the end, not all <code>TRAMPOLINE_SIZE</code> bytes need be copied.
  190. </p>
  191. <p>If the target requires any other actions, such as flushing caches or
  192. enabling stack execution, these actions should be performed after
  193. initializing the trampoline proper.
  194. </p></dd></dl>
  195. <dl>
  196. <dt><a name="index-TARGET_005fTRAMPOLINE_005fADJUST_005fADDRESS"></a>Target Hook: <em>rtx</em> <strong>TARGET_TRAMPOLINE_ADJUST_ADDRESS</strong> <em>(rtx <var>addr</var>)</em></dt>
  197. <dd><p>This hook should perform any machine-specific adjustment in
  198. the address of the trampoline. Its argument contains the address of the
  199. memory block that was passed to <code>TARGET_TRAMPOLINE_INIT</code>. In case
  200. the address to be used for a function call should be different from the
  201. address at which the template was stored, the different address should
  202. be returned; otherwise <var>addr</var> should be returned unchanged.
  203. If this hook is not defined, <var>addr</var> will be used for function calls.
  204. </p></dd></dl>
  205. <p>Implementing trampolines is difficult on many machines because they have
  206. separate instruction and data caches. Writing into a stack location
  207. fails to clear the memory in the instruction cache, so when the program
  208. jumps to that location, it executes the old contents.
  209. </p>
  210. <p>Here are two possible solutions. One is to clear the relevant parts of
  211. the instruction cache whenever a trampoline is set up. The other is to
  212. make all trampolines identical, by having them jump to a standard
  213. subroutine. The former technique makes trampoline execution faster; the
  214. latter makes initialization faster.
  215. </p>
  216. <p>To clear the instruction cache when a trampoline is initialized, define
  217. the following macro.
  218. </p>
  219. <dl>
  220. <dt><a name="index-CLEAR_005fINSN_005fCACHE"></a>Macro: <strong>CLEAR_INSN_CACHE</strong> <em>(<var>beg</var>, <var>end</var>)</em></dt>
  221. <dd><p>If defined, expands to a C expression clearing the <em>instruction
  222. cache</em> in the specified interval. The definition of this macro would
  223. typically be a series of <code>asm</code> statements. Both <var>beg</var> and
  224. <var>end</var> are both pointer expressions.
  225. </p></dd></dl>
  226. <p>To use a standard subroutine, define the following macro. In addition,
  227. you must make sure that the instructions in a trampoline fill an entire
  228. cache line with identical instructions, or else ensure that the
  229. beginning of the trampoline code is always aligned at the same point in
  230. its cache line. Look in <samp>m68k.h</samp> as a guide.
  231. </p>
  232. <dl>
  233. <dt><a name="index-TRANSFER_005fFROM_005fTRAMPOLINE"></a>Macro: <strong>TRANSFER_FROM_TRAMPOLINE</strong></dt>
  234. <dd><p>Define this macro if trampolines need a special subroutine to do their
  235. work. The macro should expand to a series of <code>asm</code> statements
  236. which will be compiled with GCC. They go in a library function named
  237. <code>__transfer_from_trampoline</code>.
  238. </p>
  239. <p>If you need to avoid executing the ordinary prologue code of a compiled
  240. C function when you jump to the subroutine, you can do so by placing a
  241. special label of your own in the assembler code. Use one <code>asm</code>
  242. statement to generate an assembler label, and another to make the label
  243. global. Then trampolines can use that label to jump directly to your
  244. special assembler code.
  245. </p></dd></dl>
  246. <hr>
  247. <div class="header">
  248. <p>
  249. Next: <a href="Library-Calls.html#Library-Calls" accesskey="n" rel="next">Library Calls</a>, Previous: <a href="Varargs.html#Varargs" accesskey="p" rel="prev">Varargs</a>, Up: <a href="Target-Macros.html#Target-Macros" accesskey="u" rel="up">Target Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  250. </div>
  251. </body>
  252. </html>