1 /*
2  * Copyright (c) 2024 CISPA Helmholtz Center for Information Security
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * This extension tests a relocation edge case in RISC-V:
9  * U-type instructions in conjunction with I-type/S-type instructions can be used to
10  * relocate symbols within a 32-bit range from the PC (medany code model) or 0
11  * (medlow code model).
12  * The compiler usually emits the U-type instructions and I-type/S-type instructions in sequence.
13  * However, this is not guaranteed.
14  * The accompanying assembly listing generates a scenario in which this assumption does NOT hold
15  * and tests that the llext loader can handle it.
16  */
17 
18 #include <stdbool.h>
19 #include <zephyr/llext/symbol.h>
20 #include <zephyr/ztest_assert.h>
21 
22 extern int _riscv_edge_case_non_paired_hi20_lo12(void);
23 
24 
25 #define DATA_SEGMENT_SYMBOL_INITIAL 21
26 #define DATA_SEGMENT_SYMBOL_EXPECTED (DATA_SEGMENT_SYMBOL_INITIAL+42)
27 
28 /* changed from the assembly script */
29 volatile int _data_segment_symbol = DATA_SEGMENT_SYMBOL_INITIAL;
30 
31 
test_entry(void)32 void test_entry(void)
33 {
34 	int ret_value;
35 
36 	ret_value = _riscv_edge_case_non_paired_hi20_lo12();
37 
38 	zassert_equal(ret_value, DATA_SEGMENT_SYMBOL_INITIAL);
39 
40 	zassert_equal(_data_segment_symbol, DATA_SEGMENT_SYMBOL_EXPECTED);
41 }
42 EXPORT_SYMBOL(test_entry);
43