Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / doc_generated / devguide / devcycle / dynamic-loading.html
1 {{+bindTo:partials.standard_nacl_article}}
2
3 <section id="dynamic-linking-and-loading-with-glibc">
4 <h1 id="dynamic-linking-and-loading-with-glibc">Dynamic Linking and Loading with glibc</h1>
5 <div class="contents local" id="contents" style="display: none">
6 <ul class="small-gap">
7 <li><a class="reference internal" href="#c-standard-libraries-glibc-and-newlib" id="id1">C standard libraries: glibc and newlib</a></li>
8 <li><a class="reference internal" href="#sdk-toolchains" id="id2">SDK toolchains</a></li>
9 <li><p class="first"><a class="reference internal" href="#specifying-and-delivering-shared-libraries" id="id3">Specifying and delivering shared libraries</a></p>
10 <ul class="small-gap">
11 <li><a class="reference internal" href="#building-a-dynamically-linked-application" id="id4">Building a dynamically linked application</a></li>
12 <li><a class="reference internal" href="#generating-a-native-client-manifest-file-for-a-dynamically-linked-application" id="id5">Generating a Native Client manifest file for a dynamically linked application</a></li>
13 <li><a class="reference internal" href="#deploying-a-dynamically-linked-application" id="id6">Deploying a dynamically linked application</a></li>
14 <li><a class="reference internal" href="#opening-a-shared-library-at-runtime" id="id7">Opening a shared library at runtime</a></li>
15 <li><a class="reference internal" href="#troubleshooting" id="id8">Troubleshooting</a></li>
16 </ul>
17 </li>
18 </ul>
19
20 </div><aside class="caution">
21 Portable Native Client currently only supports static linking, and the
22 only C library available for it is newlib. This page is only valid for
23 Native Client, though PNaCl will eventually support some form of
24 dynamic linking.
25 </aside>
26 <p>This document describes how to create and deploy dynamically linked and loaded
27 applications with the glibc library in the Native Client SDK. Before reading
28 this document, we recommend reading <a class="reference internal" href="/native-client/devguide/devcycle/building.html"><em>Building Native Client Modules</em></a></p>
29 <h2 id="c-standard-libraries-glibc-and-newlib"><span id="c-libraries"></span>C standard libraries: glibc and newlib</h2>
30 <p>The Native Client SDK comes with two C standard libraries &#8212; glibc and
31 newlib. These libraries are described in the table below.</p>
32 <table border="1" class="docutils">
33 <colgroup>
34 </colgroup>
35 <thead valign="bottom">
36 <tr class="row-odd"><th class="head">Library</th>
37 <th class="head">Linking</th>
38 <th class="head">License</th>
39 </tr>
40 </thead>
41 <tbody valign="top">
42 <tr class="row-even"><td><dl class="first last docutils">
43 <dt>glibc</dt>
44 <dd>The GNU implementation of the <a class="reference external" href="http://en.wikipedia.org/wiki/POSIX">POSIX</a> standard
45 runtime library for the C programming language.
46 Designed for portability and performance, glibc is
47 one of the most popular implementations of the C
48 library. It is comprised of a set of interdependent
49 libraries including libc, libpthreads, libdl, and
50 others. For documentation, FAQs, and additional
51 information about glibc, see <a class="reference external" href="http://www.gnu.org/software/libc/index.html">GLIBC</a>.</dd>
52 </dl>
53 </td>
54 <td>dynamic
55 or static</td>
56 <td>GNU Lesser
57 General
58 Public
59 License
60 (LGPL)</td>
61 </tr>
62 <tr class="row-odd"><td><dl class="first last docutils">
63 <dt>newlib</dt>
64 <dd>newlib is a C library intended for use in embedded
65 systems. Like glibc, newlib is a conglomeration of
66 several libraries. It is available for use under
67 BSD-type free software licenses, which generally
68 makes it more suitable to link statically in
69 commercial, closed-source applications. For
70 documentation, FAQs, and additional information
71 about newlib, see <a class="reference external" href="http://sourceware.org/newlib/">newlib</a>.</dd>
72 </dl>
73 </td>
74 <td>static</td>
75 <td>Berkeley
76 Software
77 Distribution
78 (BSD) type
79 free
80 software
81 licenses</td>
82 </tr>
83 </tbody>
84 </table>
85 <p>For proprietary (closed-source) applications, your options are to either
86 statically link to newlib, or dynamically link to glibc. We recommend
87 dynamically linking to glibc, for a couple of reasons:</p>
88 <ul class="small-gap">
89 <li>The glibc library is widely distributed (it&#8217;s included in Linux
90 distributions), and as such it&#8217;s mature, hardened, and feature-rich. Your
91 code is more likely to compile out-of-the-box with glibc.</li>
92 <li>Dynamic loading can provide a big performance benefit for your application if
93 you can structure the application to defer loading of code that&#8217;s not needed
94 for initial interaction with the user. It takes some work to put such code in
95 shared libraries and to load the libraries at runtime, but the payoff is
96 usually worth it. In future releases, Chrome may also support caching of
97 common dynamically linked libraries such as libc.so between applications.
98 This could significantly reduce download size and provide a further potential
99 performance benefit (for example, the hello_world example would only require
100 downloading a .nexe file that&#8217;s on the order of 30KB, rather than a .nexe
101 file and several libraries, which are on the order of 1.5MB).</li>
102 </ul>
103 <p>Native Client support for dynamic linking and loading is based on glibc. Thus,
104 <strong>if your Native Client application must dynamically link and load code (e.g.,
105 due to licensing considerations), we recommend that you use the glibc
106 library.</strong></p>
107 <aside class="note">
108 <p><strong>Disclaimer:</strong></p>
109 <ul class="small-gap">
110 <li><strong>None of the above constitutes legal advice, or a description of the legal
111 obligations you need to fulfill in order to be compliant with the LGPL or
112 newlib licenses. The above description is only a technical explanation of
113 the differences between newlib and glibc, and the choice you must make
114 between the two libraries.</strong></li>
115 </ul>
116
117 </aside>
118 <aside class="note">
119 <p><strong>Notes:</strong></p>
120 <ul class="small-gap">
121 <li>Static linking with glibc is rarely used. Use this feature with caution.</li>
122 <li>The standard C++ runtime in Native Client is provided by libstdc++; this
123 library is independent from and layered on top of glibc. Because of
124 licensing restrictions, libstdc++ must be statically linked for commercial
125 uses, even if the rest of an application is dynamically linked.</li>
126 </ul>
127
128 </aside>
129 <h2 id="sdk-toolchains">SDK toolchains</h2>
130 <p>The Native Client SDK contains multiple toolchains, which are differentiated by
131 <a class="reference internal" href="/native-client/devguide/devcycle/building.html#target-architectures"><em>target architecture</em></a> and C library:</p>
132 <table border="1" class="docutils">
133 <colgroup>
134 </colgroup>
135 <thead valign="bottom">
136 <tr class="row-odd"><th class="head">Target architecture</th>
137 <th class="head">C library</th>
138 <th class="head">Toolchain directory</th>
139 </tr>
140 </thead>
141 <tbody valign="top">
142 <tr class="row-even"><td>x86</td>
143 <td>newlib</td>
144 <td>toolchain/&lt;platform&gt;_x86_newlib</td>
145 </tr>
146 <tr class="row-odd"><td>x86</td>
147 <td>glibc</td>
148 <td>toolchain/&lt;platform&gt;_x86_glibc</td>
149 </tr>
150 <tr class="row-even"><td>ARM</td>
151 <td>newlib</td>
152 <td>toolchain/&lt;platform&gt;_arm_newlib</td>
153 </tr>
154 <tr class="row-odd"><td>PNaCl</td>
155 <td>newlib</td>
156 <td>toolchain/&lt;platform&gt;_pnacl</td>
157 </tr>
158 </tbody>
159 </table>
160 <p>In the directories listed above, &lt;platform&gt; is the platform of your development
161 machine (i.e., win, mac, or linux). For example, in the Windows SDK, the x86
162 toolchain that uses glibc is in <code>toolchain/win_x86_glibc</code>.</p>
163 <aside class="note">
164 <strong>Note:</strong> The ARM and PNaCl toolchains are currently restricted to newlib.
165 </aside>
166 <p>To use the glibc library and dynamic linking in your application, you <strong>must</strong>
167 use a glibc toolchain. (Currently the only glibc toolchain is
168 <code>&lt;platform&gt;_x86_glibc</code>.) Note that you must build all code in your application
169 with one toolchain. Code from multiple toolchains cannot be mixed.</p>
170 <h2 id="specifying-and-delivering-shared-libraries">Specifying and delivering shared libraries</h2>
171 <p>One significant difference between newlib and glibc applications is that glibc
172 applications must explicitly list and deploy the shared libraries that they
173 use.</p>
174 <p>In a desktop environment, when the user launches a dynamically linked
175 application, the operating system&#8217;s program loader determines the set of
176 libraries the application requires by reading explicit inter-module
177 dependencies from executable file headers, and loads the required libraries
178 into the address space of the application process. Typically the required
179 libraries will have been installed on the system as a part of the application&#8217;s
180 installation process. Often the desktop application developer doesn&#8217;t know or
181 think about the libraries that are required by an application, as those details
182 are taken care of by the user&#8217;s operating system.</p>
183 <p>In the Native Client sandbox, dynamic linking can&#8217;t rely in the same way on the
184 operating system or the local file system. Instead, the application developer
185 must identify the set of libraries that are required by an application, list
186 those libraries in a Native Client <a class="reference internal" href="/native-client/devguide/coding/application-structure.html#manifest-file"><em>manifest file</em></a>, and
187 deploy the libraries along with the application. Instructions for how to build
188 a dynamically linked Native Client application, generate a Native Client
189 manifest (.nmf) file, and deploy an application are provided below.</p>
190 <h3 id="building-a-dynamically-linked-application">Building a dynamically linked application</h3>
191 <p>Applications built with the glibc toolchain will by dynamically linked by
192 default. Application that load shared libraries at runtime using <code>dlopen()</code>
193 must link with the libdl library (<code>-ldl</code>).</p>
194 <p>Like other gcc-based toolchains building a dynamic library for NaCl is normally
195 done by linking with the <code>-shared</code> flag and compiling with the <code>-fPIC</code> flag.
196 The SDK build system will do this automatically when the <code>SO_RULE</code> Makefile
197 rule is used.</p>
198 <p>The Native Client SDK includes an example that demonstrates how to build a
199 shared library, and how to use the <code>dlopen()</code> interface to load that library
200 at runtime (after the application is already running). Many applications load
201 and link shared libraries at launch rather than at runtime, and hence do not
202 use the <code>dlopen()</code> interface. The SDK example is nevertheless instructive, as
203 it demonstrates how to build Native Client modules (.nexe files) and shared
204 libraries (.so files) with the x86 glibc toolchain, and how to generate a
205 Native Client manifest file for glibc applications.</p>
206 <p>The SDK example, located in <code>examples/tutorial/dlopen</code>, includes three C++
207 files:</p>
208 <dl class="docutils">
209 <dt>eightball.cc</dt>
210 <dd>This file implements the function <code>Magic8Ball()</code>, which is used to provide
211 whimsical answers to user questions. This file is compiled into a shared
212 library called <code>libeightball.so</code>. This library gets included in the
213 .nmf file and is therefore directly loadable with <code>dlopen()</code>.</dd>
214 <dt>reverse.cc</dt>
215 <dd>This file implements the function <code>Reverse()</code>, which returns reversed
216 copies of strings that are passed to it. This file is compiled into a shared
217 library called <code>libreverse.so</code>. This library is <strong>not</strong> included in the
218 .nmf file and is loaded via an http mount using the <a class="reference internal" href="/native-client/devguide/coding/nacl_io.html#nacl-io"><em>nacl_io library</em></a>.</dd>
219 <dt>dlopen.cc</dt>
220 <dd>This file implements the Native Client module, which loads the two shared
221 libraries and handles communcation with with JavaScript. The file is compiled
222 into a Native Client executable (.nexe).</dd>
223 </dl>
224 <p>Run <code>make</code> in the dlopen directory to see the commands the Makefile executes
225 to build x86 32-bit and 64-bit .nexe and .so files, and to generate a .nmf
226 file. These commands are described below.</p>
227 <aside class="note">
228 <strong>Note:</strong> The Makefiles for most of the examples in the SDK build the
229 examples using multiple toolchains (x86 newlib, x86 glibc, ARM, and PNaCl).
230 With a few exceptions (listed in the <a class="reference internal" href="/native-client/sdk/release-notes.html#sdk-release-notes"><em>Release Notes</em></a>), running &#8220;make&#8221; in each example&#8217;s directory builds
231 multiple versions of the example using the SDK toolchains. The dlopen example
232 is one of those exceptions â€“ it is only built with the x86 glibc toolchain,
233 as that is currently the only toolchain that supports glibc and thus dynamic
234 linking and loading. Take a look at the example Makefiles and the generated
235 .nmf files for details on how to build dynamically linked applications.
236 </aside>
237 <h3 id="generating-a-native-client-manifest-file-for-a-dynamically-linked-application"><span id="dynamic-loading-manifest"></span>Generating a Native Client manifest file for a dynamically linked application</h3>
238 <p>The Native Client manifest file specifies the name of the executable to run
239 and must also specify any shared libraries that the application directly
240 depends on. For indirect dependencies (such as libraries opened via
241 <code>dlopen()</code>) it is also convenient to list libraries in the manifest file.
242 However it is possile to load arbitrary shared libraries at runtime that
243 are not mentioned in the manifest by using the <a class="reference external" href="nacl_io">nacl_io library</a>
244 to mount a filesystem that contains the shared libraries which will then
245 allow <code>dlopen()</code> to access them.</p>
246 <p>In this example we demonstrate both loading directly from via the manifest
247 file (<code>libeightball.so</code>) and loading indirectly via a http mount
248 (<code>libreverse.so</code>).</p>
249 <p>Take a look at the manifest file in the dlopen example to see how
250 a glibc-style manifest file is structured. (Run <code>make</code> in the dlopen directory to
251 generate the manifest file if you haven&#8217;t done so already.) Here is an excerpt
252 from <code>dlopen.nmf</code>:</p>
253 <pre class="prettyprint">
254 {
255   &quot;files&quot;: {
256     &quot;libeightball.so&quot;: {
257       &quot;x86-64&quot;: {
258         &quot;url&quot;: &quot;lib64/libeightball.so&quot;
259       },
260       &quot;x86-32&quot;: {
261         &quot;url&quot;: &quot;lib32/libeightball.so&quot;
262       }
263     },
264     &quot;libstdc++.so.6&quot;: {
265       &quot;x86-64&quot;: {
266         &quot;url&quot;: &quot;lib64/libstdc++.so.6&quot;
267       },
268       &quot;x86-32&quot;: {
269         &quot;url&quot;: &quot;lib32/libstdc++.so.6&quot;
270       }
271     },
272     &quot;libppapi_cpp.so&quot;: {
273       &quot;x86-64&quot;: {
274         &quot;url&quot;: &quot;lib64/libppapi_cpp.so&quot;
275       },
276       &quot;x86-32&quot;: {
277         &quot;url&quot;: &quot;lib32/libppapi_cpp.so&quot;
278       }
279     },
280 ... etc.
281 </pre>
282 <p>In most cases, you can use the <code>create_nmf.py</code> script in the SDK to generate
283 a manifest file for your application. The script is located in the tools
284 directory (e.g. <code>pepper_28/tools</code>).</p>
285 <p>The Makefile in the dlopen example generates the manifest automatically using
286 the <code>NMF_RULE</code> provided by the SDK build system. Running <code>make V=1</code> will
287 show the full command line which is used to generate the nmf:</p>
288 <pre class="prettyprint">
289 create_nmf.py -o dlopen.nmf glibc/Release/dlopen_x86_32.nexe \
290    glibc/Release/dlopen_x86_64.nexe glibc/Release/libeightball_x86_32.so \
291    glibc/Release/libeightball_x86_64.so  -s ./glibc/Release \
292    -n libeightball_x86_32.so,libeightball.so \
293    -n libeightball_x86_64.so,libeightball.so
294 </pre>
295 <p>Run python <code>create_nmf.py --help</code> to see a full description of the command-line
296 flags. A few of the important flags are described below.</p>
297 <dl class="docutils">
298 <dt><code>-s</code> <em>directory</em></dt>
299 <dd>use <em>directory</em> to stage libraries (libraries are added to <code>lib32</code> and
300 <code>lib64</code> subfolders)</dd>
301 <dt><code>-L</code> <em>directory</em></dt>
302 <dd>add <em>directory</em> to the library search path. The default search path
303 already includes the toolchain and SDK libraries directories.</dd>
304 </dl>
305 <aside class="note">
306 <strong>Note:</strong> The <code>create_nmf</code> script can only automatically detect explicit
307 shared library dependencies (for example, dependencies specified with the -l
308 flag for the compiler/linker). If you want to include libraries that you
309 intend to dlopen() at runtime you must explcitly list them in your call to
310 <code>create_nmf</code>.
311 </aside>
312 <p>As an alternative to using <code>create_nmf</code>, it is possible to manually calculate
313 the list of shared library dependencies using tools such as <code>objdump_</code>.</p>
314 <h3 id="deploying-a-dynamically-linked-application">Deploying a dynamically linked application</h3>
315 <p>As described above, an application&#8217;s manifest file must explicitly list all the
316 executable code modules that the application directly depends on, including
317 modules from the application itself (.nexe and .so files), modules from the
318 Native Client SDK (e.g., libppapi_cpp.so), and perhaps also modules from
319 <a class="reference external" href="http://code.google.com/p/naclports/">naclports</a> or from
320 <a class="reference external" href="../../community/middleware">middleware systems</a> that
321 the application uses. You must provide all of those modules as part of the
322 application deployment process.</p>
323 <p>As explained in <a class="reference internal" href="/native-client/devguide/distributing.html"><em>Distributing Your Application</em></a>, there are two basic ways to deploy an application:</p>
324 <ul class="small-gap">
325 <li><strong>hosted application:</strong> all modules are hosted together on a web server of
326 your choice</li>
327 <li><strong>packaged application:</strong> all modules are packaged into one file, hosted in
328 the Chrome Web Store, and downloaded to the user&#8217;s machine</li>
329 </ul>
330 <p>You must deploy all the modules listed in your application&#8217;s manifest file for
331 either the hosted application or the packaged application case. For hosted
332 applications, you must upload the modules to your web server. For packaged
333 applications, you must include the modules in the application&#8217;s Chrome Web
334 Store .crx file. Modules should use URLs/names that are consistent with those
335 in the Native Client manifest file, and be named relative to the location of
336 the manifest file. Remember that some of the libraries named in the manifest
337 file may be located in directories you specified with the -L option to
338 <code>create_nmf.py</code>. You are free to rename/rearrange files and directories
339 referenced by the Native Client manifest file, so long as the modules are
340 available in the locations indicated by the manifest file. If you move or
341 rename modules, it may be easier to re-run <code>create_nmf.py</code> to generate a new
342 manifest file rather than edit the original manifest file. For hosted
343 applications, you can check for name mismatches during testing by watching the
344 request log of the web server hosting your test deployment.</p>
345 <h3 id="opening-a-shared-library-at-runtime">Opening a shared library at runtime</h3>
346 <p>Native Client supports a version of the POSIX standard <code>dlopen()</code> interface
347 for opening libraries explicitly, after an application is already running.
348 Calling <code>dlopen()</code> may cause a library download to occur, and automatically
349 loads all libraries that are required by the named library.</p>
350 <aside class="note">
351 <strong>Caution:</strong> Since <code>dlopen()</code> can potentially block, you must initially
352 call <code>dlopen()</code> off your application&#8217;s main thread. Initial calls to
353 <code>dlopen()</code> from the main thread will always fail in the current
354 implementation of Native Client.
355 </aside>
356 <p>The best practice for opening libraries with <code>dlopen()</code> is to use a worker
357 thread to pre-load libraries asynchronously during initialization of your
358 application, so that the libraries are available when they&#8217;re needed. You can
359 call <code>dlopen()</code> a second time when you need to use a library &#8211; per the
360 specification, subsequent calls to <code>dlopen()</code> return a handle to the
361 previously loaded library. Note that you should only call <code>dlclose()</code> to
362 close a library when you no longer need the library; otherwise, subsequent
363 calls to <code>dlopen()</code> could cause the library to be fetched again.</p>
364 <p>The dlopen example in the SDK demonstrates how to open a shared libraries
365 at runtime. To reiterate, the example includes three C++ files:</p>
366 <ul class="small-gap">
367 <li><code>eightball.cc</code>: this is the shared library that implements the function
368 <code>Magic8Ball()</code> (this file is compiled into libeightball.so)</li>
369 <li><code>reverse.cc</code>: this is the shared library that implements the function
370 <code>Reverse()</code> (this file is compiled into libreverse.so)</li>
371 <li><code>dlopen.cc</code>: this is the Native Client module that loads the shared libraries
372 and makes calls to <code>Magic8Ball()</code> and <code>Reverse()</code> in response to requests
373 from JavaScript.</li>
374 </ul>
375 <p>When the Native Client module starts, it kicks off a worker thread that calls
376 <code>dlopen()</code> to load the two shared libraries. Once the module has a handle to
377 the library, it fetches the addresses of the <code>Magic8Ball()</code> and <code>Reverse()</code>
378 functions using <code>dlsym()</code>. When a user types in a query and clicks the &#8216;ASK!&#8217;
379 button, the module calls <code>Magic8Ball()</code> to generate an answer, and returns
380 the result to the user. Likewise when the user clicks the &#8216;Reverse&#8217; button
381 it calls the <code>Reverse()</code> function to reverse the string.</p>
382 <h3 id="troubleshooting">Troubleshooting</h3>
383 <p>If your .nexe isn&#8217;t loading, the best place to look for information that can
384 help you troubleshoot the JavaScript console and standard output from Chrome.
385 See <a class="reference internal" href="/native-client/devguide/devcycle/debugging.html#devcycle-debugging"><em>Debugging</em></a> for more information.</p>
386 <p>Here are a few common error messages and explanations of what they mean:</p>
387 <dl class="docutils">
388 <dt><strong>/main.nexe: error while loading shared libraries: /main.nexe: failed to allocate code and data space for executable</strong></dt>
389 <dd>The .nexe may not have been compiled correctly (e.g., the .nexe may be
390 statically linked). Try cleaning and recompiling with the glibc toolchain.</dd>
391 <dt><strong>/main.nexe: error while loading shared libraries: libpthread.so.xxxx: cannot open shared object file: Permission denied</strong></dt>
392 <dd>(xxxx is a version number, for example, 5055067a.) This error can result from
393 having the wrong path in the .nmf file. Double-check that the path in the
394 .nmf file is correct.</dd>
395 <dt><strong>/main.nexe: error while loading shared libraries: /main.nexe: cannot open shared object file: No such file or directory</strong></dt>
396 <dd>If there are no obvious problems with your main.nexe entry in the .nmf file,
397 check where main.nexe is being requested from. Use Chrome&#8217;s Developer Tools:
398 Click the menu icon <img alt="menu-icon" src="/native-client/images/menu-icon.png" />, select Tools &gt; Developer Tools, click the
399 Network tab, and look at the path in the Name column.</dd>
400 <dt><strong>NaCl module load failed: ELF executable text/rodata segment has wrong starting address</strong></dt>
401 <dd>This error happens when using a newlib-style .nmf file instead of a
402 glibc-style .nmf file. Make sure you build your application with the glic
403 toolchain, and use the create_nmf.py script to generate your .nmf file.</dd>
404 <dt><strong>NativeClient: NaCl module load failed: Nexe crashed during startup</strong></dt>
405 <dd>This error message indicates that a module crashed while being loaded. You
406 can determine which module crashed by looking at the Network tab in Chrome&#8217;s
407 Developer Tools (see above). The module that crashed will be the last one
408 that was loaded.</dd>
409 <dt><strong>/lib/main.nexe: error while loading shared libraries: /lib/main.nexe: only ET_DYN and ET_EXEC can be loaded</strong></dt>
410 <dd>This error message indicates that there is an error with the .so files listed
411 in the .nmf file &#8211; either the files are the wrong type or kind, or an
412 expected library is missing.</dd>
413 <dt><strong>undefined reference to &#8216;dlopen&#8217; collect2: ld returned 1 exit status</strong></dt>
414 <dd>This is a linker ordering problem that usually results from improper ordering
415 of command line flags when linking. Reconfigure your command line string to
416 list libraries after the -o flag.</dd>
417 </dl>
418 </section>
419
420 {{/partials.standard_nacl_article}}