| 1 |
= LibrarySupport |
| 2 |
|
| 3 |
MLton supports both linking to and creating system-level libraries. |
| 4 |
While Standard ML libraries should be designed with the <<MLBasis#>> system to work with other Standard ML programs, |
| 5 |
system-level library support allows MLton to create libraries for use by other programming languages. |
| 6 |
Even more importantly, system-level library support allows MLton to access libraries from other languages. |
| 7 |
This article will explain how to use libraries portably with MLton. |
| 8 |
|
| 9 |
== The Basics |
| 10 |
|
| 11 |
A Dynamic Shared Object (DSO) is a piece of executable code written in a format understood by the operating system. |
| 12 |
Executable programs and dynamic libraries are the two most common examples of a DSO. |
| 13 |
They are called shared because if they are used more than once, they are only loaded once into main memory. |
| 14 |
For example, if you start two instances of your web browser (an executable), there may be two processes running, but the program code of the executable is only loaded once. |
| 15 |
A dynamic library, for example a graphical toolkit, might be used by several different executable programs, each possibly running multiple times. |
| 16 |
Nevertheless, the dynamic library is only loaded once and it's program code is shared between all of the processes. |
| 17 |
|
| 18 |
In addition to program code, DSOs contain a table of textual strings called symbols. |
| 19 |
These are used in order to make the DSO do something useful, like execute. |
| 20 |
For example, on linux the symbol `_start` refers to the point in the program code where the operating system should start executing the program. |
| 21 |
Dynamic libraries generally provide many symbols, corresponding to functions which can be called and variables which can be read or written. |
| 22 |
Symbols can be used by the DSO itself, or by other DSOs which require services. |
| 23 |
|
| 24 |
When a DSO creates a symbol, this is called 'exporting'. |
| 25 |
If a DSO needs to use a symbol, this is called 'importing'. |
| 26 |
A DSO might need to use symbols defined within itself or perhaps from another DSO. |
| 27 |
In both cases, it is importing that symbol, but the scope of the import differs. |
| 28 |
Similarly, a DSO might export a symbol for use only within itself, or it might export a symbol for use by other DSOs. |
| 29 |
Some symbols are resolved at compile time by the linker (those used within the DSO) and some are resolved at runtime by the dynamic link loader (symbols accessed between DSOs). |
| 30 |
|
| 31 |
== Symbols in MLton |
| 32 |
|
| 33 |
Symbols in MLton are both imported and exported via the <<ForeignFunctionInterface#>>. |
| 34 |
The notation `_import "symbolname"` imports functions, `_symbol "symbolname"` imports variables, and `_address "symbolname"` imports an address. |
| 35 |
To create and export a symbol, `_export "symbolname"` creates a function symbol and `_symbol "symbolname" 'alloc'` creates and exports a variable. |
| 36 |
For details of the syntax and restrictions on the supported FFI types, read the <<ForeignFunctionInterface#>> page. |
| 37 |
In this discussion it only matters that every FFI use is either an import or an export. |
| 38 |
|
| 39 |
When exporting a symbol, MLton supports controlling the export scope. |
| 40 |
If the symbol should only be used within the same DSO, that symbol has '`private`' scope. |
| 41 |
Conversely, if the symbol should also be available to other DSOs the symbol has '`public`' scope. |
| 42 |
Generally, one should have as few public exports as possible. |
| 43 |
Since they are public, other DSOs will come to depend on them, limiting your ability to change them. |
| 44 |
You specify the export scope in MLton by putting `private` or `public` after the symbol's name in an FFI directive. |
| 45 |
eg: `_export "foo" private: int\->int;` or `_export "bar" public: int\->int;` . |
| 46 |
|
| 47 |
For technical reasons, the linker and loader on various platforms need to know the scope of a symbol being imported. |
| 48 |
If the symbol is exported by the same DSO, use `public` or `private` as appropriate. |
| 49 |
If the symbol is exported by a different DSO, then the scope '`external`' should be used to import it. |
| 50 |
Within a DSO, all references to a symbol must use the same scope. |
| 51 |
MLton will check this at compile time, reporting: `symbol "foo" redeclared as public (previously external)`. This may cause linker errors. |
| 52 |
However, MLton can only check usage within Standard ML. |
| 53 |
All objects being linked into a resulting DSO must agree, and it is the programmer's responsibility to ensure this. |
| 54 |
|
| 55 |
Summary of symbol scopes: |
| 56 |
|
| 57 |
* `private`: used for symbols exported within a DSO only for use within that DSO |
| 58 |
* `public`: used for symbols exported within a DSO that may also be used outside that DSO |
| 59 |
* `external`: used for importing symbols from another DSO |
| 60 |
* All uses of a symbol within a DSO (both imports and exports) must agree on the symbol scope |
| 61 |
|
| 62 |
== Output Formats |
| 63 |
|
| 64 |
MLton can create executables (`-format executable`) and dynamic shared libraries (`-format library`). |
| 65 |
To link a shared library, use `-link-opt -l<dso_name>`. |
| 66 |
The default output format is executable. |
| 67 |
|
| 68 |
MLton can also create archives. |
| 69 |
An archive is not a DSO, but it does have a collection of symbols. |
| 70 |
When an archive is linked into a DSO, it is completely absorbed. |
| 71 |
Other objects being compiled into the DSO should refer to the public symbols in the archive as public, since they are still in the same DSO. |
| 72 |
However, in the interest of modular programming, private symbols in an archive cannot be used outside of that archive, even within the same DSO. |
| 73 |
|
| 74 |
Although both executables and libraries are DSOs, some implementation details differ on some platforms. |
| 75 |
For this reason, MLton can create two types or archives. |
| 76 |
A normal archive (`-format archive`) is appropriate for linking into an executable. |
| 77 |
Conversely, a libarchive (`-format libarchive`) should be used if it will be linked into a dynamic library. |
| 78 |
|
| 79 |
When MLton does not create an executable, it creates two special symbols. |
| 80 |
The symbol `libname_open` is a function which must be called before any other symbols are accessed. |
| 81 |
The `libname` is controlled by the `-libname` compile option and defaults to the name of the output, with any prefixing lib stripped (eg: `foo` -> `foo`, `libfoo` -> `foo`). |
| 82 |
The symbol `libname_close` is a function which should be called to clean up memory once done. |
| 83 |
|
| 84 |
Summary of `-format` options: |
| 85 |
|
| 86 |
* `executable`: create an executable (a DSO) |
| 87 |
* `library`: create a dynamic shared library (a DSO) |
| 88 |
* `archive`: create an archive of symbols (not a DSO) that can be linked into an executable |
| 89 |
* `libarchive`: create an archive of symbols (not a DSO) that can be linked into a library |
| 90 |
|
| 91 |
Related options: |
| 92 |
|
| 93 |
* `-libname x`: controls the name of the special `_open` and `_close` functions. |
| 94 |
|
| 95 |
|
| 96 |
== Interfacing with C |
| 97 |
|
| 98 |
MLton can generate a C header file. |
| 99 |
When the output format is not an executable, it creates one by default named `libname.h`. |
| 100 |
This can be overridden with `-export-header foo.h`. |
| 101 |
This header file should be included by any C files using the exported Standard ML symbols. |
| 102 |
|
| 103 |
If C is being linked with Standard ML into the same output archive or DSO, |
| 104 |
then the C code should `#define PART_OF_LIBNAME` before it includes the header file. |
| 105 |
This ensures that the C code is using the symbols with correct scope. |
| 106 |
Any symbols exported from C should also be marked using the `PRIVATE`/`PUBLIC`/`EXTERNAL` macros defined in the Standard ML export header. |
| 107 |
The declared C scope on exported C symbols should match the import scope used in Standard ML. |
| 108 |
|
| 109 |
An example: |
| 110 |
[source,c] |
| 111 |
---- |
| 112 |
#define PART_OF_FOO |
| 113 |
#include "foo.h" |
| 114 |
|
| 115 |
PUBLIC int cFoo() { |
| 116 |
return smlFoo(); |
| 117 |
} |
| 118 |
---- |
| 119 |
|
| 120 |
[source,sml] |
| 121 |
---- |
| 122 |
val () = _export "smlFoo" private: unit -> int; (fn () => 5) |
| 123 |
val cFoo = _import "cFoo" public: unit -> int; |
| 124 |
---- |
| 125 |
|
| 126 |
|
| 127 |
== Operating-system specific details |
| 128 |
|
| 129 |
On Windows, `libarchive` and `archive` are the same. |
| 130 |
However, depending on this will lead to portability problems. |
| 131 |
Windows is also especially sensitive to mixups of '`public`' and '`external`'. |
| 132 |
If an archive is linked, make sure it's symbols are imported as `public`. |
| 133 |
If a DLL is linked, make sure it's symbols are imported as `external`. |
| 134 |
Using `external` instead of `public` will result in link errors that `__imp__foo is undefined`. |
| 135 |
Using `public` instead of `external` will result in inconsistent function pointer addresses and failure to update the imported variables. |
| 136 |
|
| 137 |
On Linux, `libarchive` and `archive` are different. |
| 138 |
Libarchives are quite rare, but necessary if creating a library from an archive. |
| 139 |
It is common for a library to provide both an archive and a dynamic library on this platform. |
| 140 |
The linker will pick one or the other, usually preferring the dynamic library. |
| 141 |
While a quirk of the operating system allows external import to work for both archives and libraries, |
| 142 |
portable projects should not depend on this behaviour. |
| 143 |
On other systems it can matter how the library is linked (static or dynamic). |