Skip to content

Commit c69fb0d

Browse files
AdithyaBaglodynashif
authored andcommitted
userspace: app_shared_mem: Fixed incorrect implementation.
This feature was failing on a default ARM core MPU. The linker script that was getting created was not able to align the required partitions at prebuilt time. The old implementation relied on the prebuilt to finish then extract the size information which was then used to align the regions. This fails because the size of the alignment and the fill in the linker needs to be available at prebuilt time else it cant manage the final elf file generation. We cant have 2 different sizes of prebuilt and final elf file. This implementation will get the alignment requirements met at prebuilt time. Signed-off-by: Adithya Baglody <[email protected]>
1 parent 3832378 commit c69fb0d

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ add_custom_target(
700700
linker_script
701701
DEPENDS
702702
${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
703+
${APP_SMEM_DEP}
703704
linker.cmd
704705
offsets_h
705706
)
@@ -1016,20 +1017,22 @@ configure_file(
10161017
if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
10171018

10181019
if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APP_SHARED_MEM)
1019-
set(GEN_APP_SMEM $ENV{ZEPHYR_BASE}/scripts/gen_app_smem.py)
10201020
set(APP_SMEM_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem.ld")
10211021
set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
10221022

10231023
add_custom_target(
10241024
${APP_SMEM_DEP} ALL
1025-
DEPENDS zephyr_prebuilt
1025+
DEPENDS app
1026+
DEPENDS ${PRIV_STACK_DEP}
10261027
)
10271028

10281029
add_custom_command(
10291030
TARGET ${APP_SMEM_DEP}
1030-
COMMAND ${PYTHON_EXECUTABLE} ${GEN_APP_SMEM}
1031+
COMMAND ${PYTHON_EXECUTABLE}
1032+
${ZEPHYR_BASE}/scripts/gen_app_partitions.py
10311033
-d ${OBJ_FILE_DIR}
10321034
-o ${APP_SMEM_LD}
1035+
$<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
10331036
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
10341037
COMMENT "Generating power of 2 aligned app_smem linker section"
10351038
)
@@ -1145,7 +1148,7 @@ if(GKOF OR GKSF)
11451148
add_executable( kernel_elf misc/empty_file.c ${GKSF})
11461149
target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk})
11471150
set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
1148-
add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} ${APP_SMEM_DEP} linker_pass_final_script)
1151+
add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script)
11491152
else()
11501153
set(logical_target_for_zephyr_elf zephyr_prebuilt)
11511154
# Use the prebuilt elf as the final elf since we don't have a

scripts/gen_app_partitions.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import argparse
4+
import os
5+
import re
6+
import string
7+
from elf_helper import ElfHelper
8+
from elftools.elf.elffile import ELFFile
9+
10+
11+
# This script will create linker comands for power of two aligned MPU
12+
# when APP_SHARED_MEM is enabled.
13+
print_template = """
14+
/* Auto generated code do not modify */
15+
. = ALIGN( 1 << LOG2CEIL(data_smem_{0}b_end - data_smem_{0}));
16+
data_smem_{0} = .;
17+
KEEP(*(SORT(data_smem_{0}*)))
18+
. = ALIGN(_app_data_align);
19+
data_smem_{0}b_end = .;
20+
. = ALIGN( 1 << LOG2CEIL(data_smem_{0}b_end - data_smem_{0}));
21+
"""
22+
23+
24+
def find_partitions(filename, full_list_of_partitions, partitions_source_file):
25+
with open(filename, 'rb') as f:
26+
full_lib = ELFFile( f)
27+
if (not full_lib):
28+
print("Error parsing file: ",filename)
29+
os.exit(1)
30+
31+
sections = [ x for x in full_lib.iter_sections()]
32+
for section in sections:
33+
if ("smem" in section.name and not ".rel" in section.name):
34+
partition_name = section.name.split("data_smem_")[1]
35+
if partition_name not in full_list_of_partitions:
36+
full_list_of_partitions.append(partition_name)
37+
if args.verbose:
38+
partitions_source_file.update({partition_name: filename})
39+
40+
return( full_list_of_partitions, partitions_source_file)
41+
42+
def cleanup_remove_bss_regions(full_list_of_partitions):
43+
for partition in full_list_of_partitions:
44+
if (partition+"b" in full_list_of_partitions):
45+
full_list_of_partitions.remove(partition+"b")
46+
return full_list_of_partitions
47+
48+
def generate_final_linker(linker_file, full_list_of_partitions):
49+
string= ''
50+
for partition in full_list_of_partitions:
51+
string += print_template.format(partition)
52+
53+
with open(linker_file, "w") as fw:
54+
fw.write(string)
55+
56+
57+
def parse_args():
58+
global args
59+
parser = argparse.ArgumentParser(
60+
description=__doc__,
61+
formatter_class=argparse.RawDescriptionHelpFormatter)
62+
parser.add_argument("-d", "--directory", required=True,
63+
help="Root build directory")
64+
parser.add_argument("-o", "--output", required=False,
65+
help="Output ld file")
66+
parser.add_argument("-v", "--verbose", action="count", default =0,
67+
help="Verbose Output")
68+
args = parser.parse_args()
69+
70+
71+
def main():
72+
parse_args()
73+
root_directory = args.directory
74+
linker_file = args.output
75+
full_list_of_partitions = []
76+
partitions_source_file= {}
77+
78+
for dirpath, dirs, files in os.walk(root_directory):
79+
for filename in files:
80+
if re.match(".*\.obj$",filename):
81+
fullname = os.path.join(dirpath, filename)
82+
full_list_of_partitions, partitions_source_file = find_partitions(fullname, full_list_of_partitions, partitions_source_file)
83+
84+
full_list_of_partitions = cleanup_remove_bss_regions(full_list_of_partitions)
85+
generate_final_linker(linker_file, full_list_of_partitions)
86+
if args.verbose:
87+
print("Partitions retrieved: PARTITION, FILENAME")
88+
print([key + " "+ partitions_source_file[key] for key in full_list_of_partitions])
89+
90+
if __name__ == '__main__':
91+
main()

0 commit comments

Comments
 (0)