raz / commit e5d33f12

commit
e5d33f12f3362ccc75302551e90570b1adfc1835
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c57f87ee
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.

patch · log

15 files changed, +134 -32

PROPOSALS.md modified +3 -10

@@ -1069,16 +1069,9 @@
10691069 of Linux. Each piece below takes away one of those work-arounds, and
10701070 stands on its own.
10711071
1072-**1. A load address chosen at the command line.** The executable is
1073-loaded at `Link.Base`, 400000H, fixed. A kernel is often wanted
1074-elsewhere: Limine wants one in the top 2 GB of the address space, and
1075-a kernel that maps itself may want room below it. The proposal:
1076-`raz -base ADDR`, a multiple of the page, and `Link.Base` a variable
1077-it sets. Code reaches its data relative to `rip` already; what holds
1078-an address whole (`Chunk.addAbsFixup`) is patched by the linker, which
1079-knows the base, so the cost is the option and the check that an
1080-address above 2 GB, as a higher-half kernel's, still fits where the
1081-code needs 32 bits of it. A few lines.
1072+**1. A load address chosen at the command line.** Done (2026-10-17):
1073+`raz -base ADDR`, a multiple of the page, the program wholly in the low
1074+or the top 2 GB (`doc/compiler.md`, the linker).
10821075
10831076 **2. A program with no `Rt` body.** `Rt` is always used, and its body
10841077 runs before every other module's: it sets the handlers of SIGSEGV and

README.md (history) modified +3 -2

@@ -89,7 +89,7 @@
8989
9090 ## Using it
9191
92- raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] Main.raz
92+ raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] Main.raz
9393 raz run [option]... Main.raz [arg]...
9494 raz -h
9595 raz --version
@@ -115,7 +115,8 @@
115115 instructions made for it. `-s` strips what is there for finding faults:
116116 the symbols and line numbers, the places a failure is told by, and the
117117 profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves
118-out only the profiler.
118+out only the profiler. `-base addr` loads the program at `addr`, not
119+400000H, in the low or the top 2 GB, as a kernel may want.
119120
120121 At run time, a failed check says where it is and what failed, as in
121122 `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends

doc/compiler.md (history) modified +11 -2

@@ -24,9 +24,9 @@
2424 | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls |
2626 | `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings |
27-| `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
27+| `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
29-| `src/Raz.raz` | 326 | 253 | The command line |
29+| `src/Raz.raz` | 361 | 283 | The command line |
3030
3131 Every file opens with a comment that says what it is, what it depends
3232 on and what it leaves out. Read those first; they are the map.
@@ -1648,6 +1648,15 @@
16481648 alignment, then copies their bytes into the images of text and rodata,
16491649 patching every fixup with the distance from the end of its field to its
16501650 target, and every data fixup with its target's address.
1651+
1652+The file is loaded at `base`, 400000H unless `raz -base` says another
1653+(`setBase`), as a kernel wants: Limine one in the top 2 GB. Code reaches
1654+its data by `rip`, wherever it lies, but `X64.memAbs` holds an address
1655+in 32 bits that the machine extends by their sign, and the places keep
1656+each procedure's start and stop in 32 bits: so `linkAll` refuses an
1657+image not wholly in the low or the top 2 GB, where both hold, checked by
1658+its size so that no sum overflows. `Rt.procAt` compares the low 32 bits
1659+of an address with them, the same order in either.
16511660
16521661 Between placing text and placing rodata, `makePlaces` writes the
16531662 places, a rodata chunk: for the trap report, each live procedure's

doc/raz.1 (history) modified +9 -1

@@ -1,5 +1,5 @@
11 .\" raz(1) - the raz compiler
2-.Dd October 12, 2026
2+.Dd October 17, 2026
33 .Dt RAZ 1
44 .Os
55 .Sh NAME
@@ -99,6 +99,14 @@
9999 profiler.
100100 A failure then says only its address, and the program is much
101101 smaller: hello, world is 7 KB, against 28 KB.
102+.It Fl base Ar addr
103+Load the program at
104+.Ar addr ,
105+not 400000H, as a kernel may want: a multiple of 4096, written as raz
106+writes a number, as
107+.Cm 0FFFFFFFF80000000H .
108+The program must lie wholly in the low or the top 2 GB of the address
109+space, which a 32-bit address reaches.
102110 .It Fl noprofile
103111 Leave out the profiler, which
104112 .Ev RAZ_PROFILE

lib/Rt.raz (history) modified +6 -1

@@ -355,12 +355,17 @@
355355 return q^;
356356 end;
357357
358-(* The procedure whose code holds address a, or nil *)
358+(* The procedure whose code holds address a, or nil. A procedure's
359+ start and stop are the low 32 bits of its addresses, which are all
360+ the compiler keeps; the program's code lies in the low or the top
361+ 2 GB (raz -base), and in either the low 32 bits of a, which is in
362+ the code, are in the same order as a itself. *)
359363 proc procAt(a: UINT): ^Proc;
360364 var p: ^Proc; i: INT;
361365 begin
362366 if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end;
363367 p := ^Proc(places + 5);
368+ a := a & 0FFFFFFFFH;
364369 for i := 0 to places[3] - 1 do
365370 if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end;
366371 end;

src/Elf.raz modified +3 -3

@@ -197,7 +197,7 @@
197197 putStr(cwd);
198198 putStr("raz");
199199 putN(0, 4); (* the line table, at 0 *)
200- putN(Link.Base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *)
200+ putN(Link.base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *)
201201 patch(off[1], len - off[1] - 4, 4);
202202 size[1] := len - off[1];
203203
@@ -355,7 +355,7 @@
355355 phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1);
356356 end;
357357 (* the first segment maps the headers too, so text can follow them *)
358- phdr(PtLoad, 5, 0, Link.Base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page);
358+ phdr(PtLoad, 5, 0, Link.base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page);
359359 if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end;
360360 if dyn then
361361 phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page);
@@ -424,7 +424,7 @@
424424 link is its string table's index: 5 .strtab, 8 .dynstr. The order
425425 here is the index the code above relies on. *)
426426 shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0);
427- shdr(".text", 1, 6, Link.Base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0);
427+ shdr(".text", 1, 6, Link.base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0);
428428 shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0);
429429 shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0);
430430 shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24);

src/Link.raz (history) modified +36 -10

@@ -19,11 +19,12 @@
1919 import Fmt, Diag, Scan, Chunk;
2020
2121 const
22- Base* = 400000H;
2322 Page* = 4096;
23+ Low* = 80000000H; (* the image lies below Low, or at -Low or above *)
2424
2525 var
26- textOff*, textSize*: INT; (* file offset and size of text, at Base + textOff *)
26+ base*: INT; (* where the file is loaded: raz -base, 400000H if not *)
27+ textOff*, textSize*: INT; (* file offset and size of text, at base + textOff *)
2728 roOff*, roSize*: INT; (* rodata, the room for the loader's tables included *)
2829 roAdr*: INT; (* where rodata is loaded *)
2930 extraAdr*: INT; (* that room *)
@@ -33,6 +34,11 @@
3334 placesChunk*: INT; (* the rodata chunk of the places, made by the caller *)
3435 linesOff*, linesSize*: INT; (* the line table, in the places: .debug_line *)
3536
37+proc setBase*(a: INT);
38+begin
39+ base := a;
40+end;
41+
3642 proc setPlaces*(c: INT);
3743 begin
3844 placesChunk := c;
@@ -315,11 +321,20 @@
315321 Chunk.setData(placesChunk, buf, names + lpLen, 8);
316322 end;
317323
324+(* The address a as raz writes it, in hexadecimal: 0FFFFFFFF80000000H *)
325+proc literal(a: INT): []U8;
326+var s: []U8;
327+begin
328+ s := Diag.say("{}H", [Fmt.hex(U64(a))]);
329+ if s[0] > '9' then s := Diag.say("0{}", [Fmt.str(s)]); end;
330+ return s;
331+end;
332+
318333 (* Places the live chunks, leaving roExtra bytes at the end of rodata
319334 and dynSize at the start of the writable segment. The segments follow
320335 each other in the file with no padding, and each is loaded a page
321- further from its file offset than the one before: rodata at Base +
322- Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an
336+ further from its file offset than the one before: rodata at base +
337+ Page + roOff, the writable segment at base + 2 * Page + bssOff. So an
323338 address and its offset agree within a page, as the loader needs, and
324339 no two segments, with their different permissions, share a page.
325340 The bss of a program without the loader's table has no bytes in the
@@ -328,23 +343,33 @@
328343 var c, f, at, p, e, v: INT;
329344 begin
330345 textOff := (headerSize + 31) div 32 * 32; (* as each code chunk is *)
331- e := place(Chunk.secText, Base + textOff);
332- textSize := e - (Base + textOff);
333- if placesChunk >= 0 then makePlaces(Base + textOff, e); end;
346+ e := place(Chunk.secText, base + textOff);
347+ textSize := e - (base + textOff);
348+ if placesChunk >= 0 then makePlaces(base + textOff, e); end;
334349 roOff := (textOff + textSize + 31) div 32 * 32;
335- roAdr := Base + Page + roOff;
350+ roAdr := base + Page + roOff;
336351 e := place(Chunk.secRodata, roAdr);
337352 extraAdr := (e + 7) div 8 * 8;
338353 if roExtra > 0 then e := extraAdr + roExtra; end;
339354 roSize := e - roAdr;
340355 bssOff := (roOff + roSize + 31) div 32 * 32;
341356 if dynSize > 0 then
342- bssAdr := Base + 2 * Page + bssOff;
357+ bssAdr := base + 2 * Page + bssOff;
343358 else
344359 bssAdr := (roAdr + roSize + Page - 1) div Page * Page;
345360 end;
346361 e := place(Chunk.secBss, bssAdr + dynSize);
347362 bssSize := e - bssAdr;
363+ (* An address X64.memAbs makes is 32 bits, which the machine extends
364+ by their sign, so the image must lie where such an address reaches:
365+ the low 2 GB, as at 400000H, or the top 2 GB, as a higher-half
366+ kernel at 0FFFFFFFF80000000H. The places keep each procedure's
367+ start and stop in 32 bits too, which Rt.procAt reads so. *)
368+ (* By the size, e - base, which can't overflow, where e itself might *)
369+ if not (((base >= 0) and (base <= Low - (e - base))) or ((base >= -Low) and (base <= -(e - base)))) then
370+ 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",
371+ [Fmt.str(literal(base))]));
372+ end;
348373
349374 textImg := Scan.grab(textSize + 1);
350375 (* the gaps between code chunks are int3, a trap if ever run *)
@@ -354,7 +379,7 @@
354379 for c := 0 to Chunk.numChunks - 1 do
355380 if Chunk.chunks[c].live then
356381 if Chunk.chunks[c].sec = Chunk.secText then
357- p := Chunk.chunks[c].adr - (Base + textOff);
382+ p := Chunk.chunks[c].adr - (base + textOff);
358383 COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]);
359384 for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do
360385 at := Chunk.fixups[f].at - Chunk.chunks[c].start;
@@ -378,4 +403,5 @@
378403
379404 begin
380405 placesChunk := -1;
406+ base := 400000H;
381407 end Link.

src/Raz.raz (history) modified +38 -3

@@ -1,6 +1,6 @@
11 (* Raz: the command line.
22
3- raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] Main.raz
3+ raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] Main.raz
44 raz run [-I dir]... Main.raz [arg]...
55 raz --version
66
@@ -93,6 +93,32 @@
9393 return c # nil;
9494 end;
9595
96+(* The address s, a number as raz writes one: decimal digits, or
97+ hexadecimal ones that start with a digit and end in H; and whether it
98+ is one, and fits in 64 bits. Past high(INT) it is taken as a UINT,
99+ whose bits it keeps: 0FFFFFFFF80000000H is -80000000H. *)
100+proc address(s: []U8): (INT, BOOL);
101+var i, n, d, radix: INT; v: UINT;
102+begin
103+ n := LEN(s);
104+ radix := 10;
105+ if (n > 1) and (s[n - 1] = 'H') then radix := 16; DEC(n); end;
106+ if (n = 0) or (s[0] < '0') or (s[0] > '9') then return 0, false; end;
107+ v := 0;
108+ for i := 0 to n - 1 do
109+ if (s[i] >= '0') and (s[i] <= '9') then
110+ d := INT(s[i]) - 48;
111+ elsif (radix = 16) and (s[i] >= 'A') and (s[i] <= 'F') then
112+ d := INT(s[i]) - 55;
113+ else
114+ return 0, false;
115+ end;
116+ if v > (UINT(0FFFFFFFFFFFFFFFFH) - UINT(d)) div UINT(radix) then return 0, false; end;
117+ v := v * UINT(radix) + UINT(d);
118+ end;
119+ return INT(v), true;
120+end;
121+
96122 (* What raz -h says *)
97123 proc usage;
98124 begin
@@ -105,6 +131,8 @@
105131 Out.str(" -S lists the main module's code, under each line of source\n");
106132 Out.str(" -s strips symbols, line numbers, the places a failure is told\n");
107133 Out.str(" by and the profiler: a failure says only its address\n");
134+ Out.str(" -base addr loads the program at addr, not 400000H: a multiple of 4096\n");
135+ Out.str(" in the low or the top 2 GB, as 0FFFFFFFF80000000H\n");
108136 Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n");
109137 Out.str(" -libc name links a program that uses C against glibc or musl; by\n");
110138 Out.str(" default glibc, or musl where only its loader is there\n");
@@ -170,9 +198,9 @@
170198 proc main*: INT;
171199 var
172200 path, outPath, s: []U8; dir: []U8;
173- i, first, entry, fd, last, errno: INT;
201+ i, first, entry, fd, last, errno, base: INT;
174202 m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj;
175- run, noProfile, listing, dynamic, libcSaid: BOOL; err: ERROR;
203+ run, noProfile, listing, dynamic, libcSaid, ok: BOOL; err: ERROR;
176204 begin
177205 path := "";
178206 outPath := "";
@@ -197,6 +225,13 @@
197225 X64.setListing(true);
198226 elsif s = "-s" then
199227 Elf.setStripped(true);
228+ elsif s = "-base" then
229+ INC(i);
230+ base, ok := address(Files.arg(i));
231+ Link.setBase(base);
232+ if not ok or (base mod Link.Page # 0) then
233+ Diag.fatal(say1("-base {}: expected an address, a multiple of 4096 as raz writes it, as 0FFFFFFFF80000000H", Files.arg(i)));
234+ end;
200235 elsif s = "-noprofile" then
201236 noProfile := true;
202237 elsif s = "-libc" then

test/check.sh (history) modified +11 -0

@@ -126,6 +126,17 @@
126126 if [ $? = 2 ] && grep -q '^hello$' $out/IndexS.out && grep -q '^at 0x[0-9a-f]*: index 5 is out of range' $out/IndexS.out \
127127 && [ $(wc -c < $out/IndexS) -lt $(wc -c < $out/Index) ]; then pass=$((pass + 1))
128128 else echo "FAIL raz -s:"; cat $out/IndexS.out; fail=$((fail + 1)); fi
129+# raz -base: loaded elsewhere, a program says the same of a failed
130+# check, the places found as at 400000H; one for the top 2 GB, as a
131+# higher-half kernel's, is made, though Linux won't run it there
132+entry() { od -An -tx8 -j24 -N8 $1 | tr -d ' '; }
133+$raz -base 7FE00000H -o $out/IndexB test/strip/Index.raz
134+$out/Index > $out/Index.out 2>&1
135+$out/IndexB > $out/IndexB.out 2>&1
136+$raz -base 0FFFFFFFF80000000H -o $out/IndexK test/strip/Index.raz
137+if [ $? = 0 ] && cmp -s $out/Index.out $out/IndexB.out && grep -q 'Index.main' $out/IndexB.out \
138+ && entry $out/IndexB | grep -q '^000000007fe' && entry $out/IndexK | grep -q '^ffffffff800'; then pass=$((pass + 1))
139+else echo "FAIL raz -base:"; cat $out/IndexB.out; fail=$((fail + 1)); fi
129140
130141 # raz -S: test/list/X.raz lists as X.expect
131142 for f in test/list/*.raz; do

test/err/BaseHigh.args (history) added +1 -0

@@ -0,0 +1 @@
1+-base 80000000H

test/err/BaseHigh.err (history) added +1 -0

@@ -0,0 +1 @@
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

test/err/BaseHigh.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module BaseHigh;
2+proc main*;
3+begin
4+end;
5+end BaseHigh.

test/err/BaseOdd.args (history) added +1 -0

@@ -0,0 +1 @@
1+-base 1000

test/err/BaseOdd.err (history) added +1 -0

@@ -0,0 +1 @@
1+raz: -base 1000: expected an address, a multiple of 4096 as raz writes it, as 0FFFFFFFF80000000H

test/err/BaseOdd.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module BaseOdd;
2+proc main*;
3+begin
4+end;
5+end BaseOdd.