blob: db0938628e35cff1c7400f93b2b9ba27e9875e40 [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
9import pprint
10import os
11import struct
Andy Gross6042ae92018-01-22 14:26:49 -060012from elf_helper import ElfHelper
Chunlin Han18560a02018-02-01 01:19:49 -060013
Andy Gross6042ae92018-01-22 14:26:49 -060014kobjects = [
Chunlin Han18560a02018-02-01 01:19:49 -060015 "k_stack",
16 "_k_thread_stack_element",
17 ]
18
19
Chunlin Han18560a02018-02-01 01:19:49 -060020header = """%compare-lengths
21%define lookup-function-name _k_priv_stack_map_lookup
22%language=ANSI-C
23%global-table
24%struct-type
25"""
26
27
Andy Gross6042ae92018-01-22 14:26:49 -060028priv_stack_decl_temp = ("static u8_t __used"
29 " __aligned(CONFIG_PRIVILEGED_STACK_SIZE)"
30 " priv_stack_%x[CONFIG_PRIVILEGED_STACK_SIZE];\n")
Chunlin Han18560a02018-02-01 01:19:49 -060031priv_stack_decl_size = "CONFIG_PRIVILEGED_STACK_SIZE"
32
33
34includes = """#include <kernel.h>
35#include <string.h>
36"""
37
38
39structure = """struct _k_priv_stack_map {
40 char *name;
41 u8_t *priv_stack_addr;
42};
43%%
44"""
45
46
Andy Gross6042ae92018-01-22 14:26:49 -060047# Different versions of gperf have different prototypes for the lookup
48# function, best to implement the wrapper here. The pointer value itself is
49# turned into a string, we told gperf to expect binary strings that are not
50# NULL-terminated.
Chunlin Han18560a02018-02-01 01:19:49 -060051footer = """%%
52u8_t *_k_priv_stack_find(void *obj)
53{
54 const struct _k_priv_stack_map *map =
55 _k_priv_stack_map_lookup((const char *)obj, sizeof(void *));
56 return map->priv_stack_addr;
57}
58"""
59
60
Andy Gross6042ae92018-01-22 14:26:49 -060061def write_gperf_table(fp, eh, objs, static_begin, static_end):
Chunlin Han18560a02018-02-01 01:19:49 -060062 fp.write(header)
63
64 # priv stack declarations
65 fp.write("%{\n")
66 fp.write(includes)
67 for obj_addr, ko in objs.items():
68 fp.write(priv_stack_decl_temp % (obj_addr))
69 fp.write("%}\n")
70
71 # structure declaration
72 fp.write(structure)
73
74 for obj_addr, ko in objs.items():
Andy Gross6042ae92018-01-22 14:26:49 -060075 byte_str = struct.pack("<I" if eh.little_endian else ">I", obj_addr)
Chunlin Han18560a02018-02-01 01:19:49 -060076 fp.write("\"")
77 for byte in byte_str:
78 val = "\\x%02x" % byte
79 fp.write(val)
80
81 fp.write("\",priv_stack_%x\n" % obj_addr)
82
83 fp.write(footer)
84
85
Chunlin Han18560a02018-02-01 01:19:49 -060086def parse_args():
87 global args
88
Andy Gross6042ae92018-01-22 14:26:49 -060089 parser = argparse.ArgumentParser(
90 description=__doc__,
91 formatter_class=argparse.RawDescriptionHelpFormatter)
Chunlin Han18560a02018-02-01 01:19:49 -060092
93 parser.add_argument("-k", "--kernel", required=True,
Andy Gross6042ae92018-01-22 14:26:49 -060094 help="Input zephyr ELF binary")
95 parser.add_argument(
96 "-o", "--output", required=True,
97 help="Output list of kernel object addresses for gperf use")
Chunlin Han18560a02018-02-01 01:19:49 -060098 parser.add_argument("-v", "--verbose", action="store_true",
Andy Gross6042ae92018-01-22 14:26:49 -060099 help="Print extra debugging information")
Chunlin Han18560a02018-02-01 01:19:49 -0600100 args = parser.parse_args()
101
102
103def main():
104 parse_args()
105
Andy Gross6042ae92018-01-22 14:26:49 -0600106 eh = ElfHelper(args.kernel, args.verbose, kobjects, [])
107 syms = eh.get_symbols()
108 max_threads = syms["CONFIG_MAX_THREAD_BYTES"] * 8
109 objs = eh.find_kobjects(syms)
110
111 if eh.get_thread_counter() > max_threads:
112 sys.stderr.write("Too many thread objects (%d)\n" % thread_counter)
113 sys.stderr.write("Increase CONFIG_MAX_THREAD_BYTES to %d\n",
114 -(-thread_counter // 8))
115 sys.exit(1)
Chunlin Han18560a02018-02-01 01:19:49 -0600116
117 with open(args.output, "w") as fp:
Andy Gross6042ae92018-01-22 14:26:49 -0600118 write_gperf_table(fp, eh, objs, syms["_static_kernel_objects_begin"],
119 syms["_static_kernel_objects_end"])
120
Chunlin Han18560a02018-02-01 01:19:49 -0600121
122if __name__ == "__main__":
123 main()