summaryrefslogtreecommitdiff
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
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>
-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
-rw-r--r--schulung_tools/linking/helloat/.gitignore15
-rw-r--r--schulung_tools/linking/helloat/AUTHORS0
-rw-r--r--schulung_tools/linking/helloat/ChangeLog0
-rw-r--r--schulung_tools/linking/helloat/Makefile.am20
-rw-r--r--schulung_tools/linking/helloat/NEWS0
-rw-r--r--schulung_tools/linking/helloat/README4
-rwxr-xr-xschulung_tools/linking/helloat/autogen.sh2
-rw-r--r--schulung_tools/linking/helloat/configure.ac25
-rw-r--r--schulung_tools/linking/helloat/func1.c6
-rw-r--r--schulung_tools/linking/helloat/func2.c6
-rw-r--r--schulung_tools/linking/helloat/hello.c12
17 files changed, 178 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
diff --git a/schulung_tools/linking/helloat/.gitignore b/schulung_tools/linking/helloat/.gitignore
new file mode 100644
index 0000000..9984288
--- /dev/null
+++ b/schulung_tools/linking/helloat/.gitignore
@@ -0,0 +1,15 @@
+Makefile.in
+aclocal.m4
+autom4te.cache/
+compile
+config.guess
+config.h.in
+config.h.in~
+config.log
+config.status
+config.sub
+configure
+depcomp
+install-sh
+ltmain.sh
+missing
diff --git a/schulung_tools/linking/helloat/AUTHORS b/schulung_tools/linking/helloat/AUTHORS
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/schulung_tools/linking/helloat/AUTHORS
diff --git a/schulung_tools/linking/helloat/ChangeLog b/schulung_tools/linking/helloat/ChangeLog
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/schulung_tools/linking/helloat/ChangeLog
diff --git a/schulung_tools/linking/helloat/Makefile.am b/schulung_tools/linking/helloat/Makefile.am
new file mode 100644
index 0000000..6dccd03
--- /dev/null
+++ b/schulung_tools/linking/helloat/Makefile.am
@@ -0,0 +1,20 @@
+bin_PROGRAMS = hello hello2
+
+hello_SOURCES = hello.c
+hello_LDFLAGS = -lfunc
+hello2_SOURCES = hello.c
+hello2_LDFLAGS = -static -lfunc
+
+lib_LTLIBRARIES = libfunc.la
+# 1) If the library source code has changed at all since the last update, then
+# increment revision ("c:r:a" becomes "c:r+1:a").
+# 2) If any interfaces have been added, removed, or changed since the last
+# update, increment current, and set revision to 0.
+# 3) If any interfaces have been added since the last public release,
+# then increment age.
+# 4) If any interfaces have been removed or changed since the last public
+# release, then set age to 0.
+libfunc_la_LDFLAGS = -release @PACKAGE_VERSION@ -version-info 0:0:0
+libfunc_la_SOURCES = func1.c func2.c
+
+ACLOCAL_AMFLAGS = '-I m4'
diff --git a/schulung_tools/linking/helloat/NEWS b/schulung_tools/linking/helloat/NEWS
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/schulung_tools/linking/helloat/NEWS
diff --git a/schulung_tools/linking/helloat/README b/schulung_tools/linking/helloat/README
new file mode 100644
index 0000000..14ffe6a
--- /dev/null
+++ b/schulung_tools/linking/helloat/README
@@ -0,0 +1,4 @@
+./autogen.sh
+./configure --prefix=/usr
+make
+make install DESTDIR=/tmp/test
diff --git a/schulung_tools/linking/helloat/autogen.sh b/schulung_tools/linking/helloat/autogen.sh
new file mode 100755
index 0000000..27fbc8e
--- /dev/null
+++ b/schulung_tools/linking/helloat/autogen.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+autoreconf -sif
diff --git a/schulung_tools/linking/helloat/configure.ac b/schulung_tools/linking/helloat/configure.ac
new file mode 100644
index 0000000..b412589
--- /dev/null
+++ b/schulung_tools/linking/helloat/configure.ac
@@ -0,0 +1,25 @@
+# -*- Autoconf -*-
+# Process this file with autoconf to produce a configure script.
+
+AC_PREREQ([2.69])
+AC_INIT([hello], [1.0], [manut@linutronix.de])
+AC_CONFIG_SRCDIR([func1.c])
+AC_CONFIG_HEADERS([config.h])
+
+AM_INIT_AUTOMAKE
+LT_INIT
+
+# Checks for programs.
+AC_PROG_CC
+
+
+# Checks for libraries.
+
+# Checks for header files.
+
+# Checks for typedefs, structures, and compiler characteristics.
+
+# Checks for library functions.
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/schulung_tools/linking/helloat/func1.c b/schulung_tools/linking/helloat/func1.c
new file mode 100644
index 0000000..ab006bd
--- /dev/null
+++ b/schulung_tools/linking/helloat/func1.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void func1(void)
+{
+ printf("Hello from %s!\n", __func__);
+}
diff --git a/schulung_tools/linking/helloat/func2.c b/schulung_tools/linking/helloat/func2.c
new file mode 100644
index 0000000..9dc05ab
--- /dev/null
+++ b/schulung_tools/linking/helloat/func2.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void func2(void)
+{
+ printf("Hello from %s!\n", __func__);
+}
diff --git a/schulung_tools/linking/helloat/hello.c b/schulung_tools/linking/helloat/hello.c
new file mode 100644
index 0000000..82b8d53
--- /dev/null
+++ b/schulung_tools/linking/helloat/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;
+}