Skip to content

Commit bd593bf

Browse files
danieldegrassecarlescufi
authored andcommitted
tests: code_relocation: Add additional test cases for library and genex
Add test cases for generator expressions and library relocation to the code_relocation test, in order to verify functionality for these new API features. Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent de8478b commit bd593bf

File tree

9 files changed

+158
-3
lines changed

9 files changed

+158
-3
lines changed

tests/application_development/code_relocation/CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright 2022 NXP
23

34
cmake_minimum_required(VERSION 3.20.0)
45

@@ -18,15 +19,29 @@ zephyr_code_relocate(FILES src/test_file1.c ${SRAM2_PHDR} LOCATION SRAM2)
1819

1920
zephyr_code_relocate(FILES src/test_file2.c ${RAM_PHDR} LOCATION RAM)
2021

22+
# Add custom library that we can relocate code for
23+
add_subdirectory(test_lib)
24+
target_link_libraries(app PUBLIC test_lib)
25+
target_include_directories(app PUBLIC ${CMAKE_CURRENT_LIST_DIR}/test_lib)
26+
# Relocate library code
27+
zephyr_code_relocate(LIBRARY test_lib LOCATION SRAM2)
28+
29+
# Test support for a simple generator expression to relocate two files
30+
set(reloc_files src/test_file4.c src/test_file5.c)
31+
set(genex_expr
32+
${CMAKE_CURRENT_LIST_DIR}/$<JOIN:${reloc_files},$<SEMICOLON>${CMAKE_CURRENT_LIST_DIR}/>)
33+
zephyr_code_relocate(FILES ${genex_expr} LOCATION SRAM2)
34+
2135
zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_LITERAL)
2236
zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_TEXT)
2337
zephyr_code_relocate(FILES src/test_file3.c LOCATION RAM_DATA)
2438
zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_BSS)
2539

26-
zephyr_code_relocate(FILES ../../../kernel/sem.c ${RAM_PHDR} LOCATION RAM)
40+
41+
zephyr_code_relocate(FILES ${ZEPHYR_BASE}/kernel/sem.c ${RAM_PHDR} LOCATION RAM)
2742

2843
if (CONFIG_RELOCATE_TO_ITCM)
29-
zephyr_code_relocate(FILES ../../../lib/libc/minimal/source/string/string.c
44+
zephyr_code_relocate(FILES ${ZEPHYR_BASE}/lib/libc/minimal/source/string/string.c
3045
LOCATION ITCM_TEXT)
3146
endif()
3247

tests/application_development/code_relocation/src/test_file1.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <zephyr/sys/printk.h>
99
#include <zephyr/ztest.h>
1010

11+
#include "test_lib.h"
12+
1113
/*
1214
* These values will typically be placed in the appropriate sections, but may be moved around
1315
* by the compiler; for instance var_sram2_data might end up in .rodata if the compiler can prove
@@ -63,6 +65,8 @@ ZTEST(code_relocation, test_function_in_sram2)
6365

6466
/* Print values from sram */
6567
function_in_sram(var_sram2_data);
68+
/* Call library function */
69+
relocated_library();
6670

6771
/* Print values which were placed using attributes */
6872
printk("Address of custom_section, func placed using attributes %p\n",

tests/application_development/code_relocation/src/test_file2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ void function_in_sram(int32_t value)
1616

1717
printk("Hello World! %s\n", CONFIG_BOARD);
1818
memcpy(dst, src, 8);
19-
printk("Address of memcpy %p\n", &memcpy);
19+
printk("Address of memcpy %p\n\n", &memcpy);
2020
zassert_mem_equal(src, dst, 8, "memcpy compare error");
2121
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
#include <zephyr/ztest.h>
10+
11+
__in_section(data, sram2, var) uint32_t var_file4_sram2_data = 10U;
12+
__in_section(bss, sram2, var) uint32_t var_file4_sram2_bss;
13+
14+
ZTEST(code_relocation, test_function_genex_relocate_1)
15+
{
16+
extern uintptr_t __sram2_data_start;
17+
extern uintptr_t __sram2_data_end;
18+
extern uintptr_t __sram2_bss_start;
19+
extern uintptr_t __sram2_bss_end;
20+
21+
printk("Address of var_file4_sram2_data %p\n", &var_file4_sram2_data);
22+
printk("Address of var_file4_sram2_bss %p\n\n", &var_file4_sram2_bss);
23+
24+
zassert_between_inclusive((uintptr_t)&var_file4_sram2_data,
25+
(uintptr_t)&__sram2_data_start,
26+
(uintptr_t)&__sram2_data_end,
27+
"var_file4_sram2_data not in sram2_data region");
28+
zassert_between_inclusive((uintptr_t)&var_file4_sram2_bss,
29+
(uintptr_t)&__sram2_bss_start,
30+
(uintptr_t)&__sram2_bss_end,
31+
"var_file4_sram2_bss not in sram2_bss region");
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
#include <zephyr/ztest.h>
10+
11+
__in_section(data, sram2, var) uint32_t var_file5_sram2_data = 10U;
12+
__in_section(bss, sram2, var) uint32_t var_file5_sram2_bss;
13+
14+
ZTEST(code_relocation, test_function_genex_relocate_2)
15+
{
16+
extern uintptr_t __sram2_data_start;
17+
extern uintptr_t __sram2_data_end;
18+
extern uintptr_t __sram2_bss_start;
19+
extern uintptr_t __sram2_bss_end;
20+
21+
printk("Address of var_file5_sram2_data %p\n", &var_file5_sram2_data);
22+
printk("Address of var_file5_sram2_bss %p\n\n", &var_file5_sram2_bss);
23+
24+
zassert_between_inclusive((uintptr_t)&var_file5_sram2_data,
25+
(uintptr_t)&__sram2_data_start,
26+
(uintptr_t)&__sram2_data_end,
27+
"var_file5_sram2_data not in sram2_data region");
28+
zassert_between_inclusive((uintptr_t)&var_file5_sram2_bss,
29+
(uintptr_t)&__sram2_bss_start,
30+
(uintptr_t)&__sram2_bss_end,
31+
"var_file5_sram2_bss not in sram2_bss region");
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright 2022 NXP
3+
4+
add_library(test_lib STATIC "")
5+
target_sources(test_lib PRIVATE test_lib1.c test_lib2.c)
6+
get_target_property(include_dirs app INCLUDE_DIRECTORIES)
7+
target_link_libraries(test_lib PUBLIC zephyr_interface)
8+
add_dependencies(test_lib zephyr_generated_headers)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* External library function, called from test_file1.c */
8+
void relocated_library(void);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
#include <zephyr/ztest.h>
10+
11+
__in_section(data, sram2, var) uint32_t var_lib1_sram2_data = 10U;
12+
__in_section(bss, sram2, var) uint32_t var_lib1_sram2_bss;
13+
14+
/* Helper function, declared in test_lib2.c */
15+
extern void relocated_helper(void);
16+
17+
void relocated_library(void)
18+
{
19+
extern uintptr_t __sram2_text_start;
20+
extern uintptr_t __sram2_text_end;
21+
extern uintptr_t __sram2_data_start;
22+
extern uintptr_t __sram2_data_end;
23+
extern uintptr_t __sram2_bss_start;
24+
extern uintptr_t __sram2_bss_end;
25+
26+
printk("Address of var_lib1_sram2_data %p\n", &var_lib1_sram2_data);
27+
printk("Address of var_lib1_sram2_bss %p\n", &var_lib1_sram2_bss);
28+
printk("Address of relocated_lib_helper %p\n\n", &relocated_helper);
29+
30+
zassert_between_inclusive((uintptr_t)&var_lib1_sram2_data,
31+
(uintptr_t)&__sram2_data_start,
32+
(uintptr_t)&__sram2_data_end,
33+
"var_lib1_sram2_data not in sram2_data region");
34+
zassert_between_inclusive((uintptr_t)&var_lib1_sram2_bss,
35+
(uintptr_t)&__sram2_bss_start,
36+
(uintptr_t)&__sram2_bss_end,
37+
"var_lib1_sram2_bss not in sram2_bss region");
38+
zassert_between_inclusive((uintptr_t)&relocated_helper,
39+
(uintptr_t)&__sram2_text_start,
40+
(uintptr_t)&__sram2_text_end,
41+
"relocated_helper not in sram2_text region");
42+
relocated_helper();
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
10+
void relocated_helper(void)
11+
{
12+
printk("Relocated helper function running on %s\n\n", CONFIG_BOARD);
13+
}

0 commit comments

Comments
 (0)