blob: 7c2e8392a4a7ac0b718a666ca6e77d0f51051ec1 [file] [log] [blame]
Chunlin Han18560a02018-02-01 01:19:49 -06001#!/usr/bin/env python3
2#
3# Copyright (c) 2017 Linaro Limited
4#
5# SPDX-License-Identifier: Apache-2.0
6
7import sys
8import argparse
Chunlin Han18560a02018-02-01 01:19:49 -06009import struct
Andy Gross6042ae92018-01-22 14:26:49 -060010from elf_helper import ElfHelper
Chunlin Han18560a02018-02-01 01:19:49 -060011
Andy Gross6042ae92018-01-22 14:26:49 -060012kobjects = [
Chunlin Han18560a02018-02-01 01:19:49 -060013 "k_stack",
14 "_k_thread_stack_element",
15 ]
16
17
Chunlin Han18560a02018-02-01 01:19:49 -060018header = """%compare-lengths
19%define lookup-function-name _k_priv_stack_map_lookup
20%language=ANSI-C
21%global-table
22%struct-type
23"""
24
25
Andy Gross6042ae92018-01-22 14:26:49 -060026priv_stack_decl_temp = ("static u8_t __used"
27 " __aligned(CONFIG_PRIVILEGED_STACK_SIZE)"
28 " priv_stack_%x[CONFIG_PRIVILEGED_STACK_SIZE];\n")
Chunlin Han18560a02018-02-01 01:19:49 -060029priv_stack_decl_size = "CONFIG_PRIVILEGED_STACK_SIZE"
30
31
32includes = """#include <kernel.h>
33#include <string.h>
34"""
35
36
37structure = """struct _k_priv_stack_map {
38 char *name;
39 u8_t *priv_stack_addr;
40};
41%%
42"""
43
44
Andy Gross6042ae92018-01-22 14:26:49 -060045# Different versions of gperf have different prototypes for the lookup
46# function, best to implement the wrapper here. The pointer value itself is
47# turned into a string, we told gperf to expect binary strings that are not
48# NULL-terminated.
Chunlin Han18560a02018-02-01 01:19:49 -060049footer = """%%
50u8_t *_k_priv_stack_find(void *obj)
51{
52 const struct _k_priv_stack_map *map =
53 _k_priv_stack_map_lookup((const char *)obj, sizeof(void *));
54 return map->priv_stack_addr;
55}
56"""
57
58
Andy Gross6042ae92018-01-22 14:26:49 -060059def write_gperf_table(fp, eh, objs, static_begin, static_end):
Chunlin Han18560a02018-02-01 01:19:49 -060060 fp.write(header)
61
62 # priv stack declarations
63 fp.write("%{\n")
64 fp.write(includes)
65 for obj_addr, ko in objs.items():
66 fp.write(priv_stack_decl_temp % (obj_addr))
67 fp.write("%}\n")
68
69 # structure declaration
70 fp.write(structure)
71
72 for obj_addr, ko in objs.items():
Andy Gross6042ae92018-01-22 14:26:49 -060073 byte_str = struct.pack("<I" if eh.little_endian else ">I", obj_addr)
Chunlin Han18560a02018-02-01 01:19:49 -060074 fp.write("\"")
75 for byte in byte_str:
76 val = "\\x%02x" % byte
77 fp.write(val)
78
79 fp.write("\",priv_stack_%x\n" % obj_addr)
80
81 fp.write(footer)
82
83
Chunlin Han18560a02018-02-01 01:19:49 -060084def parse_args():
85 global args
86
Andy Gross6042ae92018-01-22 14:26:49 -060087 parser = argparse.ArgumentParser(
88 description=__doc__,
89 formatter_class=argparse.RawDescriptionHelpFormatter)
Chunlin Han18560a02018-02-01 01:19:49 -060090
91 parser.add_argument("-k", "--kernel", required=True,
Andy Gross6042ae92018-01-22 14:26:49 -060092 help="Input zephyr ELF binary")
93 parser.add_argument(
94 "-o", "--output", required=True,
95 help="Output list of kernel object addresses for gperf use")
Chunlin Han18560a02018-02-01 01:19:49 -060096 parser.add_argument("-v", "--verbose", action="store_true",
Andy Gross6042ae92018-01-22 14:26:49 -060097 help="Print extra debugging information")
Chunlin Han18560a02018-02-01 01:19:49 -060098 args = parser.parse_args()
99
100
101def main():
102 parse_args()
103
Andy Gross6042ae92018-01-22 14:26:49 -0600104 eh = ElfHelper(args.kernel, args.verbose, kobjects, [])
105 syms = eh.get_symbols()
106 max_threads = syms["CONFIG_MAX_THREAD_BYTES"] * 8
107 objs = eh.find_kobjects(syms)
108
Anas Nashif2e7bdb62018-09-16 14:56:07 -0500109 thread_counter = eh.get_thread_counter()
110 if thread_counter > max_threads:
Andy Gross6042ae92018-01-22 14:26:49 -0600111 sys.stderr.write("Too many thread objects (%d)\n" % thread_counter)
112 sys.stderr.write("Increase CONFIG_MAX_THREAD_BYTES to %d\n",
113 -(-thread_counter // 8))
114 sys.exit(1)
Chunlin Han18560a02018-02-01 01:19:49 -0600115
116 with open(args.output, "w") as fp:
Andy Gross6042ae92018-01-22 14:26:49 -0600117 write_gperf_table(fp, eh, objs, syms["_static_kernel_objects_begin"],
118 syms["_static_kernel_objects_end"])
119
Chunlin Han18560a02018-02-01 01:19:49 -0600120
121if __name__ == "__main__":
122 main()