← board

writeln(StdErr, ...) goes to stdout — StdErr not connected to fd 2

Symptom

writeln(StdErr, 'x') is written to stdout (fd 1), not stderr (fd 2). The standard StdErr text file is not bound to fd 2.

Minimal repro

program se;
begin
  writeln('to-stdout');
  writeln(StdErr, 'to-stderr');
end.
$ ./se 2>/dev/null      # stderr discarded
to-stdout
to-stderr               # <-- StdErr line still appears: it went to fd 1
$ ./se 2>&1 1>/dev/null  # capture only stderr
                        # <-- empty: nothing went to fd 2

Expected: to-stderr on fd 2, absent from fd 1.

Why it matters

Likely cause

The RTL/runtime initialises the Output text file to fd 1 but does not bind StdErr (and probably ErrOutput) to fd 2 — so writes to it fall back to fd 1. Check where Output/Input are set up at program start and add the fd-2 binding for StdErr.

Acceptance

Fix log

Follow-up (cross targets)

i386 / arm32 / aarch64 still send StdErr writes to fd 1: the IR carries the fd generically, but only the x86-64 write sites read CurWriteFd. Each cross backend's write helpers (ir_codegen386.inc STDOUT sites; the arm32/aarch64 syscall fd loads in emit.inc) need the same CurWriteFd swap + a CurWriteFd := IRIVal set in their IR_WRITE/IR_WRITELN handlers. No regression today (those handlers leave CurWriteFd at its STDOUT default).