| | @@ -1,11 +1,15 @@ |
| 1 | 1 | { jed.pas: the command line. |
| 2 | 2 | |
| 3 | 3 | jed [-I dir]... [-o file] Main.jed |
| 4 | + jed run [-I dir]... Main.jed [arg]... |
| 4 | 5 | jed --version |
| 5 | 6 | |
| 6 | 7 | Compiles the main module and every module it imports, writes the |
| 7 | 8 | entry point, links what it reaches, and writes the executable: by |
| 8 | | - default the main module's file without its .jed. |
| 9 | + default the main module's file without its .jed. jed run writes it |
| 10 | + to a memfd instead, a file that lives only in memory, and executes |
| 11 | + it in jed's place with the arguments after Main.jed, so its status |
| 12 | + is the program's; its argv[0] is Main.jed. |
| 9 | 13 | |
| 10 | 14 | The program starts by saving argc and argv, runs every module's body |
| 11 | 15 | (those of the modules the program imports, imported modules first), |
| | @@ -21,11 +25,13 @@ |
| 21 | 25 | |
| 22 | 26 | {$mode objfpc}{$H-} |
| 23 | 27 | |
| 24 | | -uses math, scan, sym, x64, gen, parse, link, elf; |
| 28 | +uses math, BaseUnix, syscall, scan, sym, x64, gen, parse, link, elf; |
| 25 | 29 | |
| 26 | 30 | const |
| 27 | 31 | Version = '0.3.0'; |
| 28 | 32 | Report = '2026-10.0'; { the version of doc/jed.md it follows } |
| 33 | + MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 34 | + MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 29 | 35 | |
| 30 | 36 | { The modules the program uses: the main module, Rt, and every module |
| 31 | 37 | they import. A module read only for the interface check is not. } |
| | @@ -82,10 +88,10 @@ |
| 82 | 88 | |
| 83 | 89 | var |
| 84 | 90 | path, outPath, dir, s: shortstring; |
| 85 | | - i, entry, start: longint; |
| 91 | + i, first, entry, start, fd: longint; |
| 86 | 92 | m, rt: PModule; |
| 87 | 93 | main: PObj; |
| 88 | | - hasResult, dynamic: boolean; |
| 94 | + hasResult, dynamic, run: boolean; |
| 89 | 95 | |
| 90 | 96 | begin |
| 91 | 97 | { constants fold as the machine computes: NaN and infinities, no traps } |
| | @@ -93,7 +99,8 @@ |
| 93 | 99 | path := ''; |
| 94 | 100 | outPath := ''; |
| 95 | 101 | numInc := 0; |
| 96 | | - i := 1; |
| 102 | + run := (ParamCount >= 1) and (ParamStr(1) = 'run'); |
| 103 | + i := 1 + ord(run); |
| 97 | 104 | while i <= ParamCount do begin |
| 98 | 105 | s := ParamStr(i); |
| 99 | 106 | if s = '-I' then begin |
| | @@ -109,10 +116,16 @@ |
| 109 | 116 | Halt(0); |
| 110 | 117 | end else if (s <> '') and (s[1] = '-') then |
| 111 | 118 | Fatal('unknown option ' + s) |
| 112 | | - else path := s; |
| 119 | + else begin |
| 120 | + path := s; |
| 121 | + first := i; |
| 122 | + if run then break; { the rest are the program's } |
| 123 | + end; |
| 113 | 124 | inc(i); |
| 114 | 125 | end; |
| 115 | | - if path = '' then Fatal('usage: jed [-I dir]... [-o file] Main.jed'); |
| 126 | + if path = '' then |
| 127 | + Fatal('usage: jed [-I dir]... [-o file] Main.jed, or jed run [-I dir]... Main.jed [arg]...'); |
| 128 | + if run and (outPath <> '') then Fatal('jed run takes no -o'); |
| 116 | 129 | dir := ParamStr(0); |
| 117 | 130 | i := length(dir); |
| 118 | 131 | while (i > 0) and (dir[i] <> '/') do dec(i); |
| | @@ -187,5 +200,15 @@ |
| 187 | 200 | EndChunk(start); |
| 188 | 201 | |
| 189 | 202 | mainPath := path; |
| 203 | + if run then begin |
| 204 | + { argv from Main.jed on is the program's, ending in nil as it must } |
| 205 | + fd := Do_SysCall(MemfdCreate, TSysParam(PChar('jed')), MfdCloexec); |
| 206 | + if fd < 0 then Fatal('can''t create a memfd'); |
| 207 | + Str(fd, s); |
| 208 | + outPath := '/proc/self/fd/' + s + #0; |
| 209 | + WriteElf(outPath, entry); |
| 210 | + FpExecve(@outPath[1], @argv[first], envp); |
| 211 | + Fatal('can''t run ' + path); |
| 212 | + end; |
| 190 | 213 | WriteElf(outPath, entry); |
| 191 | 214 | end. |