From 2e2e59824e5d3a8c95cd7f7d575f6664aa0f61e7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 14 Dec 2024 11:22:59 -0800 Subject: [PATCH] Add test case for 'link_early_args' parameter. Test to make sure using --defsym in the link_early_args parameter is passed in the correct position in the linker command line. Both C and C++ tests validate that the link_early_args values are processed before objects and libraries by making sure that linking redirects from a symbol not defined by the library to a symbol that is defined by the library. Signed-off-by: Keith Packard --- .../common/281 link-early-args/cpplib.cpp | 10 +++++++ .../common/281 link-early-args/cpplib.h | 13 +++++++++ .../common/281 link-early-args/cppmain.cpp | 5 ++++ .../common/281 link-early-args/libfile.c | 14 ++++++++++ test cases/common/281 link-early-args/main.c | 12 +++++++++ .../common/281 link-early-args/meson.build | 27 +++++++++++++++++++ .../common/281 link-early-args/test.json | 2 ++ 7 files changed, 83 insertions(+) create mode 100644 test cases/common/281 link-early-args/cpplib.cpp create mode 100644 test cases/common/281 link-early-args/cpplib.h create mode 100644 test cases/common/281 link-early-args/cppmain.cpp create mode 100644 test cases/common/281 link-early-args/libfile.c create mode 100644 test cases/common/281 link-early-args/main.c create mode 100644 test cases/common/281 link-early-args/meson.build create mode 100644 test cases/common/281 link-early-args/test.json diff --git a/test cases/common/281 link-early-args/cpplib.cpp b/test cases/common/281 link-early-args/cpplib.cpp new file mode 100644 index 000000000..c337a5270 --- /dev/null +++ b/test cases/common/281 link-early-args/cpplib.cpp @@ -0,0 +1,10 @@ +#define BUILDING_DLL +#include "cpplib.h" + +int DLL_PUBLIC cppfunc(void) { + return 42; +} + +int DLL_PUBLIC cppfunc_sym(void) { + return 43; +} diff --git a/test cases/common/281 link-early-args/cpplib.h b/test cases/common/281 link-early-args/cpplib.h new file mode 100644 index 000000000..4b000780b --- /dev/null +++ b/test cases/common/281 link-early-args/cpplib.h @@ -0,0 +1,13 @@ +/* See http://gcc.gnu.org/wiki/Visibility#How_to_use_the_new_C.2B-.2B-_visibility_support */ +#if defined(_WIN32) || defined(__CYGWIN__) + #ifdef BUILDING_DLL + #define DLL_PUBLIC __declspec(dllexport) + #else + #define DLL_PUBLIC __declspec(dllimport) + #endif +#else + #define DLL_PUBLIC __attribute__ ((visibility ("default"))) +#endif + +int DLL_PUBLIC cppfunc_sym(void); +int DLL_PUBLIC cppfunc(void); diff --git a/test cases/common/281 link-early-args/cppmain.cpp b/test cases/common/281 link-early-args/cppmain.cpp new file mode 100644 index 000000000..602b66231 --- /dev/null +++ b/test cases/common/281 link-early-args/cppmain.cpp @@ -0,0 +1,5 @@ +#include "cpplib.h" + +int main(void) { + return cppfunc_sym() != 42; +} diff --git a/test cases/common/281 link-early-args/libfile.c b/test cases/common/281 link-early-args/libfile.c new file mode 100644 index 000000000..91489b287 --- /dev/null +++ b/test cases/common/281 link-early-args/libfile.c @@ -0,0 +1,14 @@ +#if defined _WIN32 || defined __CYGWIN__ + #define DLL_PUBLIC __declspec(dllexport) +#else + #if defined __GNUC__ + #define DLL_PUBLIC __attribute__ ((visibility("default"))) + #else + #pragma message ("Compiler does not support symbol visibility.") + #define DLL_PUBLIC + #endif +#endif + +int DLL_PUBLIC func(void) { + return 0; +} diff --git a/test cases/common/281 link-early-args/main.c b/test cases/common/281 link-early-args/main.c new file mode 100644 index 000000000..90f6e9dec --- /dev/null +++ b/test cases/common/281 link-early-args/main.c @@ -0,0 +1,12 @@ +#if defined _WIN32 || defined __CYGWIN__ + #define DLL_IMPORT __declspec(dllimport) +#else + #define DLL_IMPORT +#endif + +int DLL_IMPORT func_sym(void); +int DLL_IMPORT func(void); + +int main(void) { + return func_sym(); +} diff --git a/test cases/common/281 link-early-args/meson.build b/test cases/common/281 link-early-args/meson.build new file mode 100644 index 000000000..80ffdcacf --- /dev/null +++ b/test cases/common/281 link-early-args/meson.build @@ -0,0 +1,27 @@ +project('link_early_args linking test', ['c', 'cpp']) + +message('host_machine ' + host_machine.system() + ' build_machine ' + build_machine.system()) + +if host_machine.system() == 'linux' + c_link_early_args = '-Wl,--defsym=func_sym=func' + cpp_link_early_args = '-Wl,--defsym=_Z11cppfunc_symv=_Z7cppfuncv' +else + message('unsupported on ' + host_machine.system()) + c_link_early_args = '' + cpp_link_early_args = '' +endif + +if c_link_early_args != '' + lib = static_library('mylib', 'libfile.c', install : false) + exe = executable('prog', 'main.c', + link_early_args : c_link_early_args, + link_with : lib) + + test('runtest', exe) + + cpplib = static_library('mycpplib', 'cpplib.cpp') + cppexe = executable('cppprog', 'cppmain.cpp', + link_early_args : cpp_link_early_args, + link_with : cpplib) + test('cpptest', cppexe) +endif diff --git a/test cases/common/281 link-early-args/test.json b/test cases/common/281 link-early-args/test.json new file mode 100644 index 000000000..2c63c0851 --- /dev/null +++ b/test cases/common/281 link-early-args/test.json @@ -0,0 +1,2 @@ +{ +}