diff --git a/palm-client/src/sections.h b/palm-client/src/sections.h index 84ef48c..debc07a 100644 --- a/palm-client/src/sections.h +++ b/palm-client/src/sections.h @@ -1,7 +1,8 @@ /* * Code segments. The app's code is more than the ~32 KB one 68k code resource - * can reach, so some modules live in a second one (section vmcode2, see - * visormesh.def; multigen makes the stub and linker script). + * can reach, so modules also live in vmcode2 and vmcode3 (VM_SECT2/VM_SECT3; see + * visormesh.def; multigen makes the stub and linker script). Everything below + * applies to any extra section. * * How prc-tools/GCC 2.95 handles it (checked in the compiled objects): * - A function declared VM_SECT2 is placed in vmcode2; callers that see the @@ -28,8 +29,10 @@ #if defined(__palmos__) && !defined(__clang__) /* clang: the editor's checker, which rejects the section name */ #define VM_SECT2 __attribute__ ((section ("vmcode2"))) +#define VM_SECT3 __attribute__ ((section ("vmcode3"))) #else #define VM_SECT2 +#define VM_SECT3 #endif #endif diff --git a/palm-client/tools/__pycache__/check_sections.cpython-311.pyc b/palm-client/tools/__pycache__/check_sections.cpython-311.pyc new file mode 100644 index 0000000..7185fb6 Binary files /dev/null and b/palm-client/tools/__pycache__/check_sections.cpython-311.pyc differ diff --git a/palm-client/tools/check_sections.py b/palm-client/tools/check_sections.py index a818b5e..81a4934 100644 --- a/palm-client/tools/check_sections.py +++ b/palm-client/tools/check_sections.py @@ -2,33 +2,49 @@ """Checks the rules in src/sections.h on the compiled objects (run in the toolchain container by `make`, after linking). -Code in the second segment (section vmcode2) may only use PC-relative -references (DISP8/DISP16 relocations) to symbols that are also in vmcode2: +Code in an extra segment (sections vmcode2, vmcode3, ...) may only use +PC-relative references (DISP8/DISP16 relocations) to symbols in the same section: a PC-relative reference to .text, .data or anything in another section is -wrong at run time on the Visor, and nothing else would catch it. Typical -causes: a prototype missing VM_SECT2, a string literal, or static const data. +wrong at run time on the Visor, and nothing else would catch it. GCC routes +correct cross-section references through the section bases (END16 + 32-bit +relocations), so a PC-relative one means something it couldn't see, such as +-fwritable-strings literals. check_sections.py OBJECT... exits 1 and lists every bad reference """ +import re import subprocess import sys -SECTION = 'vmcode2' +SECTIONS = ('vmcode2', 'vmcode3') OBJDUMP = 'm68k-palmos-objdump' +def section_names(obj): + """Section names in header order (COFF symbols refer to them as (sec 1), (sec 2), ...).""" + out = subprocess.run([OBJDUMP, '-h', obj], capture_output=True, text=True).stdout + names = [] + for line in out.splitlines(): + parts = line.split() + if len(parts) >= 2 and parts[0].isdigit(): + names.append(parts[1]) + return names + + def symbols_by_section(objs): """{symbol: section} for symbols defined in the objects.""" where = {} for obj in objs: + names = section_names(obj) out = subprocess.run([OBJDUMP, '-t', obj], capture_output=True, text=True).stdout for line in out.splitlines(): - parts = line.split() - # "00000000 g F vmcode2 00000012 VmSha256" / "00000000 l d .text ..." - if len(parts) >= 4 and parts[-2] != '*UND*' and not line.startswith(obj): - sect = next((p for p in parts if p in ('.text', '.data', '.bss', SECTION)), None) - if sect: - where[parts[-1]] = sect + # COFF: "[ 5](sec 1)(fl 0x00)(ty 0)(scl 6) (nx 0) 0x00000000 MaxTop" + m = re.search(r'\(sec\s+(-?\d+)\).*\s(\S+)$', line) + if not m: + continue + sec = int(m.group(1)) + if 1 <= sec <= len(names) and names[sec - 1] in ('.text', '.data', '.bss') + SECTIONS: + where.setdefault(m.group(2), names[sec - 1]) return where @@ -37,19 +53,20 @@ def main(): where = symbols_by_section(objs) bad = [] for obj in objs: - out = subprocess.run([OBJDUMP, '-r', '-j', SECTION, obj], capture_output=True, text=True) - if out.returncode != 0: # no vmcode2 section in this object - continue - for line in out.stdout.splitlines(): - parts = line.split() - if len(parts) != 3 or not parts[1].startswith('DISP'): + for section in SECTIONS: + out = subprocess.run([OBJDUMP, '-r', '-j', section, obj], capture_output=True, text=True) + if out.returncode != 0: # no such section in this object continue - target = parts[2].split('+')[0].split('-')[0] - sect = target if target.startswith('.') else where.get(target, SECTION) - if sect != SECTION: - bad.append(f'{obj}: PC-relative {parts[1]} at {parts[0]} to {parts[2]} (in {sect})') + for line in out.stdout.splitlines(): + parts = line.split() + if len(parts) != 3 or not parts[1].startswith('DISP'): + continue + target = parts[2].split('+')[0].split('-')[0] + sect = target if target.startswith('.') or target in SECTIONS else where.get(target, section) + if sect != section: + bad.append(f'{obj}: {section}: PC-relative {parts[1]} at {parts[0]} to {parts[2]} (in {sect})') if bad: - print('Second code segment refers PC-relative outside itself (see src/sections.h):') + print('An extra code segment refers PC-relative outside itself (see src/sections.h):') print('\n'.join(' ' + b for b in bad)) sys.exit(1) print(f'sections OK: {len(objs)} objects checked') diff --git a/palm-client/tools/sect2.py b/palm-client/tools/sect2.py index ae06f73..08e5129 100644 --- a/palm-client/tools/sect2.py +++ b/palm-client/tools/sect2.py @@ -1,18 +1,23 @@ #!/usr/bin/env python3 -"""Puts a C file's static functions in the second code segment: inserts a -'static VM_SECT2;' forward declaration for each one after the -last #include (see src/sections.h). Run once per file moved; idempotent.""" +"""Puts a C file's static functions in an extra code segment: inserts a +'static VM_SECT2;' (or --sect3: VM_SECT3) forward declaration for +each one after the last #include (see src/sections.h). Run once per file +moved; idempotent.""" import re import sys -for path in sys.argv[1:]: +macro = 'VM_SECT3' if '--sect3' in sys.argv else 'VM_SECT2' +for path in [a for a in sys.argv[1:] if not a.startswith('--')]: src = open(path).read() - marker = '/* In the second code segment: see sections.h for the rules. */' + marker = '/* In an extra code segment (' + macro + '): see sections.h for the rules. */' + if 'code segment: see sections.h' in src or 'code segment (VM_SECT' in src: + print(f'{path}: already done') + continue if marker in src: print(f'{path}: already done') continue sigs = re.findall(r'^(static\s+(?!const\s+UInt|UInt8\s+g|Char\s+\*g)[^=;{]*?\w+\s*\([^;{]*?\))\s*\n\{', src, re.M) - decls = '\n'.join(re.sub(r'\s+', ' ', s) + ' VM_SECT2;' for s in sigs) + decls = '\n'.join(re.sub(r'\s+', ' ', s) + ' ' + macro + ';' for s in sigs) last_inc = list(re.finditer(r'^#include .*\n', src, re.M))[-1] src = src[:last_inc.end()] + f'\n{marker}\n{decls}\n' + src[last_inc.end():] open(path, 'w').write(src) diff --git a/palm-client/visormesh.def b/palm-client/visormesh.def index 21ce7dd..1d83d24 100644 --- a/palm-client/visormesh.def +++ b/palm-client/visormesh.def @@ -1,2 +1,2 @@ application { "VisorMesh" VMsh backup } -multiple code { "vmcode2" } +multiple code { "vmcode2" "vmcode3" }