WHOPR.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: WHOPR</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: WHOPR">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: WHOPR">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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="LTO.html#LTO" rel="up" title="LTO">
  30. <link href="Internal-flags.html#Internal-flags" rel="next" title="Internal flags">
  31. <link href="IPA.html#IPA" rel="prev" title="IPA">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="WHOPR"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Internal-flags.html#Internal-flags" accesskey="n" rel="next">Internal flags</a>, Previous: <a href="IPA.html#IPA" accesskey="p" rel="prev">IPA</a>, Up: <a href="LTO.html#LTO" accesskey="u" rel="up">LTO</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>
  66. </div>
  67. <hr>
  68. <a name="Whole-program-assumptions_002c-linker-plugin-and-symbol-visibilities"></a>
  69. <h3 class="section">24.4 Whole program assumptions, linker plugin and symbol visibilities</h3>
  70. <p>Link-time optimization gives relatively minor benefits when used
  71. alone. The problem is that propagation of inter-procedural
  72. information does not work well across functions and variables
  73. that are called or referenced by other compilation units (such as
  74. from a dynamically linked library). We say that such functions
  75. and variables are <em>externally visible</em>.
  76. </p>
  77. <p>To make the situation even more difficult, many applications
  78. organize themselves as a set of shared libraries, and the default
  79. ELF visibility rules allow one to overwrite any externally
  80. visible symbol with a different symbol at runtime. This
  81. basically disables any optimizations across such functions and
  82. variables, because the compiler cannot be sure that the function
  83. body it is seeing is the same function body that will be used at
  84. runtime. Any function or variable not declared <code>static</code> in
  85. the sources degrades the quality of inter-procedural
  86. optimization.
  87. </p>
  88. <p>To avoid this problem the compiler must assume that it sees the
  89. whole program when doing link-time optimization. Strictly
  90. speaking, the whole program is rarely visible even at link-time.
  91. Standard system libraries are usually linked dynamically or not
  92. provided with the link-time information. In GCC, the whole
  93. program option (<samp>-fwhole-program</samp>) asserts that every
  94. function and variable defined in the current compilation
  95. unit is static, except for function <code>main</code> (note: at
  96. link time, the current unit is the union of all objects compiled
  97. with LTO). Since some functions and variables need to
  98. be referenced externally, for example by another DSO or from an
  99. assembler file, GCC also provides the function and variable
  100. attribute <code>externally_visible</code> which can be used to disable
  101. the effect of <samp>-fwhole-program</samp> on a specific symbol.
  102. </p>
  103. <p>The whole program mode assumptions are slightly more complex in
  104. C++, where inline functions in headers are put into <em>COMDAT</em>
  105. sections. COMDAT function and variables can be defined by
  106. multiple object files and their bodies are unified at link-time
  107. and dynamic link-time. COMDAT functions are changed to local only
  108. when their address is not taken and thus un-sharing them with a
  109. library is not harmful. COMDAT variables always remain externally
  110. visible, however for readonly variables it is assumed that their
  111. initializers cannot be overwritten by a different value.
  112. </p>
  113. <p>GCC provides the function and variable attribute
  114. <code>visibility</code> that can be used to specify the visibility of
  115. externally visible symbols (or alternatively an
  116. <samp>-fdefault-visibility</samp> command line option). ELF defines
  117. the <code>default</code>, <code>protected</code>, <code>hidden</code> and
  118. <code>internal</code> visibilities.
  119. </p>
  120. <p>The most commonly used is visibility is <code>hidden</code>. It
  121. specifies that the symbol cannot be referenced from outside of
  122. the current shared library. Unfortunately, this information
  123. cannot be used directly by the link-time optimization in the
  124. compiler since the whole shared library also might contain
  125. non-LTO objects and those are not visible to the compiler.
  126. </p>
  127. <p>GCC solves this problem using linker plugins. A <em>linker
  128. plugin</em> is an interface to the linker that allows an external
  129. program to claim the ownership of a given object file. The linker
  130. then performs the linking procedure by querying the plugin about
  131. the symbol table of the claimed objects and once the linking
  132. decisions are complete, the plugin is allowed to provide the
  133. final object file before the actual linking is made. The linker
  134. plugin obtains the symbol resolution information which specifies
  135. which symbols provided by the claimed objects are bound from the
  136. rest of a binary being linked.
  137. </p>
  138. <p>GCC is designed to be independent of the rest of the toolchain
  139. and aims to support linkers without plugin support. For this
  140. reason it does not use the linker plugin by default. Instead,
  141. the object files are examined by <code>collect2</code> before being
  142. passed to the linker and objects found to have LTO sections are
  143. passed to <code>lto1</code> first. This mode does not work for
  144. library archives. The decision on what object files from the
  145. archive are needed depends on the actual linking and thus GCC
  146. would have to implement the linker itself. The resolution
  147. information is missing too and thus GCC needs to make an educated
  148. guess based on <samp>-fwhole-program</samp>. Without the linker
  149. plugin GCC also assumes that symbols are declared <code>hidden</code>
  150. and not referred by non-LTO code by default.
  151. </p>
  152. <hr>
  153. <div class="header">
  154. <p>
  155. Next: <a href="Internal-flags.html#Internal-flags" accesskey="n" rel="next">Internal flags</a>, Previous: <a href="IPA.html#IPA" accesskey="p" rel="prev">IPA</a>, Up: <a href="LTO.html#LTO" accesskey="u" rel="up">LTO</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>
  156. </div>
  157. </body>
  158. </html>