summaryrefslogtreecommitdiff
path: root/Demos/DynamicObject
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2020-10-10 18:26:16 +0300
committerAndreas Kling <kling@serenityos.org>2020-12-14 23:05:53 +0100
commit09ccdc697b02f5aa070523d3cbd918b00f550281 (patch)
tree94cac08a4b1936e245433155ed64feed6af44c96 /Demos/DynamicObject
parent79769ee74e5cf566dc10adb164b87f0a98e33277 (diff)
downloadserenity-09ccdc697b02f5aa070523d3cbd918b00f550281.zip
Demos: Add "DynamicObjectDemo" to demo the dynamic loader
Diffstat (limited to 'Demos/DynamicObject')
-rw-r--r--Demos/DynamicObject/CMakeLists.txt11
-rw-r--r--Demos/DynamicObject/SampleLib/CMakeLists.txt11
-rw-r--r--Demos/DynamicObject/SampleLib/lib.cpp20
-rw-r--r--Demos/DynamicObject/main.cpp24
4 files changed, 66 insertions, 0 deletions
diff --git a/Demos/DynamicObject/CMakeLists.txt b/Demos/DynamicObject/CMakeLists.txt
new file mode 100644
index 0000000000..9ac8b607b8
--- /dev/null
+++ b/Demos/DynamicObject/CMakeLists.txt
@@ -0,0 +1,11 @@
+set(SOURCES
+ main.cpp
+ ../../Libraries/LibC/crt0_shared.cpp
+)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostartfiles -pie -fpic")
+
+serenity_bin(DynamicObjectDemo)
+target_link_libraries(DynamicObjectDemo SampleLib LibCShared)
+
+add_subdirectory(SampleLib)
diff --git a/Demos/DynamicObject/SampleLib/CMakeLists.txt b/Demos/DynamicObject/SampleLib/CMakeLists.txt
new file mode 100644
index 0000000000..0233405a7e
--- /dev/null
+++ b/Demos/DynamicObject/SampleLib/CMakeLists.txt
@@ -0,0 +1,11 @@
+
+set(SOURCES
+ lib.cpp
+)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib -fpic")
+
+add_library(SampleLib SHARED ${SOURCES})
+target_link_libraries(SampleLib LibCShared)
+#target_link_libraries(SampleLib)
+install(TARGETS SampleLib DESTINATION usr/lib)
diff --git a/Demos/DynamicObject/SampleLib/lib.cpp b/Demos/DynamicObject/SampleLib/lib.cpp
new file mode 100644
index 0000000000..1bfb7693bd
--- /dev/null
+++ b/Demos/DynamicObject/SampleLib/lib.cpp
@@ -0,0 +1,20 @@
+
+#include "../lib.h"
+
+int func();
+
+__thread int g_tls1 = 0;
+__thread int g_tls2 = 0;
+
+static void init_function() __attribute__((constructor));
+
+void init_function()
+{
+ g_tls1 = 1;
+ g_tls2 = 2;
+}
+
+int func()
+{
+ return 3;
+}
diff --git a/Demos/DynamicObject/main.cpp b/Demos/DynamicObject/main.cpp
new file mode 100644
index 0000000000..c4d401d4ba
--- /dev/null
+++ b/Demos/DynamicObject/main.cpp
@@ -0,0 +1,24 @@
+#include "lib.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/internals.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int main(int argc, char** argv, char** env)
+{
+ (void)argc;
+ (void)argv;
+ (void)env;
+
+ printf("Well Hello Friends!\n");
+ printf("trying to open /etc/fstab for writing..\n");
+ int rc = open("/etc/fstab", O_RDWR);
+ if (rc == -1) {
+ int _errno = errno;
+ perror("open failed");
+ printf("rc: %d, errno: %d\n", rc, _errno);
+ return func() + g_tls1 + g_tls2;
+ }
+}