summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-06 12:59:56 +0200
committerCarlos Maiolino <[email protected]>2025-09-06 13:01:26 +0200
commit4d1c070b813f3b3b64bd8bf96723756041840ac0 (patch)
tree72a58d5c43e218e2700c30ac29db1b5a98ba8b65
parent133a564a7ba1f57ff229e7c181227d43b4bae584 (diff)
Create shared library
Play with shared libs. Building the same library with different functionalities and loading them at runtime, separately. Fun way to see how we can change runtime behavior without recompiling everything. Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--.gitignore2
-rw-r--r--C/HF/chap8/Makefile24
-rwxr-xr-xC/HF/chap8/bin/ecatbin12944 -> 0 bytes
-rw-r--r--C/HF/chap8/bin/libs/libencrypt.abin3754 -> 0 bytes
-rw-r--r--C/HF/chap8/src/libs/encrypt2.c11
5 files changed, 37 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 5761abc..33ec174 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
*.o
+*.so
+*.a
diff --git a/C/HF/chap8/Makefile b/C/HF/chap8/Makefile
index df3283e..8377c1b 100644
--- a/C/HF/chap8/Makefile
+++ b/C/HF/chap8/Makefile
@@ -13,3 +13,27 @@ bin/libs/xor.o: $(INCLUDE_DIR)/libs/xor.h src/libs/xor.c
bin/libs/encrypt.o: $(INCLUDE_DIR)/libs/encrypt.h src/libs/encrypt.c
cc -I $(INCLUDE_DIR) -c src/libs/encrypt.c -o bin/libs/encrypt.o
+
+# Create DSO and PIC versions
+#
+ecatS: bin/libs/libencrypt.a
+ cc -I $(INCLUDE_DIR) -L bin/libs -o bin/ecats src/ecat.c -lencrypts
+
+bin/libs/libencrypts.so: bin/libs/xor-pic.o bin/libs/encrypt-pic.o
+ cc -I $(INCLUDE_DIR) -shared -o bin/libs/libencrypts.so bin/libs/xor-pic.o bin/libs/encrypt-pic.o
+
+# This creates the same lib with a broken encrypt_arr call.
+# We can run the same program passing the different lib at runtime
+# and change the behavior of encrypt_arr at runtime
+bin/broken_libs/libencrypts.so: bin/libs/xor-pic.o bin/libs/encrypt2-pic.o
+ cc -I $(INCLUDE_DIR) -shared -o bin/broken_libs/libencrypts.so bin/libs/xor-pic.o bin/libs/encrypt2-pic.o
+
+
+bin/libs/xor-pic.o: $(INCLUDE_DIR)/libs/xor.h src/libs/xor.c
+ cc -fPIC -I $(INCLUDE_DIR) -c src/libs/xor.c -o bin/libs/xor-pic.o
+
+bin/libs/encrypt-pic.o: $(INCLUDE_DIR)/libs/encrypt.h src/libs/encrypt.c
+ cc -fPIC -I $(INCLUDE_DIR) -c src/libs/encrypt.c -o bin/libs/encrypt-pic.o
+
+bin/libs/encrypt2-pic.o: $(INCLUDE_DIR)/libs/encrypt.h src/libs/encrypt2.c
+ cc -fPIC -I $(INCLUDE_DIR) -c src/libs/encrypt2.c -o bin/libs/encrypt2-pic.o
diff --git a/C/HF/chap8/bin/ecat b/C/HF/chap8/bin/ecat
deleted file mode 100755
index 1167d31..0000000
--- a/C/HF/chap8/bin/ecat
+++ /dev/null
Binary files differ
diff --git a/C/HF/chap8/bin/libs/libencrypt.a b/C/HF/chap8/bin/libs/libencrypt.a
deleted file mode 100644
index f609c17..0000000
--- a/C/HF/chap8/bin/libs/libencrypt.a
+++ /dev/null
Binary files differ
diff --git a/C/HF/chap8/src/libs/encrypt2.c b/C/HF/chap8/src/libs/encrypt2.c
new file mode 100644
index 0000000..939df8b
--- /dev/null
+++ b/C/HF/chap8/src/libs/encrypt2.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <libs/encrypt.h>
+
+void encrypt(char *message, encrypt_fn_t encrypt_fn) {
+ encrypt_fn(message);
+}
+
+void encrypt_arr(char *message, encrypt_arr_t fn_arr, int size)
+{
+ printf("BROKEN LIB\n");
+}