Skip to content

Commit

Permalink
tests: Add test for symbol versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
qookei committed Dec 27, 2024
1 parent 8ff6644 commit 0c525e9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/rtld/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rtld_test_cases = [
'scope5',
'tls_align',
'relr',
'symver',
]

host_libc_rtld_nosan_test_cases = [
Expand Down
15 changes: 15 additions & 0 deletions tests/rtld/symver/libfoo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
int foo_v1(void) {
return 1;
}

int foo_v2(void) {
return 2;
}

int foo_v3(void) {
return 3;
}

asm(".symver foo_v1, foo@FOO_1");
asm(".symver foo_v2, foo@FOO_2");
asm(".symver foo_v3, foo@@FOO_3"); // default version (due to @@ vs @)
14 changes: 14 additions & 0 deletions tests/rtld/symver/libfoo.ver
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FOO_1 {
global: foo;
local: *;
};

FOO_2 {
global: foo;
local: *;
};

FOO_3 {
global: foo;
local: *;
};
11 changes: 11 additions & 0 deletions tests/rtld/symver/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version_script = meson.current_source_dir() / 'libfoo.ver'

libfoo = shared_library('foo', 'libfoo.c',
link_args : ['-Wl,--version-script', version_script])
test_depends = [libfoo]
test_link_with = [libfoo]

libfoo_native = shared_library('native-foo', 'libfoo.c', native: true,
link_args : ['-Wl,--version-script', version_script])
test_native_depends = [libfoo_native]
test_native_link_with = [libfoo_native]
39 changes: 39 additions & 0 deletions tests/rtld/symver/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <assert.h>
#include <stdio.h>
#include <dlfcn.h>

#ifdef USE_HOST_LIBC
#define LIBFOO "libnative-foo.so"
#else
#define LIBFOO "libfoo.so"
#endif

int foo(void);

int main() {
int ver = foo();
fprintf(stderr, "called foo version %d\n", ver);
assert(ver == 3); // version 3 is the default for libfoo.so

void *libfoo_h = dlopen(LIBFOO, RTLD_GLOBAL | RTLD_NOW);
assert(libfoo_h);

int (*foo1)(void) = dlvsym(libfoo_h, "foo", "FOO_1");
assert(foo1);

int (*foo2)(void) = dlvsym(libfoo_h, "foo", "FOO_2");
assert(foo2);

int (*foo3)(void) = dlvsym(libfoo_h, "foo", "FOO_3");
assert(foo3);

int (*foo_def)(void) = dlsym(libfoo_h, "foo");
assert(foo_def);

assert(foo1() == 1);
assert(foo2() == 2);
assert(foo3() == 3);
assert(foo3 == foo_def);

return 0;
}

0 comments on commit 0c525e9

Please sign in to comment.