2018-02-01 15:19:49 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Linaro Limited
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import pprint
|
|
|
|
import os
|
|
|
|
import struct
|
2018-01-23 04:26:49 +08:00
|
|
|
from elf_helper import ElfHelper
|
2018-02-01 15:19:49 +08:00
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
kobjects = [
|
2018-02-01 15:19:49 +08:00
|
|
|
"k_stack",
|
|
|
|
"_k_thread_stack_element",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
header = """%compare-lengths
|
|
|
|
%define lookup-function-name _k_priv_stack_map_lookup
|
|
|
|
%language=ANSI-C
|
|
|
|
%global-table
|
|
|
|
%struct-type
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
priv_stack_decl_temp = ("static u8_t __used"
|
|
|
|
" __aligned(CONFIG_PRIVILEGED_STACK_SIZE)"
|
|
|
|
" priv_stack_%x[CONFIG_PRIVILEGED_STACK_SIZE];\n")
|
2018-02-01 15:19:49 +08:00
|
|
|
priv_stack_decl_size = "CONFIG_PRIVILEGED_STACK_SIZE"
|
|
|
|
|
|
|
|
|
|
|
|
includes = """#include <kernel.h>
|
|
|
|
#include <string.h>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
structure = """struct _k_priv_stack_map {
|
|
|
|
char *name;
|
|
|
|
u8_t *priv_stack_addr;
|
|
|
|
};
|
|
|
|
%%
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
# Different versions of gperf have different prototypes for the lookup
|
|
|
|
# function, best to implement the wrapper here. The pointer value itself is
|
|
|
|
# turned into a string, we told gperf to expect binary strings that are not
|
|
|
|
# NULL-terminated.
|
2018-02-01 15:19:49 +08:00
|
|
|
footer = """%%
|
|
|
|
u8_t *_k_priv_stack_find(void *obj)
|
|
|
|
{
|
|
|
|
const struct _k_priv_stack_map *map =
|
|
|
|
_k_priv_stack_map_lookup((const char *)obj, sizeof(void *));
|
|
|
|
return map->priv_stack_addr;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
def write_gperf_table(fp, eh, objs, static_begin, static_end):
|
2018-02-01 15:19:49 +08:00
|
|
|
fp.write(header)
|
|
|
|
|
|
|
|
# priv stack declarations
|
|
|
|
fp.write("%{\n")
|
|
|
|
fp.write(includes)
|
|
|
|
for obj_addr, ko in objs.items():
|
|
|
|
fp.write(priv_stack_decl_temp % (obj_addr))
|
|
|
|
fp.write("%}\n")
|
|
|
|
|
|
|
|
# structure declaration
|
|
|
|
fp.write(structure)
|
|
|
|
|
|
|
|
for obj_addr, ko in objs.items():
|
2018-01-23 04:26:49 +08:00
|
|
|
byte_str = struct.pack("<I" if eh.little_endian else ">I", obj_addr)
|
2018-02-01 15:19:49 +08:00
|
|
|
fp.write("\"")
|
|
|
|
for byte in byte_str:
|
|
|
|
val = "\\x%02x" % byte
|
|
|
|
fp.write(val)
|
|
|
|
|
|
|
|
fp.write("\",priv_stack_%x\n" % obj_addr)
|
|
|
|
|
|
|
|
fp.write(footer)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
global args
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
parser.add_argument("-k", "--kernel", required=True,
|
2018-01-23 04:26:49 +08:00
|
|
|
help="Input zephyr ELF binary")
|
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--output", required=True,
|
|
|
|
help="Output list of kernel object addresses for gperf use")
|
2018-02-01 15:19:49 +08:00
|
|
|
parser.add_argument("-v", "--verbose", action="store_true",
|
2018-01-23 04:26:49 +08:00
|
|
|
help="Print extra debugging information")
|
2018-02-01 15:19:49 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parse_args()
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
eh = ElfHelper(args.kernel, args.verbose, kobjects, [])
|
|
|
|
syms = eh.get_symbols()
|
|
|
|
max_threads = syms["CONFIG_MAX_THREAD_BYTES"] * 8
|
|
|
|
objs = eh.find_kobjects(syms)
|
|
|
|
|
|
|
|
if eh.get_thread_counter() > max_threads:
|
|
|
|
sys.stderr.write("Too many thread objects (%d)\n" % thread_counter)
|
|
|
|
sys.stderr.write("Increase CONFIG_MAX_THREAD_BYTES to %d\n",
|
|
|
|
-(-thread_counter // 8))
|
|
|
|
sys.exit(1)
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
with open(args.output, "w") as fp:
|
2018-01-23 04:26:49 +08:00
|
|
|
write_gperf_table(fp, eh, objs, syms["_static_kernel_objects_begin"],
|
|
|
|
syms["_static_kernel_objects_end"])
|
|
|
|
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|