Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikolasburk/dynamic_linking
https://github.com/nikolasburk/dynamic_linking
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/nikolasburk/dynamic_linking
- Owner: nikolasburk
- Created: 2016-08-24T09:44:05.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-24T09:49:57.000Z (about 8 years ago)
- Last Synced: 2024-10-02T17:21:13.011Z (about 1 month ago)
- Language: C
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Dynamic Linking
Note: The instructions found in this README are heavily inspired by [this blog post](https://pewpewthespells.com/blog/static_and_dynamic_libraries.html) about Static vs. Dynamic Linking by [queersorceress](https://twitter.com/queersorceress).
#### 1. Creating [Mach-O](https://en.wikipedia.org/wiki/Mach-O) file (`.o`)
clang -c calculator.c -o calculator.o
#### 2. Creating library
The following command creates the dynamic library (_dylib_) and links against `libSystem`. The dynamic library is a Mach-O binary file and will be loaded dynamically at launch time by `dyld` as a dependency of another binary.
libtool -dynamic calculator.o -o libcalc_dynamic.dylib -lSystem
If other (static?) frameworks should be linked, the `-framework` flag can be used, for example:libtool -dynamic calculator.o -o libcalc_dynamic.dylib -framework CoreFoundation -lSystem
#### 3. Generate object file for `main`
clang -c main.c -o main.o
#### 4. Linking
ld main.o -lSystem -L. -lcalc_dynamic -arch x86_64 -o test_dynamic -v
This command geneates a binary executable from the main object file, also passing:
- `lSystem` for `dyld_stub_binder`
- `lcalc_dynamic` for linking agfainst `libcalc_dynamic.dylib`
- `-arch x86_64` to specify for which architecture the program should be executable#### 5. Running
./test_dynamic
#### 7. Checking symbols
The `nm` tool can be used to display the name list (symbol table) of each object file in the argument list. Each symbol is preceded by its value (blanks if undefined). Unless the option `-m` is specified, this value is followed by one of the following characters, representing the symbol type: U (undefined), A (absolute), T (text section symbol), D (data section symbol), B (bss section symbol), C (common symbol), S (symbol in section other than those mentioned above), I (indirect symbol).
$ nm test_dynamic
0000000100000000 T __mh_execute_header
U _add
0000000100000ee0 T _main
U _multiply
U _printf
U dyld_stub_binderThe symbols `_add` and `_multiply` do not have addresses, because they don't exist inside of the main binary, they only exist in the dynamic library that was created.
#### 8. Checking references
The `otool` tool can be used to display specified parts of object files or libraries. The `L` option prints all shared libraries that are used.
$ otool -L test_dynamic
test_dynamic:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
libcalc_dynamic.dylib (compatibility version 0.0.0, current version 0.0.0)The output shows that the binary `test_dynamic` only links against `libSytem` and the custom dynamic library that was created before. The library `calc_dynamic` (`libcalc_dynamic.dylib`) is responsible for linking against any additional libraries it needs. This is resolved at launch time, dynamically. In this case, the search path for `libcalc_dynamic.dylib` is going to be the same as the search location as the main executable.
Dynamic libraries and frameworks are loaded at launch time by the dynamic linker. They have associated search paths to help the linker find where they are located on the file system and load them.