From bd593bf93e54c9aaa3f6141739e85b5bdf360039 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Fri, 11 Nov 2022 17:00:49 -0600 Subject: [PATCH] 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 --- .../code_relocation/CMakeLists.txt | 19 +++++++- .../code_relocation/src/test_file1.c | 4 ++ .../code_relocation/src/test_file2.c | 2 +- .../code_relocation/src/test_file4.c | 32 ++++++++++++++ .../code_relocation/src/test_file5.c | 32 ++++++++++++++ .../code_relocation/test_lib/CMakeLists.txt | 8 ++++ .../code_relocation/test_lib/test_lib.h | 8 ++++ .../code_relocation/test_lib/test_lib1.c | 43 +++++++++++++++++++ .../code_relocation/test_lib/test_lib2.c | 13 ++++++ 9 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 tests/application_development/code_relocation/src/test_file4.c create mode 100644 tests/application_development/code_relocation/src/test_file5.c create mode 100644 tests/application_development/code_relocation/test_lib/CMakeLists.txt create mode 100644 tests/application_development/code_relocation/test_lib/test_lib.h create mode 100644 tests/application_development/code_relocation/test_lib/test_lib1.c create mode 100644 tests/application_development/code_relocation/test_lib/test_lib2.c diff --git a/tests/application_development/code_relocation/CMakeLists.txt b/tests/application_development/code_relocation/CMakeLists.txt index 7accdcb6127..f731fea70ed 100644 --- a/tests/application_development/code_relocation/CMakeLists.txt +++ b/tests/application_development/code_relocation/CMakeLists.txt @@ -1,4 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 +# Copyright 2022 NXP cmake_minimum_required(VERSION 3.20.0) @@ -18,15 +19,29 @@ zephyr_code_relocate(FILES src/test_file1.c ${SRAM2_PHDR} LOCATION SRAM2) zephyr_code_relocate(FILES src/test_file2.c ${RAM_PHDR} LOCATION RAM) +# Add custom library that we can relocate code for +add_subdirectory(test_lib) +target_link_libraries(app PUBLIC test_lib) +target_include_directories(app PUBLIC ${CMAKE_CURRENT_LIST_DIR}/test_lib) +# Relocate library code +zephyr_code_relocate(LIBRARY test_lib LOCATION SRAM2) + +# Test support for a simple generator expression to relocate two files +set(reloc_files src/test_file4.c src/test_file5.c) +set(genex_expr + ${CMAKE_CURRENT_LIST_DIR}/$${CMAKE_CURRENT_LIST_DIR}/>) +zephyr_code_relocate(FILES ${genex_expr} LOCATION SRAM2) + zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_LITERAL) zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_TEXT) zephyr_code_relocate(FILES src/test_file3.c LOCATION RAM_DATA) zephyr_code_relocate(FILES src/test_file3.c LOCATION SRAM2_BSS) -zephyr_code_relocate(FILES ../../../kernel/sem.c ${RAM_PHDR} LOCATION RAM) + +zephyr_code_relocate(FILES ${ZEPHYR_BASE}/kernel/sem.c ${RAM_PHDR} LOCATION RAM) if (CONFIG_RELOCATE_TO_ITCM) -zephyr_code_relocate(FILES ../../../lib/libc/minimal/source/string/string.c +zephyr_code_relocate(FILES ${ZEPHYR_BASE}/lib/libc/minimal/source/string/string.c LOCATION ITCM_TEXT) endif() diff --git a/tests/application_development/code_relocation/src/test_file1.c b/tests/application_development/code_relocation/src/test_file1.c index 55fa850c1c9..ff4f537ec02 100644 --- a/tests/application_development/code_relocation/src/test_file1.c +++ b/tests/application_development/code_relocation/src/test_file1.c @@ -8,6 +8,8 @@ #include #include +#include "test_lib.h" + /* * These values will typically be placed in the appropriate sections, but may be moved around * 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) /* Print values from sram */ function_in_sram(var_sram2_data); + /* Call library function */ + relocated_library(); /* Print values which were placed using attributes */ printk("Address of custom_section, func placed using attributes %p\n", diff --git a/tests/application_development/code_relocation/src/test_file2.c b/tests/application_development/code_relocation/src/test_file2.c index 2139187d796..d8b56730d39 100644 --- a/tests/application_development/code_relocation/src/test_file2.c +++ b/tests/application_development/code_relocation/src/test_file2.c @@ -16,6 +16,6 @@ void function_in_sram(int32_t value) printk("Hello World! %s\n", CONFIG_BOARD); memcpy(dst, src, 8); - printk("Address of memcpy %p\n", &memcpy); + printk("Address of memcpy %p\n\n", &memcpy); zassert_mem_equal(src, dst, 8, "memcpy compare error"); } diff --git a/tests/application_development/code_relocation/src/test_file4.c b/tests/application_development/code_relocation/src/test_file4.c new file mode 100644 index 00000000000..19d9a6f29b3 --- /dev/null +++ b/tests/application_development/code_relocation/src/test_file4.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022, NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +__in_section(data, sram2, var) uint32_t var_file4_sram2_data = 10U; +__in_section(bss, sram2, var) uint32_t var_file4_sram2_bss; + +ZTEST(code_relocation, test_function_genex_relocate_1) +{ + extern uintptr_t __sram2_data_start; + extern uintptr_t __sram2_data_end; + extern uintptr_t __sram2_bss_start; + extern uintptr_t __sram2_bss_end; + + printk("Address of var_file4_sram2_data %p\n", &var_file4_sram2_data); + printk("Address of var_file4_sram2_bss %p\n\n", &var_file4_sram2_bss); + + zassert_between_inclusive((uintptr_t)&var_file4_sram2_data, + (uintptr_t)&__sram2_data_start, + (uintptr_t)&__sram2_data_end, + "var_file4_sram2_data not in sram2_data region"); + zassert_between_inclusive((uintptr_t)&var_file4_sram2_bss, + (uintptr_t)&__sram2_bss_start, + (uintptr_t)&__sram2_bss_end, + "var_file4_sram2_bss not in sram2_bss region"); +} diff --git a/tests/application_development/code_relocation/src/test_file5.c b/tests/application_development/code_relocation/src/test_file5.c new file mode 100644 index 00000000000..b06004dc349 --- /dev/null +++ b/tests/application_development/code_relocation/src/test_file5.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022, NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +__in_section(data, sram2, var) uint32_t var_file5_sram2_data = 10U; +__in_section(bss, sram2, var) uint32_t var_file5_sram2_bss; + +ZTEST(code_relocation, test_function_genex_relocate_2) +{ + extern uintptr_t __sram2_data_start; + extern uintptr_t __sram2_data_end; + extern uintptr_t __sram2_bss_start; + extern uintptr_t __sram2_bss_end; + + printk("Address of var_file5_sram2_data %p\n", &var_file5_sram2_data); + printk("Address of var_file5_sram2_bss %p\n\n", &var_file5_sram2_bss); + + zassert_between_inclusive((uintptr_t)&var_file5_sram2_data, + (uintptr_t)&__sram2_data_start, + (uintptr_t)&__sram2_data_end, + "var_file5_sram2_data not in sram2_data region"); + zassert_between_inclusive((uintptr_t)&var_file5_sram2_bss, + (uintptr_t)&__sram2_bss_start, + (uintptr_t)&__sram2_bss_end, + "var_file5_sram2_bss not in sram2_bss region"); +} diff --git a/tests/application_development/code_relocation/test_lib/CMakeLists.txt b/tests/application_development/code_relocation/test_lib/CMakeLists.txt new file mode 100644 index 00000000000..86fa18b38d6 --- /dev/null +++ b/tests/application_development/code_relocation/test_lib/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2022 NXP + +add_library(test_lib STATIC "") +target_sources(test_lib PRIVATE test_lib1.c test_lib2.c) +get_target_property(include_dirs app INCLUDE_DIRECTORIES) +target_link_libraries(test_lib PUBLIC zephyr_interface) +add_dependencies(test_lib zephyr_generated_headers) diff --git a/tests/application_development/code_relocation/test_lib/test_lib.h b/tests/application_development/code_relocation/test_lib/test_lib.h new file mode 100644 index 00000000000..20820cccef8 --- /dev/null +++ b/tests/application_development/code_relocation/test_lib/test_lib.h @@ -0,0 +1,8 @@ +/* + * Copyright 2022 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* External library function, called from test_file1.c */ +void relocated_library(void); diff --git a/tests/application_development/code_relocation/test_lib/test_lib1.c b/tests/application_development/code_relocation/test_lib/test_lib1.c new file mode 100644 index 00000000000..d0b693102a8 --- /dev/null +++ b/tests/application_development/code_relocation/test_lib/test_lib1.c @@ -0,0 +1,43 @@ +/* + * Copyright 2022 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +__in_section(data, sram2, var) uint32_t var_lib1_sram2_data = 10U; +__in_section(bss, sram2, var) uint32_t var_lib1_sram2_bss; + +/* Helper function, declared in test_lib2.c */ +extern void relocated_helper(void); + +void relocated_library(void) +{ + extern uintptr_t __sram2_text_start; + extern uintptr_t __sram2_text_end; + extern uintptr_t __sram2_data_start; + extern uintptr_t __sram2_data_end; + extern uintptr_t __sram2_bss_start; + extern uintptr_t __sram2_bss_end; + + printk("Address of var_lib1_sram2_data %p\n", &var_lib1_sram2_data); + printk("Address of var_lib1_sram2_bss %p\n", &var_lib1_sram2_bss); + printk("Address of relocated_lib_helper %p\n\n", &relocated_helper); + + zassert_between_inclusive((uintptr_t)&var_lib1_sram2_data, + (uintptr_t)&__sram2_data_start, + (uintptr_t)&__sram2_data_end, + "var_lib1_sram2_data not in sram2_data region"); + zassert_between_inclusive((uintptr_t)&var_lib1_sram2_bss, + (uintptr_t)&__sram2_bss_start, + (uintptr_t)&__sram2_bss_end, + "var_lib1_sram2_bss not in sram2_bss region"); + zassert_between_inclusive((uintptr_t)&relocated_helper, + (uintptr_t)&__sram2_text_start, + (uintptr_t)&__sram2_text_end, + "relocated_helper not in sram2_text region"); + relocated_helper(); +} diff --git a/tests/application_development/code_relocation/test_lib/test_lib2.c b/tests/application_development/code_relocation/test_lib/test_lib2.c new file mode 100644 index 00000000000..d5a29c07039 --- /dev/null +++ b/tests/application_development/code_relocation/test_lib/test_lib2.c @@ -0,0 +1,13 @@ +/* + * Copyright 2022 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +void relocated_helper(void) +{ + printk("Relocated helper function running on %s\n\n", CONFIG_BOARD); +}