From b17aeb2f120be00e6f218aeb2c3da07d49df939a Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Wed, 12 Feb 2025 21:37:34 -0700 Subject: Start linting against global variables --- .editorconfig | 2 +- build-aux/lint-bin | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/sbc_harness/main.c | 2 +- lib9p/9p.generated.c | 4 ++-- lib9p/idl.gen | 4 ++-- lib9p/internal.h | 4 ++-- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100755 build-aux/lint-bin diff --git a/.editorconfig b/.editorconfig index 0baab4f..4bf1ae1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -33,7 +33,7 @@ _mode = 9p [{lib9p/tests/test_server/static.h.gen,build-aux/embed-sources.h.gen,build-aux/lint-{generic,unknown},build-aux/get-dscname}] _mode = sh -[{build-aux/lint-h,build-aux/linux-errno.txt.gen,libusb/include/libusb/tusb_helpers.h.gen,lib9p/tests/runtest}] +[{build-aux/lint-h,build-aux/lint-bin,build-aux/linux-errno.txt.gen,libusb/include/libusb/tusb_helpers.h.gen,lib9p/tests/runtest}] _mode = bash [{lib9p/idl.gen,lib9p/include/lib9p/linux-errno.h.gen,build-aux/stack.c.gen}] diff --git a/build-aux/lint-bin b/build-aux/lint-bin new file mode 100755 index 0000000..2fe8e4b --- /dev/null +++ b/build-aux/lint-bin @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# build-aux/lint-bin - Lint final binary images +# +# Copyright (C) 2025 Luke T. Shumaker +# SPDX-License-Identifier: AGPL-3.0-or-later + +set -euE -o pipefail + +# Output is a series of lines in the format "symbol location size +# source". Whitespace may seem silly. +objdump_globals() { + sed -E -n '/^ \.t?(data|bss)\./{ / 0x/{ p; D; }; N; s/\n/ /; p; }' <"$1" +} + +lint_globals() { + local in_mapfile + in_mapfile=$1 + + local rel_base + rel_base=${in_mapfile#build/*/} + rel_base=${rel_base%/*} + + local topdir + topdir=$PWD + + { + echo 'Source Symbol Size' + objdump_globals "$in_mapfile" | + { + cd "$rel_base" + total=0 + while read -r symbol addr size source; do + : "$addr" + case "$source" in + /*) + # libg.a(whatever.o) -> libg.a + source="${source%(*)}" + # resolve `..` components + source="$(realpath --canonicalize-missing --no-symlinks -- "$source")" + ;; + CMakeFiles/*.dir/*.obj) + # CMakeFiles/sbc_harnes_objs.dir/... + source="${source#CMakeFiles/*.dir/}" + source="${source%.obj}" + source="${source//__/..}" + source="$(realpath --canonicalize-missing --no-symlinks --relative-to="$topdir" -- "$source")" + ;; + esac + printf '%s %s 0x%04x\n' "$source" "$symbol" "$size" + total=$((total + size)) + done + printf '~ Total 0x%04x\n' "$total" + } | + LC_COLLATE=C sort + } | column -t +} + +main() { + echo 'Global variables:' + lint_globals 'build/rp2040-Release/cmd/sbc_harness/sbc_harness.elf.map' | sed 's/^/ /' +} + +main "$@" diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c index ce80711..157f51e 100644 --- a/cmd/sbc_harness/main.c +++ b/cmd/sbc_harness/main.c @@ -67,7 +67,7 @@ static COROUTINE read9p_cr(void *) { cr_end(); } -const char *hexdig = "0123456789ABCDEF"; +const char *const hexdig = "0123456789ABCDEF"; static_assert(CONFIG_9P_SRV_MAX_REQS*_CONFIG_9P_NUM_SOCKS <= 16); COROUTINE init_cr(void *) { diff --git a/lib9p/9p.generated.c b/lib9p/9p.generated.c index eca0666..fe042e6 100644 --- a/lib9p/9p.generated.c +++ b/lib9p/9p.generated.c @@ -48,7 +48,7 @@ /* strings ********************************************************************/ -const char *_lib9p_table_ver_name[LIB9P_VER_NUM] = { +const char *const _lib9p_table_ver_name[LIB9P_VER_NUM] = { [LIB9P_VER_unknown] = "unknown", #if CONFIG_9P_ENABLE_9P2000 [LIB9P_VER_9P2000] = "9P2000", @@ -68,7 +68,7 @@ const char *_lib9p_table_ver_name[LIB9P_VER_NUM] = { }; #define _MSG_NAME(typ) [LIB9P_TYP_##typ] = #typ -const char * _lib9p_table_msg_name[LIB9P_VER_NUM][0x100] = { +const char *const _lib9p_table_msg_name[LIB9P_VER_NUM][0x100] = { [LIB9P_VER_unknown] = { _MSG_NAME(Tversion), _MSG_NAME(Rversion), diff --git a/lib9p/idl.gen b/lib9p/idl.gen index f2b4f13..db5f37d 100755 --- a/lib9p/idl.gen +++ b/lib9p/idl.gen @@ -693,7 +693,7 @@ def gen_c(versions: set[str], typs: list[idl.Type]) -> str: ret += f""" /* strings ********************************************************************/ -const char *_{idprefix}table_ver_name[{c_ver_enum('NUM')}] = {{ +const char *const _{idprefix}table_ver_name[{c_ver_enum('NUM')}] = {{ """ for ver in ["unknown", *sorted(versions)]: if ver in versions: @@ -704,7 +704,7 @@ const char *_{idprefix}table_ver_name[{c_ver_enum('NUM')}] = {{ ret += "\n" ret += f"#define _MSG_NAME(typ) [{idprefix.upper()}TYP_##typ] = #typ\n" - ret += msg_table("msg", "name", "char *", (0, 0x100, 1)) + ret += msg_table("msg", "name", "char *const", (0, 0x100, 1)) # bitmasks ################################################################# ret += f""" diff --git a/lib9p/internal.h b/lib9p/internal.h index d27348e..1f4975f 100644 --- a/lib9p/internal.h +++ b/lib9p/internal.h @@ -88,8 +88,8 @@ struct _lib9p_send_tentry { _marshal_fn_t marshal; }; -extern const char * _lib9p_table_ver_name[LIB9P_VER_NUM]; -extern const char * _lib9p_table_msg_name[LIB9P_VER_NUM][0x100]; +extern const char *const _lib9p_table_ver_name[LIB9P_VER_NUM]; +extern const char *const _lib9p_table_msg_name[LIB9P_VER_NUM][0x100]; extern const uint32_t _lib9p_table_msg_min_size[LIB9P_VER_NUM]; extern const struct _lib9p_recv_tentry _lib9p_table_Tmsg_recv[LIB9P_VER_NUM][0x80]; extern const struct _lib9p_recv_tentry _lib9p_table_Rmsg_recv[LIB9P_VER_NUM][0x80]; -- cgit v1.2.3-2-g168b