summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorusualtreee <[email protected]>2026-11-13 09:31:55 +0800
committerGitHub <[email protected]>2026-11-13 09:31:55 +0800
commit20feab6ad387042ef2be5980afec2f3c26974537 (patch)
tree26f14a0e116231515d5a790324dec03142e5816c /components
parent45f59a16abba9ba907d2f320bc446dee5b0adb23 (diff)
[dlmodule][utest] add QEMU regression coverage
新增动态模块 ELF 测试样本、加载及卸载回归用例,并接入 QEMU CI 和结果校验。
Diffstat (limited to 'components')
-rw-r--r--components/libc/posix/libdl/SConscript2
-rw-r--r--components/libc/posix/libdl/utest/Kconfig10
-rw-r--r--components/libc/posix/libdl/utest/SConscript23
-rw-r--r--components/libc/posix/libdl/utest/build_fixtures.py108
-rw-r--r--components/libc/posix/libdl/utest/dlmodule_tc.c325
-rw-r--r--components/libc/posix/libdl/utest/fixtures/common_target.c20
-rw-r--r--components/libc/posix/libdl/utest/fixtures/globals.c35
-rw-r--r--components/libc/posix/libdl/utest/fixtures/multi_bss.c19
-rw-r--r--components/libc/posix/libdl/utest/fixtures/multi_bss.ld7
-rw-r--r--components/libc/posix/libdl/utest/fixtures/multi_text.c10
-rw-r--r--components/libc/posix/libdl/utest/fixtures/nonzero.ld7
-rw-r--r--components/libc/posix/libdl/utest/fixtures/sections.c16
-rw-r--r--components/libc/posix/libdl/utest/fixtures/undefined.c12
-rw-r--r--components/utilities/utest/Kconfig1
14 files changed, 595 insertions, 0 deletions
diff --git a/components/libc/posix/libdl/SConscript b/components/libc/posix/libdl/SConscript
index cf6648a4a9..f17b847282 100644
--- a/components/libc/posix/libdl/SConscript
+++ b/components/libc/posix/libdl/SConscript
@@ -8,5 +8,7 @@ CPPPATH = [cwd]
if rtconfig.PLATFORM in ['gcc']:
group = DefineGroup('Libc', src, depend = ['RT_USING_MODULE'], CPPPATH = CPPPATH)
+ if GetDepend('RT_UTEST_DLMODULE'):
+ group += SConscript('utest/SConscript')
Return('group')
diff --git a/components/libc/posix/libdl/utest/Kconfig b/components/libc/posix/libdl/utest/Kconfig
new file mode 100644
index 0000000000..49ed556e25
--- /dev/null
+++ b/components/libc/posix/libdl/utest/Kconfig
@@ -0,0 +1,10 @@
+config RT_UTEST_DLMODULE
+ bool "dlmodule ELF loading and relocation tests (32-bit ARM GCC)"
+ depends on ARCH_ARM_CORTEX_A && !ARCH_CPU_64BIT && !RT_USING_SMART
+ depends on RT_USING_MODULE && RT_USING_DFS && RT_USING_POSIX_FS
+ default n
+ help
+ Build real 32-bit ARM ELF modules and test dlopen, dlsym and dlclose.
+ Requires GCC and a writable filesystem mounted at /.
+ Fixture generation uses -marm -mcpu=arm926ej-s and verifies the
+ GCC-generated ELF layout, section types and symbol properties.
diff --git a/components/libc/posix/libdl/utest/SConscript b/components/libc/posix/libdl/utest/SConscript
new file mode 100644
index 0000000000..0a68794229
--- /dev/null
+++ b/components/libc/posix/libdl/utest/SConscript
@@ -0,0 +1,23 @@
+import os
+import subprocess
+import sys
+from building import *
+
+Import('env')
+
+cwd = GetCurrentDir()
+variant = Dir('.').abspath
+fixture_sources = Glob('fixtures/*.c') + Glob('fixtures/*.ld')
+builder = os.path.join(cwd, 'build_fixtures.py')
+
+def build_fixtures(target, source, env):
+ subprocess.check_call([sys.executable, builder, '--cc', env.subst('$CC'),
+ '--output', str(target[0])], env=env['ENV'])
+
+header = env.Command('dlmodule_fixtures.h', [builder] + fixture_sources,
+ Action(build_fixtures, 'Building dlmodule ELF fixtures'))
+group = DefineGroup('dlmodule-utest', ['dlmodule_tc.c'],
+ depend=['RT_UTEST_DLMODULE'], CPPPATH=[cwd, variant])
+env.Depends(group, header)
+
+Return('group')
diff --git a/components/libc/posix/libdl/utest/build_fixtures.py b/components/libc/posix/libdl/utest/build_fixtures.py
new file mode 100644
index 0000000000..7ae6f6af92
--- /dev/null
+++ b/components/libc/posix/libdl/utest/build_fixtures.py
@@ -0,0 +1,108 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: Apache-2.0
+"""Build ARM test modules from source; never commit generated ELF/header files."""
+import argparse
+from pathlib import Path
+import shlex
+import struct
+import subprocess
+
+
+def verify_fixture(name, data):
+ """Check the ELF shape so a compiler change cannot silently remove a test."""
+ if data[:6] != b'\x7fELF\x01\x01':
+ raise RuntimeError(name + ': expected little-endian ELF32')
+ kind, machine = struct.unpack_from('<HH', data, 16)
+ if machine != 40 or kind != (3 if name in ('dg_dyn', 'dg_imp') else 1):
+ raise RuntimeError(name + ': incorrect ARM ELF type')
+ offset = struct.unpack_from('<I', data, 32)[0]
+ stride, count, strings = struct.unpack_from('<HHH', data, 46)
+ sections = [struct.unpack_from('<10I', data, offset + i * stride) for i in range(count)]
+ names = data[sections[strings][4]:sections[strings][4] + sections[strings][5]]
+ section_names = [names[s[0]:].split(b'\0', 1)[0].decode() for s in sections]
+ symbols = {}
+ for section in sections:
+ if section[1] != 2:
+ continue
+ strings = sections[section[6]]
+ table = data[strings[4]:strings[4] + strings[5]]
+ for pos in range(section[4], section[4] + section[5], 16):
+ symbol = struct.unpack_from('<IIIBBH', data, pos)
+ label = table[symbol[0]:].split(b'\0', 1)[0].decode()
+ if label:
+ symbols[label] = symbol
+ if name == 'dg_rel':
+ assert symbols['common_value'][5] == 0xfff2
+ assert symbols['aligned_value'][5] == 0xfff2 and symbols['aligned_value'][1] == 16
+ elif name == 'dc_ptr':
+ assert symbols['common_value'][5] == 0xfff2
+ assert any(s[1] == 9 and section_names[s[7]] == '.data' and s[5] > 0 for s in sections)
+ elif name == 'db_multi':
+ assert section_names[symbols['bss_value'][5]] == '.bss'
+ assert section_names[symbols['common_value'][5]] == '.common'
+ assert sections[symbols['bss_value'][5]][1] == 8
+ assert sections[symbols['common_value'][5]][1] == 8
+ elif name == 'dn_addr':
+ assert sections[symbols['probe'][5]][3] == 0x1000 and symbols['probe'][1] == 0
+ assert sections[symbols['initialized'][5]][3] == 0x2000
+ elif name == 'dd_dbg':
+ assert any(s[1] == 9 and section_names[s[7]].startswith('.debug') for s in sections)
+ elif name == 'dt_text':
+ assert symbols['first'][5] != symbols['second'][5]
+ assert symbols['first'][1] == symbols['second'][1] == 0
+ elif name in ('du_sym', 'dz_none'):
+ assert symbols['external_func'][5] == 0 and symbols['external_func'][3] == 0x12
+ if name == 'dz_none':
+ assert not any(s[3] == 0x12 and s[5] != 0 for s in symbols.values())
+ elif name == 'dg_imp':
+ # Dynamic symbols are in .dynsym; inspect the string table as well.
+ assert b'rt_kprintf\0' in data
+
+
+def build(cc, output):
+ sources = Path(__file__).resolve().parent / 'fixtures'
+ directory = output.parent / 'dlmodule-fixtures'
+ directory.mkdir(parents=True, exist_ok=True)
+ # ARMv5 literal-pool relocations are supported by libdl/arch/arm.c.
+ # Cortex-A9 also executes these instructions; avoid unsupported MOVW/MOVT relocations.
+ flags = ['-marm', '-mcpu=arm926ej-s', '-O0', '-fcommon', '-fno-pic',
+ '-fno-stack-protector', '-fno-unwind-tables', '-fno-asynchronous-unwind-tables']
+ modules = [
+ ('dg_rel', 'globals', [], []),
+ ('dg_dyn', 'globals', ['-fPIC', '-mlong-calls', '-DDYNAMIC_FIXTURE'], ['-shared']),
+ ('dg_imp', 'globals', ['-fPIC', '-mlong-calls', '-DDYNAMIC_FIXTURE', '-DKERNEL_IMPORT'], ['-shared']),
+ ('dc_ptr', 'common_target', [], []),
+ ('db_multi', 'multi_bss', [], ['-Wl,-d,-T,' + str(sources / 'multi_bss.ld')]),
+ ('dn_addr', 'sections', [], ['-Wl,-T,' + str(sources / 'nonzero.ld')]),
+ ('dd_dbg', 'sections', ['-g'], []),
+ ('dt_text', 'multi_text', [], []),
+ ('du_sym', 'undefined', [], []),
+ ('dz_none', 'undefined', ['-DZERO_EXPORTS'], []),
+ ]
+ lines = ['/* Generated ELF fixtures; do not edit. */',
+ 'struct dlmodule_fixture { const char *name; const unsigned char *data; unsigned size; };']
+ for name, source, extra, link in modules:
+ obj = directory / (name + '.o')
+ elf = directory / (name + '.mo')
+ subprocess.run(cc + flags + extra + ['-c', str(sources / (source + '.c')), '-o', str(obj)], check=True)
+ mode = [] if '-shared' in link else ['-r']
+ subprocess.run(cc + ['-nostdlib'] + mode + link + [str(obj), '-o', str(elf)], check=True)
+ data = elf.read_bytes()
+ verify_fixture(name, data)
+ lines.append('static const unsigned char ' + name + '[] = {')
+ for offset in range(0, len(data), 16):
+ lines.append(' ' + ', '.join('0x%02x' % byte for byte in data[offset:offset + 16]) + ',')
+ lines.append('};')
+ lines.append('static const struct dlmodule_fixture dlmodule_fixtures[] = {')
+ for name, _, _, _ in modules:
+ lines.append(' {"%s.mo", %s, sizeof(%s)},' % (name, name, name))
+ lines.append('};')
+ output.write_text('\n'.join(lines) + '\n')
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--cc', required=True)
+ parser.add_argument('--output', type=Path, required=True)
+ args = parser.parse_args()
+ build(shlex.split(args.cc), args.output.resolve())
diff --git a/components/libc/posix/libdl/utest/dlmodule_tc.c b/components/libc/posix/libdl/utest/dlmodule_tc.c
new file mode 100644
index 0000000000..8f1f16e709
--- /dev/null
+++ b/components/libc/posix/libdl/utest/dlmodule_tc.c
@@ -0,0 +1,325 @@
+/*
+ * Copyright (c) 2026, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdint.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <dlfcn.h>
+#include "utest.h"
+#include "../dlmodule.h"
+#include "dlmodule_fixtures.h"
+
+#define TEST_DIR "/dlmutest"
+
+typedef uint32_t *(*probe_fn)(void);
+typedef int (*value_fn)(void);
+
+static rt_bool_t in_module(void *handle, const void *address, rt_size_t size)
+{
+ struct rt_dlmodule *module = handle;
+ rt_ubase_t base = (rt_ubase_t)module->mem_space;
+ rt_ubase_t value = (rt_ubase_t)address;
+
+ return address != RT_NULL && value >= base && size <= module->mem_size &&
+ value - base <= module->mem_size - size;
+}
+
+static void fixture_path(char *path, rt_size_t size, unsigned index)
+{
+ rt_snprintf(path, size, "%s/%s", TEST_DIR, dlmodule_fixtures[index].name);
+}
+
+static void *load_fixture(unsigned index)
+{
+ const struct dlmodule_fixture *fixture = &dlmodule_fixtures[index];
+ char path[64];
+ unsigned written = 0;
+ int fd;
+
+ fixture_path(path, sizeof(path), index);
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ uassert_true(fd >= 0);
+ if (fd < 0)
+ {
+ return RT_NULL;
+ }
+
+ while (written < fixture->size)
+ {
+ int count = write(fd, fixture->data + written, fixture->size - written);
+ if (count <= 0)
+ {
+ uassert_true(count > 0);
+ close(fd);
+ return RT_NULL;
+ }
+ written += count;
+ }
+ if (close(fd) != 0)
+ {
+ uassert_true(0);
+ return RT_NULL;
+ }
+
+ return dlopen(path, RTLD_NOW);
+}
+
+static uint32_t *get_results(void *handle, unsigned count)
+{
+ probe_fn probe = (probe_fn)dlsym(handle, "probe");
+ uint32_t *result;
+ rt_bool_t valid;
+
+ valid = in_module(handle, (void *)probe, 4);
+ uassert_true(valid);
+ if (!valid)
+ {
+ return RT_NULL;
+ }
+ result = probe();
+ valid = in_module(handle, result, count * sizeof(*result));
+ uassert_true(valid);
+ return valid ? result : RT_NULL;
+}
+
+static void close_fixture(void *handle)
+{
+ uassert_int_equal(dlclose(handle), RT_TRUE);
+}
+
+static void globals_test(unsigned index)
+{
+ void *handle = load_fixture(index);
+ uint32_t *result;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+
+ result = get_results(handle, 8);
+ if (result != RT_NULL)
+ {
+ rt_bool_t addresses_valid = RT_TRUE;
+ for (unsigned i = 0; i < 3; i++)
+ {
+ rt_bool_t valid = in_module(handle, (void *)result[i], sizeof(uint32_t));
+ uassert_true(valid);
+ addresses_valid = addresses_valid && valid;
+ }
+ uassert_int_not_equal(result[0], result[1]);
+ uassert_int_not_equal(result[1], result[2]);
+ uassert_int_not_equal(result[0], result[2]);
+ if (index == 0)
+ {
+ uassert_int_equal(result[2] & 15, 0);
+ }
+ uassert_int_equal(result[3], 7);
+ uassert_int_equal(result[4], 0);
+ uassert_int_equal(result[5], 0);
+ uassert_int_equal(result[6], 0);
+ uassert_true((void *)result[7] == dlsym(handle, "probe"));
+ if (addresses_valid)
+ {
+ uassert_int_equal(*(uint32_t *)result[0], 0x11223344);
+ uassert_int_equal(*(uint32_t *)result[1], 0x55667788);
+ uassert_int_equal(*(uint32_t *)result[2], 0x12345678);
+ }
+ }
+ close_fixture(handle);
+}
+
+static void globals_rel(void)
+{
+ globals_test(0);
+}
+
+static void globals_dyn(void)
+{
+ globals_test(1);
+}
+
+static void kernel_import(void)
+{
+ globals_test(2);
+}
+
+static void common_target(void)
+{
+ void *handle = load_fixture(3);
+ uint32_t *result;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ result = get_results(handle, 4);
+ if (result != RT_NULL)
+ {
+ uassert_true(in_module(handle, (void *)result[0], 4));
+ uassert_int_equal(result[0], result[1]);
+ uassert_int_equal(result[2], 42);
+ uassert_int_equal(result[3], 0);
+ }
+ close_fixture(handle);
+}
+
+static void multi_nobits(void)
+{
+ void *handle = load_fixture(4);
+ uint32_t *result;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ result = get_results(handle, 6);
+ if (result != RT_NULL)
+ {
+ uassert_true(in_module(handle, (void *)result[0], 4));
+ uassert_true(in_module(handle, (void *)result[1], 4));
+ uassert_int_not_equal(result[0], result[1]);
+ uassert_int_equal(result[2], 0);
+ uassert_int_equal(result[3], 0);
+ uassert_int_equal(result[4], 0x11223344);
+ uassert_int_equal(result[5], 0x55667788);
+ }
+ close_fixture(handle);
+}
+
+static void sections_test(unsigned index)
+{
+ void *handle = load_fixture(index);
+ uint32_t *result;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ result = get_results(handle, 4);
+ if (result != RT_NULL)
+ {
+ uassert_int_equal(result[0], 7);
+ uassert_int_equal(result[1], 9);
+ uassert_int_equal(result[2], 0);
+ uassert_true((void *)result[3] == dlsym(handle, "probe"));
+ }
+ close_fixture(handle);
+}
+
+static void nonzero_sections(void)
+{
+ sections_test(5);
+}
+
+static void debug_sections(void)
+{
+ sections_test(6);
+}
+
+static void multi_text(void)
+{
+ void *handle = load_fixture(7);
+ value_fn first, second;
+ rt_bool_t valid;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ first = (value_fn)dlsym(handle, "first");
+ second = (value_fn)dlsym(handle, "second");
+ valid = in_module(handle, (void *)first, 4) && in_module(handle, (void *)second, 4);
+ uassert_true(valid);
+ uassert_true(first != second);
+ if (valid)
+ {
+ uassert_int_equal(first(), 11);
+ uassert_int_equal(second(), 22);
+ }
+ close_fixture(handle);
+}
+
+static void undefined_symbol(void)
+{
+ void *handle = load_fixture(8);
+ value_fn present;
+ rt_bool_t valid;
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ uassert_null(dlsym(handle, "external_func"));
+ present = (value_fn)dlsym(handle, "present");
+ valid = in_module(handle, (void *)present, 4);
+ uassert_true(valid);
+ if (valid)
+ {
+ uassert_int_equal(present(), 42);
+ }
+ close_fixture(handle);
+}
+
+static void zero_exports(void)
+{
+ void *handle = load_fixture(9);
+
+ uassert_not_null(handle);
+ if (handle == RT_NULL)
+ {
+ return;
+ }
+ uassert_int_equal(((struct rt_dlmodule *)handle)->nsym, 0);
+ uassert_null(dlsym(handle, "external_func"));
+ close_fixture(handle);
+}
+
+static rt_err_t test_init(void)
+{
+ struct stat st;
+
+ if (stat(TEST_DIR, &st) == 0)
+ {
+ return S_ISDIR(st.st_mode) ? RT_EOK : -RT_ERROR;
+ }
+ return mkdir(TEST_DIR, 0700) == 0 ? RT_EOK : -RT_ERROR;
+}
+
+static rt_err_t test_cleanup(void)
+{
+ char path[64];
+
+ for (unsigned i = 0; i < sizeof(dlmodule_fixtures) / sizeof(dlmodule_fixtures[0]); i++)
+ {
+ fixture_path(path, sizeof(path), i);
+ unlink(path);
+ }
+ return rmdir(TEST_DIR) == 0 ? RT_EOK : -RT_ERROR;
+}
+
+static void testcase(void)
+{
+ UTEST_UNIT_RUN(globals_rel);
+ UTEST_UNIT_RUN(globals_dyn);
+ UTEST_UNIT_RUN(kernel_import);
+ UTEST_UNIT_RUN(common_target);
+ UTEST_UNIT_RUN(multi_nobits);
+ UTEST_UNIT_RUN(nonzero_sections);
+ UTEST_UNIT_RUN(debug_sections);
+ UTEST_UNIT_RUN(multi_text);
+ UTEST_UNIT_RUN(undefined_symbol);
+ UTEST_UNIT_RUN(zero_exports);
+}
+
+UTEST_TC_EXPORT(testcase, "components.libc.posix.libdl.regression", test_init, test_cleanup, 30);
diff --git a/components/libc/posix/libdl/utest/fixtures/common_target.c b/components/libc/posix/libdl/utest/fixtures/common_target.c
new file mode 100644
index 0000000000..404346fcc3
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/common_target.c
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+#include <stdint.h>
+
+int common_value;
+int *common_pointer = &common_value;
+static uint32_t results[4];
+
+int function(void)
+{
+ return 42;
+}
+
+uint32_t *probe(void)
+{
+ results[0] = (uint32_t)&common_value;
+ results[1] = (uint32_t)common_pointer;
+ results[2] = function();
+ results[3] = common_value;
+ return results;
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/globals.c b/components/libc/posix/libdl/utest/fixtures/globals.c
new file mode 100644
index 0000000000..137344430e
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/globals.c
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+#include <stdint.h>
+
+int common_value;
+#ifdef DYNAMIC_FIXTURE
+int aligned_value;
+#else
+int aligned_value __attribute__((aligned(16)));
+#endif
+int initialized = 7;
+static int bss_value;
+static uint32_t results[8];
+
+#ifdef KERNEL_IMPORT
+extern int rt_kprintf(const char *, ...);
+#endif
+
+uint32_t *probe(void)
+{
+#ifdef KERNEL_IMPORT
+ rt_kprintf("dlmodule fixture: kernel import\n");
+#endif
+ results[0] = (uint32_t)&bss_value;
+ results[1] = (uint32_t)&common_value;
+ results[2] = (uint32_t)&aligned_value;
+ results[3] = initialized;
+ results[4] = bss_value;
+ results[5] = common_value;
+ results[6] = aligned_value;
+ results[7] = (uint32_t)&probe;
+ bss_value = 0x11223344;
+ common_value = 0x55667788;
+ aligned_value = 0x12345678;
+ return results;
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/multi_bss.c b/components/libc/posix/libdl/utest/fixtures/multi_bss.c
new file mode 100644
index 0000000000..847508dff5
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/multi_bss.c
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+#include <stdint.h>
+
+int bss_value = 0;
+int common_value;
+static uint32_t results[6];
+
+uint32_t *probe(void)
+{
+ results[0] = (uint32_t)&bss_value;
+ results[1] = (uint32_t)&common_value;
+ results[2] = bss_value;
+ results[3] = common_value;
+ bss_value = 0x11223344;
+ common_value = 0x55667788;
+ results[4] = bss_value;
+ results[5] = common_value;
+ return results;
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/multi_bss.ld b/components/libc/posix/libdl/utest/fixtures/multi_bss.ld
new file mode 100644
index 0000000000..5802281f93
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/multi_bss.ld
@@ -0,0 +1,7 @@
+SECTIONS
+{
+ .text 0 : { *(.text*) }
+ .data 0 : { *(.data*) }
+ .bss 0 : { *(.bss*) }
+ .common 0 : { *(COMMON) }
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/multi_text.c b/components/libc/posix/libdl/utest/fixtures/multi_text.c
new file mode 100644
index 0000000000..89ef63a8e9
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/multi_text.c
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+__attribute__((section(".text.first"))) int first(void)
+{
+ return 11;
+}
+
+__attribute__((section(".text.second"))) int second(void)
+{
+ return 22;
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/nonzero.ld b/components/libc/posix/libdl/utest/fixtures/nonzero.ld
new file mode 100644
index 0000000000..21eb78a316
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/nonzero.ld
@@ -0,0 +1,7 @@
+SECTIONS
+{
+ .text 0x1000 : { *(.text*) }
+ .rodata 0x1800 : { *(.rodata*) }
+ .data 0x2000 : { *(.data*) }
+ .bss 0x3000 : { *(.bss*) }
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/sections.c b/components/libc/posix/libdl/utest/fixtures/sections.c
new file mode 100644
index 0000000000..02cbc0ea22
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/sections.c
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+#include <stdint.h>
+
+int initialized = 7;
+static const int rodata_value = 9;
+static int bss_value;
+static uint32_t results[4];
+
+uint32_t *probe(void)
+{
+ results[0] = initialized;
+ results[1] = *(volatile const int *)&rodata_value;
+ results[2] = bss_value;
+ results[3] = (uint32_t)&probe;
+ return results;
+}
diff --git a/components/libc/posix/libdl/utest/fixtures/undefined.c b/components/libc/posix/libdl/utest/fixtures/undefined.c
new file mode 100644
index 0000000000..97eab30f33
--- /dev/null
+++ b/components/libc/posix/libdl/utest/fixtures/undefined.c
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/* Keep a GLOBAL/FUNC/UND symbol without introducing an import relocation. */
+__asm__(".global external_func\n.type external_func, %function\n");
+
+#ifdef ZERO_EXPORTS
+int initialized = 7;
+#else
+int present(void)
+{
+ return 42;
+}
+#endif
diff --git a/components/utilities/utest/Kconfig b/components/utilities/utest/Kconfig
index da9dc7c0e5..b9bf62979b 100644
--- a/components/utilities/utest/Kconfig
+++ b/components/utilities/utest/Kconfig
@@ -30,6 +30,7 @@ menu "RT-Thread Utestcases"
rsource "../../../components/dfs/utest/Kconfig"
rsource "../../../components/dfs/dfs_v2/filesystems/tmpfs/utest/Kconfig"
rsource "../../../components/libc/compilers/common/utest/Kconfig"
+ rsource "../../../components/libc/posix/libdl/utest/Kconfig"
rsource "../../../components/libc/cplusplus/utest/Kconfig"
rsource "../../../components/lwp/utest/Kconfig"
rsource "../../../components/mm/utest/Kconfig"