WIN32.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the GNU linker LD
  4. (GNU Binutils)
  5. version 2.28.
  6. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  7. Permission is granted to copy, distribute and/or modify this document
  8. under the terms of the GNU Free Documentation License, Version 1.3
  9. or any later version published by the Free Software Foundation;
  10. with no Invariant Sections, with no Front-Cover Texts, and with no
  11. Back-Cover Texts. A copy of the license is included in the
  12. section entitled "GNU Free Documentation License". -->
  13. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>LD: WIN32</title>
  16. <meta name="description" content="LD: WIN32">
  17. <meta name="keywords" content="LD: WIN32">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Machine-Dependent.html#Machine-Dependent" rel="up" title="Machine Dependent">
  26. <link href="Xtensa.html#Xtensa" rel="next" title="Xtensa">
  27. <link href="TI-COFF.html#TI-COFF" rel="prev" title="TI COFF">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="WIN32"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Xtensa.html#Xtensa" accesskey="n" rel="next">Xtensa</a>, Previous: <a href="TI-COFF.html#TI-COFF" accesskey="p" rel="prev">TI COFF</a>, Up: <a href="Machine-Dependent.html#Machine-Dependent" accesskey="u" rel="up">Machine Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="ld-and-WIN32-_0028cygwin_002fmingw_0029"></a>
  65. <h3 class="section">4.16 <code>ld</code> and WIN32 (cygwin/mingw)</h3>
  66. <p>This section describes some of the win32 specific <code>ld</code> issues.
  67. See <a href="Options.html#Options">Command Line Options</a> for detailed description of the
  68. command line options mentioned here.
  69. </p>
  70. <dl compact="compact">
  71. <dd><a name="index-import-libraries"></a>
  72. </dd>
  73. <dt><em>import libraries</em></dt>
  74. <dd><p>The standard Windows linker creates and uses so-called import
  75. libraries, which contains information for linking to dll&rsquo;s. They are
  76. regular static archives and are handled as any other static
  77. archive. The cygwin and mingw ports of <code>ld</code> have specific
  78. support for creating such libraries provided with the
  79. &lsquo;<samp>--out-implib</samp>&rsquo; command line option.
  80. </p>
  81. </dd>
  82. <dt><em>exporting DLL symbols</em></dt>
  83. <dd><a name="index-exporting-DLL-symbols"></a>
  84. <p>The cygwin/mingw <code>ld</code> has several ways to export symbols for dll&rsquo;s.
  85. </p>
  86. <dl compact="compact">
  87. <dt><em>using auto-export functionality</em></dt>
  88. <dd><a name="index-using-auto_002dexport-functionality"></a>
  89. <p>By default <code>ld</code> exports symbols with the auto-export functionality,
  90. which is controlled by the following command line options:
  91. </p>
  92. <ul>
  93. <li> &ndash;export-all-symbols [This is the default]
  94. </li><li> &ndash;exclude-symbols
  95. </li><li> &ndash;exclude-libs
  96. </li><li> &ndash;exclude-modules-for-implib
  97. </li><li> &ndash;version-script
  98. </li></ul>
  99. <p>When auto-export is in operation, <code>ld</code> will export all the non-local
  100. (global and common) symbols it finds in a DLL, with the exception of a few
  101. symbols known to belong to the system&rsquo;s runtime and libraries. As it will
  102. often not be desirable to export all of a DLL&rsquo;s symbols, which may include
  103. private functions that are not part of any public interface, the command-line
  104. options listed above may be used to filter symbols out from the list for
  105. exporting. The &lsquo;<samp>--output-def</samp>&rsquo; option can be used in order to see the
  106. final list of exported symbols with all exclusions taken into effect.
  107. </p>
  108. <p>If &lsquo;<samp>--export-all-symbols</samp>&rsquo; is not given explicitly on the
  109. command line, then the default auto-export behavior will be <em>disabled</em>
  110. if either of the following are true:
  111. </p>
  112. <ul>
  113. <li> A DEF file is used.
  114. </li><li> Any symbol in any object file was marked with the __declspec(dllexport) attribute.
  115. </li></ul>
  116. </dd>
  117. <dt><em>using a DEF file</em></dt>
  118. <dd><a name="index-using-a-DEF-file"></a>
  119. <p>Another way of exporting symbols is using a DEF file. A DEF file is
  120. an ASCII file containing definitions of symbols which should be
  121. exported when a dll is created. Usually it is named &lsquo;<samp>&lt;dll
  122. name&gt;.def</samp>&rsquo; and is added as any other object file to the linker&rsquo;s
  123. command line. The file&rsquo;s name must end in &lsquo;<samp>.def</samp>&rsquo; or &lsquo;<samp>.DEF</samp>&rsquo;.
  124. </p>
  125. <div class="example">
  126. <pre class="example">gcc -o &lt;output&gt; &lt;objectfiles&gt; &lt;dll name&gt;.def
  127. </pre></div>
  128. <p>Using a DEF file turns off the normal auto-export behavior, unless the
  129. &lsquo;<samp>--export-all-symbols</samp>&rsquo; option is also used.
  130. </p>
  131. <p>Here is an example of a DEF file for a shared library called &lsquo;<samp>xyz.dll</samp>&rsquo;:
  132. </p>
  133. <div class="example">
  134. <pre class="example">LIBRARY &quot;xyz.dll&quot; BASE=0x20000000
  135. EXPORTS
  136. foo
  137. bar
  138. _bar = bar
  139. another_foo = abc.dll.afoo
  140. var1 DATA
  141. doo = foo == foo2
  142. eoo DATA == var1
  143. </pre></div>
  144. <p>This example defines a DLL with a non-default base address and seven
  145. symbols in the export table. The third exported symbol <code>_bar</code> is an
  146. alias for the second. The fourth symbol, <code>another_foo</code> is resolved
  147. by &quot;forwarding&quot; to another module and treating it as an alias for
  148. <code>afoo</code> exported from the DLL &lsquo;<samp>abc.dll</samp>&rsquo;. The final symbol
  149. <code>var1</code> is declared to be a data object. The &lsquo;<samp>doo</samp>&rsquo; symbol in
  150. export library is an alias of &lsquo;<samp>foo</samp>&rsquo;, which gets the string name
  151. in export table &lsquo;<samp>foo2</samp>&rsquo;. The &lsquo;<samp>eoo</samp>&rsquo; symbol is an data export
  152. symbol, which gets in export table the name &lsquo;<samp>var1</samp>&rsquo;.
  153. </p>
  154. <p>The optional <code>LIBRARY &lt;name&gt;</code> command indicates the <em>internal</em>
  155. name of the output DLL. If &lsquo;<samp>&lt;name&gt;</samp>&rsquo; does not include a suffix,
  156. the default library suffix, &lsquo;<samp>.DLL</samp>&rsquo; is appended.
  157. </p>
  158. <p>When the .DEF file is used to build an application, rather than a
  159. library, the <code>NAME &lt;name&gt;</code> command should be used instead of
  160. <code>LIBRARY</code>. If &lsquo;<samp>&lt;name&gt;</samp>&rsquo; does not include a suffix, the default
  161. executable suffix, &lsquo;<samp>.EXE</samp>&rsquo; is appended.
  162. </p>
  163. <p>With either <code>LIBRARY &lt;name&gt;</code> or <code>NAME &lt;name&gt;</code> the optional
  164. specification <code>BASE = &lt;number&gt;</code> may be used to specify a
  165. non-default base address for the image.
  166. </p>
  167. <p>If neither <code>LIBRARY &lt;name&gt;</code> nor <code>NAME &lt;name&gt;</code> is specified,
  168. or they specify an empty string, the internal name is the same as the
  169. filename specified on the command line.
  170. </p>
  171. <p>The complete specification of an export symbol is:
  172. </p>
  173. <div class="example">
  174. <pre class="example">EXPORTS
  175. ( ( ( &lt;name1&gt; [ = &lt;name2&gt; ] )
  176. | ( &lt;name1&gt; = &lt;module-name&gt; . &lt;external-name&gt;))
  177. [ @ &lt;integer&gt; ] [NONAME] [DATA] [CONSTANT] [PRIVATE] [== &lt;name3&gt;] ) *
  178. </pre></div>
  179. <p>Declares &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as an exported symbol from the DLL, or declares
  180. &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as an exported alias for &lsquo;<samp>&lt;name2&gt;</samp>&rsquo;; or declares
  181. &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as a &quot;forward&quot; alias for the symbol
  182. &lsquo;<samp>&lt;external-name&gt;</samp>&rsquo; in the DLL &lsquo;<samp>&lt;module-name&gt;</samp>&rsquo;.
  183. Optionally, the symbol may be exported by the specified ordinal
  184. &lsquo;<samp>&lt;integer&gt;</samp>&rsquo; alias. The optional &lsquo;<samp>&lt;name3&gt;</samp>&rsquo; is the to be used
  185. string in import/export table for the symbol.
  186. </p>
  187. <p>The optional keywords that follow the declaration indicate:
  188. </p>
  189. <p><code>NONAME</code>: Do not put the symbol name in the DLL&rsquo;s export table. It
  190. will still be exported by its ordinal alias (either the value specified
  191. by the .def specification or, otherwise, the value assigned by the
  192. linker). The symbol name, however, does remain visible in the import
  193. library (if any), unless <code>PRIVATE</code> is also specified.
  194. </p>
  195. <p><code>DATA</code>: The symbol is a variable or object, rather than a function.
  196. The import lib will export only an indirect reference to <code>foo</code> as
  197. the symbol <code>_imp__foo</code> (ie, <code>foo</code> must be resolved as
  198. <code>*_imp__foo</code>).
  199. </p>
  200. <p><code>CONSTANT</code>: Like <code>DATA</code>, but put the undecorated <code>foo</code> as
  201. well as <code>_imp__foo</code> into the import library. Both refer to the
  202. read-only import address table&rsquo;s pointer to the variable, not to the
  203. variable itself. This can be dangerous. If the user code fails to add
  204. the <code>dllimport</code> attribute and also fails to explicitly add the
  205. extra indirection that the use of the attribute enforces, the
  206. application will behave unexpectedly.
  207. </p>
  208. <p><code>PRIVATE</code>: Put the symbol in the DLL&rsquo;s export table, but do not put
  209. it into the static import library used to resolve imports at link time. The
  210. symbol can still be imported using the <code>LoadLibrary/GetProcAddress</code>
  211. API at runtime or by by using the GNU ld extension of linking directly to
  212. the DLL without an import library.
  213. </p>
  214. <p>See ld/deffilep.y in the binutils sources for the full specification of
  215. other DEF file statements
  216. </p>
  217. <a name="index-creating-a-DEF-file"></a>
  218. <p>While linking a shared dll, <code>ld</code> is able to create a DEF file
  219. with the &lsquo;<samp>--output-def &lt;file&gt;</samp>&rsquo; command line option.
  220. </p>
  221. </dd>
  222. <dt><em>Using decorations</em></dt>
  223. <dd><a name="index-Using-decorations"></a>
  224. <p>Another way of marking symbols for export is to modify the source code
  225. itself, so that when building the DLL each symbol to be exported is
  226. declared as:
  227. </p>
  228. <div class="example">
  229. <pre class="example">__declspec(dllexport) int a_variable
  230. __declspec(dllexport) void a_function(int with_args)
  231. </pre></div>
  232. <p>All such symbols will be exported from the DLL. If, however,
  233. any of the object files in the DLL contain symbols decorated in
  234. this way, then the normal auto-export behavior is disabled, unless
  235. the &lsquo;<samp>--export-all-symbols</samp>&rsquo; option is also used.
  236. </p>
  237. <p>Note that object files that wish to access these symbols must <em>not</em>
  238. decorate them with dllexport. Instead, they should use dllimport,
  239. instead:
  240. </p>
  241. <div class="example">
  242. <pre class="example">__declspec(dllimport) int a_variable
  243. __declspec(dllimport) void a_function(int with_args)
  244. </pre></div>
  245. <p>This complicates the structure of library header files, because
  246. when included by the library itself the header must declare the
  247. variables and functions as dllexport, but when included by client
  248. code the header must declare them as dllimport. There are a number
  249. of idioms that are typically used to do this; often client code can
  250. omit the __declspec() declaration completely. See
  251. &lsquo;<samp>--enable-auto-import</samp>&rsquo; and &lsquo;<samp>automatic data imports</samp>&rsquo; for more
  252. information.
  253. </p></dd>
  254. </dl>
  255. <a name="index-automatic-data-imports"></a>
  256. </dd>
  257. <dt><em>automatic data imports</em></dt>
  258. <dd><p>The standard Windows dll format supports data imports from dlls only
  259. by adding special decorations (dllimport/dllexport), which let the
  260. compiler produce specific assembler instructions to deal with this
  261. issue. This increases the effort necessary to port existing Un*x
  262. code to these platforms, especially for large
  263. c++ libraries and applications. The auto-import feature, which was
  264. initially provided by Paul Sokolovsky, allows one to omit the
  265. decorations to achieve a behavior that conforms to that on POSIX/Un*x
  266. platforms. This feature is enabled with the &lsquo;<samp>--enable-auto-import</samp>&rsquo;
  267. command-line option, although it is enabled by default on cygwin/mingw.
  268. The &lsquo;<samp>--enable-auto-import</samp>&rsquo; option itself now serves mainly to
  269. suppress any warnings that are ordinarily emitted when linked objects
  270. trigger the feature&rsquo;s use.
  271. </p>
  272. <p>auto-import of variables does not always work flawlessly without
  273. additional assistance. Sometimes, you will see this message
  274. </p>
  275. <p>&quot;variable &rsquo;&lt;var&gt;&rsquo; can&rsquo;t be auto-imported. Please read the
  276. documentation for ld&rsquo;s <code>--enable-auto-import</code> for details.&quot;
  277. </p>
  278. <p>The &lsquo;<samp>--enable-auto-import</samp>&rsquo; documentation explains why this error
  279. occurs, and several methods that can be used to overcome this difficulty.
  280. One of these methods is the <em>runtime pseudo-relocs</em> feature, described
  281. below.
  282. </p>
  283. <a name="index-runtime-pseudo_002drelocation"></a>
  284. <p>For complex variables imported from DLLs (such as structs or classes),
  285. object files typically contain a base address for the variable and an
  286. offset (<em>addend</em>) within the variable&ndash;to specify a particular
  287. field or public member, for instance. Unfortunately, the runtime loader used
  288. in win32 environments is incapable of fixing these references at runtime
  289. without the additional information supplied by dllimport/dllexport decorations.
  290. The standard auto-import feature described above is unable to resolve these
  291. references.
  292. </p>
  293. <p>The &lsquo;<samp>--enable-runtime-pseudo-relocs</samp>&rsquo; switch allows these references to
  294. be resolved without error, while leaving the task of adjusting the references
  295. themselves (with their non-zero addends) to specialized code provided by the
  296. runtime environment. Recent versions of the cygwin and mingw environments and
  297. compilers provide this runtime support; older versions do not. However, the
  298. support is only necessary on the developer&rsquo;s platform; the compiled result will
  299. run without error on an older system.
  300. </p>
  301. <p>&lsquo;<samp>--enable-runtime-pseudo-relocs</samp>&rsquo; is not the default; it must be explicitly
  302. enabled as needed.
  303. </p>
  304. <a name="index-direct-linking-to-a-dll"></a>
  305. </dd>
  306. <dt><em>direct linking to a dll</em></dt>
  307. <dd><p>The cygwin/mingw ports of <code>ld</code> support the direct linking,
  308. including data symbols, to a dll without the usage of any import
  309. libraries. This is much faster and uses much less memory than does the
  310. traditional import library method, especially when linking large
  311. libraries or applications. When <code>ld</code> creates an import lib, each
  312. function or variable exported from the dll is stored in its own bfd, even
  313. though a single bfd could contain many exports. The overhead involved in
  314. storing, loading, and processing so many bfd&rsquo;s is quite large, and explains the
  315. tremendous time, memory, and storage needed to link against particularly
  316. large or complex libraries when using import libs.
  317. </p>
  318. <p>Linking directly to a dll uses no extra command-line switches other than
  319. &lsquo;<samp>-L</samp>&rsquo; and &lsquo;<samp>-l</samp>&rsquo;, because <code>ld</code> already searches for a number
  320. of names to match each library. All that is needed from the developer&rsquo;s
  321. perspective is an understanding of this search, in order to force ld to
  322. select the dll instead of an import library.
  323. </p>
  324. <p>For instance, when ld is called with the argument &lsquo;<samp>-lxxx</samp>&rsquo; it will attempt
  325. to find, in the first directory of its search path,
  326. </p>
  327. <div class="example">
  328. <pre class="example">libxxx.dll.a
  329. xxx.dll.a
  330. libxxx.a
  331. xxx.lib
  332. cygxxx.dll (*)
  333. libxxx.dll
  334. xxx.dll
  335. </pre></div>
  336. <p>before moving on to the next directory in the search path.
  337. </p>
  338. <p>(*) Actually, this is not &lsquo;<samp>cygxxx.dll</samp>&rsquo; but in fact is &lsquo;<samp>&lt;prefix&gt;xxx.dll</samp>&rsquo;,
  339. where &lsquo;<samp>&lt;prefix&gt;</samp>&rsquo; is set by the <code>ld</code> option
  340. &lsquo;<samp>--dll-search-prefix=&lt;prefix&gt;</samp>&rsquo;. In the case of cygwin, the standard gcc spec
  341. file includes &lsquo;<samp>--dll-search-prefix=cyg</samp>&rsquo;, so in effect we actually search for
  342. &lsquo;<samp>cygxxx.dll</samp>&rsquo;.
  343. </p>
  344. <p>Other win32-based unix environments, such as mingw or pw32, may use other
  345. &lsquo;<samp>&lt;prefix&gt;</samp>&rsquo;es, although at present only cygwin makes use of this feature. It
  346. was originally intended to help avoid name conflicts among dll&rsquo;s built for the
  347. various win32/un*x environments, so that (for example) two versions of a zlib dll
  348. could coexist on the same machine.
  349. </p>
  350. <p>The generic cygwin/mingw path layout uses a &lsquo;<samp>bin</samp>&rsquo; directory for
  351. applications and dll&rsquo;s and a &lsquo;<samp>lib</samp>&rsquo; directory for the import
  352. libraries (using cygwin nomenclature):
  353. </p>
  354. <div class="example">
  355. <pre class="example">bin/
  356. cygxxx.dll
  357. lib/
  358. libxxx.dll.a (in case of dll's)
  359. libxxx.a (in case of static archive)
  360. </pre></div>
  361. <p>Linking directly to a dll without using the import library can be
  362. done two ways:
  363. </p>
  364. <p>1. Use the dll directly by adding the &lsquo;<samp>bin</samp>&rsquo; path to the link line
  365. </p><div class="example">
  366. <pre class="example">gcc -Wl,-verbose -o a.exe -L../bin/ -lxxx
  367. </pre></div>
  368. <p>However, as the dll&rsquo;s often have version numbers appended to their names
  369. (&lsquo;<samp>cygncurses-5.dll</samp>&rsquo;) this will often fail, unless one specifies
  370. &lsquo;<samp>-L../bin -lncurses-5</samp>&rsquo; to include the version. Import libs are generally
  371. not versioned, and do not have this difficulty.
  372. </p>
  373. <p>2. Create a symbolic link from the dll to a file in the &lsquo;<samp>lib</samp>&rsquo;
  374. directory according to the above mentioned search pattern. This
  375. should be used to avoid unwanted changes in the tools needed for
  376. making the app/dll.
  377. </p>
  378. <div class="example">
  379. <pre class="example">ln -s bin/cygxxx.dll lib/[cyg|lib|]xxx.dll[.a]
  380. </pre></div>
  381. <p>Then you can link without any make environment changes.
  382. </p>
  383. <div class="example">
  384. <pre class="example">gcc -Wl,-verbose -o a.exe -L../lib/ -lxxx
  385. </pre></div>
  386. <p>This technique also avoids the version number problems, because the following is
  387. perfectly legal
  388. </p>
  389. <div class="example">
  390. <pre class="example">bin/
  391. cygxxx-5.dll
  392. lib/
  393. libxxx.dll.a -&gt; ../bin/cygxxx-5.dll
  394. </pre></div>
  395. <p>Linking directly to a dll without using an import lib will work
  396. even when auto-import features are exercised, and even when
  397. &lsquo;<samp>--enable-runtime-pseudo-relocs</samp>&rsquo; is used.
  398. </p>
  399. <p>Given the improvements in speed and memory usage, one might justifiably
  400. wonder why import libraries are used at all. There are three reasons:
  401. </p>
  402. <p>1. Until recently, the link-directly-to-dll functionality did <em>not</em>
  403. work with auto-imported data.
  404. </p>
  405. <p>2. Sometimes it is necessary to include pure static objects within the
  406. import library (which otherwise contains only bfd&rsquo;s for indirection
  407. symbols that point to the exports of a dll). Again, the import lib
  408. for the cygwin kernel makes use of this ability, and it is not
  409. possible to do this without an import lib.
  410. </p>
  411. <p>3. Symbol aliases can only be resolved using an import lib. This is
  412. critical when linking against OS-supplied dll&rsquo;s (eg, the win32 API)
  413. in which symbols are usually exported as undecorated aliases of their
  414. stdcall-decorated assembly names.
  415. </p>
  416. <p>So, import libs are not going away. But the ability to replace
  417. true import libs with a simple symbolic link to (or a copy of)
  418. a dll, in many cases, is a useful addition to the suite of tools
  419. binutils makes available to the win32 developer. Given the
  420. massive improvements in memory requirements during linking, storage
  421. requirements, and linking speed, we expect that many developers
  422. will soon begin to use this feature whenever possible.
  423. </p>
  424. </dd>
  425. <dt><em>symbol aliasing</em></dt>
  426. <dd><dl compact="compact">
  427. <dt><em>adding additional names</em></dt>
  428. <dd><p>Sometimes, it is useful to export symbols with additional names.
  429. A symbol &lsquo;<samp>foo</samp>&rsquo; will be exported as &lsquo;<samp>foo</samp>&rsquo;, but it can also be
  430. exported as &lsquo;<samp>_foo</samp>&rsquo; by using special directives in the DEF file
  431. when creating the dll. This will affect also the optional created
  432. import library. Consider the following DEF file:
  433. </p>
  434. <div class="example">
  435. <pre class="example">LIBRARY &quot;xyz.dll&quot; BASE=0x61000000
  436. EXPORTS
  437. foo
  438. _foo = foo
  439. </pre></div>
  440. <p>The line &lsquo;<samp>_foo = foo</samp>&rsquo; maps the symbol &lsquo;<samp>foo</samp>&rsquo; to &lsquo;<samp>_foo</samp>&rsquo;.
  441. </p>
  442. <p>Another method for creating a symbol alias is to create it in the
  443. source code using the &quot;weak&quot; attribute:
  444. </p>
  445. <div class="example">
  446. <pre class="example">void foo () { /* Do something. */; }
  447. void _foo () __attribute__ ((weak, alias (&quot;foo&quot;)));
  448. </pre></div>
  449. <p>See the gcc manual for more information about attributes and weak
  450. symbols.
  451. </p>
  452. </dd>
  453. <dt><em>renaming symbols</em></dt>
  454. <dd><p>Sometimes it is useful to rename exports. For instance, the cygwin
  455. kernel does this regularly. A symbol &lsquo;<samp>_foo</samp>&rsquo; can be exported as
  456. &lsquo;<samp>foo</samp>&rsquo; but not as &lsquo;<samp>_foo</samp>&rsquo; by using special directives in the
  457. DEF file. (This will also affect the import library, if it is
  458. created). In the following example:
  459. </p>
  460. <div class="example">
  461. <pre class="example">LIBRARY &quot;xyz.dll&quot; BASE=0x61000000
  462. EXPORTS
  463. _foo = foo
  464. </pre></div>
  465. <p>The line &lsquo;<samp>_foo = foo</samp>&rsquo; maps the exported symbol &lsquo;<samp>foo</samp>&rsquo; to
  466. &lsquo;<samp>_foo</samp>&rsquo;.
  467. </p></dd>
  468. </dl>
  469. <p>Note: using a DEF file disables the default auto-export behavior,
  470. unless the &lsquo;<samp>--export-all-symbols</samp>&rsquo; command line option is used.
  471. If, however, you are trying to rename symbols, then you should list
  472. <em>all</em> desired exports in the DEF file, including the symbols
  473. that are not being renamed, and do <em>not</em> use the
  474. &lsquo;<samp>--export-all-symbols</samp>&rsquo; option. If you list only the
  475. renamed symbols in the DEF file, and use &lsquo;<samp>--export-all-symbols</samp>&rsquo;
  476. to handle the other symbols, then the both the new names <em>and</em>
  477. the original names for the renamed symbols will be exported.
  478. In effect, you&rsquo;d be aliasing those symbols, not renaming them,
  479. which is probably not what you wanted.
  480. </p>
  481. <a name="index-weak-externals"></a>
  482. </dd>
  483. <dt><em>weak externals</em></dt>
  484. <dd><p>The Windows object format, PE, specifies a form of weak symbols called
  485. weak externals. When a weak symbol is linked and the symbol is not
  486. defined, the weak symbol becomes an alias for some other symbol. There
  487. are three variants of weak externals:
  488. </p><ul>
  489. <li> Definition is searched for in objects and libraries, historically
  490. called lazy externals.
  491. </li><li> Definition is searched for only in other objects, not in libraries.
  492. This form is not presently implemented.
  493. </li><li> No search; the symbol is an alias. This form is not presently
  494. implemented.
  495. </li></ul>
  496. <p>As a GNU extension, weak symbols that do not specify an alternate symbol
  497. are supported. If the symbol is undefined when linking, the symbol
  498. uses a default value.
  499. </p>
  500. <a name="index-aligned-common-symbols"></a>
  501. </dd>
  502. <dt><em>aligned common symbols</em></dt>
  503. <dd><p>As a GNU extension to the PE file format, it is possible to specify the
  504. desired alignment for a common symbol. This information is conveyed from
  505. the assembler or compiler to the linker by means of GNU-specific commands
  506. carried in the object file&rsquo;s &lsquo;<samp>.drectve</samp>&rsquo; section, which are recognized
  507. by <code>ld</code> and respected when laying out the common symbols. Native
  508. tools will be able to process object files employing this GNU extension,
  509. but will fail to respect the alignment instructions, and may issue noisy
  510. warnings about unknown linker directives.
  511. </p>
  512. </dd>
  513. </dl>
  514. <hr>
  515. <div class="header">
  516. <p>
  517. Next: <a href="Xtensa.html#Xtensa" accesskey="n" rel="next">Xtensa</a>, Previous: <a href="TI-COFF.html#TI-COFF" accesskey="p" rel="prev">TI COFF</a>, Up: <a href="Machine-Dependent.html#Machine-Dependent" accesskey="u" rel="up">Machine Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p>
  518. </div>
  519. </body>
  520. </html>