1e1051a39Sopenharmony_ciNotes for UNIX-like platforms
2e1051a39Sopenharmony_ci=============================
3e1051a39Sopenharmony_ci
4e1051a39Sopenharmony_ci For Unix/POSIX runtime systems on Windows,
5e1051a39Sopenharmony_ci please see the [Notes for Windows platforms](NOTES-WINDOWS.md).
6e1051a39Sopenharmony_ci
7e1051a39Sopenharmony_ci OpenSSL uses the compiler to link programs and shared libraries
8e1051a39Sopenharmony_ci ---------------------------------------------------------------
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci OpenSSL's generated Makefile uses the C compiler command line to
11e1051a39Sopenharmony_ci link programs, shared libraries and dynamically loadable shared
12e1051a39Sopenharmony_ci objects.  Because of this, any linking option that's given to the
13e1051a39Sopenharmony_ci configuration scripts MUST be in a form that the compiler can accept.
14e1051a39Sopenharmony_ci This varies between systems, where some have compilers that accept
15e1051a39Sopenharmony_ci linker flags directly, while others take them in `-Wl,` form.  You need
16e1051a39Sopenharmony_ci to read your compiler documentation to figure out what is acceptable,
17e1051a39Sopenharmony_ci and `ld(1)` to figure out what linker options are available.
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci Shared libraries and installation in non-default locations
20e1051a39Sopenharmony_ci ----------------------------------------------------------
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci Every Unix system has its own set of default locations for shared
23e1051a39Sopenharmony_ci libraries, such as `/lib`, `/usr/lib` or possibly `/usr/local/lib`.  If
24e1051a39Sopenharmony_ci libraries are installed in non-default locations, dynamically linked
25e1051a39Sopenharmony_ci binaries will not find them and therefore fail to run, unless they get
26e1051a39Sopenharmony_ci a bit of help from a defined runtime shared library search path.
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci For OpenSSL's application (the `openssl` command), our configuration
29e1051a39Sopenharmony_ci scripts do NOT generally set the runtime shared library search path for
30e1051a39Sopenharmony_ci you.  It's therefore advisable to set it explicitly when configuring,
31e1051a39Sopenharmony_ci unless the libraries are to be installed in directories that you know
32e1051a39Sopenharmony_ci to be in the default list.
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci Runtime shared library search paths are specified with different
35e1051a39Sopenharmony_ci linking options depending on operating system and versions thereof, and
36e1051a39Sopenharmony_ci are talked about differently in their respective documentation;
37e1051a39Sopenharmony_ci variations of RPATH are the most usual (note: ELF systems have two such
38e1051a39Sopenharmony_ci tags, more on that below).
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci Possible options to set the runtime shared library search path include
41e1051a39Sopenharmony_ci the following:
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    -Wl,-rpath,/whatever/path   # Linux, *BSD, etc.
44e1051a39Sopenharmony_ci    -R /whatever/path           # Solaris
45e1051a39Sopenharmony_ci    -Wl,-R,/whatever/path       # AIX (-bsvr4 is passed internally)
46e1051a39Sopenharmony_ci    -Wl,+b,/whatever/path       # HP-UX
47e1051a39Sopenharmony_ci    -rpath /whatever/path       # Tru64, IRIX
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci OpenSSL's configuration scripts recognise all these options and pass
50e1051a39Sopenharmony_ci them to the Makefile that they build. (In fact, all arguments starting
51e1051a39Sopenharmony_ci with `-Wl,` are recognised as linker options.)
52e1051a39Sopenharmony_ci Please note that 'l' in '-Wl' is lowercase L and not 1.
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci Please do not use verbatim directories in your runtime shared library
55e1051a39Sopenharmony_ci search path!  Some OpenSSL config targets add an extra directory level
56e1051a39Sopenharmony_ci for multilib installations.  To help with that, the produced Makefile
57e1051a39Sopenharmony_ci includes the variable LIBRPATH, which is a convenience variable to be
58e1051a39Sopenharmony_ci used with the runtime shared library search path options, as shown in
59e1051a39Sopenharmony_ci this example:
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ci    $ ./Configure --prefix=/usr/local/ssl --openssldir=/usr/local/ssl \
62e1051a39Sopenharmony_ci        '-Wl,-rpath,$(LIBRPATH)'
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci On modern ELF based systems, there are two runtime search paths tags to
65e1051a39Sopenharmony_ci consider, `DT_RPATH` and `DT_RUNPATH`.  Shared objects are searched for in
66e1051a39Sopenharmony_ci this order:
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_ci  1. Using directories specified in DT_RPATH, unless DT_RUNPATH is also set.
69e1051a39Sopenharmony_ci  2. Using the environment variable LD_LIBRARY_PATH
70e1051a39Sopenharmony_ci  3. Using directories specified in DT_RUNPATH.
71e1051a39Sopenharmony_ci  4. Using system shared object caches and default directories.
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci This means that the values in the environment variable `LD_LIBRARY_PATH`
74e1051a39Sopenharmony_ci won't matter if the library is found in the paths given by `DT_RPATH`
75e1051a39Sopenharmony_ci (and `DT_RUNPATH` isn't set).
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci Exactly which of `DT_RPATH` or `DT_RUNPATH` is set by default appears to
78e1051a39Sopenharmony_ci depend on the system.  For example, according to documentation,
79e1051a39Sopenharmony_ci `DT_RPATH` appears to be deprecated on Solaris in favor of `DT_RUNPATH`,
80e1051a39Sopenharmony_ci while on Debian GNU/Linux, either can be set, and `DT_RPATH` is the
81e1051a39Sopenharmony_ci default at the time of writing.
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci How to choose which runtime search path tag is to be set depends on
84e1051a39Sopenharmony_ci your system, please refer to ld(1) for the exact information on your
85e1051a39Sopenharmony_ci system.  As an example, the way to ensure the `DT_RUNPATH` is set on
86e1051a39Sopenharmony_ci Debian GNU/Linux systems rather than DT_RPATH is to tell the linker to
87e1051a39Sopenharmony_ci set new dtags, like this:
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    $ ./Configure --prefix=/usr/local/ssl --openssldir=/usr/local/ssl \
90e1051a39Sopenharmony_ci        '-Wl,--enable-new-dtags,-rpath,$(LIBRPATH)'
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci It might be worth noting that some/most ELF systems implement support
93e1051a39Sopenharmony_ci for runtime search path relative to the directory containing current
94e1051a39Sopenharmony_ci executable, by interpreting `$ORIGIN` along with some other internal
95e1051a39Sopenharmony_ci variables. Consult your system documentation.
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ci Linking your application
98e1051a39Sopenharmony_ci ------------------------
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci Third-party applications dynamically linked with OpenSSL (or any other)
101e1051a39Sopenharmony_ci shared library face exactly the same problem with non-default locations.
102e1051a39Sopenharmony_ci The OpenSSL config options mentioned above might or might not have bearing
103e1051a39Sopenharmony_ci on linking of the target application. "Might" means that under some
104e1051a39Sopenharmony_ci circumstances it would be sufficient to link with OpenSSL shared library
105e1051a39Sopenharmony_ci "naturally", i.e. with `-L/whatever/path -lssl -lcrypto`. But there are
106e1051a39Sopenharmony_ci also cases when you'd have to explicitly specify runtime search path
107e1051a39Sopenharmony_ci when linking your application. Consult your system documentation and use
108e1051a39Sopenharmony_ci above section as inspiration...
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci Shared OpenSSL builds also install static libraries. Linking with the
111e1051a39Sopenharmony_ci latter is likely to require special care, because linkers usually look
112e1051a39Sopenharmony_ci for shared libraries first and tend to remain "blind" to static OpenSSL
113e1051a39Sopenharmony_ci libraries. Referring to system documentation would suffice, if not for
114e1051a39Sopenharmony_ci a corner case. On AIX static libraries (in shared build) are named
115e1051a39Sopenharmony_ci differently, add `_a` suffix to link with them, e.g. `-lcrypto_a`.
116