From e5d33f12f3362ccc75302551e90570b1adfc1835 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:58:15 +0000 Subject: [PATCH] raz -base: the load address chosen at the command line Link.Base becomes Link.base, 400000H unless raz -base sets it: a multiple of 4096, written as raz writes a number. The image must lie wholly in the low or the top 2 GB, where X64.memAbs's sign-extended 32-bit address reaches; linkAll checks it by the image's size, so no sum overflows. The places keep each procedure's bounds in 32 bits, so Rt.procAt compares the low 32 bits of an address with them, the same order in either range. The second of PROPOSALS.md's three changes for a kernel. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 7293c87..1baf5d0 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -1069,16 +1069,9 @@ of Linux. Each piece below takes away one of those work-arounds, and stands on its own. -**1. A load address chosen at the command line.** The executable is -loaded at `Link.Base`, 400000H, fixed. A kernel is often wanted -elsewhere: Limine wants one in the top 2 GB of the address space, and -a kernel that maps itself may want room below it. The proposal: -`raz -base ADDR`, a multiple of the page, and `Link.Base` a variable -it sets. Code reaches its data relative to `rip` already; what holds -an address whole (`Chunk.addAbsFixup`) is patched by the linker, which -knows the base, so the cost is the option and the check that an -address above 2 GB, as a higher-half kernel's, still fits where the -code needs 32 bits of it. A few lines. +**1. A load address chosen at the command line.** Done (2026-10-17): +`raz -base ADDR`, a multiple of the page, the program wholly in the low +or the top 2 GB (`doc/compiler.md`, the linker). **2. A program with no `Rt` body.** `Rt` is always used, and its body runs before every other module's: it sets the handlers of SIGSEGV and diff --git a/README.md b/README.md index 16bfe03..e9526a3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ ## Using it - raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] Main.raz raz run [option]... Main.raz [arg]... raz -h raz --version @@ -115,7 +115,8 @@ instructions made for it. `-s` strips what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves -out only the profiler. +out only the profiler. `-base addr` loads the program at `addr`, not +400000H, in the low or the top 2 GB, as a kernel may want. At run time, a failed check says where it is and what failed, as in `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends diff --git a/doc/compiler.md b/doc/compiler.md index 01ab221..a8d7f94 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -24,9 +24,9 @@ | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | +| `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 326 | 253 | The command line | +| `src/Raz.raz` | 361 | 283 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -1648,6 +1648,15 @@ alignment, then copies their bytes into the images of text and rodata, patching every fixup with the distance from the end of its field to its target, and every data fixup with its target's address. + +The file is loaded at `base`, 400000H unless `raz -base` says another +(`setBase`), as a kernel wants: Limine one in the top 2 GB. Code reaches +its data by `rip`, wherever it lies, but `X64.memAbs` holds an address +in 32 bits that the machine extends by their sign, and the places keep +each procedure's start and stop in 32 bits: so `linkAll` refuses an +image not wholly in the low or the top 2 GB, where both hold, checked by +its size so that no sum overflows. `Rt.procAt` compares the low 32 bits +of an address with them, the same order in either. Between placing text and placing rodata, `makePlaces` writes the places, a rodata chunk: for the trap report, each live procedure's diff --git a/doc/raz.1 b/doc/raz.1 index a95ea52..a4e1321 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -1,5 +1,5 @@ .\" raz(1) - the raz compiler -.Dd October 12, 2026 +.Dd October 17, 2026 .Dt RAZ 1 .Os .Sh NAME @@ -99,6 +99,14 @@ profiler. A failure then says only its address, and the program is much smaller: hello, world is 7 KB, against 28 KB. +.It Fl base Ar addr +Load the program at +.Ar addr , +not 400000H, as a kernel may want: a multiple of 4096, written as raz +writes a number, as +.Cm 0FFFFFFFF80000000H . +The program must lie wholly in the low or the top 2 GB of the address +space, which a 32-bit address reaches. .It Fl noprofile Leave out the profiler, which .Ev RAZ_PROFILE diff --git a/lib/Rt.raz b/lib/Rt.raz index 8b5bc3a..5a8b2ad 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -355,12 +355,17 @@ return q^; end; -(* The procedure whose code holds address a, or nil *) +(* The procedure whose code holds address a, or nil. A procedure's + start and stop are the low 32 bits of its addresses, which are all + the compiler keeps; the program's code lies in the low or the top + 2 GB (raz -base), and in either the low 32 bits of a, which is in + the code, are in the same order as a itself. *) proc procAt(a: UINT): ^Proc; var p: ^Proc; i: INT; begin if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end; p := ^Proc(places + 5); + a := a & 0FFFFFFFFH; for i := 0 to places[3] - 1 do if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end; end; diff --git a/src/Elf.raz b/src/Elf.raz index 6f4b84e..9339f23 100644 --- a/src/Elf.raz +++ b/src/Elf.raz @@ -197,7 +197,7 @@ putStr(cwd); putStr("raz"); putN(0, 4); (* the line table, at 0 *) - putN(Link.Base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *) + putN(Link.base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *) patch(off[1], len - off[1] - 4, 4); size[1] := len - off[1]; @@ -355,7 +355,7 @@ phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1); end; (* the first segment maps the headers too, so text can follow them *) - phdr(PtLoad, 5, 0, Link.Base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); + phdr(PtLoad, 5, 0, Link.base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end; if dyn then phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page); @@ -424,7 +424,7 @@ link is its string table's index: 5 .strtab, 8 .dynstr. The order here is the index the code above relies on. *) shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0); - shdr(".text", 1, 6, Link.Base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); + shdr(".text", 1, 6, Link.base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0); shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0); shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); diff --git a/src/Link.raz b/src/Link.raz index dee903f..264df20 100644 --- a/src/Link.raz +++ b/src/Link.raz @@ -19,11 +19,12 @@ import Fmt, Diag, Scan, Chunk; const - Base* = 400000H; Page* = 4096; + Low* = 80000000H; (* the image lies below Low, or at -Low or above *) var - textOff*, textSize*: INT; (* file offset and size of text, at Base + textOff *) + base*: INT; (* where the file is loaded: raz -base, 400000H if not *) + textOff*, textSize*: INT; (* file offset and size of text, at base + textOff *) roOff*, roSize*: INT; (* rodata, the room for the loader's tables included *) roAdr*: INT; (* where rodata is loaded *) extraAdr*: INT; (* that room *) @@ -33,6 +34,11 @@ placesChunk*: INT; (* the rodata chunk of the places, made by the caller *) linesOff*, linesSize*: INT; (* the line table, in the places: .debug_line *) +proc setBase*(a: INT); +begin + base := a; +end; + proc setPlaces*(c: INT); begin placesChunk := c; @@ -315,11 +321,20 @@ Chunk.setData(placesChunk, buf, names + lpLen, 8); end; +(* The address a as raz writes it, in hexadecimal: 0FFFFFFFF80000000H *) +proc literal(a: INT): []U8; +var s: []U8; +begin + s := Diag.say("{}H", [Fmt.hex(U64(a))]); + if s[0] > '9' then s := Diag.say("0{}", [Fmt.str(s)]); end; + return s; +end; + (* Places the live chunks, leaving roExtra bytes at the end of rodata and dynSize at the start of the writable segment. The segments follow each other in the file with no padding, and each is loaded a page - further from its file offset than the one before: rodata at Base + - Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an + further from its file offset than the one before: rodata at base + + Page + roOff, the writable segment at base + 2 * Page + bssOff. So an address and its offset agree within a page, as the loader needs, and no two segments, with their different permissions, share a page. The bss of a program without the loader's table has no bytes in the @@ -328,23 +343,33 @@ var c, f, at, p, e, v: INT; begin textOff := (headerSize + 31) div 32 * 32; (* as each code chunk is *) - e := place(Chunk.secText, Base + textOff); - textSize := e - (Base + textOff); - if placesChunk >= 0 then makePlaces(Base + textOff, e); end; + e := place(Chunk.secText, base + textOff); + textSize := e - (base + textOff); + if placesChunk >= 0 then makePlaces(base + textOff, e); end; roOff := (textOff + textSize + 31) div 32 * 32; - roAdr := Base + Page + roOff; + roAdr := base + Page + roOff; e := place(Chunk.secRodata, roAdr); extraAdr := (e + 7) div 8 * 8; if roExtra > 0 then e := extraAdr + roExtra; end; roSize := e - roAdr; bssOff := (roOff + roSize + 31) div 32 * 32; if dynSize > 0 then - bssAdr := Base + 2 * Page + bssOff; + bssAdr := base + 2 * Page + bssOff; else bssAdr := (roAdr + roSize + Page - 1) div Page * Page; end; e := place(Chunk.secBss, bssAdr + dynSize); bssSize := e - bssAdr; + (* An address X64.memAbs makes is 32 bits, which the machine extends + by their sign, so the image must lie where such an address reaches: + the low 2 GB, as at 400000H, or the top 2 GB, as a higher-half + kernel at 0FFFFFFFF80000000H. The places keep each procedure's + start and stop in 32 bits too, which Rt.procAt reads so. *) + (* By the size, e - base, which can't overflow, where e itself might *) + if not (((base >= 0) and (base <= Low - (e - base))) or ((base >= -Low) and (base <= -(e - base)))) then + Diag.fatal(Diag.say("-base {}: the program must lie in the low or the top 2 GB, where a 32-bit address reaches, and from there it doesn't", + [Fmt.str(literal(base))])); + end; textImg := Scan.grab(textSize + 1); (* the gaps between code chunks are int3, a trap if ever run *) @@ -354,7 +379,7 @@ for c := 0 to Chunk.numChunks - 1 do if Chunk.chunks[c].live then if Chunk.chunks[c].sec = Chunk.secText then - p := Chunk.chunks[c].adr - (Base + textOff); + p := Chunk.chunks[c].adr - (base + textOff); COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]); for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do at := Chunk.fixups[f].at - Chunk.chunks[c].start; @@ -378,4 +403,5 @@ begin placesChunk := -1; + base := 400000H; end Link. diff --git a/src/Raz.raz b/src/Raz.raz index 6db030c..25b55c8 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -1,6 +1,6 @@ (* Raz: the command line. - raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] Main.raz raz run [-I dir]... Main.raz [arg]... raz --version @@ -93,6 +93,32 @@ return c # nil; end; +(* The address s, a number as raz writes one: decimal digits, or + hexadecimal ones that start with a digit and end in H; and whether it + is one, and fits in 64 bits. Past high(INT) it is taken as a UINT, + whose bits it keeps: 0FFFFFFFF80000000H is -80000000H. *) +proc address(s: []U8): (INT, BOOL); +var i, n, d, radix: INT; v: UINT; +begin + n := LEN(s); + radix := 10; + if (n > 1) and (s[n - 1] = 'H') then radix := 16; DEC(n); end; + if (n = 0) or (s[0] < '0') or (s[0] > '9') then return 0, false; end; + v := 0; + for i := 0 to n - 1 do + if (s[i] >= '0') and (s[i] <= '9') then + d := INT(s[i]) - 48; + elsif (radix = 16) and (s[i] >= 'A') and (s[i] <= 'F') then + d := INT(s[i]) - 55; + else + return 0, false; + end; + if v > (UINT(0FFFFFFFFFFFFFFFFH) - UINT(d)) div UINT(radix) then return 0, false; end; + v := v * UINT(radix) + UINT(d); + end; + return INT(v), true; +end; + (* What raz -h says *) proc usage; begin @@ -105,6 +131,8 @@ Out.str(" -S lists the main module's code, under each line of source\n"); Out.str(" -s strips symbols, line numbers, the places a failure is told\n"); Out.str(" by and the profiler: a failure says only its address\n"); + Out.str(" -base addr loads the program at addr, not 400000H: a multiple of 4096\n"); + Out.str(" in the low or the top 2 GB, as 0FFFFFFFF80000000H\n"); Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n"); Out.str(" -libc name links a program that uses C against glibc or musl; by\n"); Out.str(" default glibc, or musl where only its loader is there\n"); @@ -170,9 +198,9 @@ proc main*: INT; var path, outPath, s: []U8; dir: []U8; - i, first, entry, fd, last, errno: INT; + i, first, entry, fd, last, errno, base: INT; m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; - run, noProfile, listing, dynamic, libcSaid: BOOL; err: ERROR; + run, noProfile, listing, dynamic, libcSaid, ok: BOOL; err: ERROR; begin path := ""; outPath := ""; @@ -197,6 +225,13 @@ X64.setListing(true); elsif s = "-s" then Elf.setStripped(true); + elsif s = "-base" then + INC(i); + base, ok := address(Files.arg(i)); + Link.setBase(base); + if not ok or (base mod Link.Page # 0) then + Diag.fatal(say1("-base {}: expected an address, a multiple of 4096 as raz writes it, as 0FFFFFFFF80000000H", Files.arg(i))); + end; elsif s = "-noprofile" then noProfile := true; elsif s = "-libc" then diff --git a/test/check.sh b/test/check.sh index 6d58260..5dd63b8 100755 --- a/test/check.sh +++ b/test/check.sh @@ -126,6 +126,17 @@ if [ $? = 2 ] && grep -q '^hello$' $out/IndexS.out && grep -q '^at 0x[0-9a-f]*: index 5 is out of range' $out/IndexS.out \ && [ $(wc -c < $out/IndexS) -lt $(wc -c < $out/Index) ]; then pass=$((pass + 1)) else echo "FAIL raz -s:"; cat $out/IndexS.out; fail=$((fail + 1)); fi +# raz -base: loaded elsewhere, a program says the same of a failed +# check, the places found as at 400000H; one for the top 2 GB, as a +# higher-half kernel's, is made, though Linux won't run it there +entry() { od -An -tx8 -j24 -N8 $1 | tr -d ' '; } +$raz -base 7FE00000H -o $out/IndexB test/strip/Index.raz +$out/Index > $out/Index.out 2>&1 +$out/IndexB > $out/IndexB.out 2>&1 +$raz -base 0FFFFFFFF80000000H -o $out/IndexK test/strip/Index.raz +if [ $? = 0 ] && cmp -s $out/Index.out $out/IndexB.out && grep -q 'Index.main' $out/IndexB.out \ + && entry $out/IndexB | grep -q '^000000007fe' && entry $out/IndexK | grep -q '^ffffffff800'; then pass=$((pass + 1)) +else echo "FAIL raz -base:"; cat $out/IndexB.out; fail=$((fail + 1)); fi # raz -S: test/list/X.raz lists as X.expect for f in test/list/*.raz; do diff --git a/test/err/BaseHigh.args b/test/err/BaseHigh.args new file mode 100644 index 0000000..aa1b2a0 --- /dev/null +++ b/test/err/BaseHigh.args @@ -0,0 +1 @@ +-base 80000000H diff --git a/test/err/BaseHigh.err b/test/err/BaseHigh.err new file mode 100644 index 0000000..14c8160 --- /dev/null +++ b/test/err/BaseHigh.err @@ -0,0 +1 @@ +raz: -base 80000000H: the program must lie in the low or the top 2 GB, where a 32-bit address reaches, and from there it doesn't diff --git a/test/err/BaseHigh.raz b/test/err/BaseHigh.raz new file mode 100644 index 0000000..68ce9eb --- /dev/null +++ b/test/err/BaseHigh.raz @@ -0,0 +1,5 @@ +module BaseHigh; +proc main*; +begin +end; +end BaseHigh. diff --git a/test/err/BaseOdd.args b/test/err/BaseOdd.args new file mode 100644 index 0000000..a132a57 --- /dev/null +++ b/test/err/BaseOdd.args @@ -0,0 +1 @@ +-base 1000 diff --git a/test/err/BaseOdd.err b/test/err/BaseOdd.err new file mode 100644 index 0000000..0385f4b --- /dev/null +++ b/test/err/BaseOdd.err @@ -0,0 +1 @@ +raz: -base 1000: expected an address, a multiple of 4096 as raz writes it, as 0FFFFFFFF80000000H diff --git a/test/err/BaseOdd.raz b/test/err/BaseOdd.raz new file mode 100644 index 0000000..717ccad --- /dev/null +++ b/test/err/BaseOdd.raz @@ -0,0 +1,5 @@ +module BaseOdd; +proc main*; +begin +end; +end BaseOdd.