summaryrefslogtreecommitdiff
path: root/schulung_tools/linking/hello
diff options
context:
space:
mode:
authorManuel Traut <manut@linutronix.de>2019-01-09 16:42:49 +0100
committerJohn Ogness <john.ogness@linutronix.de>2019-01-28 17:25:56 +0106
commitb990733af9d6a07c2130daeb5fd39c75f6804441 (patch)
tree6c5f6486948ab3c1a9ec1a0db6d045d6bb1543c4 /schulung_tools/linking/hello
parent6f5bc54bec1449b1713504950cce078b0dac47c1 (diff)
add examples for dynamic linking / loading
one example by doing it manually with an ugly Makefile, another one by using autotools. Signed-off-by: Manuel Traut <manut@linutronix.de>
Diffstat (limited to 'schulung_tools/linking/hello')
-rw-r--r--schulung_tools/linking/hello/Makefile33
-rw-r--r--schulung_tools/linking/hello/func1.c6
-rw-r--r--schulung_tools/linking/hello/func2.c6
-rw-r--r--schulung_tools/linking/hello/hello.c12
-rw-r--r--schulung_tools/linking/hello/objdump.txt11
-rw-r--r--schulung_tools/linking/hello/shared_library.txt20
6 files changed, 88 insertions, 0 deletions
diff --git a/schulung_tools/linking/hello/Makefile b/schulung_tools/linking/hello/Makefile
new file mode 100644
index 0000000..a5b4c26
--- /dev/null
+++ b/schulung_tools/linking/hello/Makefile
@@ -0,0 +1,33 @@
+hello: hello.c libfunc.so
+ $(CROSS_COMPILE)gcc -g -ohello hello.c -L. -lfunc
+
+hello2: hello.c libfunc.a
+ rm -f libfunc.so
+ $(CROSS_COMPILE)gcc -g -ohello2 hello.c -L. -lfunc
+
+libfunc.so: libfunc.so.0.0.1
+ ln -s libfunc.so.0.0.1 libfunc.so
+
+libfunc.so.0.0.1: func1.o func2.o
+ $(CROSS_COMPILE)gcc -shared -olibfunc.so.0.0.1 -Wl,-soname,libfunc.so.0 func1.o func2.o
+
+libfunc.a: func1.o func2.o
+ $(CROSS_COMPILE)ar cr libfunc.a func1.o func2.o
+ $(CROSS_COMPILE)ranlib libfunc.a
+
+func1.o: func1.c
+ $(CROSS_COMPILE)gcc -fPIC -g -c func1.c
+
+func2.o: func2.c
+ $(CROSS_COMPILE)gcc -fPIC -g -c func2.c
+
+install_lib: libfunc.so.0.0.1
+ echo "/opt/acme/lib" > /etc/ld.so.conf.d/acme.conf
+ mkdir -p /opt/acme/lib
+ cp libfunc.so.0.0.1 /opt/acme/lib
+ ldconfig
+
+clean:
+ rm -f hello hello2 *.o lib*
+
+.PHONY: clean
diff --git a/schulung_tools/linking/hello/func1.c b/schulung_tools/linking/hello/func1.c
new file mode 100644
index 0000000..ab006bd
--- /dev/null
+++ b/schulung_tools/linking/hello/func1.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void func1(void)
+{
+ printf("Hello from %s!\n", __func__);
+}
diff --git a/schulung_tools/linking/hello/func2.c b/schulung_tools/linking/hello/func2.c
new file mode 100644
index 0000000..9dc05ab
--- /dev/null
+++ b/schulung_tools/linking/hello/func2.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void func2(void)
+{
+ printf("Hello from %s!\n", __func__);
+}
diff --git a/schulung_tools/linking/hello/hello.c b/schulung_tools/linking/hello/hello.c
new file mode 100644
index 0000000..82b8d53
--- /dev/null
+++ b/schulung_tools/linking/hello/hello.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+void func1(void);
+void func2(void);
+
+int main(void)
+{
+ func1();
+ printf("Hello, world!\n");
+ func2();
+ return 0;
+}
diff --git a/schulung_tools/linking/hello/objdump.txt b/schulung_tools/linking/hello/objdump.txt
new file mode 100644
index 0000000..9c77720
--- /dev/null
+++ b/schulung_tools/linking/hello/objdump.txt
@@ -0,0 +1,11 @@
+# identify dependencies
+objdump -x some_elf_file | grep NEEDED
+
+# identify SONAME used by dynamic loader
+objdump -x some_lib | grep SONAME
+
+# identify runtime library search paths
+objdump -x some_elf_file | grep PATH
+
+# identify path of dynamic loader (search for "interp")
+objdump -s some_elf_file | less
diff --git a/schulung_tools/linking/hello/shared_library.txt b/schulung_tools/linking/hello/shared_library.txt
new file mode 100644
index 0000000..1dd0f9f
--- /dev/null
+++ b/schulung_tools/linking/hello/shared_library.txt
@@ -0,0 +1,20 @@
+# build shared library
+gcc -fPIC -shared -Wl,-soname,libhello.so.1 -olibhello.so.1.0.3 libhello.c
+
+# install shared library
+sudo mkdir -p /opt/myfirma/lib
+sudo cp libhello.so.1.0.3 /opt/myfirma/lib
+
+# add custom path to dynamic loader search path
+echo "/opt/myfirma/lib" | sudo tee /etc/ld.so.conf.d/myfirma.conf
+
+# refresh dynamic loader cache
+sudo ldconfig
+
+# verify custom library is in cache
+/sbin/ldconfig -p | grep libhello.so.1
+
+# compile program using custom library
+ln -s /opt/myfirma/lib/libhello.so.1.0.3 libhello.so
+gcc -L. -ohello hello.c -lhello
+rm libhello.so