Skip to content

Commit

Permalink
Add test case for 'link_syms' parameter.
Browse files Browse the repository at this point in the history
Test to make sure using --defsym in the link_syms parameter causes
correct linker behavior between object files and static libraries for
both C and C++.

The C test validates that linking redirects from a symbol not defined
by the library to a symbol that is defined by the library.

The C++ test validates that linking redirects from one defined symbol
to another defined symbol.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed Dec 14, 2024
1 parent f480f2b commit 26d7363
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test cases/common/281 link-syms/cpplib.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions test cases/common/281 link-syms/cpplib.h
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions test cases/common/281 link-syms/cppmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "cpplib.h"

int main(void) {
return cppfunc_sym() != 42;
}
14 changes: 14 additions & 0 deletions test cases/common/281 link-syms/libfile.c
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions test cases/common/281 link-syms/main.c
Original file line number Diff line number Diff line change
@@ -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();
}
25 changes: 25 additions & 0 deletions test cases/common/281 link-syms/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
project('link_syms linking test', ['c', 'cpp'])

if build_machine.system() == 'linux'
c_link_syms = '-Wl,--defsym=func_sym=func'
cpp_link_syms = '-Wl,--defsym=_Z11cppfunc_symv=_Z7cppfuncv'
else
message('unsupported on ' + build_machine.system())
c_link_syms = ''
cpp_link_syms = ''
endif

if c_link_syms != ''
lib = static_library('mylib', 'libfile.c', install : false)
exe = executable('prog', 'main.c',
link_syms : c_link_syms,
link_with : lib)

test('runtest', exe)

cpplib = static_library('mycpplib', 'cpplib.cpp')
cppexe = executable('cppprog', 'cppmain.cpp',
link_syms : cpp_link_syms,
link_with : cpplib)
test('cpptest', cppexe)
endif
2 changes: 2 additions & 0 deletions test cases/common/281 link-syms/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 26d7363

Please sign in to comment.