From ab90ef452dbd079a96e35e24bac30f4362913eb3 Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Sat, 14 Sep 2024 11:25:36 +0200 Subject: [PATCH] Add dynamic linking support JIRA: RTOS-664 --- .../rootfs-overlay/etc/rc.psh | 1 + .../rootfs-overlay/etc/rc.psh | 1 + .../rootfs-overlay/etc/rc.psh | 1 + .../rootfs-overlay/etc/rc.psh | 1 + .../rootfs-overlay/etc/rc.psh | 1 + .../rootfs-overlay/etc/rc.psh | 6 +++ _user/dlopen/Makefile | 20 ++++++++++ _user/dlopen/hello.c | 24 +++++++++++ _user/dlopen/hello.h | 21 ++++++++++ _user/dlopen/main.c | 40 +++++++++++++++++++ _user/sharedlib/Makefile | 21 ++++++++++ _user/sharedlib/dyn.c | 25 ++++++++++++ _user/sharedlib/dyn.h | 22 ++++++++++ _user/sharedlib/main.c | 25 ++++++++++++ libphoenix | 2 +- phoenix-rtos-build | 2 +- phoenix-rtos-kernel | 2 +- phoenix-rtos-tests | 2 +- phoenix-rtos-utils | 2 +- 19 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 _projects/sparcv8leon3-generic-qemu/rootfs-overlay/etc/rc.psh create mode 100644 _user/dlopen/Makefile create mode 100644 _user/dlopen/hello.c create mode 100644 _user/dlopen/hello.h create mode 100644 _user/dlopen/main.c create mode 100644 _user/sharedlib/Makefile create mode 100644 _user/sharedlib/dyn.c create mode 100644 _user/sharedlib/dyn.h create mode 100644 _user/sharedlib/main.c diff --git a/_projects/armv7a7-imx6ull-evk/rootfs-overlay/etc/rc.psh b/_projects/armv7a7-imx6ull-evk/rootfs-overlay/etc/rc.psh index 7e2508a9..584f5d55 100644 --- a/_projects/armv7a7-imx6ull-evk/rootfs-overlay/etc/rc.psh +++ b/_projects/armv7a7-imx6ull-evk/rootfs-overlay/etc/rc.psh @@ -7,4 +7,5 @@ W /sbin/dummyfs -m /var -D W /sbin/dummyfs -m /tmp -D X /sbin/lwip enet:0x02188000:150:PHY:0.2:irq:-5:/dev/gpio5 enet:0x020b4000:152:no-mdio:PHY:0.1:irq:-6:/dev/gpio5 X /bin/posixsrv +W /bin/ldconfig X /bin/psh diff --git a/_projects/armv7a9-zynq7000-qemu/rootfs-overlay/etc/rc.psh b/_projects/armv7a9-zynq7000-qemu/rootfs-overlay/etc/rc.psh index 75ebe493..49b710ec 100755 --- a/_projects/armv7a9-zynq7000-qemu/rootfs-overlay/etc/rc.psh +++ b/_projects/armv7a9-zynq7000-qemu/rootfs-overlay/etc/rc.psh @@ -5,4 +5,5 @@ W /bin/bind devfs /dev W /sbin/dummyfs -m /tmp -D X /bin/posixsrv T /dev/console +W /bin/ldconfig X /linuxrc diff --git a/_projects/armv7a9-zynq7000-zedboard/rootfs-overlay/etc/rc.psh b/_projects/armv7a9-zynq7000-zedboard/rootfs-overlay/etc/rc.psh index 55b8f507..d225120d 100644 --- a/_projects/armv7a9-zynq7000-zedboard/rootfs-overlay/etc/rc.psh +++ b/_projects/armv7a9-zynq7000-zedboard/rootfs-overlay/etc/rc.psh @@ -4,4 +4,5 @@ export HOME=/root W /bin/bind devfs /dev W /sbin/dummyfs -m /tmp -D X /bin/posixsrv +W /bin/ldconfig X /bin/psh diff --git a/_projects/ia32-generic-qemu/rootfs-overlay/etc/rc.psh b/_projects/ia32-generic-qemu/rootfs-overlay/etc/rc.psh index 803b70d7..74237c7f 100755 --- a/_projects/ia32-generic-qemu/rootfs-overlay/etc/rc.psh +++ b/_projects/ia32-generic-qemu/rootfs-overlay/etc/rc.psh @@ -6,4 +6,5 @@ W /sbin/dummyfs -m /tmp -D X /bin/posixsrv X /sbin/lwip rtl:0x18:11 T /dev/console +W /bin/ldconfig X /bin/psh diff --git a/_projects/riscv64-generic-qemu/rootfs-overlay/etc/rc.psh b/_projects/riscv64-generic-qemu/rootfs-overlay/etc/rc.psh index 55b8f507..d225120d 100644 --- a/_projects/riscv64-generic-qemu/rootfs-overlay/etc/rc.psh +++ b/_projects/riscv64-generic-qemu/rootfs-overlay/etc/rc.psh @@ -4,4 +4,5 @@ export HOME=/root W /bin/bind devfs /dev W /sbin/dummyfs -m /tmp -D X /bin/posixsrv +W /bin/ldconfig X /bin/psh diff --git a/_projects/sparcv8leon3-generic-qemu/rootfs-overlay/etc/rc.psh b/_projects/sparcv8leon3-generic-qemu/rootfs-overlay/etc/rc.psh new file mode 100644 index 00000000..6a81cbd8 --- /dev/null +++ b/_projects/sparcv8leon3-generic-qemu/rootfs-overlay/etc/rc.psh @@ -0,0 +1,6 @@ +:{}: +export PATH=/bin:/sbin:/usr/bin:/usr/sbin +export HOME=/root +W /bin/bind devfs /dev +X /bin/posixsrv +X /bin/psh diff --git a/_user/dlopen/Makefile b/_user/dlopen/Makefile new file mode 100644 index 00000000..90ccde7f --- /dev/null +++ b/_user/dlopen/Makefile @@ -0,0 +1,20 @@ +# +# Makefile for user application +# +# Copyright 2024 Phoenix Systems +# +ifeq ($(HAVE_SHLIB), y) + +NAME := libhello +LOCAL_HEADERS := hello.h +LOCAL_SRCS := hello.c + +include $(shared-lib.mk) + + +NAME := dlopen +LOCAL_SRCS := main.c + +include $(binary-dyn.mk) + +endif diff --git a/_user/dlopen/hello.c b/_user/dlopen/hello.c new file mode 100644 index 00000000..bd7767f0 --- /dev/null +++ b/_user/dlopen/hello.c @@ -0,0 +1,24 @@ +/* + * Phoenix-RTOS + * + * dlopen + * + * Example of user application using dlopen. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include "hello.h" + +#include + + +int hello(void) +{ + return printf("Hello shared world!\n"); +} diff --git a/_user/dlopen/hello.h b/_user/dlopen/hello.h new file mode 100644 index 00000000..7637be1f --- /dev/null +++ b/_user/dlopen/hello.h @@ -0,0 +1,21 @@ +/* + * Phoenix-RTOS + * + * dlopen + * + * Example of user application using dlopen. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#ifndef _USER_DLOPEN_DYN_H_ +#define _USER_DLOPEN_DYN_H_ + +int hello(void); + +#endif diff --git a/_user/dlopen/main.c b/_user/dlopen/main.c new file mode 100644 index 00000000..0b14f7c2 --- /dev/null +++ b/_user/dlopen/main.c @@ -0,0 +1,40 @@ +/* + * Phoenix-RTOS + * + * dlopen + * + * Example of user application using dlopen. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include +#include +#include +#include + +int main(void) +{ + void *handle = dlopen("/usr/lib/libhello.so", RTLD_LAZY); + if (handle == NULL) { + printf("%s\n", dlerror()); + return 1; + } + int (*hello)(void) = (int (*)(void))dlsym(handle, "hello"); + if (hello == NULL) { + printf("%s\n", dlerror()); + (void)dlclose(handle); + return 1; + } + + hello(); + + (void)dlclose(handle); + + return 0; +} diff --git a/_user/sharedlib/Makefile b/_user/sharedlib/Makefile new file mode 100644 index 00000000..8adb4b7f --- /dev/null +++ b/_user/sharedlib/Makefile @@ -0,0 +1,21 @@ +# +# Makefile for user application +# +# Copyright 2024 Phoenix Systems +# +ifeq ($(HAVE_SHLIB), y) + +NAME := libdyn +LOCAL_HEADERS := dyn.h +LOCAL_SRCS := dyn.c + +include $(shared-lib.mk) + + +NAME := sharedlib +LOCAL_SRCS := main.c +DEP_LIBS_SHARED := libdyn + +include $(binary-dyn.mk) + +endif diff --git a/_user/sharedlib/dyn.c b/_user/sharedlib/dyn.c new file mode 100644 index 00000000..281f0321 --- /dev/null +++ b/_user/sharedlib/dyn.c @@ -0,0 +1,25 @@ +/* + * Phoenix-RTOS + * + * sharedlib + * + * Example of user application using shared libraries. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include "dyn.h" + + +extern void *_DYNAMIC; + + +void *dyn(void) +{ + return &_DYNAMIC; +} diff --git a/_user/sharedlib/dyn.h b/_user/sharedlib/dyn.h new file mode 100644 index 00000000..02ce4ed7 --- /dev/null +++ b/_user/sharedlib/dyn.h @@ -0,0 +1,22 @@ +/* + * Phoenix-RTOS + * + * sharedlib + * + * Example of user application using shared libraries. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + + +#ifndef _USER_SHAREDLIB_DYN_H_ +#define _USER_SHAREDLIB_DYN_H_ + +void *dyn(void); + +#endif diff --git a/_user/sharedlib/main.c b/_user/sharedlib/main.c new file mode 100644 index 00000000..904214b4 --- /dev/null +++ b/_user/sharedlib/main.c @@ -0,0 +1,25 @@ +/* + * Phoenix-RTOS + * + * sharedlib + * + * Example of user application using shared libraries. + * + * Copyright 2024 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include + +#include "dyn.h" + +int main(void) +{ + printf("Dynamic section pointer %p\n", dyn()); + + return 0; +} diff --git a/libphoenix b/libphoenix index 4296f3a8..046a16f2 160000 --- a/libphoenix +++ b/libphoenix @@ -1 +1 @@ -Subproject commit 4296f3a8c54731f48030908441de509c09d3cf2e +Subproject commit 046a16f2839b6b9d2b635298ee54c82c65c0d5d9 diff --git a/phoenix-rtos-build b/phoenix-rtos-build index e2683246..b7827d7f 160000 --- a/phoenix-rtos-build +++ b/phoenix-rtos-build @@ -1 +1 @@ -Subproject commit e26832465712b8acb8eb814ac81f9a7943febd10 +Subproject commit b7827d7f748d316783ae8ec5f54aa18a738ec932 diff --git a/phoenix-rtos-kernel b/phoenix-rtos-kernel index 84b66c29..63cefa62 160000 --- a/phoenix-rtos-kernel +++ b/phoenix-rtos-kernel @@ -1 +1 @@ -Subproject commit 84b66c29a2eb68b0273c4a8e2f251e92cc854de1 +Subproject commit 63cefa62e429533999f84a01ab4694118a0f7fc9 diff --git a/phoenix-rtos-tests b/phoenix-rtos-tests index 91e0a3ea..e97f122a 160000 --- a/phoenix-rtos-tests +++ b/phoenix-rtos-tests @@ -1 +1 @@ -Subproject commit 91e0a3ea53f759dacae62aac143375a4ea7ce957 +Subproject commit e97f122a7e9c59e545fcf87fa787707eac2d99f4 diff --git a/phoenix-rtos-utils b/phoenix-rtos-utils index 3165f3a8..7bdb54fc 160000 --- a/phoenix-rtos-utils +++ b/phoenix-rtos-utils @@ -1 +1 @@ -Subproject commit 3165f3a85331dff9e320b10de509a444b621fe32 +Subproject commit 7bdb54fc955cf6aa7fc5d3c8e4d427c961ced78e