1 /*
2 * Copyright (c) 2025 Arduino SA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * Define symbols with different alignment requirements and verify that LLEXT
9 * correctly handles them by testing the runtime address and the contents of
10 * each defined symbol.
11 */
12
13 #include <stdint.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/llext/symbol.h>
16 #include <zephyr/ztest_assert.h>
17
18 /*
19 * Create constants requesting a specific alignment in memory and with a value
20 * that is related but not equal to their alignment requirement.
21 */
22 #define ALIGNED_ENTRY(name, n) \
23 const int name ## _ ## n __aligned(n) = n / 2 + 1
24
25 ALIGNED_ENTRY(common, 8);
26 ALIGNED_ENTRY(common, 16);
27 ALIGNED_ENTRY(common, 32);
28 ALIGNED_ENTRY(common, 64);
29 ALIGNED_ENTRY(common, 128);
30 ALIGNED_ENTRY(common, 256);
31 ALIGNED_ENTRY(common, 512);
32
33 /*
34 * Create similar constants in a set of independent sections to test merging.
35 */
36 #define ALIGNED_SECT_ENTRY(name, n) \
37 Z_GENERIC_SECTION(name ## _sect_ ## n) ALIGNED_ENTRY(name, n)
38
39 ALIGNED_SECT_ENTRY(independent, 8);
40 ALIGNED_SECT_ENTRY(independent, 16);
41 ALIGNED_SECT_ENTRY(independent, 32);
42 ALIGNED_SECT_ENTRY(independent, 64);
43 ALIGNED_SECT_ENTRY(independent, 128);
44 ALIGNED_SECT_ENTRY(independent, 256);
45 ALIGNED_SECT_ENTRY(independent, 512);
46
47 /*
48 * Test that each symbol matches its expected value and alignment.
49 */
50 #define ASSERT_ENTRY(name, n) \
51 zassert_equal(name ## _ ## n, n / 2 + 1); \
52 zassert_true(IS_ALIGNED(&name ## _ ## n, n))
53
test_entry(void)54 void test_entry(void)
55 {
56 ASSERT_ENTRY(common, 8);
57 ASSERT_ENTRY(common, 16);
58 ASSERT_ENTRY(common, 32);
59 ASSERT_ENTRY(common, 64);
60 ASSERT_ENTRY(common, 128);
61 ASSERT_ENTRY(common, 256);
62 ASSERT_ENTRY(common, 512);
63
64 ASSERT_ENTRY(independent, 8);
65 ASSERT_ENTRY(independent, 16);
66 ASSERT_ENTRY(independent, 32);
67 ASSERT_ENTRY(independent, 64);
68 ASSERT_ENTRY(independent, 128);
69 ASSERT_ENTRY(independent, 256);
70 ASSERT_ENTRY(independent, 512);
71 }
72 EXPORT_SYMBOL(test_entry);
73