diff --git a/tests/demo/demo_dlopen_based/Makefile b/tests/demo/demo_dlopen_based/Makefile new file mode 100644 index 0000000..785dd6a --- /dev/null +++ b/tests/demo/demo_dlopen_based/Makefile @@ -0,0 +1,50 @@ +# * +# * If not stated otherwise in this file or this component's LICENSE file the +# * following copyright and licenses apply: +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * + +# Makefile for demonstrating weak vs strong symbols using dlopen + +ifeq ($(TARGET),) +CC = gcc +else +#CC = rm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer +endif +CFLAGS = -Wall -fPIC +LDFLAGS = -ldl + +# Targets +EXECUTABLE = main +SHARED_LIB = libplugin.so + +# Source files +MAIN_SRC = main.c +PLUGIN_SRC = plugin.c + +.PHONY: all clean + +all: $(EXECUTABLE) $(SHARED_LIB) + +$(EXECUTABLE): $(MAIN_SRC) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + +$(SHARED_LIB): $(PLUGIN_SRC) + $(CC) $(CFLAGS) -shared -o $@ $^ + +clean: + rm -f $(EXECUTABLE) $(SHARED_LIB) + diff --git a/tests/demo/demo_dlopen_based/README.md b/tests/demo/demo_dlopen_based/README.md new file mode 100644 index 0000000..07aed1e --- /dev/null +++ b/tests/demo/demo_dlopen_based/README.md @@ -0,0 +1,83 @@ +# Plugin Loader Project using dlopen + +## Overview +This project demonstrates how to create a simple C program that dynamically loads a shared library (plugin) and executes its functions. The project consists of : +- a main program (`main.c`) that loads the plugin at runtime +- and interacts with it through an interface defined in `plugin.h`. +- The plugin itself is implemented in `plugin.c`. + +## Prerequisites + +To build and run this project, you need: + +- A C compiler (e.g., `gcc`) +- The `dl` library for dynamic linking (commonly available on Linux) +- GNU `make` + +## Compilation Instructions + +1. Build the project for the native architecture: + ```bash + make + ``` + +2. Build the project for an ARM target: + ```bash + make TARGET=arm + ``` + Ensure you have the appropriate cross-compiler toolchain configured for ARM in the `Makefile`. + +3. Clean up the generated files: + ```bash + make clean + ``` + +## Usage + +The program dynamically loads the shared library and attempts to use the strong implementations. If a strong implementation is not found, the weak implementation is used instead. + +1. Example Output for linux + ```bash + ./main + Plugin initialized. + Plugin action performed. + Plugin cleaned up. + ``` + +2. Example Output for arm + ```bash + root@xione-uk:/opt/jyo# ./main + Plugin initialized. + Plugin action performed. + Plugin cleaned up. + ``` + +3. Example when plugin not found: + ```bash + Plugin not found. Running without plugin. + ``` + +## Project Details + +**main.c:** + +This file contains the main program that dynamically loads the libplugin.so shared library and uses the PluginInterface to call the plugin’s functions. + +**plugin.c:** + +This file defines the implementation of the functions specified in the PluginInterface. The functions include plugin_initialize(), plugin_perform_action(), and plugin_cleanup(). + +**plugin.h:** + +The header file that defines the PluginInterface structure, which declares the function pointers used by the plugin. + + +## Dependencies + +- GCC for compilation. +- `libdl` (part of the standard library) for dynamic linking. + +## Notes + +- Ensure the shared library (`libplugin.so`) is located in the same directory as the `main` executable, or adjust the `LD_LIBRARY_PATH` environment variable to include its location. +- This example is designed for educational purposes and demonstrates basic concepts of dynamic linking and symbol resolution in C. \ No newline at end of file diff --git a/tests/demo/demo_dlopen_based/main.c b/tests/demo/demo_dlopen_based/main.c new file mode 100644 index 0000000..9836f52 --- /dev/null +++ b/tests/demo/demo_dlopen_based/main.c @@ -0,0 +1,73 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file main.c + * @brief Demonstrates the usage of weak symbols and dynamic loading with `dlopen` and `dlsym`. + * + * This program attempts to load strong implementations of functions from a shared library. + * If a strong implementation is not found, the weak implementations in this file are used. + */ + +/** + * @file main.c + * @brief Main program to demonstrate dynamic loading of a plugin. + * + * This program attempts to dynamically load a shared library plugin and + * execute its functions if available. If the plugin is not found, it runs without the plugin. + */ + +#include +#include +#include "plugin.h" + +/** + * @brief Entry point for the program. + * + * Attempts to dynamically load a plugin shared library and execute its + * interface functions (`initialize`, `perform_action`, `cleanup`). If the + * plugin is unavailable, the program proceeds without it. + * + * @returns 0 on successful execution. + */ +int main(void) +{ + void *handle; /**< Handle for the dynamically loaded shared library. */ + PluginInterface *plugin = NULL; /**< Pointer to the plugin interface. */ + + // Attempt to load the plugin dynamically + handle = dlopen("./libplugin.so", RTLD_LAZY); + if (handle) + { + plugin = (PluginInterface *)dlsym(handle, "plugin"); + if (plugin) + { + plugin->initialize(); + plugin->perform_action(); + plugin->cleanup(); + } + dlclose(handle); + } + else + { + printf("Plugin not found. Running without plugin.\n"); + } + + return 0; +} \ No newline at end of file diff --git a/tests/demo/demo_dlopen_based/plugin.c b/tests/demo/demo_dlopen_based/plugin.c new file mode 100644 index 0000000..3a75fe1 --- /dev/null +++ b/tests/demo/demo_dlopen_based/plugin.c @@ -0,0 +1,70 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file plugin.c + * @brief Implementation of the plugin interface. + * + * This file defines the functions that form the plugin's interface, + * including initialization, performing an action, and cleanup. + */ + +#include "plugin.h" +#include + +/** + * @brief Initializes the plugin. + * + * This function performs any necessary setup for the plugin. + */ +void plugin_initialize(void) +{ + printf("Plugin initialized.\n"); +} + +/** + * @brief Executes the main action of the plugin. + * + * This function is called to perform the core functionality of the plugin. + */ +void plugin_perform_action(void) +{ + printf("Plugin action performed.\n"); +} + +/** + * @brief Cleans up the plugin. + * + * This function performs any necessary cleanup before the plugin is unloaded. + */ +void plugin_cleanup(void) +{ + printf("Plugin cleaned up.\n"); +} + +/** + * @brief Global instance of the plugin interface. + * + * This structure defines the plugin's interface, mapping its functions to the + * corresponding implementation. + */ +PluginInterface plugin = { + .initialize = plugin_initialize, + .perform_action = plugin_perform_action, + .cleanup = plugin_cleanup}; \ No newline at end of file diff --git a/tests/demo/demo_dlopen_based/plugin.h b/tests/demo/demo_dlopen_based/plugin.h new file mode 100644 index 0000000..3ce745e --- /dev/null +++ b/tests/demo/demo_dlopen_based/plugin.h @@ -0,0 +1,66 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file plugin.h + * @brief Defines the interface for the plugin. + * + * This header file declares the `PluginInterface` structure, which contains + * function pointers representing the plugin's operations: initialization, + * performing an action, and cleanup. + */ + +#ifndef PLUGIN_H +#define PLUGIN_H + +/** + * @brief Structure representing the plugin interface. + * + * This structure contains pointers to functions that define the behavior + * of a plugin. These functions are expected to be implemented by any plugin + * conforming to this interface. + */ +typedef struct +{ + /** + * @brief Initializes the plugin. + * + * This function pointer should point to a function that sets up the plugin + * before it is used. + */ + void (*initialize)(void); + + /** + * @brief Performs the primary action of the plugin. + * + * This function pointer should point to a function that executes the main + * functionality of the plugin. + */ + void (*perform_action)(void); + + /** + * @brief Cleans up the plugin. + * + * This function pointer should point to a function that releases any + * resources or performs finalization tasks for the plugin. + */ + void (*cleanup)(void); +} PluginInterface; + +#endif // PLUGIN_H \ No newline at end of file diff --git a/tests/demo/demo_weak_vs_strong_library_based/Makefile b/tests/demo/demo_weak_vs_strong_library_based/Makefile new file mode 100644 index 0000000..16bd20b --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/Makefile @@ -0,0 +1,51 @@ +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * + + +ifeq ($(TARGET),) +CC = gcc +else +#CC = rm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer +endif +CFLAGS = -Wall -Wextra +LDFLAGS = + +all: weak_linked_first_static_lib strong_linked_first_static_lib weak_first_link_shared_lib strong_first_link_shared_lib + +libweak/libweak.a: + $(MAKE) -C libweak/ all + +libstrong/libstrong.a: + $(MAKE) -C libstrong/ all + +weak_linked_first_static_lib: main.c libweak/libweak.a libstrong/libstrong.a + $(CC) $(CFLAGS) -o weak_linked_first_static_lib main.c -Llibweak -lweak -Llibstrong -lstrong + +strong_linked_first_static_lib: main.c libweak/libweak.a libstrong/libstrong.a + $(CC) $(CFLAGS) -o strong_linked_first_static_lib main.c -Llibstrong -lstrong -Llibweak -lweak + +weak_first_link_shared_lib: main.c libweak/libweak1.so libstrong/libstrong1.so + $(CC) $(CFLAGS) -o weak_first_link_shared_lib main.c -Llibweak -lweak1 -Llibstrong -lstrong1 + cp libweak/libweak1.so . + cp libstrong/libstrong1.so . + +strong_first_link_shared_lib: main.c libweak/libweak1.so libstrong/libstrong1.so + $(CC) $(CFLAGS) -o strong_first_link_shared_lib main.c -Llibstrong -lstrong1 -Llibweak -lweak1 + +clean: + $(MAKE) -C libweak clean + $(MAKE) -C libstrong clean + rm -f weak* strong* libstrong1.so libweak1.so diff --git a/tests/demo/demo_weak_vs_strong_library_based/README.md b/tests/demo/demo_weak_vs_strong_library_based/README.md new file mode 100644 index 0000000..08ad8f1 --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/README.md @@ -0,0 +1,131 @@ +# Demonstration of Weak and Strong Symbols in C + +This project demonstrates the concept of weak and strong symbols in C, showcasing how symbol resolution order affects program behavior. It includes both static and dynamic linking examples using libraries. + +## Project Structure + +### Directories: +- `libweak/`: Contains the weak implementations of functions, built as static (`libweak.a`) and shared (`libweak1.so`) libraries. +- `libstrong/`: Contains the strong implementations of functions, built as static (`libstrong.a`) and shared (`libstrong1.so`) libraries. + +### Targets: +1. **Static Linking**: + - `weak_linked_first_static_lib`: Links `main.c` with `libweak.a` first, followed by `libstrong.a`. + - `strong_linked_first_static_lib`: Links `main.c` with `libstrong.a` first, followed by `libweak.a`. +2. **Dynamic Linking**: + - `weak_first_link_shared_lib`: Dynamically links `main.c` with `libweak1.so` first, followed by `libstrong1.so`. + - `strong_first_link_shared_lib`: Dynamically links `main.c` with `libstrong1.so` first, followed by `libweak1.so`. + +### Makefile Targets: +- `all`: Builds all executables for both static and dynamic linking examples. +- `clean`: Cleans up all generated files and libraries. + +## Build Instructions + +1. Build the project for linux: + ```bash + make + ``` + +2. Build the project for arm: + ```bash + make TARGET=arm + ``` + +3. Clean the project: + ```bash + make clean + ``` + +## Usage + +### Static Linking +1. Run `weak_linked_first_static_lib`: + ```bash + ./weak_linked_first_static_lib + ``` + Links `libweak.a` first, so weak symbols take precedence. + +2. Run `strong_linked_first_static_lib`: + ```bash + ./strong_linked_first_static_lib + ``` + Links `libstrong.a` first, so strong symbols take precedence. + +### Dynamic Linking +1. Run `weak_first_link_shared_lib`: + ```bash + ./weak_first_link_shared_lib + ``` + Dynamically links `libweak1.so` first, so weak symbols take precedence unless overridden. + +2. Run `strong_first_link_shared_lib`: + ```bash + ./strong_first_link_shared_lib + ``` + Dynamically links `libstrong1.so` first, so strong symbols take precedence. + +### With run.sh +1. For linux + ```bash + ./run.sh +Using strong_linked_first_static_lib +This is the strong implementation of my_function. +This is the strong implementation of func() weak_prototype from strong.c. +This is the weak implementation of func() weak_implementation_only . +Using weak_linked_first_static_lib +This is the weak implementation of my_function. +This is the weak implementation of func() weak_implementation_only . +Using strong_first_link_shared_lib +This is the strong implementation of my_function. +This is the strong implementation of func() weak_prototype from strong.c. +This is the weak implementation of func() weak_implementation_only . +Using weak_first_link_shared_lib +This is the weak implementation of my_function. +This is the weak implementation of func() weak_implementation_only . + + ``` +2. For arm +```bash +root@xione-uk:/opt/jyo# ./run.sh +Using strong_linked_first_static_lib +This is the strong implementation of my_function. +This is the strong implementation of func() weak_prototype from strong.c. +This is the weak implementation of func() weak_implementation_only . +Using weak_linked_first_static_lib +This is the weak implementation of my_function. +This is the weak implementation of func() weak_implementation_only . +Using strong_first_link_shared_lib +This is the strong implementation of my_function. +This is the strong implementation of func() weak_prototype from strong.c. +This is the weak implementation of func() weak_implementation_only . +Using weak_first_link_shared_lib +This is the weak implementation of my_function. +This is the strong implementation of func() weak_prototype from strong.c. +This is the weak implementation of func() weak_implementation_only . + ``` + +## Key Concepts + +- **Weak Symbols**: + - Defined with the `__attribute__((weak))` attribute. + - Serve as a default implementation that can be overridden by strong symbols. + +- **Strong Symbols**: + - Default behavior in C. + - Override weak symbols during linking. + +- **Static Linking**: + - Symbols are resolved at compile time. + - Precedence is determined by the order of libraries during the linking process. + +- **Dynamic Linking**: + - Symbols are resolved at runtime. + - Precedence is determined by the order of linked shared libraries. + +## Notes + +- Do ensure that both weak and strong implementations are in separate librararies and link the strong library first so that its symbols take precedence over the weak ones and if its not found in strong then it will fall back to weak. +- When a weak function is defined in the main executable, the linker resolves calls to that function within the same binary and does not look for overrides in dynamically loaded shared libraries. +- Ensure `libweak/` and `libstrong/` directories are built before running the targets. +- For dynamic linking, ensure `libweak1.so` and `libstrong1.so` are in the same directory as the executables or in the library search path (e.g., `LD_LIBRARY_PATH`). diff --git a/tests/demo/demo_weak_vs_strong_library_based/libstrong/Makefile b/tests/demo/demo_weak_vs_strong_library_based/libstrong/Makefile new file mode 100644 index 0000000..826e664 --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/libstrong/Makefile @@ -0,0 +1,41 @@ +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * + + +ifeq ($(TARGET),) +CC = gcc +else +#CC = rm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer +endif +CFLAGS = -Wall -Wextra -fPIC +LDFLAGS = -shared +AR = ar +ARFLAGS = rcs + +all: libstrong.a libstrong1.so + +libstrong.a: strong.o + $(AR) $(ARFLAGS) libstrong.a strong.o + +libstrong1.so: strong.o + $(CC) $(LDFLAGS) -shared -o libstrong1.so strong.o + +strong.o: strong.c + $(CC) $(CFLAGS) -c strong.c + + +clean: + rm -f *.o libstrong*.* diff --git a/tests/demo/demo_weak_vs_strong_library_based/libstrong/strong.c b/tests/demo/demo_weak_vs_strong_library_based/libstrong/strong.c new file mode 100644 index 0000000..6961a19 --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/libstrong/strong.c @@ -0,0 +1,50 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @file strong_my_function.c + * @brief Contains the strong implementation of `my_function`. + * + * This file provides the implementation of `my_function`, overriding any weak definitions + * if linked in the same binary. The function outputs a message to indicate that the strong + * implementation has been invoked. + */ + +#include + +/** + * @brief Strong implementation of the `my_function`. + * + * Prints a message to indicate that the strong implementation is being executed. + */ +void my_function() +{ + printf("This is the strong implementation of my_function.\n"); +} + +/** + * @brief Strong implementation of the `weak_prototype` function which is declared with weak prototype. + * + * Prints a message to indicate that the strong implementation is being executed. + */ +void weak_prototype() +{ + printf("This is the strong implementation of func() weak_prototype from strong.c.\n"); +} diff --git a/tests/demo/demo_weak_vs_strong_library_based/libweak/Makefile b/tests/demo/demo_weak_vs_strong_library_based/libweak/Makefile new file mode 100644 index 0000000..d66ee4c --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/libweak/Makefile @@ -0,0 +1,40 @@ +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * + + +ifeq ($(TARGET),) +CC = gcc +else +#CC = rm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer +endif +CFLAGS = -Wall -Wextra -fPIC +LDFLAGS = -shared +AR = ar +ARFLAGS = rcs + +all: libweak.a libweak1.so + +libweak.a: weak.o + $(AR) $(ARFLAGS) libweak.a weak.o + +libweak1.so: weak.o + $(CC) $(LDFLAGS) -shared -o libweak1.so weak.o + +weak.o: weak.c + $(CC) $(CFLAGS) -c weak.c + +clean: + rm -f *.o libweak*.* diff --git a/tests/demo/demo_weak_vs_strong_library_based/libweak/weak.c b/tests/demo/demo_weak_vs_strong_library_based/libweak/weak.c new file mode 100644 index 0000000..fcf9193 --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/libweak/weak.c @@ -0,0 +1,52 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @file weak_my_function.c + * @brief Contains the weak implementation of `my_function`. + * + * This file provides a weak definition of `my_function`. If a strong definition + * is linked into the binary, it will override this weak implementation. This function + * serves as a default behavior when no strong implementation is available. + */ + +#include + +/** + * @brief Weak implementation of the `my_function`. + * + * Prints a message to indicate that the weak implementation is being executed. + * If a strong implementation is linked, this function will be overridden. + */ +void __attribute__((weak)) my_function() +{ + printf("This is the weak implementation of my_function.\n"); +} + +/** + * @brief Weak implementation of the `weak_implementation_only`. + * + * Prints a message to indicate that the weak implementation is being executed. + * If a strong implementation is linked, this function will be overridden. + */ +void __attribute__((weak)) weak_implementation_only() +{ + printf("This is the weak implementation of func() weak_implementation_only .\n"); +} \ No newline at end of file diff --git a/tests/demo/demo_weak_vs_strong_library_based/main.c b/tests/demo/demo_weak_vs_strong_library_based/main.c new file mode 100644 index 0000000..c53a85a --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/main.c @@ -0,0 +1,71 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2023 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @file main.c + * @brief Demonstrates calling a function whose implementation is linked from a library. + * + * This file declares a function `my_function` and invokes it in the `main` function. + * The actual implementation of `my_function` is expected to be provided by an external library. + */ + +#include + +/** + * @brief Function declaration for `my_function`. + * + * The implementation of this function is linked from an external library. + * The behavior of this function depends on the library providing the implementation. + */ +void my_function(); + +/** + * @brief Weak Function declaration for `weak_prototype`. + * + * The implementation of this function is linked from an external library. + * The behavior of this function depends on the library providing the implementation. + */ +void __attribute__((weak)) weak_prototype(); + +/** + * @brief Function declaration for `weak_implementation_only`. + * + * The implementation of this function is linked from an external library. + * The behavior of this function depends on the library providing the implementation. + * Only weak implementation exists for this function + */ +void weak_implementation_only(); + +/** + * @brief Entry point of the program. + * + * Calls the `my_function` function, which is implemented in an external library. + * + * @returns 0 on successful execution. + */ +int main() { + my_function(); + if (weak_prototype) + { + weak_prototype(); + } + weak_implementation_only(); + return 0; +} diff --git a/tests/demo/demo_weak_vs_strong_library_based/run.sh b/tests/demo/demo_weak_vs_strong_library_based/run.sh new file mode 100755 index 0000000..20f44f1 --- /dev/null +++ b/tests/demo/demo_weak_vs_strong_library_based/run.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# * +# * If not stated otherwise in this file or this component's LICENSE file the +# * following copyright and licenses apply: +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * + +set -e # error out if required + +export LD_LIBRARY_PATH=/usr/lib:/lib:/home/root:./. + +echo "Using strong_linked_first_static_lib" +./strong_linked_first_static_lib + +echo "Using weak_linked_first_static_lib " +./weak_linked_first_static_lib + +echo "Using strong_first_link_shared_lib " +./strong_first_link_shared_lib + +echo "Using weak_first_link_shared_lib " +./weak_first_link_shared_lib