選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

556 行
25KB

  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 Arm Embedded Toolchain 10-2020-q4-major)
  5. version 2.35.1.
  6. Copyright (C) 1991-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>WIN32 (LD)</title>
  17. <meta name="description" content="WIN32 (LD)">
  18. <meta name="keywords" content="WIN32 (LD)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="WIN32"></a>
  58. <div class="header">
  59. <p>
  60. 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>
  61. </div>
  62. <hr>
  63. <a name="ld-and-WIN32-_0028cygwin_002fmingw_0029"></a>
  64. <h3 class="section">4.16 <code>ld</code> and WIN32 (cygwin/mingw)</h3>
  65. <p>This section describes some of the win32 specific <code>ld</code> issues.
  66. See <a href="Options.html#Options">Command-line Options</a> for detailed description of the
  67. command-line options mentioned here.
  68. </p>
  69. <dl compact="compact">
  70. <dd><a name="index-import-libraries"></a>
  71. </dd>
  72. <dt><em>import libraries</em></dt>
  73. <dd><p>The standard Windows linker creates and uses so-called import
  74. libraries, which contains information for linking to dll&rsquo;s. They are
  75. regular static archives and are handled as any other static
  76. archive. The cygwin and mingw ports of <code>ld</code> have specific
  77. support for creating such libraries provided with the
  78. &lsquo;<samp>--out-implib</samp>&rsquo; command-line option.
  79. </p>
  80. </dd>
  81. <dt><em>exporting DLL symbols</em></dt>
  82. <dd><a name="index-exporting-DLL-symbols"></a>
  83. <p>The cygwin/mingw <code>ld</code> has several ways to export symbols for dll&rsquo;s.
  84. </p>
  85. <dl compact="compact">
  86. <dt><em>using auto-export functionality</em></dt>
  87. <dd><a name="index-using-auto_002dexport-functionality"></a>
  88. <p>By default <code>ld</code> exports symbols with the auto-export functionality,
  89. which is controlled by the following command-line options:
  90. </p>
  91. <ul>
  92. <li> &ndash;export-all-symbols [This is the default]
  93. </li><li> &ndash;exclude-symbols
  94. </li><li> &ndash;exclude-libs
  95. </li><li> &ndash;exclude-modules-for-implib
  96. </li><li> &ndash;version-script
  97. </li></ul>
  98. <p>When auto-export is in operation, <code>ld</code> will export all the non-local
  99. (global and common) symbols it finds in a DLL, with the exception of a few
  100. symbols known to belong to the system&rsquo;s runtime and libraries. As it will
  101. often not be desirable to export all of a DLL&rsquo;s symbols, which may include
  102. private functions that are not part of any public interface, the command-line
  103. options listed above may be used to filter symbols out from the list for
  104. exporting. The &lsquo;<samp>--output-def</samp>&rsquo; option can be used in order to see the
  105. final list of exported symbols with all exclusions taken into effect.
  106. </p>
  107. <p>If &lsquo;<samp>--export-all-symbols</samp>&rsquo; is not given explicitly on the
  108. command line, then the default auto-export behavior will be <em>disabled</em>
  109. if either of the following are true:
  110. </p>
  111. <ul>
  112. <li> A DEF file is used.
  113. </li><li> Any symbol in any object file was marked with the __declspec(dllexport) attribute.
  114. </li></ul>
  115. </dd>
  116. <dt><em>using a DEF file</em></dt>
  117. <dd><a name="index-using-a-DEF-file"></a>
  118. <p>Another way of exporting symbols is using a DEF file. A DEF file is
  119. an ASCII file containing definitions of symbols which should be
  120. exported when a dll is created. Usually it is named &lsquo;<samp>&lt;dll
  121. name&gt;.def</samp>&rsquo; and is added as any other object file to the linker&rsquo;s
  122. command line. The file&rsquo;s name must end in &lsquo;<samp>.def</samp>&rsquo; or &lsquo;<samp>.DEF</samp>&rsquo;.
  123. </p>
  124. <div class="example">
  125. <pre class="example">gcc -o &lt;output&gt; &lt;objectfiles&gt; &lt;dll name&gt;.def
  126. </pre></div>
  127. <p>Using a DEF file turns off the normal auto-export behavior, unless the
  128. &lsquo;<samp>--export-all-symbols</samp>&rsquo; option is also used.
  129. </p>
  130. <p>Here is an example of a DEF file for a shared library called &lsquo;<samp>xyz.dll</samp>&rsquo;:
  131. </p>
  132. <div class="example">
  133. <pre class="example">LIBRARY &quot;xyz.dll&quot; BASE=0x20000000
  134. EXPORTS
  135. foo
  136. bar
  137. _bar = bar
  138. another_foo = abc.dll.afoo
  139. var1 DATA
  140. doo = foo == foo2
  141. eoo DATA == var1
  142. </pre></div>
  143. <p>This example defines a DLL with a non-default base address and seven
  144. symbols in the export table. The third exported symbol <code>_bar</code> is an
  145. alias for the second. The fourth symbol, <code>another_foo</code> is resolved
  146. by &quot;forwarding&quot; to another module and treating it as an alias for
  147. <code>afoo</code> exported from the DLL &lsquo;<samp>abc.dll</samp>&rsquo;. The final symbol
  148. <code>var1</code> is declared to be a data object. The &lsquo;<samp>doo</samp>&rsquo; symbol in
  149. export library is an alias of &lsquo;<samp>foo</samp>&rsquo;, which gets the string name
  150. in export table &lsquo;<samp>foo2</samp>&rsquo;. The &lsquo;<samp>eoo</samp>&rsquo; symbol is an data export
  151. symbol, which gets in export table the name &lsquo;<samp>var1</samp>&rsquo;.
  152. </p>
  153. <p>The optional <code>LIBRARY &lt;name&gt;</code> command indicates the <em>internal</em>
  154. name of the output DLL. If &lsquo;<samp>&lt;name&gt;</samp>&rsquo; does not include a suffix,
  155. the default library suffix, &lsquo;<samp>.DLL</samp>&rsquo; is appended.
  156. </p>
  157. <p>When the .DEF file is used to build an application, rather than a
  158. library, the <code>NAME &lt;name&gt;</code> command should be used instead of
  159. <code>LIBRARY</code>. If &lsquo;<samp>&lt;name&gt;</samp>&rsquo; does not include a suffix, the default
  160. executable suffix, &lsquo;<samp>.EXE</samp>&rsquo; is appended.
  161. </p>
  162. <p>With either <code>LIBRARY &lt;name&gt;</code> or <code>NAME &lt;name&gt;</code> the optional
  163. specification <code>BASE = &lt;number&gt;</code> may be used to specify a
  164. non-default base address for the image.
  165. </p>
  166. <p>If neither <code>LIBRARY &lt;name&gt;</code> nor <code>NAME &lt;name&gt;</code> is specified,
  167. or they specify an empty string, the internal name is the same as the
  168. filename specified on the command line.
  169. </p>
  170. <p>The complete specification of an export symbol is:
  171. </p>
  172. <div class="example">
  173. <pre class="example">EXPORTS
  174. ( ( ( &lt;name1&gt; [ = &lt;name2&gt; ] )
  175. | ( &lt;name1&gt; = &lt;module-name&gt; . &lt;external-name&gt;))
  176. [ @ &lt;integer&gt; ] [NONAME] [DATA] [CONSTANT] [PRIVATE] [== &lt;name3&gt;] ) *
  177. </pre></div>
  178. <p>Declares &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as an exported symbol from the DLL, or declares
  179. &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as an exported alias for &lsquo;<samp>&lt;name2&gt;</samp>&rsquo;; or declares
  180. &lsquo;<samp>&lt;name1&gt;</samp>&rsquo; as a &quot;forward&quot; alias for the symbol
  181. &lsquo;<samp>&lt;external-name&gt;</samp>&rsquo; in the DLL &lsquo;<samp>&lt;module-name&gt;</samp>&rsquo;.
  182. Optionally, the symbol may be exported by the specified ordinal
  183. &lsquo;<samp>&lt;integer&gt;</samp>&rsquo; alias. The optional &lsquo;<samp>&lt;name3&gt;</samp>&rsquo; is the to be used
  184. string in import/export table for the symbol.
  185. </p>
  186. <p>The optional keywords that follow the declaration indicate:
  187. </p>
  188. <p><code>NONAME</code>: Do not put the symbol name in the DLL&rsquo;s export table. It
  189. will still be exported by its ordinal alias (either the value specified
  190. by the .def specification or, otherwise, the value assigned by the
  191. linker). The symbol name, however, does remain visible in the import
  192. library (if any), unless <code>PRIVATE</code> is also specified.
  193. </p>
  194. <p><code>DATA</code>: The symbol is a variable or object, rather than a function.
  195. The import lib will export only an indirect reference to <code>foo</code> as
  196. the symbol <code>_imp__foo</code> (ie, <code>foo</code> must be resolved as
  197. <code>*_imp__foo</code>).
  198. </p>
  199. <p><code>CONSTANT</code>: Like <code>DATA</code>, but put the undecorated <code>foo</code> as
  200. well as <code>_imp__foo</code> into the import library. Both refer to the
  201. read-only import address table&rsquo;s pointer to the variable, not to the
  202. variable itself. This can be dangerous. If the user code fails to add
  203. the <code>dllimport</code> attribute and also fails to explicitly add the
  204. extra indirection that the use of the attribute enforces, the
  205. application will behave unexpectedly.
  206. </p>
  207. <p><code>PRIVATE</code>: Put the symbol in the DLL&rsquo;s export table, but do not put
  208. it into the static import library used to resolve imports at link time. The
  209. symbol can still be imported using the <code>LoadLibrary/GetProcAddress</code>
  210. API at runtime or by using the GNU ld extension of linking directly to
  211. the DLL without an import library.
  212. </p>
  213. <p>See ld/deffilep.y in the binutils sources for the full specification of
  214. other DEF file statements
  215. </p>
  216. <a name="index-creating-a-DEF-file"></a>
  217. <p>While linking a shared dll, <code>ld</code> is able to create a DEF file
  218. with the &lsquo;<samp>--output-def &lt;file&gt;</samp>&rsquo; command-line option.
  219. </p>
  220. </dd>
  221. <dt><em>Using decorations</em></dt>
  222. <dd><a name="index-Using-decorations"></a>
  223. <p>Another way of marking symbols for export is to modify the source code
  224. itself, so that when building the DLL each symbol to be exported is
  225. declared as:
  226. </p>
  227. <div class="example">
  228. <pre class="example">__declspec(dllexport) int a_variable
  229. __declspec(dllexport) void a_function(int with_args)
  230. </pre></div>
  231. <p>All such symbols will be exported from the DLL. If, however,
  232. any of the object files in the DLL contain symbols decorated in
  233. this way, then the normal auto-export behavior is disabled, unless
  234. the &lsquo;<samp>--export-all-symbols</samp>&rsquo; option is also used.
  235. </p>
  236. <p>Note that object files that wish to access these symbols must <em>not</em>
  237. decorate them with dllexport. Instead, they should use dllimport,
  238. instead:
  239. </p>
  240. <div class="example">
  241. <pre class="example">__declspec(dllimport) int a_variable
  242. __declspec(dllimport) void a_function(int with_args)
  243. </pre></div>
  244. <p>This complicates the structure of library header files, because
  245. when included by the library itself the header must declare the
  246. variables and functions as dllexport, but when included by client
  247. code the header must declare them as dllimport. There are a number
  248. of idioms that are typically used to do this; often client code can
  249. omit the __declspec() declaration completely. See
  250. &lsquo;<samp>--enable-auto-import</samp>&rsquo; and &lsquo;<samp>automatic data imports</samp>&rsquo; for more
  251. information.
  252. </p></dd>
  253. </dl>
  254. <a name="index-automatic-data-imports"></a>
  255. </dd>
  256. <dt><em>automatic data imports</em></dt>
  257. <dd><p>The standard Windows dll format supports data imports from dlls only
  258. by adding special decorations (dllimport/dllexport), which let the
  259. compiler produce specific assembler instructions to deal with this
  260. issue. This increases the effort necessary to port existing Un*x
  261. code to these platforms, especially for large
  262. c++ libraries and applications. The auto-import feature, which was
  263. initially provided by Paul Sokolovsky, allows one to omit the
  264. decorations to achieve a behavior that conforms to that on POSIX/Un*x
  265. platforms. This feature is enabled with the &lsquo;<samp>--enable-auto-import</samp>&rsquo;
  266. command-line option, although it is enabled by default on cygwin/mingw.
  267. The &lsquo;<samp>--enable-auto-import</samp>&rsquo; option itself now serves mainly to
  268. suppress any warnings that are ordinarily emitted when linked objects
  269. trigger the feature&rsquo;s use.
  270. </p>
  271. <p>auto-import of variables does not always work flawlessly without
  272. additional assistance. Sometimes, you will see this message
  273. </p>
  274. <p>&quot;variable &rsquo;&lt;var&gt;&rsquo; can&rsquo;t be auto-imported. Please read the
  275. documentation for ld&rsquo;s <code>--enable-auto-import</code> for details.&quot;
  276. </p>
  277. <p>The &lsquo;<samp>--enable-auto-import</samp>&rsquo; documentation explains why this error
  278. occurs, and several methods that can be used to overcome this difficulty.
  279. One of these methods is the <em>runtime pseudo-relocs</em> feature, described
  280. below.
  281. </p>
  282. <a name="index-runtime-pseudo_002drelocation"></a>
  283. <p>For complex variables imported from DLLs (such as structs or classes),
  284. object files typically contain a base address for the variable and an
  285. offset (<em>addend</em>) within the variable&ndash;to specify a particular
  286. field or public member, for instance. Unfortunately, the runtime loader used
  287. in win32 environments is incapable of fixing these references at runtime
  288. without the additional information supplied by dllimport/dllexport decorations.
  289. The standard auto-import feature described above is unable to resolve these
  290. references.
  291. </p>
  292. <p>The &lsquo;<samp>--enable-runtime-pseudo-relocs</samp>&rsquo; switch allows these references to
  293. be resolved without error, while leaving the task of adjusting the references
  294. themselves (with their non-zero addends) to specialized code provided by the
  295. runtime environment. Recent versions of the cygwin and mingw environments and
  296. compilers provide this runtime support; older versions do not. However, the
  297. support is only necessary on the developer&rsquo;s platform; the compiled result will
  298. run without error on an older system.
  299. </p>
  300. <p>&lsquo;<samp>--enable-runtime-pseudo-relocs</samp>&rsquo; is not the default; it must be explicitly
  301. enabled as needed.
  302. </p>
  303. <a name="index-direct-linking-to-a-dll"></a>
  304. </dd>
  305. <dt><em>direct linking to a dll</em></dt>
  306. <dd><p>The cygwin/mingw ports of <code>ld</code> support the direct linking,
  307. including data symbols, to a dll without the usage of any import
  308. libraries. This is much faster and uses much less memory than does the
  309. traditional import library method, especially when linking large
  310. libraries or applications. When <code>ld</code> creates an import lib, each
  311. function or variable exported from the dll is stored in its own bfd, even
  312. though a single bfd could contain many exports. The overhead involved in
  313. storing, loading, and processing so many bfd&rsquo;s is quite large, and explains the
  314. tremendous time, memory, and storage needed to link against particularly
  315. large or complex libraries when using import libs.
  316. </p>
  317. <p>Linking directly to a dll uses no extra command-line switches other than
  318. &lsquo;<samp>-L</samp>&rsquo; and &lsquo;<samp>-l</samp>&rsquo;, because <code>ld</code> already searches for a number
  319. of names to match each library. All that is needed from the developer&rsquo;s
  320. perspective is an understanding of this search, in order to force ld to
  321. select the dll instead of an import library.
  322. </p>
  323. <p>For instance, when ld is called with the argument &lsquo;<samp>-lxxx</samp>&rsquo; it will attempt
  324. to find, in the first directory of its search path,
  325. </p>
  326. <div class="example">
  327. <pre class="example">libxxx.dll.a
  328. xxx.dll.a
  329. libxxx.a
  330. xxx.lib
  331. libxxx.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>