def write_gperf_table(fp, syms, objs, little_endian, static_begin, static_end):
    fp.write(header)
    if (sys_mutex_counter != 0):
        fp.write(('static struct k_mutex kernel_mutexes[%d] = {\n' % sys_mutex_counter))
        for i in range(sys_mutex_counter):
            fp.write(('Z_MUTEX_INITIALIZER(kernel_mutexes[%d])' % i))
            if (i != (sys_mutex_counter - 1)):
                fp.write(', ')
        fp.write('};\n')
    if (futex_counter != 0):
        fp.write(('static struct z_futex_data futex_data[%d] = {\n' % futex_counter))
        for i in range(futex_counter):
            fp.write(('Z_FUTEX_DATA_INITIALIZER(futex_data[%d])' % i))
            if (i != (futex_counter - 1)):
                fp.write(', ')
        fp.write('};\n')
    metadata_names = {'K_OBJ_THREAD': 'thread_id', 'K_OBJ_SYS_MUTEX': 'mutex', 'K_OBJ_FUTEX': 'futex_data'}
    if ('CONFIG_GEN_PRIV_STACKS' in syms):
        metadata_names['K_OBJ_THREAD_STACK_ELEMENT'] = 'stack_data'
        if (stack_counter != 0):
            fp.write(('static u8_t Z_GENERIC_SECTION(.priv_stacks.noinit)  __aligned(Z_PRIVILEGE_STACK_ALIGN) priv_stacks[%d][CONFIG_PRIVILEGED_STACK_SIZE];\n' % stack_counter))
            fp.write(('static struct z_stack_data stack_data[%d] = {\n' % stack_counter))
            counter = 0
            for (_, ko) in objs.items():
                if (ko.type_name != 'K_OBJ_THREAD_STACK_ELEMENT'):
                    continue
                size = ko.data
                ko.data = ('&stack_data[%d]' % counter)
                fp.write(('\t{ %d, (u8_t *)(&priv_stacks[%d]) }' % (size, counter)))
                if (counter != (stack_counter - 1)):
                    fp.write(',')
                fp.write('\n')
                counter += 1
            fp.write('};\n')
    else:
        metadata_names['K_OBJ_THREAD_STACK_ELEMENT'] = 'stack_size'
    fp.write('%%\n')
    thread_max_bytes = syms['CONFIG_MAX_THREAD_BYTES']
    thread_idx_map = {}
    for i in range(0, thread_max_bytes):
        thread_idx_map[i] = 255
    for (obj_addr, ko) in objs.items():
        obj_type = ko.type_name
        initialized = (static_begin <= obj_addr < static_end)
        is_driver = obj_type.startswith('K_OBJ_DRIVER_')
        if ('CONFIG_64BIT' in syms):
            format_code = 'Q'
        else:
            format_code = 'I'
        if little_endian:
            endian = '<'
        else:
            endian = '>'
        byte_str = struct.pack((endian + format_code), obj_addr)
        fp.write('"')
        for byte in byte_str:
            val = ('\\x%02x' % byte)
            fp.write(val)
        flags = '0'
        if initialized:
            flags += ' | K_OBJ_FLAG_INITIALIZED'
        if is_driver:
            flags += ' | K_OBJ_FLAG_DRIVER'
        if (ko.type_name in metadata_names):
            tname = metadata_names[ko.type_name]
        else:
            tname = 'unused'
        fp.write(('", {}, %s, %s, { .%s = %s }\n' % (obj_type, flags, tname, str(ko.data))))
        if (obj_type == 'K_OBJ_THREAD'):
            idx = math.floor((ko.data / 8))
            bit = (ko.data % 8)
            thread_idx_map[idx] = (thread_idx_map[idx] & (~ (2 ** bit)))
    fp.write(footer)
    fp.write('\n')
    fp.write('Z_GENERIC_SECTION(.kobject_data.data) ')
    fp.write(('u8_t _thread_idx_map[%d] = {' % thread_max_bytes))
    for i in range(0, thread_max_bytes):
        fp.write((' 0x%x, ' % thread_idx_map[i]))
    fp.write('};\n')
def write_gperf_table(fp, syms, objs, little_endian, static_begin, static_end):
    fp.write(header)
    if (sys_mutex_counter != 0):
        fp.write(('static struct k_mutex kernel_mutexes[%d] = {\n' % sys_mutex_counter))
        for i in range(sys_mutex_counter):
            fp.write(('Z_MUTEX_INITIALIZER(kernel_mutexes[%d])' % i))
            if (i != (sys_mutex_counter - 1)):
                fp.write(', ')
        fp.write('};\n')
    if (futex_counter != 0):
        fp.write(('static struct z_futex_data futex_data[%d] = {\n' % futex_counter))
        for i in range(futex_counter):
            fp.write(('Z_FUTEX_DATA_INITIALIZER(futex_data[%d])' % i))
            if (i != (futex_counter - 1)):
                fp.write(', ')
        fp.write('};\n')
    metadata_names = {'K_OBJ_THREAD': 'thread_id', 'K_OBJ_SYS_MUTEX': 'mutex', 'K_OBJ_FUTEX': 'futex_data'}
    if ('CONFIG_GEN_PRIV_STACKS' in syms):
        metadata_names['K_OBJ_THREAD_STACK_ELEMENT'] = 'stack_data'
        if (stack_counter != 0):
            fp.write(('static u8_t Z_GENERIC_SECTION(.priv_stacks.noinit)  __aligned(Z_PRIVILEGE_STACK_ALIGN) priv_stacks[%d][CONFIG_PRIVILEGED_STACK_SIZE];\n' % stack_counter))
            fp.write(('static struct z_stack_data stack_data[%d] = {\n' % stack_counter))
            counter = 0
            for (_, ko) in objs.items():
                if (ko.type_name != 'K_OBJ_THREAD_STACK_ELEMENT'):
                    continue
                size = ko.data
                ko.data = ('&stack_data[%d]' % counter)
                fp.write(('\t{ %d, (u8_t *)(&priv_stacks[%d]) }' % (size, counter)))
                if (counter != (stack_counter - 1)):
                    fp.write(',')
                fp.write('\n')
                counter += 1
            fp.write('};\n')
    else:
        metadata_names['K_OBJ_THREAD_STACK_ELEMENT'] = 'stack_size'
    fp.write('%%\n')
    thread_max_bytes = syms['CONFIG_MAX_THREAD_BYTES']
    thread_idx_map = {}
    for i in range(0, thread_max_bytes):
        thread_idx_map[i] = 255
    for (obj_addr, ko) in objs.items():
        obj_type = ko.type_name
        initialized = (static_begin <= obj_addr < static_end)
        is_driver = obj_type.startswith('K_OBJ_DRIVER_')
        if ('CONFIG_64BIT' in syms):
            format_code = 'Q'
        else:
            format_code = 'I'
        if little_endian:
            endian = '<'
        else:
            endian = '>'
        byte_str = struct.pack((endian + format_code), obj_addr)
        fp.write('"')
        for byte in byte_str:
            val = ('\\x%02x' % byte)
            fp.write(val)
        flags = '0'
        if initialized:
            flags += ' | K_OBJ_FLAG_INITIALIZED'
        if is_driver:
            flags += ' | K_OBJ_FLAG_DRIVER'
        if (ko.type_name in metadata_names):
            tname = metadata_names[ko.type_name]
        else:
            tname = 'unused'
        fp.write(('", {}, %s, %s, { .%s = %s }\n' % (obj_type, flags, tname, str(ko.data))))
        if (obj_type == 'K_OBJ_THREAD'):
            idx = math.floor((ko.data / 8))
            bit = (ko.data % 8)
            thread_idx_map[idx] = (thread_idx_map[idx] & (~ (2 ** bit)))
    fp.write(footer)
    fp.write('\n')
    fp.write('Z_GENERIC_SECTION(.kobject_data.data) ')
    fp.write(('u8_t _thread_idx_map[%d] = {' % thread_max_bytes))
    for i in range(0, thread_max_bytes):
        fp.write((' 0x%x, ' % thread_idx_map[i]))
    fp.write('};\n')