From 05c0293a395508f7e9787c9dbeb555fa2b026d94 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:31:48 -0600 Subject: [PATCH] link: embld --help prints the usage on stdout and exits 0 `embld --help` (and `-h`) was refused as "unknown option '--help'" with exit status 2, while the other Emb tools with a usage text answer it. The usage text is now one function, usage(FILE *), used by --help (stdout, exit 0) and by a bare invocation with no inputs (stderr, exit 2, as before). The test is tests/golden/embld-help.sh. Against the code before this change it failed with: FAIL: --help exited 2, want 0 embld: unknown option '--help' After it, the test passes (--help, -h, and the bare invocation are each checked for exit status and for which stream carries the text). Also run: make embcc embld, then tests/golden/embld-{help,merge, defined-wins,bss-align,reloc-range,veneers}.sh with EMBLD and EMBCC set to the built binaries. Several of them skip on this host (no llvm-readelf, clang for riscv64, qemu-system-arm). Not run: make check, make test, make test-arm64. This host is Darwin, not Linux. embld-doctor.sh fails on this host with "doctor exited 0 with symbols undefined". The same failure happens with the original tools/embld/embld.c, so it is not caused by this change; it is not investigated here. --- tests/golden/embld-help.sh | 33 +++++++++++++++++++++++++++++++++ tools/embld/embld.c | 24 +++++++++++++++++------- 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100755 tests/golden/embld-help.sh diff --git a/tests/golden/embld-help.sh b/tests/golden/embld-help.sh new file mode 100755 index 00000000..036a6078 --- /dev/null +++ b/tests/golden/embld-help.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# `embld --help` (and -h) prints the usage on stdout and exits 0, the way +# `embsim --help` and `embmap --help` do. A bare `embld` with no inputs keeps +# printing the same usage to stderr and exits 2. Each case checks the exit +# status and which stream got the text, not only that the words appear. +set -eu +echo "TEST-MARKER embld-help" + +EMBLD=${EMBLD:-./embld} +[ -x "$EMBLD" ] || { echo "skipped: embld is not built (make embld)"; exit 0; } +out=tests/golden/out/embld-help +rm -rf "$out"; mkdir -p "$out" + +for flag in --help -h; do + rc=0 + "$EMBLD" $flag > "$out/help.out" 2> "$out/help.err" || rc=$? + [ "$rc" -eq 0 ] || { echo "FAIL: $flag exited $rc, want 0"; cat "$out/help.err"; exit 1; } + [ ! -s "$out/help.err" ] || { echo "FAIL: $flag wrote to stderr"; cat "$out/help.err"; exit 1; } + grep -q '^usage: embld ' "$out/help.out" || + { echo "FAIL: $flag printed no usage on stdout"; cat "$out/help.out"; exit 1; } + grep -q 'embld --doctor' "$out/help.out" || + { echo "FAIL: $flag usage lacks the --doctor line"; cat "$out/help.out"; exit 1; } + echo "$flag: usage on stdout, exit 0" +done + +rc=0 +"$EMBLD" > "$out/bare.out" 2> "$out/bare.err" || rc=$? +[ "$rc" -eq 2 ] || { echo "FAIL: bare embld exited $rc, want 2"; exit 1; } +[ ! -s "$out/bare.out" ] || { echo "FAIL: bare embld wrote to stdout"; exit 1; } +grep -q '^usage: embld ' "$out/bare.err" || + { echo "FAIL: bare embld printed no usage on stderr"; cat "$out/bare.err"; exit 1; } +echo "bare embld: usage on stderr, exit 2" +echo "PASS embld-help" diff --git a/tools/embld/embld.c b/tools/embld/embld.c index 32a6b218..bd582646 100644 --- a/tools/embld/embld.c +++ b/tools/embld/embld.c @@ -25,6 +25,19 @@ #include "doctor.h" #include "../../src/embx/embx.h" +/* The usage text, printed to stdout for --help and to stderr when there + * is nothing to link. */ +static void usage(FILE *f) +{ + fprintf(f, "usage: embld [-o OUT] [-e ENTRY] [-Ttext ADDR] [-Tstack ADDR]\n" + " [-T SCRIPT [-L DIR]... [--orphan-handling=place|warn|error]]\n" + " [--gc-sections [--print-gc-sections]] [-Map FILE]\n" + " [--print-memory-usage] [--cmse-implib [--out-implib=FILE]]\n" + " [--embx [--cap NAME]...] INPUT.o|INPUT.a ...\n" + " embld --doctor INPUT.o|INPUT.a ... " + "(why the link fails)\n"); +} + int main(int argc, char **argv) { const char *out = "a.out"; @@ -184,6 +197,9 @@ int main(int argc, char **argv) return 2; } else if (strcmp(argv[i], "--doctor") == 0) { doctor = 1; + } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + usage(stdout); /* asked for, so stdout and success */ + return 0; } else if (argv[i][0] == '-') { fprintf(stderr, "embld: unknown option '%s'\n", argv[i]); return 2; @@ -193,13 +209,7 @@ int main(int argc, char **argv) } } if (!ninputs) { - fprintf(stderr, "usage: embld [-o OUT] [-e ENTRY] [-Ttext ADDR] [-Tstack ADDR]\n" - " [-T SCRIPT [-L DIR]... [--orphan-handling=place|warn|error]]\n" - " [--gc-sections [--print-gc-sections]] [-Map FILE]\n" - " [--print-memory-usage] [--cmse-implib [--out-implib=FILE]]\n" - " [--embx [--cap NAME]...] INPUT.o|INPUT.a ...\n" - " embld --doctor INPUT.o|INPUT.a ... " - "(why the link fails)\n"); + usage(stderr); /* a bare invocation is a mistake */ return 2; } /* each -l, now that every -L is known: libNAME.a (or, for -l:FILE,