raz / commit 5dadea44

commit
5dadea4419a0b4b9e000c6d53bd32175f5f623d7
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
cee2693c
One table of lines, not two

The places held Rt's own table, 16 bytes a line note, beside DWARF's
line table, which gdb read and wasn't loaded. Now link.pas makes the
DWARF line program, its columns added and its small steps special
opcodes, at the end of the places, loaded; .debug_line's header
points at it there, and Rt runs it (row) to say where code comes
from, through place, which the entry point gives it as it gives
calls, so that raz -s leaves both out.

hello 35,760 -> 28,424 bytes, -s 7,304 -> 6,752; rat 607,656 ->
505,184 bytes. The guide's table of lines is true again.

patch · log

8 files changed, +336 -204

PLAN.md (history) modified +1 -1

@@ -40,7 +40,7 @@
4040 4. Done: **`while` tested at the bottom**, its test moved after the body and
4141 jumped to first, as `for` is now. ulam found nothing in it, and lost
4242 3% where the test was long: measured, and kept only if it pays.
43-5. **One table of lines, not two.** Rt finds a place in `.debug_line`,
43+5. Done: **One table of lines, not two.** Rt finds a place in `.debug_line`,
4444 loaded with the program, which gdb reads already, in place of the
4545 places' own 16 bytes a line: about 7 KB off hello, more off a big
4646 program, for a decoder of DWARF's line program in Rt.

README.md (history) modified +1 -1

@@ -98,7 +98,7 @@
9898 code instead of writing it: each line of source, and under it the
9999 instructions made for it. `-s` strips what is there for finding faults:
100100 the symbols and line numbers, the places a failure is told by, and the
101-profiler, so hello, world is 7 KB instead of 35; `-noprofile` leaves
101+profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves
102102 out only the profiler.
103103
104104 At run time, a failed check says where it is and what failed, as in

doc/compiler.md (history) modified +39 -26

@@ -12,15 +12,15 @@
1212 |---|---|---|---|
1313 | `src/diag.pas` | 316 | 233 | Messages: positions, source excerpts, colour, counts |
1414 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
15-| `src/sym.pas` | 644 | 527 | Types, objects, scopes, layout |
15+| `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout |
1616 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
17-| `src/x64.pas` | 1,128 | 936 | The instruction encoder, jumps within a procedure, the assembler |
18-| `src/gen.pas` | 1,775 | 1,294 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19-| `src/item.pas` | 1,551 | 1,278 | Items: types, constants, variables, operators, conversions, calls |
20-| `src/parse.pas` | 2,975 | 2,612 | Declarations, statements, expressions, built-ins, recovery, warnings |
21-| `src/link.pas` | 259 | 195 | Tree shaking, layout, patching, the places |
22-| `src/elf.pas` | 438 | 355 | Static and dynamic ELF, symbol table, line numbers |
23-| `src/raz.pas` | 244 | 187 | The command line |
17+| `src/x64.pas` | 1,146 | 951 | The instruction encoder, jumps within a procedure, the assembler |
18+| `src/gen.pas` | 2,010 | 1,462 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19+| `src/item.pas` | 1,574 | 1,299 | Items: types, constants, variables, operators, conversions, calls |
20+| `src/parse.pas` | 3,078 | 2,689 | Declarations, statements, expressions, built-ins, recovery, warnings |
21+| `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places |
22+| `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers |
23+| `src/raz.pas` | 245 | 188 | The command line |
2424
2525 Every file opens with a comment that says what it is, what it depends
2626 on and what it leaves out. Read those first; they are the map.
@@ -417,6 +417,13 @@
417417 it to where its loops fell: without the top aligned it lost
418418 nothing, and rat gained nothing either.
419419
420+5. **One table of lines, not two.** Rt had a table of its own in the
421+ places, 16 bytes a line note, beside the DWARF line table that gdb
422+ read, which wasn't loaded. Now the DWARF table, its columns added,
423+ is in the places, loaded, and Rt runs it (`row`, in `Rt.raz`) where
424+ it looked through its own; `.debug_line`'s section header points
425+ at it in rodata. Hello is 7 KB smaller, 28 KB; rat 17%, 505 KB.
426+
420427 ## diag.pas: messages
421428
422429 Every message the compiler prints is made here. A position, `TPos`, is
@@ -908,13 +915,17 @@
908915 target, and every data fixup with its target's address.
909916
910917 Between placing text and placing rodata, `MakePlaces` writes the
911-places, a rodata chunk: for each line note in live code, its address,
912-line, column and file; and for the trap report, each live procedure's
918+places, a rodata chunk: for the trap report, each live procedure's
913919 address range and name, and its variables, which `Body` (`parse.pas`)
914920 notes with `NoteVar`: each one's name, its place in the frame and how
915-`Rt` shows it (`VarKind`). `Rt` reads them, through `Rt.places`, which
916-the entry point sets, to say where a failed check or a signal was: the
917-place with the highest address up to the one it has. Its signal
921+`Rt` shows it (`VarKind`); then the line table (`MakeLines`), DWARF
922+4's line program, which gdb reads as `.debug_line`: one sequence of
923+rows for each procedure, a row for each line note in live code, its
924+address, file, line and column. A row that moves the address and the
925+line a little is a byte, one of DWARF's special opcodes. `Rt` reads
926+the places, through `Rt.places`, which the entry point sets, and runs
927+the line table (`row`) to say where a failed check or a signal was:
928+the row with the highest address up to the one it has. Its signal
918929 handlers, set in its body, run on a stack of their own, so that they
919930 can tell a stack overflow, and leave by returning to the faulting
920931 instruction with the handler reset, so that the signal ends the
@@ -931,12 +942,12 @@
931942 those fell in, by the same tables: each procedure's samples in its own
932943 code, and its total, counting once each sample it was on the chain of.
933944 The entry point gives Rt what the program has (`TLink`): the places,
934-`calls`, which tells the calls under way, and the profiler's start.
935-What it isn't given, nothing reaches, so the linker leaves it out:
936-`raz -noprofile` gives no profiler, and `raz -s` none of the three,
937-whose failures say only their address (and `elf.pas` writes no symbols
938-or line numbers). Hello is 35 KB, 25 KB without the profiler, and 7 KB
939-stripped.
945+`calls`, which tells the calls under way, `place`, which says where an
946+address is, and the profiler's start. What it isn't given, nothing
947+reaches, so the linker leaves it out: `raz -noprofile` gives no
948+profiler, and `raz -s` none of them, whose failures say only their
949+address (and `elf.pas` writes no symbols or line numbers). Hello is 28
950+KB, 21 KB without the profiler, and 7 KB stripped.
940951
941952 The layout is chosen so the file can be mapped as it lies, with no
942953 padding: text just after the headers, then rodata, then the dynamic
@@ -949,8 +960,9 @@
949960
950961 `WriteElf` writes the ELF header, the program headers, the text and
951962 rodata images, and then what isn't loaded: section headers, a symbol
952-table with every live procedure and variable by `Module.name`, and the
953-line table.
963+table with every live procedure and variable by `Module.name`, and
964+the compile unit that names the line table. The line table is in the
965+places, in rodata; `.debug_line`'s header points there.
954966
955967 A program that uses C is linked dynamically, in the least way the
956968 loader accepts: an interpreter header naming the loader; a dynamic
@@ -961,10 +973,10 @@
961973 `SizeTables` and `PutTables` lay these out in the room `link.pas` left
962974 at the end of rodata.
963975
964-Line numbers are DWARF 4: `PutDebug` writes a line table with one
965-sequence per procedure, from the notes `NoteLine` made, and the single
966-compile unit gdb needs to find it. There are no types or variables in
967-it; gdb can stop at a line and show where it is, not print a variable.
976+Line numbers are DWARF 4: the line table `MakeLines` made from the
977+notes `NoteLine` made, and the single compile unit gdb needs to find
978+it, which `PutDebug` writes. There are no types or variables in it;
979+gdb can stop at a line and show where it is, not print a variable.
968980
969981 ## raz.pas: the command line
970982
@@ -1063,7 +1075,8 @@
10631075 of its procedure. A generic instance first used in a procedure, or in
10641076 a module's body, is made before that code begins (`InstancesFor`).
10651077 - **The executable** carries DWARF line numbers and the names of
1066- procedures for gdb, and `Rt`'s own table of places (`Rt.places`).
1078+ procedures for gdb, and the places (`Rt.places`), which hold the
1079+ line table, DWARF's, that gdb reads and `Rt` runs.
10671080
10681081 ## Where to start changing it
10691082

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

@@ -98,7 +98,7 @@
9898 table, its line numbers, the places a failure is told by, and the
9999 profiler.
100100 A failure then says only its address, and the program is much
101-smaller: hello, world is 7 KB, against 35 KB.
101+smaller: hello, world is 7 KB, against 28 KB.
102102 .It Fl noprofile
103103 Leave out the profiler, which
104104 .Ev RAZ_PROFILE

lib/Rt.raz (history) modified +160 -52

@@ -3,7 +3,7 @@
33 failures; and the handlers that say where the program was when a
44 signal stops it.
55 The compiler finds fail, exit, cExit, places, profiler,
6- startProfile, teller and calls by these names.
6+ startProfile, teller, calls, placer and place by these names.
77 After a failed check or a signal, and on demand with trace, Rt says
88 the calls under way and their variables, from the frames' chain of
99 rbp and the compiler's tables in places. A program run with
@@ -13,9 +13,9 @@
1313 procedures it was calling. A program built with raz -s or
1414 -noprofile has no profiler: the entry point leaves profiler nil,
1515 nothing reaches startProfile, and the linker leaves it out with all
16- it uses. Built with -s it has no places either, and no teller: a
17- failure is told by its address, and calls, which would need the
18- places, is left out the same way.
16+ it uses. Built with -s it has no places either, and no teller or
17+ placer: a failure is told by its address, and calls and place,
18+ which would need the places, are left out the same way.
1919 The handlers are Linux's on x86-64, set by system calls: what the
2020 kernel hands them is laid out as it is there. *)
2121 module Rt;
@@ -35,14 +35,18 @@
3535 MaxSamples = 100000;
3636 MaxProcs = 8192;
3737 MaxPlaces = 65536;
38+ (* the line table's special opcodes, as the compiler makes them *)
39+ LineBase = -5;
40+ LineRange = 14;
41+ OpcodeBase = 13;
3842 Depth = 24; (* the callers noted with each sample, at most *)
3943 (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *)
4044 Flags = 4H + 8000000H + 4000000H + 80000000H;
4145
4246 type
43- (* The code from address at on comes from line and col of the
44- file whose path is at path in the names *)
45- Place = record at, line, col, path: U32; end;
47+ (* A row of the line table: the code from address at on comes from
48+ line and column col of file, numbered from 1 *)
49+ Row = record at: UINT; file, line, col: INT; end;
4650 (* A procedure, from address start to stop, called name, and its
4751 count variables from first on; it saves the registers of its
4852 variables, bits of regs, a quadword each from saveAt in its frame *)
@@ -65,10 +69,11 @@
6569 atExits: [MaxAtExit]proc;
6670 numAtExit: INT;
6771 cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *)
68- (* Set first thing too: the numbers of places, where the program's
69- code starts and stops, the numbers of procedures and variables;
70- the places, procedures and variables, and the paths and names.
71- The linker makes them. *)
72+ (* Set first thing too: where the line table is in them, where the
73+ program's code starts and stops, the numbers of procedures and
74+ variables; the procedures and variables, and their names; and
75+ the line table, DWARF's line program, which gdb reads as
76+ .debug_line. The linker makes them. *)
7277 places: ^INT;
7378 line: [256]U8; (* the message being made *)
7479 (* rbx, r12, r13, r14 and r15 in the frame being told of, where the
@@ -91,6 +96,8 @@
9196 (* calls, put here by the entry point, or nil: a failure in a program
9297 built with raz -s, which has no places, tells no calls *)
9398 teller: proc(ip, bp: UINT; vars: BOOL);
99+ (* place, put here by the entry point, or nil, as teller *)
100+ placer: proc(a: UINT);
94101 procCount: [MaxProcs]INT;
95102 procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *)
96103 placeCount: [MaxPlaces]INT;
@@ -177,12 +184,122 @@
177184 lineLen := 0;
178185 end;
179186
180-(* The paths and names, after the places, procedures and variables *)
187+(* The names, after the procedures and variables *)
181188 proc names: ^U8;
182189 begin
183- return ^U8(places + 5) + 16 * places[0] + 28 * places[3] + 12 * places[4];
190+ return ^U8(places + 5) + 28 * places[3] + 12 * places[4];
191+end;
192+
193+(* The line table *)
194+proc lines: ^U8;
195+begin
196+ return ^U8(places) + places[0];
184197 end;
185198
199+(* The number p points at, in LEB128, unsigned, and where the next
200+ thing starts *)
201+proc uleb(p: ^U8): (INT, ^U8);
202+var n, shift: INT;
203+begin
204+ n := 0;
205+ shift := 0;
206+ while p^ >= 80H do
207+ n := n | INT(p^ & 7FH) shl shift;
208+ INC(shift, 7);
209+ p := p + 1;
210+ end;
211+ return n | INT(p^) shl shift, p + 1;
212+end;
213+
214+(* The same, signed *)
215+proc sleb(p: ^U8): (INT, ^U8);
216+var n, shift, b: INT;
217+begin
218+ n := 0;
219+ shift := 0;
220+ repeat
221+ b := INT(p^);
222+ p := p + 1;
223+ n := n | (b & 7FH) shl shift;
224+ INC(shift, 7);
225+ until b < 80H;
226+ if (shift < 64) and (b & 40H # 0) then n := n | (-1 shl shift); end; (* its sign *)
227+ return n, p;
228+end;
229+
230+(* The path of the line table's file f, as a C string: the files'
231+ names follow the table's header of 29 bytes, each ending in a zero
232+ and followed by three numbers *)
233+proc path(f: INT): ^U8;
234+var p: ^U8; i, k: INT;
235+begin
236+ p := lines() + 29;
237+ for i := 2 to f do
238+ while p^ # 0 do p := p + 1; end;
239+ p := p + 1;
240+ for k := 1 to 3 do _, p := uleb(p); end;
241+ end;
242+ return p;
243+end;
244+
245+(* The row of the line table that the code at address a comes from,
246+ the one with the highest address up to a, and its number; or, if
247+ k >= 0, row number k. The number is -1 if there is none. The table
248+ is run as DWARF says, each opcode changing the row being made; the
249+ compiler uses only those below, a row made by copy or by a special
250+ opcode, which adds to the address and the line at once. *)
251+proc row(a: UINT; k: INT): (Row, INT);
252+var p, stop: ^U8; q: ^U32; at: ^UINT; r, best: Row; n, found, op, v: INT;
253+begin
254+ p := lines();
255+ q := ^U32(p);
256+ stop := p + 4 + INT(q^);
257+ q := ^U32(p + 6);
258+ p := p + 10 + INT(q^); (* the program, past the header *)
259+ r := {file: 1, line: 1};
260+ n := 0;
261+ found := -1;
262+ while p < stop do
263+ op := INT(p^);
264+ p := p + 1;
265+ if op >= OpcodeBase then
266+ op := op - OpcodeBase;
267+ r.at := r.at + UINT(op div LineRange);
268+ r.line := r.line + LineBase + op mod LineRange;
269+ op := 1;
270+ elsif op = 0 then (* extended: its length, then which *)
271+ v, p := uleb(p);
272+ if p^ = 1 then r := {file: 1, line: 1}; (* end_sequence *)
273+ elsif p^ = 2 then at := ^UINT(p + 1); r.at := at^; (* set_address *)
274+ end;
275+ p := p + v;
276+ elsif op = 2 then v, p := uleb(p); r.at := r.at + UINT(v); (* advance_pc *)
277+ elsif op = 3 then v, p := sleb(p); r.line := r.line + v; (* advance_line *)
278+ elsif op = 4 then r.file, p := uleb(p); (* set_file *)
279+ elsif op = 5 then r.col, p := uleb(p); (* set_column *)
280+ end;
281+ if op = 1 then (* copy, or a special opcode: the row r *)
282+ if n = k then return r, n; end;
283+ if (k < 0) and (r.at <= a) and ((found < 0) or (r.at >= best.at)) then
284+ best := r;
285+ found := n;
286+ end;
287+ INC(n);
288+ end;
289+ end;
290+ return best, found;
291+end;
292+
293+(* Puts where row r comes from, as file:line:col *)
294+proc putRow(r: Row);
295+begin
296+ putStr(path(r.file));
297+ put(":");
298+ putInt(r.line);
299+ put(":");
300+ putInt(r.col);
301+end;
302+
186303 proc putHex(u: UINT);
187304 var buf: [16]U8; i: INT;
188305 begin
@@ -196,33 +313,29 @@
196313 put(buf[i..16]);
197314 end;
198315
199-(* Puts where the code at address a comes from, as file:line:col: and
200- a space, if a is in the program's code: the place with the highest
201- address up to a. *)
316+(* The compiler's, as fail is: puts where the code at address a comes
317+ from, as file:line:col: and a space, if a is in the program's code:
318+ the row of the line table with the highest address up to a. *)
319+proc place*(a: UINT);
320+var r: Row; n: INT;
321+begin
322+ if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end;
323+ r, n := row(a, -1);
324+ if n < 0 then return; end;
325+ putRow(r);
326+ put(": ");
327+end;
328+
329+(* Puts where the code at address a is: place, or, in a program built
330+ with raz -s, which keeps no places, the address *)
202331 proc where(a: UINT);
203-var p, best: ^Place; i, n: INT;
204332 begin
205- if places = nil then
206- (* built with raz -s, which keeps no places: the address *)
333+ if placer # nil then placer(a);
334+ else
207335 put("at ");
208336 putHex(a);
209337 put(": ");
210- return;
211338 end;
212- if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end;
213- n := places[0];
214- p := ^Place(places + 5);
215- best := nil;
216- for i := 0 to n - 1 do
217- if (UINT(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end;
218- end;
219- if best = nil then return; end;
220- putStr(names() + INT(best^.path));
221- put(":");
222- putInt(INT(best^.line));
223- put(":");
224- putInt(INT(best^.col));
225- put(": ");
226339 end;
227340
228341 (* The frame pointer of the procedure that calls it, which a code
@@ -247,7 +360,7 @@
247360 var p: ^Proc; i: INT;
248361 begin
249362 if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end;
250- p := ^Proc(^U8(places + 5) + 16 * places[0]);
363+ p := ^Proc(places + 5);
251364 for i := 0 to places[3] - 1 do
252365 if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end;
253366 end;
@@ -386,7 +499,7 @@
386499 putStr(names() + INT(p^.name));
387500 put(" at ");
388501 where(ip);
389- l := ^Local(^U8(places + 5) + 16 * places[0] + 28 * places[3]) + INT(p^.first);
502+ l := ^Local(^U8(places + 5) + 28 * places[3]) + INT(p^.first);
390503 if not vars or (p^.count = 0) then DEC(lineLen, 2); end; (* where's ": " *)
391504 for i := 0 to INT(p^.count) - 1 do
392505 if vars then
@@ -553,19 +666,16 @@
553666 begin
554667 p := procAt(a);
555668 if p = nil then return -1; end;
556- return p - ^Proc(^U8(places + 5) + 16 * places[0]);
669+ return p - ^Proc(places + 5);
557670 end;
558671
559-(* The index of the place the code at address a comes from, or -1 *)
672+(* The number of the line table's row the code at address a comes
673+ from, or -1 *)
560674 proc placeAt(a: UINT): INT;
561-var p: ^Place; i, best: INT;
675+var n: INT;
562676 begin
563- best := -1;
564- p := ^Place(places + 5);
565- for i := 0 to places[0] - 1 do
566- if (UINT(p[i].at) <= a) and ((best < 0) or (p[i].at > p[best].at)) then best := i; end;
567- end;
568- return best;
677+ _, n := row(a, -1);
678+ return n;
569679 end;
570680
571681 (* Puts the share of n that k is, as a percentage four wide *)
@@ -581,10 +691,9 @@
581691 (* Says the ten that took most of count's n samples, by name, with
582692 their totals beside them if total isn't empty *)
583693 proc top(count, total: []INT; n: INT; byPlace: BOOL);
584-var i, k, best: INT; p: ^Proc; q: ^Place;
694+var i, k, best: INT; p: ^Proc; r: Row;
585695 begin
586- p := ^Proc(^U8(places + 5) + 16 * places[0]);
587- q := ^Place(places + 5);
696+ p := ^Proc(places + 5);
588697 for k := 1 to 10 do
589698 best := 0;
590699 for i := 1 to LEN(count) - 1 do
@@ -595,9 +704,8 @@
595704 if LEN(total) > 0 then put(" "); putShare(total[best], n); end;
596705 put(" ");
597706 if byPlace then
598- putStr(names() + INT(q[best].path));
599- put(":"); putInt(INT(q[best].line));
600- put(":"); putInt(INT(q[best].col));
707+ r, _ := row(0, best);
708+ putRow(r);
601709 else putStr(names() + INT(p[best].name));
602710 end;
603711 say;
@@ -658,7 +766,7 @@
658766 top(procCount[0..places[3]], procTotal[0..places[3]], n, false);
659767 put("by place:");
660768 say;
661- top(placeCount[0..places[0]], placeCount[0..0], n, true);
769+ top(placeCount, placeCount[0..0], n, true);
662770 end;
663771
664772 begin

src/elf.pas modified +7 -72

@@ -115,7 +115,7 @@
115115 PutN(0, 1);
116116 end;
117117
118-{ LEB128, unsigned and signed }
118+{ LEB128, unsigned }
119119 procedure PutU(v: qword);
120120 begin
121121 while v >= $80 do begin
@@ -125,79 +125,14 @@
125125 PutN(v, 1);
126126 end;
127127
128-procedure PutS(v: int64);
129-begin
130- while (v < -64) or (v >= 64) do begin
131- PutN(v and $7F or $80, 1);
132- v := SarInt64(v, 7);
133- end;
134- PutN(v and $7F, 1);
135-end;
136-
137-{ The line table: one sequence for each live procedure with lines,
138- rows made only with the standard opcodes, no special ones. Then the
139- compile unit, which names the table, and its abbreviation. Section
128+{ The compile unit, which names the line table, and its abbreviation;
129+ the table itself is in the places, in rodata (link.pas). Section
140130 offsets go in off, sizes in size: line, info, abbrev. }
141131 procedure PutDebug(var off, size: array of int64);
142-var
143- files: array[0..255] of longint; nfiles, i, f, c, cf, cl: longint;
144- at: int64; cwd: array[0..255] of char;
132+var cwd: array[0..255] of char;
145133 begin
146- FillChar(files, SizeOf(files), 0);
147- nfiles := 0;
148- for i := 0 to numLines - 1 do
149- if chunks^[lines^[i].chunk].live then begin
150- f := 0;
151- while (f < nfiles) and (files[f] <> lines^[i].path) do inc(f);
152- if (f = nfiles) and (nfiles <= High(files)) then begin
153- files[f] := lines^[i].path;
154- inc(nfiles);
155- end;
156- end;
157-
158- off[0] := len;
159- PutN(0, 4); { unit_length, patched below }
160- PutN(4, 2); { version }
161- PutN(0, 4); { header_length, patched below }
162- PutN(1, 1); PutN(1, 1); { minimum_instruction_length, maximum_operations }
163- PutN(1, 1); { default_is_stmt }
164- PutN(-5 and $FF, 1); PutN(14, 1); PutN(13, 1); { line_base, line_range, opcode_base }
165- for i := 1 to 12 do PutN(ord(i in [2, 3, 4, 5, 9, 12]), 1); { their operands }
166- PutN(0, 1); { no include directories }
167- for f := 0 to nfiles - 1 do begin
168- PutStr(NameOf(files[f]));
169- PutN(0, 3); { directory, time, length }
170- end;
171- PutN(0, 1);
172- Patch(off[0] + 6, len - off[0] - 10, 4);
173- i := 0;
174- while i < numLines do begin
175- c := lines^[i].chunk;
176- if not chunks^[c].live then begin inc(i); continue end;
177- PutN(0, 1); PutU(9); PutN(2, 1); { DW_LNE_set_address }
178- PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8);
179- at := lines^[i].at; cf := 1; cl := 1;
180- while (i < numLines) and (lines^[i].chunk = c) do begin
181- if lines^[i].at > at then begin
182- PutN(2, 1); PutU(lines^[i].at - at); { DW_LNS_advance_pc }
183- at := lines^[i].at;
184- end;
185- f := 1;
186- while (f <= nfiles) and (files[f - 1] <> lines^[i].path) do inc(f);
187- if f <> cf then begin PutN(4, 1); PutU(f); cf := f end; { DW_LNS_set_file }
188- if lines^[i].line <> cl then begin { DW_LNS_advance_line }
189- PutN(3, 1); PutS(lines^[i].line - cl);
190- cl := lines^[i].line;
191- end;
192- PutN(1, 1); { DW_LNS_copy: a row }
193- inc(i);
194- end;
195- PutN(2, 1); PutU(chunks^[c].start + chunks^[c].size - at);
196- PutN(0, 1); PutU(1); PutN(1, 1); { DW_LNE_end_sequence }
197- end;
198- Patch(off[0], len - off[0] - 4, 4);
199- size[0] := len - off[0];
200-
134+ off[0] := chunks^[placesChunk].adr - roAdr + roOff + linesOff;
135+ size[0] := linesSize;
201136 off[1] := len;
202137 PutN(0, 4); PutN(4, 2); PutN(0, 4); PutN(8, 1); { length, version, abbrev, address size }
203138 PutU(1);
@@ -399,7 +334,7 @@
399334 shstrOff := len;
400335 s := ShNames;
401336 PutBytes(@s[1], length(s));
402- if stripped then begin
337+ if stripped or (placesChunk < 0) then begin
403338 for c := 0 to 2 do begin dbgOff[c] := len; dbgSize[c] := 0 end;
404339 end else PutDebug(dbgOff, dbgSize);
405340

src/link.pas modified +122 -47

@@ -36,6 +36,7 @@
3636 bssOff: int64; { the file offset of that table }
3737 textImg, roImg: PBytes;
3838 placesChunk: longint = -1; { the rodata chunk of the places, made here }
39+ linesOff, linesSize: int64; { the line table, in the places: .debug_line }
3940
4041 procedure MarkLive(entry: longint);
4142 procedure LinkAll(headerSize, roExtra, dynSize: int64);
@@ -87,24 +88,116 @@
8788 a := a + chunks^[c].size;
8889 end;
8990 Place := a;
91+end;
92+
93+{ The line table, DWARF 4's line program, which gdb reads as
94+ .debug_line and Rt to say where code comes from: one sequence for
95+ each live procedure with lines, a row for each place noted, its
96+ address, file, line and column. A row whose address and line move a
97+ little is one byte, a special opcode; the others are made of the
98+ standard ones. Made in lp, lpLen bytes long. }
99+const
100+ LineBase = -5; LineRange = 14; OpcodeBase = 13; { the special opcodes' }
101+
102+var
103+ lp: PBytes;
104+ lpLen, lpCap: longint;
105+
106+procedure PutB(v: int64);
107+begin
108+ if lpLen = lpCap then begin
109+ lpCap := lpCap * 2 + 256;
110+ lp := ReAllocMem(lp, lpCap);
111+ end;
112+ lp^[lpLen] := byte(v);
113+ inc(lpLen);
114+end;
115+
116+procedure PutN(v: int64; n: longint);
117+var i: longint;
118+begin
119+ for i := 1 to n do begin PutB(v); v := v shr 8 end;
120+end;
121+
122+{ LEB128, unsigned and signed }
123+procedure PutU(v: int64);
124+begin
125+ while v >= $80 do begin PutB(v and $7F or $80); v := v shr 7 end;
126+ PutB(v);
127+end;
128+
129+procedure PutS(v: int64);
130+begin
131+ while (v < -64) or (v >= 64) do begin PutB(v and $7F or $80); v := SarInt64(v, 7) end;
132+ PutB(v and $7F);
90133 end;
91134
135+{ The line program of the files paths, nfiles of them, numbered from
136+ 1, as they are written in the table }
137+procedure MakeLines(paths: PLongs; nfiles: longint);
138+var i, f, c, cf, cl, cc, op: longint; at, da, dl: int64; s: shortstring;
139+begin
140+ lpLen := 0;
141+ PutN(0, 4); { unit_length, patched below }
142+ PutN(4, 2); { version }
143+ PutN(0, 4); { header_length, patched below }
144+ PutB(1); PutB(1); { minimum_instruction_length, maximum_operations }
145+ PutB(1); { default_is_stmt }
146+ PutB(LineBase); PutB(LineRange); PutB(OpcodeBase);
147+ for i := 1 to OpcodeBase - 1 do PutB(ord(i in [2, 3, 4, 5, 9, 12])); { their operands }
148+ PutB(0); { no include directories }
149+ for f := 0 to nfiles - 1 do begin
150+ s := ShownPath(NameOf(paths^[f]));
151+ for i := 1 to length(s) do PutB(ord(s[i]));
152+ PutN(0, 4); { its end, directory, time, length }
153+ end;
154+ PutB(0);
155+ PLongint(@lp^[6])^ := lpLen - 10;
156+ i := 0;
157+ while i < numLines do begin
158+ c := lines^[i].chunk;
159+ if not chunks^[c].live then begin inc(i); continue end;
160+ PutB(0); PutU(9); PutB(2); { DW_LNE_set_address }
161+ PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8);
162+ at := lines^[i].at; cf := 1; cl := 1; cc := 0;
163+ while (i < numLines) and (lines^[i].chunk = c) do begin
164+ f := 1;
165+ while paths^[f - 1] <> lines^[i].path do inc(f);
166+ if f <> cf then begin PutB(4); PutU(f); cf := f end; { DW_LNS_set_file }
167+ if lines^[i].col <> cc then begin PutB(5); PutU(lines^[i].col); cc := lines^[i].col end; { DW_LNS_set_column }
168+ da := lines^[i].at - at;
169+ dl := lines^[i].line - cl;
170+ op := (dl - LineBase) + LineRange * da + OpcodeBase;
171+ if (dl >= LineBase) and (dl < LineBase + LineRange) and (op <= 255) then PutB(op) { a special opcode: a row }
172+ else begin
173+ if da > 0 then begin PutB(2); PutU(da) end; { DW_LNS_advance_pc }
174+ if dl <> 0 then begin PutB(3); PutS(dl) end; { DW_LNS_advance_line }
175+ PutB(1); { DW_LNS_copy: a row }
176+ end;
177+ at := lines^[i].at; cl := lines^[i].line;
178+ inc(i);
179+ end;
180+ PutB(2); PutU(chunks^[c].start + chunks^[c].size - at);
181+ PutB(0); PutU(1); PutB(1); { DW_LNE_end_sequence }
182+ end;
183+ PLongint(@lp^[0])^ := lpLen - 4;
184+end;
185+
92186 { The places that Rt reads to say where a failed check or a signal
93- was, and what calls were under way: their numbers, the places', where
94- text starts and stops, the procedures' and their variables' (I64s);
95- for each place in live code its address, line, column and the offset
96- of its file's path (U32s); for each live procedure where it starts and
97- stops, the offset of its name, its first variable and how many it
98- has, and the registers of variables it saves, as bits of gen's
99- VarRegs, and the frame offset they are saved at (U32s); for each
100- variable the offset of its name, its place in the frame and its
101- kind, as parse.pas's VarKind (U32s); then the paths and names, each
102- ending in a zero byte. They are in no order, since Rt
103- looks at them only as the program stops. }
187+ was, and what calls were under way: where the line table is in them,
188+ where text starts and stops, the numbers of procedures and of their
189+ variables (I64s); for each live procedure where it starts and stops,
190+ the offset of its name, its first variable and how many it has, and
191+ the registers of variables it saves, as bits of gen's VarRegs, and
192+ the frame offset they are saved at (U32s); for each variable the
193+ offset of its name, its place in the frame and its kind, as
194+ parse.pas's VarKind (U32s); then the names, each ending in a zero
195+ byte; then the line table (MakeLines), at linesOff. They are in no
196+ order, since Rt looks at them only as the program stops. }
104197 procedure MakePlaces(start, stop: int64);
105198 var
106- files, offs, first, count: PLongs; buf: PBytes;
107- i, f, n, nfiles, k, np, nv, c, v, at, names: longint;
199+ files, first, count: PLongs; buf: PBytes;
200+ i, f, nfiles, k, np, nv, c, v, at, names: longint;
108201
109202 procedure Name(const s: shortstring);
110203 begin
@@ -117,7 +210,6 @@
117210
118211 begin
119212 files := GetMem(numLines * 4 + 4);
120- offs := GetMem(numLines * 4 + 4);
121213 first := GetMem(numChunks * 4);
122214 count := GetMem(numChunks * 4);
123215 FillChar(count^, numChunks * 4, 0);
@@ -125,19 +217,17 @@
125217 first^[varNotes[v].chunk] := v;
126218 inc(count^[varNotes[v].chunk]);
127219 end;
128- n := 0; nfiles := 0; k := 0; np := 0; nv := 0;
220+ nfiles := 0; k := 0; np := 0; nv := 0;
129221 for i := 0 to numLines - 1 do
130222 if chunks^[lines^[i].chunk].live then begin
131- inc(n);
132223 f := 0;
133224 while (f < nfiles) and (files^[f] <> lines^[i].path) do inc(f);
134225 if f = nfiles then begin
135226 files^[f] := lines^[i].path;
136- offs^[f] := k;
137- k := k + length(ShownPath(NameOf(files^[f]))) + 1;
138227 inc(nfiles);
139228 end;
140229 end;
230+ MakeLines(files, nfiles);
141231 for c := 0 to numChunks - 1 do
142232 if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin
143233 inc(np);
@@ -145,34 +235,11 @@
145235 for v := first^[c] to first^[c] + count^[c] - 1 do k := k + length(NameOf(varNotes[v].name)) + 1;
146236 nv := nv + count^[c];
147237 end;
148- buf := GetMem(40 + 16 * n + 28 * np + 12 * nv + k);
149- PInt64(@buf^[0])^ := n;
150- PInt64(@buf^[8])^ := start;
151- PInt64(@buf^[16])^ := stop;
152- PInt64(@buf^[24])^ := np;
153- PInt64(@buf^[32])^ := nv;
154- at := 40;
155- for i := 0 to numLines - 1 do
156- if chunks^[lines^[i].chunk].live then begin
157- f := 0;
158- while files^[f] <> lines^[i].path do inc(f);
159- PLongint(@buf^[at])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start;
160- PLongint(@buf^[at + 4])^ := lines^[i].line;
161- PLongint(@buf^[at + 8])^ := lines^[i].col;
162- PLongint(@buf^[at + 12])^ := offs^[f];
163- at := at + 16;
164- end;
165- { the pool of paths and names starts at k }
166- k := 40 + 16 * n + 28 * np + 12 * nv;
238+ buf := GetMem(40 + 28 * np + 12 * nv + k + lpLen);
239+ { the pool of names starts at k, the line table after it }
240+ k := 40 + 28 * np + 12 * nv;
167241 names := k;
168- { the paths first, at the offsets offs gave the places: Name puts
169- each in the pool, and also writes its offset at at, which a path
170- doesn't want, so at is put back for the procedures to overwrite }
171- for f := 0 to nfiles - 1 do begin
172- i := at;
173- Name(ShownPath(NameOf(files^[f])));
174- at := i;
175- end;
242+ at := 40;
176243 v := 0;
177244 for c := 0 to numChunks - 1 do
178245 if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin
@@ -195,8 +262,16 @@
195262 PLongint(@buf^[at + 4])^ := varNotes[v].kind;
196263 at := at + 8;
197264 end;
198- SetData(placesChunk, buf, names, 8);
199- FreeMem(buf); FreeMem(files); FreeMem(offs); FreeMem(first); FreeMem(count);
265+ linesOff := names;
266+ linesSize := lpLen;
267+ Move(lp^, buf^[names], lpLen);
268+ PInt64(@buf^[0])^ := linesOff;
269+ PInt64(@buf^[8])^ := start;
270+ PInt64(@buf^[16])^ := stop;
271+ PInt64(@buf^[24])^ := np;
272+ PInt64(@buf^[32])^ := nv;
273+ SetData(placesChunk, buf, names + lpLen, 8);
274+ FreeMem(buf); FreeMem(files); FreeMem(first); FreeMem(count);
200275 end;
201276
202277 { Places the live chunks, leaving roExtra bytes at the end of rodata

src/raz.pas modified +5 -4

@@ -115,7 +115,7 @@
115115 m, rt: PModule;
116116 main: PObj;
117117 run, noProfile: boolean;
118- links: array[0..2] of TLink;
118+ links: array[0..3] of TLink;
119119 numLinks: longint;
120120
121121
@@ -204,15 +204,16 @@
204204
205205 if rt = nil then Fatal('can''t find module Rt');
206206 { what Rt is given: the places, which the linker makes, for Rt to say
207- where a failed check or a signal was, and the calls under way then
208- (calls); and the profiler, which needs them too. raz -s gives none,
209- -noprofile no profiler. }
207+ where a failed check or a signal was (place), and the calls under
208+ way then (calls); and the profiler, which needs them too. raz -s
209+ gives none, -noprofile no profiler. }
210210 numLinks := 0;
211211 if not stripped then begin
212212 placesChunk := NewChunk(secRodata, -1, -1);
213213 SetData(placesChunk, nil, 0, 8);
214214 AddLink(placesChunk, RtObj(rt, 'places'));
215215 AddLink(RtObj(rt, 'calls')^.chunk, RtObj(rt, 'teller'));
216+ AddLink(RtObj(rt, 'place')^.chunk, RtObj(rt, 'placer'));
216217 if not noProfile then AddLink(RtObj(rt, 'startProfile')^.chunk, RtObj(rt, 'profiler'));
217218 end;
218219 entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk,