OXYGENHUB
# 9120e87480a6 enhanced-x16-emulator / tests / test_dbg_info.c
76.2 KiB · C · last changed chore(): initial commit 26 days ago source blame history raw
1
// Parser checks for the cc65 debug-info reader.
2
//
3
// Standalone: dbg_info.c has no dependency on the emulator core, so this links
4
// it on its own. No SDL, ROM or emulator required. Returns 0 when every check
5
// passes.
6
//
7
// Build: configure with -DBUILD_DBG_INFO_TEST=ON, then build target
8
// dbg_info_test.
9
#include "dbg_info.h"
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
15
// ── Tiny check harness ──────────────────────────────────────────────────────
16
static int g_fails = 0;
17
18
static void
19
check(int cond, const char *msg)
20
{
21
	printf("%s: %s\n", cond ? "ok  " : "FAIL", msg);
22
	if (!cond) {
23
		g_fails++;
24
	}
25
}
26
27
// A minimal but realistic cc65 .dbg file. Two source files so file/line lookup
28
// has something to disambiguate, two spans so mid-span resolution is
29
// meaningful, and two labels so symbol lookup and ordering are exercised.
30
static const char *k_dbg =
31
	"version\tmajor=2,minor=0\n"
32
	"info\tcsym=0,file=2,lib=0,line=3,mod=1,scope=1,seg=1,span=2,sym=2,type=0\n"
33
	"file\tid=0,name=\"main.s\",size=200,mtime=0x00000000,mod=0\n"
34
	"file\tid=1,name=\"util.s\",size=100,mtime=0x00000000,mod=0\n"
35
	"seg\tid=0,name=\"CODE\",start=0x000801,size=0x0040,addrsize=absolute,type=ro\n"
36
	"span\tid=0,seg=0,start=0,size=16,type=0\n"
37
	"span\tid=1,seg=0,start=16,size=16,type=0\n"
38
	"line\tid=0,file=0,line=10,span=0\n"
39
	"line\tid=1,file=0,line=11,span=1\n"
40
	"line\tid=2,file=1,line=42,span=1\n"
41
	"sym\tid=0,name=\"_main\",addrsize=absolute,size=3,scope=0,def=0,val=0x000801,type=lab\n"
42
	"sym\tid=1,name=\"_helper\",addrsize=absolute,size=3,scope=0,def=0,val=0x000811,type=lab\n"
43
	"mod\tid=0,name=\"main.o\",file=0\n";
44
45
// Two loads of the same module where an equate's value changed. Nothing prunes
46
// equates, and the lookup returns the first match, so appending rather than
47
// replacing lets the old value win for the rest of the session.
48
static const char *k_dbg_equ_v1 =
49
	"version\tmajor=2,minor=0\n"
50
	"file\tid=0,name=\"equ.s\",size=10,mtime=0x00000000,mod=0\n"
51
	"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro,oname=\"e.prg\"\n"
52
	"span\tid=0,seg=0,start=0,size=16,type=0\n"
53
	"line\tid=0,file=0,line=1,span=0\n"
54
	"sym\tid=0,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
55
	"sym\tid=1,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
56
	"mod\tid=0,name=\"equ.o\",file=0\n";
57
58
static const char *k_dbg_equ_v2 =
59
	"version\tmajor=2,minor=0\n"
60
	"file\tid=0,name=\"equ.s\",size=10,mtime=0x00000000,mod=0\n"
61
	"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro,oname=\"e.prg\"\n"
62
	"span\tid=0,seg=0,start=0,size=16,type=0\n"
63
	"line\tid=0,file=0,line=1,span=0\n"
64
	"sym\tid=0,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x00BEEF,type=equ\n"
65
	"sym\tid=1,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
66
	"mod\tid=0,name=\"equ.o\",file=0\n";
67
// Returns a path valid until this is called WRITE_TEMP_SLOTS more times.
68
// It used to hand back one static buffer, so a caller that wrote a second
69
// fixture before removing the first removed the wrong file -- and a caller that
70
// wrote three before loading any would have loaded the last one three times.
71
// Rotating the buffers makes holding a few paths at once safe, which is what
72
// every multi-module fixture here needs.
73
#define WRITE_TEMP_SLOTS 8
74
static char *
75
write_temp(const char *contents, const char *leaf)
76
{
77
	static char slots[WRITE_TEMP_SLOTS][512];
78
	static int  next_slot = 0;
79
	char       *path      = slots[next_slot];
80
	next_slot = (next_slot + 1) % WRITE_TEMP_SLOTS;
81
	const char *dir = getenv("TMPDIR");
82
	if (!dir || !*dir) {
83
		dir = getenv("TEMP");
84
	}
85
	if (!dir || !*dir) {
86
		dir = ".";
87
	}
88
	snprintf(path, 512, "%s/%s", dir, leaf);
89
90
	FILE *f = fopen(path, "wb");
91
	if (!f) {
92
		printf("FAIL: cannot write %s\n", path);
93
		g_fails++;
94
		return NULL;
95
	}
96
	fwrite(contents, 1, strlen(contents), f);
97
	fclose(f);
98
	return path;
99
}
100
101
static char *
102
write_temp_dbg(void)
103
{
104
	return write_temp(k_dbg, "x16_dbg_info_test.dbg");
105
}
106
107
// Two segments sharing the banked window at $A000, one per RAM bank, each
108
// attributed to its own source file. Different sizes are what lets a LOAD tell
109
// them apart.
110
static const char *k_dbg_banked =
111
	"version\tmajor=2,minor=0\n"
112
	"file\tid=0,name=\"a_bank5.s\",size=100,mtime=0x00000000,mod=0\n"
113
	"file\tid=1,name=\"b_bank6.s\",size=100,mtime=0x00000000,mod=0\n"
114
	"seg\tid=0,name=\"BANKA\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
115
	"seg\tid=1,name=\"BANKB\",start=0x00a000,size=0x0020,addrsize=absolute,type=ro\n"
116
	"span\tid=0,seg=0,start=0,size=16,type=0\n"
117
	"span\tid=1,seg=1,start=0,size=32,type=0\n"
118
	"line\tid=0,file=0,line=7,span=0\n"
119
	"line\tid=1,file=1,line=99,span=1\n"
120
	"mod\tid=0,name=\"banked.o\",file=0\n";
121
122
static char *
123
write_temp_banked_dbg(void)
124
{
125
	return write_temp(k_dbg_banked, "x16_dbg_info_banked.dbg");
126
}
127
128
int
129
main(void)
130
{
131
	// Nothing loaded yet: every lookup should decline rather than invent an
132
	// answer, and none of them should trip over the empty tables.
133
	check(!dbg_info_is_loaded(), "reports not loaded before any load");
134
	{
135
		const char *file = NULL;
136
		int         line = 0;
137
		check(!dbg_info_addr_to_source(0x0801, &file, &line),
138
		      "address lookup declines when nothing is loaded");
139
		dbg_addr_t addr = 0;
140
		check(!dbg_info_label_to_addr("_main", &addr),
141
		      "label lookup declines when nothing is loaded");
142
	}
143
144
	check(dbg_info_load("this/does/not/exist.dbg") != 0,
145
	      "load of a missing file reports failure");
146
147
	char *path = write_temp_dbg();
148
	if (!path) {
149
		return 1;
150
	}
151
	check(dbg_info_load(path) == 0, "loads a well-formed .dbg");
152
	check(dbg_info_is_loaded(), "reports loaded afterwards");
153
154
	// Address -> source. The first span covers $0801..$0810, attributed to
155
	// main.s:10.
156
	{
157
		const char *file = NULL;
158
		int         line = 0;
159
		check(dbg_info_addr_to_source(0x0801, &file, &line), "maps a span-start address");
160
		check(file && strstr(file, "main.s") != NULL, "  ...to the right file");
161
		check(line == 10, "  ...to the right line");
162
	}
163
164
	// Landing mid-instruction must still resolve; this is what keeps the
165
	// current-line highlight steady while stepping.
166
	{
167
		const char *file = NULL;
168
		int         line = 0;
169
		check(dbg_info_addr_to_source_nearest(0x0805, &file, &line),
170
		      "maps an address inside a span to the enclosing line");
171
		check(line == 10, "  ...to the line that opened the span");
172
	}
173
174
	// An address outside every segment has no answer.
175
	{
176
		const char *file = NULL;
177
		int         line = 0;
178
		check(!dbg_info_addr_to_source(0xC000, &file, &line),
179
		      "declines an address outside every segment");
180
	}
181
182
	// Source -> address, the direction that breakpoints-by-line need.
183
	{
184
		dbg_addr_t addr = 0;
185
		check(dbg_info_source_to_addr("main.s", 10, &addr), "maps file/line back to an address");
186
		check(addr == 0x0801, "  ...to the span start");
187
	}
188
189
	// Symbols.
190
	{
191
		dbg_addr_t addr = 0;
192
		check(dbg_info_label_to_addr("_main", &addr) && addr == 0x0801, "finds a label by name");
193
		check(dbg_info_label_to_addr("_MAIN", &addr) && addr == 0x0801,
194
		      "label lookup is case-insensitive");
195
		check(!dbg_info_label_to_addr("_nosuch", &addr), "declines an unknown label");
196
197
		const char *name = NULL;
198
		check(dbg_info_addr_to_label(0x0811, &name) && name && strcmp(name, "_helper") == 0,
199
		      "finds a label by address");
200
		check(dbg_info_symbol_count() == 2, "enumerates both labels");
201
202
		// Enumeration is sorted by address, which the symbol list relies on.
203
		dbg_addr_t prev   = 0;
204
		int      sorted = 1;
205
		for (int i = 0; i < dbg_info_symbol_count(); i++) {
206
			const char *n = NULL;
207
			dbg_addr_t  a = 0;
208
			if (!dbg_info_symbol_at(i, &n, &a) || (i && a < prev)) {
209
				sorted = 0;
210
			}
211
			prev = a;
212
		}
213
		check(sorted, "enumeration is sorted by address");
214
215
		const char *n = NULL;
216
		dbg_addr_t  a = 0;
217
		check(!dbg_info_symbol_at(dbg_info_symbol_count(), &n, &a),
218
		      "declines an out-of-range index");
219
		check(!dbg_info_symbol_at(-1, &n, &a), "declines a negative index");
220
	}
221
222
	// Source files referenced by the .dbg, so a UI can offer them before the
223
	// PC has ever been there.
224
	check(dbg_info_file_count() == 2, "enumerates both source files");
225
226
	// Span starts are what the anchored disassembler falls back to.
227
	check(dbg_info_is_span_start(0x0801), "recognises a span start");
228
	check(!dbg_info_is_span_start(0x0805), "does not claim a mid-span address");
229
230
	// Unloading a range drops that range's mappings.
231
	dbg_info_unload_range(0x0801, 0x0840);
232
	{
233
		const char *file = NULL;
234
		int         line = 0;
235
		check(!dbg_info_addr_to_source(0x0801, &file, &line),
236
		      "unload_range drops mappings in the range");
237
	}
238
239
	dbg_info_free();
240
	check(!dbg_info_is_loaded(), "reports not loaded after free");
241
	// Freeing twice must be harmless; shutdown paths are not always ordered.
242
	dbg_info_free();
243
	check(1, "double free is harmless");
244
245
	// A load that fails must not disturb what is already loaded. The obvious
246
	// ordering bug here is clearing the address map before opening the file.
247
	check(dbg_info_load(path) == 0, "reloads after free");
248
	check(dbg_info_load("this/does/not/exist.dbg") != 0, "second load of a missing file fails");
249
	{
250
		const char *file = NULL;
251
		int         line = 0;
252
		check(dbg_info_addr_to_source(0x0801, &file, &line),
253
		      "a failed load leaves the existing mapping intact");
254
	}
255
256
	// Reloading a module, which is what an overlay swap does, must replace it
257
	// rather than accumulate a second copy of its segments -- duplicates make
258
	// bank disambiguation ambiguous and it gives up.
259
	{
260
		const int files_before = dbg_info_file_count();
261
		dbg_info_unload_range(0x0801, 0x0840);
262
		check(dbg_info_load(path) == 0, "reloads the same module");
263
		const char *file = NULL;
264
		int         line = 0;
265
		check(dbg_info_addr_to_source(0x0801, &file, &line),
266
		      "the reloaded module still resolves");
267
		check(line == 10, "  ...to the original line");
268
269
		// The point of the block, which it used to measure and then discard.
270
		// Unloading a range deliberately leaves file records alone, so nothing
271
		// stops a reload appending a second copy of every one of them; the same
272
		// is true of equates, where the lookup returns the FIRST match and a
273
		// stale value would therefore win for the rest of the session.
274
		check(dbg_info_file_count() == files_before,
275
		      "  ...without accumulating a second copy of its files");
276
	}
277
	// Repeated swaps must stay stable rather than degrading each time.
278
	const int files_before_swaps = dbg_info_file_count();
279
	for (int i = 0; i < 5; i++) {
280
		dbg_info_unload_range(0x0801, 0x0840);
281
		dbg_info_load(path);
282
	}
283
	{
284
		const char *file = NULL;
285
		int         line = 0;
286
		check(dbg_info_addr_to_source(0x0801, &file, &line) && line == 10,
287
		      "still resolves after repeated module swaps");
288
		check(dbg_info_file_count() == files_before_swaps,
289
		      "and does not grow a little on every swap");
290
	}
291
292
	// A path with no room left for the ".dbg" extension must be declined, not
293
	// written past the end of the caller's buffer.
294
	{
295
		char  huge[4096];
296
		memset(huge, 'a', sizeof(huge) - 1);
297
		huge[sizeof(huge) - 1] = '\0';
298
		dbg_addr_t lo = 0, hi = 0;
299
		check(!dbg_info_peek_file_range(huge, &lo, &hi),
300
		      "declines an over-long path instead of overflowing");
301
	}
302
303
	dbg_info_free();
304
	remove(path);
305
306
	// ── Bank-aware mapping across a module swap ─────────────────────────────
307
	// Two segments share the banked window at $A000, one per RAM bank, which is
308
	// the normal shape of a banked X16 program. The .dbg format records no
309
	// bank, so they are told apart by the size of the blob each LOAD placed
310
	// there. A swap must replace a module rather than accumulate a second copy
311
	// of its segments: with duplicates present nothing can be told apart any
312
	// more, and every banked address resolves to whichever module matched first.
313
	{
314
		char *bpath = write_temp_banked_dbg();
315
		if (!bpath) {
316
			return 1;
317
		}
318
		check(dbg_info_load(bpath) == 0, "loads a banked .dbg");
319
320
		// Before any LOAD is observed, neither segment's bank is known. The
321
		// lookup must still answer -- a mapping is never lost -- but it has to
322
		// say the answer is a guess, because either segment could be the one
323
		// actually mapped in at $A000.
324
		const char *fu = NULL;
325
		int         lu = 0;
326
		check(dbg_info_addr_to_source_banked_ex(0xA000, 5, &fu, &lu) == DBG_BANK_UNKNOWN,
327
		      "flags a match against an unknown-bank segment as a guess");
328
		check(dbg_info_addr_to_source_banked(0xA000, 5, &fu, &lu),
329
		      "still reports the mapping for an unknown-bank segment");
330
331
		// Learning one segment's bank must not make the other one confident:
332
		// $A018 lies only inside the larger segment, whose bank is still unknown.
333
		dbg_info_note_bank_load(0xA000, 0x10, 5);
334
		check(dbg_info_addr_to_source_banked_ex(0xA000, 5, &fu, &lu) == DBG_BANK_RESOLVED,
335
		      "reports the newly learned segment as resolved");
336
		check(dbg_info_addr_to_source_banked_ex(0xA018, 5, &fu, &lu) == DBG_BANK_UNKNOWN,
337
		      "keeps a still-unknown segment flagged as a guess");
338
339
		dbg_info_note_bank_load(0xA000, 0x20, 6);
340
341
		const char *f5 = NULL, *f6 = NULL;
342
		int         l5 = 0, l6 = 0;
343
		bool ok5 = dbg_info_addr_to_source_banked(0xA000, 5, &f5, &l5);
344
		bool ok6 = dbg_info_addr_to_source_banked(0xA000, 6, &f6, &l6);
345
		check(ok5 && ok6, "resolves the banked address in both banks");
346
		check(ok5 && ok6 && f5 && f6 && strcmp(f5, f6) != 0,
347
		      "tells the two banks apart");
348
349
		// Now swap the module out and back, as an overlay reload would.
350
		dbg_info_unload_range(0xA000, 0xBFFF);
351
		check(dbg_info_load(bpath) == 0, "reloads the banked module");
352
		dbg_info_note_bank_load(0xA000, 0x10, 5);
353
		dbg_info_note_bank_load(0xA000, 0x20, 6);
354
355
		f5 = f6 = NULL;
356
		ok5 = dbg_info_addr_to_source_banked(0xA000, 5, &f5, &l5);
357
		ok6 = dbg_info_addr_to_source_banked(0xA000, 6, &f6, &l6);
358
		check(ok5 && ok6 && f5 && f6 && strcmp(f5, f6) != 0,
359
		      "still tells the banks apart after a module swap");
360
361
		// A bank we actually learned is reported as resolved.
362
		check(dbg_info_addr_to_source_banked_ex(0xA000, 5, &f5, &l5) == DBG_BANK_RESOLVED,
363
		      "reports a known bank as resolved");
364
365
		// A bank nothing was ever observed for must NOT be passed off as
366
		// resolved: the answer comes from another bank's segment.
367
		check(dbg_info_addr_to_source_banked_ex(0xA000, 9, &f5, &l5) == DBG_BANK_UNKNOWN,
368
		      "reports an unlearned bank as unknown rather than guessing");
369
370
		// And an address with no debug info at all is still a plain miss.
371
		check(dbg_info_addr_to_source_banked_ex(0x5000, 5, &f5, &l5) == DBG_BANK_NO_MATCH,
372
		      "reports an unmapped address as no match");
373
374
		dbg_info_free();
375
		remove(bpath);
376
	}
377
378
	// ── 24-bit addresses ────────────────────────────────────────────────────
379
	// Code on a GS/Gen2 machine lives above $FFFF. A 16-bit address type simply
380
	// could not describe it, so those mappings used to be dropped at load.
381
	{
382
		static const char *k_dbg_far =
383
			"version\tmajor=2,minor=0\n"
384
			"file\tid=0,name=\"far.s\",size=10,mtime=0x00000000,mod=0\n"
385
			"seg\tid=0,name=\"FAR\",start=0x018000,size=0x0010,addrsize=far,type=ro\n"
386
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
387
			"line\tid=0,file=0,line=5,span=0\n"
388
			"sym\tid=0,name=\"_far\",addrsize=far,size=3,scope=0,def=0,val=0x018000,type=lab\n";
389
		char *fpath = write_temp(k_dbg_far, "x16_dbg_info_far.dbg");
390
		if (!fpath) {
391
			return 1;
392
		}
393
		check(dbg_info_load(fpath) == 0, "loads a .dbg with addresses above $FFFF");
394
		const char *file = NULL;
395
		int         line = 0;
396
		check(dbg_info_addr_to_source(0x018000, &file, &line) && line == 5,
397
		      "maps a bank-1 address to source");
398
		dbg_addr_t addr = 0;
399
		check(dbg_info_label_to_addr("_far", &addr) && addr == 0x018000,
400
		      "keeps the full 24-bit value of a far label");
401
		check(dbg_info_is_span_start(0x018000), "recognises a far span start");
402
		dbg_info_free();
403
		remove(fpath);
404
	}
405
406
	// ── Long records ────────────────────────────────────────────────────────
407
	// cc65 lists every reference to a symbol on its `sym` line, so for a
408
	// heavily used label that line runs to many kilobytes -- and val/type come
409
	// after the list. A fixed read buffer would truncate it and silently lose
410
	// the symbol.
411
	{
412
		char *big = malloc(70000);
413
		if (!big) {
414
			printf("FAIL: out of memory building the long-record case\n");
415
			return 1;
416
		}
417
		int n = sprintf(big,
418
		                "version\tmajor=2,minor=0\n"
419
		                "file\tid=0,name=\"long.s\",size=10,mtime=0x00000000,mod=0\n"
420
		                "seg\tid=0,name=\"CODE\",start=0x000801,size=0x0010,addrsize=absolute,type=ro\n"
421
		                "span\tid=0,seg=0,start=0,size=16,type=0\n"
422
		                "line\tid=0,file=0,line=1,span=0\n"
423
		                "sym\tid=0,name=\"_busy\",addrsize=absolute,ref=");
424
		for (int i = 0; i < 8000; i++) {
425
			n += sprintf(big + n, "%d+", i); // a long reference list
426
		}
427
		n += sprintf(big + n, "99,size=3,scope=0,def=0,val=0x000801,type=lab\n");
428
429
		char *lpath = write_temp(big, "x16_dbg_info_long.dbg");
430
		free(big);
431
		if (!lpath) {
432
			return 1;
433
		}
434
		check(dbg_info_load(lpath) == 0, "loads a .dbg with a very long record");
435
		dbg_addr_t addr = 0;
436
		check(dbg_info_label_to_addr("_busy", &addr) && addr == 0x0801,
437
		      "reads fields that follow a multi-kilobyte reference list");
438
		dbg_info_free();
439
		remove(lpath);
440
	}
441
442
	// ── A confirmed bank outranks a narrower guess ──────────────────────────
443
	// Two segments share $A000, one per bank. Only the OUTER one has been
444
	// loaded, so only its bank is known. Preferring the innermost span
445
	// regardless answers with the inner segment's file and line and downgrades
446
	// a confirmed match to a guess -- the wrong source, reported confidently
447
	// enough to send someone debugging code that is fine.
448
	//
449
	// The block above learns the inner segment's bank first, which is the case
450
	// that already worked, so this needs its own load to reach the other order.
451
	{
452
		dbg_info_free();
453
		char *bpath = write_temp_banked_dbg();   // static buffer; do not free
454
		if (!bpath) {
455
			check(false, "could not write the banked fixture");
456
		} else {
457
			check(dbg_info_load(bpath) == 0, "reloads the banked fixture");
458
459
			dbg_info_note_bank_load(0xA000, 0x20, 6);   // the OUTER segment only
460
461
			const char *fb = NULL;
462
			int         lb = 0;
463
			check(dbg_info_addr_to_source_banked_ex(0xA000, 6, &fb, &lb)
464
			          == DBG_BANK_RESOLVED,
465
			      "a confirmed bank is not demoted by a narrower unknown one");
466
			check(fb && strstr(fb, "b_bank6") != NULL,
467
			      "  ...and answers with the segment that matched");
468
			check(lb == 99, "  ...and with its line");
469
470
			dbg_info_free();
471
			remove(bpath);
472
		}
473
	}
474
	// ── A reloaded equate replaces the old value ────────────────────────────
475
	// Nothing prunes equates, and the lookup returns the FIRST match, so
476
	// appending on reload would let a value from a module swapped out long ago
477
	// win for the rest of the session -- and the bank equates seed which RAM
478
	// bank a segment lives in, so a stale one mislabels code.
479
	{
480
		dbg_info_free();
481
		char *p1 = write_temp(k_dbg_equ_v1, "x16_dbg_info_equ.dbg");
482
		if (!p1) {
483
			check(false, "could not write the equate fixture");
484
		} else {
485
			check(dbg_info_load(p1) == 0, "loads a module with equates");
486
			dbg_addr_t v = 0;
487
			check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
488
			      "reads an equate's value");
489
490
			// The same module rebuilt, with the constant moved.
491
			char *p2 = write_temp(k_dbg_equ_v2, "x16_dbg_info_equ.dbg");
492
			if (p2) {
493
				dbg_info_unload_range(0xA000, 0xA00F);
494
				check(dbg_info_load(p2) == 0, "reloads it after a rebuild");
495
				v = 0;
496
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0xBEEF,
497
				      "and the new value replaces the old one");
498
499
				// Bank equates have no accessor of their own; what they do is
500
				// seed which RAM bank a segment is taken to live in, so a stale
501
				// one mislabels code rather than merely reporting a wrong
502
				// number. The segment is in the banked window and named to match
503
				// the equate, so the reload's bank 7 must be the one in force.
504
				const char *bf = NULL;
505
				int         bl = 0;
506
				check(dbg_info_addr_to_source_banked_ex(0xA000, 7, &bf, &bl)
507
				          == DBG_BANK_RESOLVED,
508
				      "and a reloaded bank equate reseeds the segment's bank");
509
			}
510
			remove(p1);
511
			dbg_info_free();
512
		}
513
	}
514
	// Deduplication is only correct within one module. Two linked modules may
515
	// each define the same equate name with different values, and may each
516
	// compile a file of the same name. Pooling those would not merely report a
517
	// stale number -- it destroys one module's definition outright, since
518
	// nothing puts it back when the other module unloads.
519
	{
520
		dbg_info_free();
521
		char *pa = write_temp(k_dbg_equ_v1, "x16_dbg_info_modA.dbg");
522
		if (!pa) {
523
			check(false, "could not write module A");
524
		} else {
525
			check(dbg_info_load(pa) == 0, "loads module A");
526
			// A different module, same equate name, different value.
527
			char *pb = write_temp(k_dbg_equ_v2, "x16_dbg_info_modB.dbg");
528
			if (pb) {
529
				check(dbg_info_load(pb) == 0, "loads module B alongside it");
530
				dbg_addr_t v = 0;
531
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
532
				      "and B's SCORE does not overwrite A's");
533
				remove(pb);
534
			}
535
			remove(pa);
536
			dbg_info_free();
537
		}
538
	}
539
540
	// A `line` that precedes the `file` it names. cc65 emits them the other way
541
	// round today, but nothing in the format promises that, and resolving the
542
	// reference before the record exists silently loses the source mapping.
543
	{
544
		dbg_info_free();
545
		static const char *k_dbg_line_first =
546
			"version\tmajor=2,minor=0\n"
547
			"line\tid=0,file=0,line=7,span=0\n"
548
			"file\tid=0,name=\"late.s\",size=10,mtime=0x00000000,mod=0\n"
549
			"seg\tid=0,name=\"CODE\",start=0x000801,size=0x0010,addrsize=absolute,type=ro\n"
550
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
551
			"mod\tid=0,name=\"late.o\",file=0\n";
552
		char *lp = write_temp(k_dbg_line_first, "x16_dbg_info_linefirst.dbg");
553
		if (!lp) {
554
			check(false, "could not write the record-order fixture");
555
		} else {
556
			check(dbg_info_load(lp) == 0, "loads a .dbg with line before file");
557
			const char *f = NULL;
558
			int         n = 0;
559
			check(dbg_info_addr_to_source(0x0801, &f, &n) && n == 7,
560
			      "and the line still resolves to its file");
561
562
			// The reload is what actually exercises the ordering: only then has
563
			// the ID base moved on, so the reused file record's ID and the one
564
			// a `line` would assume for itself are different numbers.
565
			dbg_info_unload_range(0x0801, 0x0810);
566
			check(dbg_info_load(lp) == 0, "reloads it");
567
			f = NULL;
568
			n = 0;
569
			check(dbg_info_addr_to_source(0x0801, &f, &n) && n == 7,
570
			      "and it still resolves after the IDs have moved on");
571
			remove(lp);
572
			dbg_info_free();
573
		}
574
	}
575
576
	// A segment whose name is a prefix of another's. Matching an equate to a
577
	// segment by prefix in either direction means RAM_BANK_CODE matches CODE2
578
	// as readily as CODE, so whichever equate happened to be parsed first used
579
	// to claim both -- and the exact match for the second was then discarded,
580
	// putting its code confidently in the wrong bank.
581
	{
582
		dbg_info_free();
583
		static const char *k_dbg_two_banks =
584
			"version\tmajor=2,minor=0\n"
585
			"file\tid=0,name=\"two.s\",size=10,mtime=0x00000000,mod=0\n"
586
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
587
			"seg\tid=1,name=\"CODE2\",start=0x00a020,size=0x0010,addrsize=absolute,type=ro\n"
588
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
589
			"span\tid=1,seg=1,start=0,size=16,type=0\n"
590
			"line\tid=0,file=0,line=1,span=0\n"
591
			"line\tid=1,file=0,line=2,span=1\n"
592
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
593
			"sym\tid=1,name=\"RAM_BANK_CODE2\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
594
			"mod\tid=0,name=\"two.o\",file=0\n";
595
		char *tp = write_temp(k_dbg_two_banks, "x16_dbg_info_twobanks.dbg");
596
		if (!tp) {
597
			check(false, "could not write the prefix-collision fixture");
598
		} else {
599
			check(dbg_info_load(tp) == 0, "loads two segments with related names");
600
			const char *f = NULL;
601
			int         l = 0;
602
			check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
603
			          == DBG_BANK_RESOLVED,
604
			      "CODE takes the bank named for it");
605
			f = NULL;
606
			l = 0;
607
			check(dbg_info_addr_to_source_banked_ex(0xA020, 7, &f, &l)
608
			          == DBG_BANK_RESOLVED,
609
			      "and CODE2 is not claimed by CODE's equate");
610
			remove(tp);
611
			dbg_info_free();
612
		}
613
	}
614
615
	// Two equates that both prefix-match one segment, with no exact match and
616
	// different banks. There is no basis for choosing between them, so the
617
	// segment must be left unknown: unknown is recoverable -- a runtime bank
618
	// observation can still resolve it -- whereas a confident wrong answer is
619
	// not, and it silently attributes code to the wrong bank.
620
	{
621
		dbg_info_free();
622
		static const char *k_dbg_ambig =
623
			"version\tmajor=2,minor=0\n"
624
			"file\tid=0,name=\"amb.s\",size=10,mtime=0x00000000,mod=0\n"
625
			"seg\tid=0,name=\"STORETILE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
626
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
627
			"line\tid=0,file=0,line=1,span=0\n"
628
			"sym\tid=0,name=\"RAM_BANK_STORE_TILEMAP\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
629
			"sym\tid=1,name=\"RAM_BANK_STORE_TILESET\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
630
			"mod\tid=0,name=\"amb.o\",file=0\n";
631
		char *ap = write_temp(k_dbg_ambig, "x16_dbg_info_ambig.dbg");
632
		if (!ap) {
633
			check(false, "could not write the ambiguity fixture");
634
		} else {
635
			check(dbg_info_load(ap) == 0, "loads an ambiguously named segment");
636
			const char *f = NULL;
637
			int         l = 0;
638
			// Neither candidate may be adopted, so neither bank resolves.
639
			dbg_bank_result_t m3 = dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l);
640
			f = NULL;
641
			l = 0;
642
			dbg_bank_result_t m7 = dbg_info_addr_to_source_banked_ex(0xA000, 7, &f, &l);
643
			check(m3 != DBG_BANK_RESOLVED && m7 != DBG_BANK_RESOLVED,
644
			      "and picks neither bank when two equates match equally well");
645
			remove(ap);
646
			dbg_info_free();
647
		}
648
	}
649
650
	// Bank equates are owned too. Two overlays commonly each define
651
	// RAM_BANK_CODE for their own CODE segment, with different values. Each
652
	// module's own equate has to win for its own segment: pooling them lets the
653
	// second module's bank decide where the first module's code lives, and
654
	// merely refusing to choose would leave both unknown.
655
	{
656
		dbg_info_free();
657
		static const char *k_bank_a =
658
			"version\tmajor=2,minor=0\n"
659
			"file\tid=0,name=\"ova.s\",size=10,mtime=0x00000000,mod=0\n"
660
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
661
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
662
			"line\tid=0,file=0,line=1,span=0\n"
663
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
664
			"mod\tid=0,name=\"ova.o\",file=0\n";
665
		static const char *k_bank_b =
666
			"version\tmajor=2,minor=0\n"
667
			"file\tid=0,name=\"ovb.s\",size=10,mtime=0x00000000,mod=0\n"
668
			"seg\tid=0,name=\"CODE\",start=0x00a020,size=0x0010,addrsize=absolute,type=ro\n"
669
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
670
			"line\tid=0,file=0,line=1,span=0\n"
671
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
672
			"mod\tid=0,name=\"ovb.o\",file=0\n";
673
		char *ba = write_temp(k_bank_a, "x16_dbg_info_bankA.dbg");
674
		if (!ba) {
675
			check(false, "could not write bank module A");
676
		} else {
677
			check(dbg_info_load(ba) == 0, "loads overlay A");
678
			char *bb = write_temp(k_bank_b, "x16_dbg_info_bankB.dbg");
679
			if (bb) {
680
				check(dbg_info_load(bb) == 0, "loads overlay B, same equate name");
681
				const char *f = NULL;
682
				int         l = 0;
683
				check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
684
				          == DBG_BANK_RESOLVED,
685
				      "A's CODE keeps the bank A declared");
686
				f = NULL;
687
				l = 0;
688
				check(dbg_info_addr_to_source_banked_ex(0xA020, 7, &f, &l)
689
				          == DBG_BANK_RESOLVED,
690
				      "and B's CODE keeps the bank B declared");
691
				remove(bb);
692
			}
693
			remove(ba);
694
			dbg_info_free();
695
		}
696
	}
697
698
	// Files are owned as well. Two modules that each compile a file of the same
699
	// name must keep two records; consumers resolve by name today, so the count
700
	// is what makes the pooling visible.
701
	{
702
		dbg_info_free();
703
		char *fa = write_temp(k_dbg_equ_v1, "x16_dbg_info_fileA.dbg");
704
		if (!fa) {
705
			check(false, "could not write file module A");
706
		} else {
707
			check(dbg_info_load(fa) == 0, "loads a module declaring equ.s");
708
			int after_a = dbg_info_file_count();
709
			char *fb = write_temp(k_dbg_equ_v2, "x16_dbg_info_fileB.dbg");
710
			if (fb) {
711
				check(dbg_info_load(fb) == 0, "loads another declaring its own equ.s");
712
				check(dbg_info_file_count() == after_a + 1,
713
				      "and the two same-named files stay separate records");
714
				remove(fb);
715
			}
716
			remove(fa);
717
			dbg_info_free();
718
		}
719
	}
720
721
	// A segment can be given its bank by another module's equate, through the
722
	// fallback pass. Those segments are not unloaded when that other module
723
	// reloads, so unless equate-derived banks are recomputed they keep a value
724
	// nobody declares any more.
725
	{
726
		dbg_info_free();
727
		static const char *k_donor_v1 =
728
			"version\tmajor=2,minor=0\n"
729
			"file\tid=0,name=\"donor.s\",size=10,mtime=0x00000000,mod=0\n"
730
			"seg\tid=0,name=\"DONOR\",start=0x000800,size=0x0010,addrsize=absolute,type=ro\n"
731
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
732
			"line\tid=0,file=0,line=1,span=0\n"
733
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
734
			"mod\tid=0,name=\"donor.o\",file=0\n";
735
		static const char *k_donor_v2 =
736
			"version\tmajor=2,minor=0\n"
737
			"file\tid=0,name=\"donor.s\",size=10,mtime=0x00000000,mod=0\n"
738
			"seg\tid=0,name=\"DONOR\",start=0x000800,size=0x0010,addrsize=absolute,type=ro\n"
739
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
740
			"line\tid=0,file=0,line=1,span=0\n"
741
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
742
			"mod\tid=0,name=\"donor.o\",file=0\n";
743
		// Declares no bank equate of its own, so it is seeded from the donor's.
744
		static const char *k_consumer =
745
			"version\tmajor=2,minor=0\n"
746
			"file\tid=0,name=\"consumer.s\",size=10,mtime=0x00000000,mod=0\n"
747
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
748
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
749
			"line\tid=0,file=0,line=1,span=0\n"
750
			"mod\tid=0,name=\"consumer.o\",file=0\n";
751
		char *dp = write_temp(k_donor_v1, "x16_dbg_info_donor.dbg");
752
		if (!dp) {
753
			check(false, "could not write the donor fixture");
754
		} else {
755
			check(dbg_info_load(dp) == 0, "loads the donor module");
756
			char *cp = write_temp(k_consumer, "x16_dbg_info_consumer.dbg");
757
			if (cp) {
758
				check(dbg_info_load(cp) == 0, "loads a consumer with no equate of its own");
759
				const char *f = NULL;
760
				int         l = 0;
761
				check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
762
				          == DBG_BANK_RESOLVED,
763
				      "which borrows the donor's bank");
764
765
				// The donor is rebuilt with a different bank and reloaded.
766
				char *dp2 = write_temp(k_donor_v2, "x16_dbg_info_donor.dbg");
767
				if (dp2) {
768
					dbg_info_unload_range(0x0800, 0x080F);
769
					check(dbg_info_load(dp2) == 0, "reloads the donor with a new bank");
770
					f = NULL;
771
					l = 0;
772
					check(dbg_info_addr_to_source_banked_ex(0xA000, 7, &f, &l)
773
					          == DBG_BANK_RESOLVED,
774
					      "and the borrowed bank follows it");
775
					f = NULL;
776
					l = 0;
777
					check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
778
					          != DBG_BANK_RESOLVED,
779
					      "rather than answering to the bank nobody declares");
780
				}
781
				remove(cp);
782
			}
783
			remove(dp);
784
			dbg_info_free();
785
		}
786
	}
787
788
	// Re-deriving equate banks must not discard what was actually observed at
789
	// runtime. An observation is evidence; an equate is only an inference.
790
	{
791
		dbg_info_free();
792
		char *op = write_temp(k_dbg_equ_v1, "x16_dbg_info_obs.dbg");
793
		if (!op) {
794
			check(false, "could not write the observation fixture");
795
		} else {
796
			check(dbg_info_load(op) == 0, "loads a module whose equate says bank 3");
797
			// The loader saw it land somewhere else.
798
			dbg_info_note_bank_load(0xA000, 0x10, 9);
799
			const char *f = NULL;
800
			int         l = 0;
801
			check(dbg_info_addr_to_source_banked_ex(0xA000, 9, &f, &l)
802
			          == DBG_BANK_RESOLVED,
803
			      "and a runtime observation overrides it");
804
805
			// Any later load re-derives equate banks; the observation must stay.
806
			char *o2 = write_temp(k_dbg_equ_v1, "x16_dbg_info_obs2.dbg");
807
			if (o2) {
808
				check(dbg_info_load(o2) == 0, "loads another module afterwards");
809
				f = NULL;
810
				l = 0;
811
				check(dbg_info_addr_to_source_banked_ex(0xA000, 9, &f, &l)
812
				          == DBG_BANK_RESOLVED,
813
				      "and re-deriving equate banks leaves the observation alone");
814
				remove(o2);
815
			}
816
			remove(op);
817
			dbg_info_free();
818
		}
819
	}
820
821
	// A module's own equate must not outrank a better match from elsewhere.
822
	// An own-module prefix match used to end the search before another
823
	// module's exact match was considered, which is the same "claimed by a name
824
	// it resembles" failure the ranking exists to prevent.
825
	{
826
		dbg_info_free();
827
		static const char *k_rank_main =
828
			"version\tmajor=2,minor=0\n"
829
			"file\tid=0,name=\"rmain.s\",size=10,mtime=0x00000000,mod=0\n"
830
			"seg\tid=0,name=\"OTHER\",start=0x000900,size=0x0010,addrsize=absolute,type=ro\n"
831
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
832
			"line\tid=0,file=0,line=1,span=0\n"
833
			"sym\tid=0,name=\"RAM_BANK_CODE2\",addrsize=absolute,size=1,scope=0,def=0,val=0x000009,type=equ\n"
834
			"mod\tid=0,name=\"rmain.o\",file=0\n";
835
		static const char *k_rank_ovl =
836
			"version\tmajor=2,minor=0\n"
837
			"file\tid=0,name=\"rovl.s\",size=10,mtime=0x00000000,mod=0\n"
838
			"seg\tid=0,name=\"CODE2\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
839
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
840
			"line\tid=0,file=0,line=1,span=0\n"
841
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000002,type=equ\n"
842
			"mod\tid=0,name=\"rovl.o\",file=0\n";
843
		char *rm = write_temp(k_rank_main, "x16_dbg_info_rankmain.dbg");
844
		if (!rm) {
845
			check(false, "could not write the ranking fixture");
846
		} else {
847
			check(dbg_info_load(rm) == 0, "loads a module declaring RAM_BANK_CODE2");
848
			char *ro = write_temp(k_rank_ovl, "x16_dbg_info_rankovl.dbg");
849
			if (ro) {
850
				// Its own RAM_BANK_CODE only prefix-matches segment CODE2;
851
				// the other module's RAM_BANK_CODE2 matches it exactly.
852
				check(dbg_info_load(ro) == 0, "loads an overlay whose own equate only resembles it");
853
				const char *f = NULL;
854
				int         l = 0;
855
				check(dbg_info_addr_to_source_banked_ex(0xA000, 9, &f, &l)
856
				          == DBG_BANK_RESOLVED,
857
				      "an exact match elsewhere beats a resemblance at home");
858
				f = NULL;
859
				l = 0;
860
				check(dbg_info_addr_to_source_banked_ex(0xA000, 2, &f, &l)
861
				          != DBG_BANK_RESOLVED,
862
				      "and the resemblance does not also answer");
863
				remove(ro);
864
			}
865
			remove(rm);
866
			dbg_info_free();
867
		}
868
	}
869
870
	// Two equates in ONE module that both reduce to the segment's name, with
871
	// different banks. The module contradicts itself, so there is nothing to
872
	// choose between them.
873
	{
874
		dbg_info_free();
875
		static const char *k_self_contradict =
876
			"version\tmajor=2,minor=0\n"
877
			"file\tid=0,name=\"sc.s\",size=10,mtime=0x00000000,mod=0\n"
878
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
879
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
880
			"line\tid=0,file=0,line=1,span=0\n"
881
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
882
			"sym\tid=1,name=\"CODE_BANK\",addrsize=absolute,size=1,scope=0,def=0,val=0x000007,type=equ\n"
883
			"mod\tid=0,name=\"sc.o\",file=0\n";
884
		char *sp = write_temp(k_self_contradict, "x16_dbg_info_selfcon.dbg");
885
		if (!sp) {
886
			check(false, "could not write the self-contradiction fixture");
887
		} else {
888
			check(dbg_info_load(sp) == 0, "loads a module naming one segment twice");
889
			const char *f = NULL;
890
			int         l = 0;
891
			dbg_bank_result_t r3 = dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l);
892
			f = NULL;
893
			l = 0;
894
			dbg_bank_result_t r7 = dbg_info_addr_to_source_banked_ex(0xA000, 7, &f, &l);
895
			check(r3 != DBG_BANK_RESOLVED && r7 != DBG_BANK_RESOLVED,
896
			      "and two exact matches that disagree resolve to neither");
897
			remove(sp);
898
			dbg_info_free();
899
		}
900
	}
901
902
	// A common prefix shorter than four characters is not evidence of anything.
903
	{
904
		dbg_info_free();
905
		static const char *k_short_prefix =
906
			"version\tmajor=2,minor=0\n"
907
			"file\tid=0,name=\"sp.s\",size=10,mtime=0x00000000,mod=0\n"
908
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
909
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
910
			"line\tid=0,file=0,line=1,span=0\n"
911
			"sym\tid=0,name=\"RAM_BANK_COD\",addrsize=absolute,size=1,scope=0,def=0,val=0x000005,type=equ\n"
912
			"mod\tid=0,name=\"sp.o\",file=0\n";
913
		char *pp = write_temp(k_short_prefix, "x16_dbg_info_shortpfx.dbg");
914
		if (!pp) {
915
			check(false, "could not write the short-prefix fixture");
916
		} else {
917
			check(dbg_info_load(pp) == 0, "loads a segment with a near-miss equate");
918
			const char *f = NULL;
919
			int         l = 0;
920
			check(dbg_info_addr_to_source_banked_ex(0xA000, 5, &f, &l)
921
			          != DBG_BANK_RESOLVED,
922
			      "and a three-character overlap does not claim it");
923
			remove(pp);
924
			dbg_info_free();
925
		}
926
	}
927
928
	// A rebuilt module that no longer declares an equate must not go on
929
	// declaring it. Replacing equates one by one as they are parsed cannot see
930
	// this: the record that needs removing is never visited.
931
	{
932
		dbg_info_free();
933
		static const char *k_gone_v1 =
934
			"version\tmajor=2,minor=0\n"
935
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=2,type=0\n"
936
			"file\tid=0,name=\"gone.s\",size=10,mtime=0x00000000,mod=0\n"
937
			"seg\tid=0,name=\"GONE\",start=0x000800,size=0x0010,addrsize=absolute,type=ro\n"
938
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
939
			"line\tid=0,file=0,line=1,span=0\n"
940
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
941
			"sym\tid=1,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
942
			"mod\tid=0,name=\"gone.o\",file=0\n";
943
		// Same module, rebuilt with both equates removed. It still has a label,
944
		// as any real linked module does -- that is what distinguishes it from
945
		// a write that was cut off before the symbols.
946
		static const char *k_gone_v2 =
947
			"version\tmajor=2,minor=0\n"
948
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=1,type=0\n"
949
			"file\tid=0,name=\"gone.s\",size=10,mtime=0x00000000,mod=0\n"
950
			"seg\tid=0,name=\"GONE\",start=0x000800,size=0x0010,addrsize=absolute,type=ro\n"
951
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
952
			"line\tid=0,file=0,line=1,span=0\n"
953
			"sym\tid=0,name=\"_gone_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x000800,type=lab\n"
954
			"mod\tid=0,name=\"gone.o\",file=0\n";
955
		static const char *k_borrower =
956
			"version\tmajor=2,minor=0\n"
957
			"file\tid=0,name=\"borrow.s\",size=10,mtime=0x00000000,mod=0\n"
958
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
959
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
960
			"line\tid=0,file=0,line=1,span=0\n"
961
			"mod\tid=0,name=\"borrow.o\",file=0\n";
962
		char *gp = write_temp(k_gone_v1, "x16_dbg_info_gone.dbg");
963
		if (!gp) {
964
			check(false, "could not write the removed-equate fixture");
965
		} else {
966
			check(dbg_info_load(gp) == 0, "loads a module with two equates");
967
			char *bp = write_temp(k_borrower, "x16_dbg_info_borrow.dbg");
968
			if (bp) {
969
				check(dbg_info_load(bp) == 0, "loads a borrower with none of its own");
970
				const char *f = NULL;
971
				int         l = 0;
972
				check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
973
				          == DBG_BANK_RESOLVED,
974
				      "which borrows the declared bank");
975
976
				char *gp2 = write_temp(k_gone_v2, "x16_dbg_info_gone.dbg");
977
				if (gp2) {
978
					dbg_info_unload_range(0x0800, 0x080F);
979
					check(dbg_info_load(gp2) == 0, "reloads it with the equates removed");
980
					dbg_addr_t v = 0;
981
					check(!dbg_info_equate_to_value("SCORE", &v),
982
					      "and the dropped equate stops resolving");
983
					f = NULL;
984
					l = 0;
985
					check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
986
					          != DBG_BANK_RESOLVED,
987
					      "and the borrowed bank is not still claimed");
988
				}
989
				remove(bp);
990
			}
991
			remove(gp);
992
			dbg_info_free();
993
		}
994
	}
995
996
	// Names too long to reduce are refused rather than truncated: a truncated
997
	// name compares equal to anything sharing its head, inventing exact matches
998
	// the full names do not support.
999
	{
1000
		dbg_info_free();
1001
		// The equate's target is exactly as long as the squash buffer allows,
1002
		// so it is accepted. The segment name shares that whole head but runs
1003
		// past the buffer, so truncating it would make the two compare equal
1004
		// even though the full names differ.
1005
		char longseg[256], longequ[256];
1006
		memset(longseg, 'A', 130);
1007
		memcpy(longseg + 130, "LEFT", 5);
1008
		memset(longequ, 'A', 127);
1009
		longequ[127] = '\0';
1010
		char buf[2048];
1011
		snprintf(buf, sizeof buf,
1012
		         "version\tmajor=2,minor=0\n"
1013
		         "file\tid=0,name=\"lng.s\",size=10,mtime=0x00000000,mod=0\n"
1014
		         "seg\tid=0,name=\"%s\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1015
		         "span\tid=0,seg=0,start=0,size=16,type=0\n"
1016
		         "line\tid=0,file=0,line=1,span=0\n"
1017
		         "sym\tid=0,name=\"RAM_BANK_%s\",addrsize=absolute,size=1,scope=0,def=0,val=0x000004,type=equ\n"
1018
		         "mod\tid=0,name=\"lng.o\",file=0\n",
1019
		         longseg, longequ);
1020
		char *lp = write_temp(buf, "x16_dbg_info_long_names.dbg");
1021
		if (!lp) {
1022
			check(false, "could not write the long-name fixture");
1023
		} else {
1024
			check(dbg_info_load(lp) == 0, "loads a module with over-long names");
1025
			const char *f = NULL;
1026
			int         l = 0;
1027
			check(dbg_info_addr_to_source_banked_ex(0xA000, 4, &f, &l)
1028
			          != DBG_BANK_RESOLVED,
1029
			      "and two names that differ past the cutoff do not match");
1030
			remove(lp);
1031
			dbg_info_free();
1032
		}
1033
	}
1034
1035
	// An ambiguous winning rank does not fall through to a weaker one. The
1036
	// better evidence has just said the question is undecidable; answering it
1037
	// from worse evidence would be inventing certainty.
1038
	{
1039
		dbg_info_free();
1040
		// Owns CODE2, and its own equate only resembles it (own-prefix).
1041
		static const char *k_ft_own =
1042
			"version\tmajor=2,minor=0\n"
1043
			"file\tid=0,name=\"ftown.s\",size=10,mtime=0x00000000,mod=0\n"
1044
			"seg\tid=0,name=\"CODE2\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1045
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1046
			"line\tid=0,file=0,line=1,span=0\n"
1047
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000002,type=equ\n"
1048
			"mod\tid=0,name=\"ftown.o\",file=0\n";
1049
		// Two other modules name CODE2 exactly, and disagree.
1050
		static const char *k_ft_x =
1051
			"version\tmajor=2,minor=0\n"
1052
			"file\tid=0,name=\"ftx.s\",size=10,mtime=0x00000000,mod=0\n"
1053
			"seg\tid=0,name=\"XONE\",start=0x000900,size=0x0010,addrsize=absolute,type=ro\n"
1054
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1055
			"line\tid=0,file=0,line=1,span=0\n"
1056
			"sym\tid=0,name=\"RAM_BANK_CODE2\",addrsize=absolute,size=1,scope=0,def=0,val=0x000005,type=equ\n"
1057
			"mod\tid=0,name=\"ftx.o\",file=0\n";
1058
		static const char *k_ft_y =
1059
			"version\tmajor=2,minor=0\n"
1060
			"file\tid=0,name=\"fty.s\",size=10,mtime=0x00000000,mod=0\n"
1061
			"seg\tid=0,name=\"YONE\",start=0x000920,size=0x0010,addrsize=absolute,type=ro\n"
1062
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1063
			"line\tid=0,file=0,line=1,span=0\n"
1064
			"sym\tid=0,name=\"RAM_BANK_CODE2\",addrsize=absolute,size=1,scope=0,def=0,val=0x000006,type=equ\n"
1065
			"mod\tid=0,name=\"fty.o\",file=0\n";
1066
		char px[512], py[512], po2[512];
1067
		char *t = write_temp(k_ft_x, "x16_dbg_info_ftx.dbg");
1068
		bool wrote = (t != NULL);
1069
		if (wrote) snprintf(px, sizeof px, "%s", t);
1070
		t = wrote ? write_temp(k_ft_y, "x16_dbg_info_fty.dbg") : NULL;
1071
		wrote = wrote && (t != NULL);
1072
		if (wrote) snprintf(py, sizeof py, "%s", t);
1073
		t = wrote ? write_temp(k_ft_own, "x16_dbg_info_ftown.dbg") : NULL;
1074
		wrote = wrote && (t != NULL);
1075
		if (wrote) snprintf(po2, sizeof po2, "%s", t);
1076
		if (!wrote) {
1077
			check(false, "could not write the fall-through fixtures");
1078
		} else {
1079
			check(dbg_info_load(px) == 0 && dbg_info_load(py) == 0 &&
1080
			          dbg_info_load(po2) == 0,
1081
			      "loads two disagreeing exact namers and one that resembles");
1082
			const char *f = NULL;
1083
			int         l = 0;
1084
			dbg_bank_result_t r5 = dbg_info_addr_to_source_banked_ex(0xA000, 5, &f, &l);
1085
			f = NULL; l = 0;
1086
			dbg_bank_result_t r6 = dbg_info_addr_to_source_banked_ex(0xA000, 6, &f, &l);
1087
			f = NULL; l = 0;
1088
			dbg_bank_result_t r2 = dbg_info_addr_to_source_banked_ex(0xA000, 2, &f, &l);
1089
			check(r5 != DBG_BANK_RESOLVED && r6 != DBG_BANK_RESOLVED,
1090
			      "the contradicting exact matches resolve to neither");
1091
			check(r2 != DBG_BANK_RESOLVED,
1092
			      "and the weaker resemblance is not used to break the tie");
1093
			remove(px);
1094
			remove(py);
1095
			remove(po2);
1096
			dbg_info_free();
1097
		}
1098
	}
1099
1100
	// Among equally good name matches, the segment's own module still wins.
1101
	{
1102
		dbg_info_free();
1103
		static const char *k_pp_other =
1104
			"version\tmajor=2,minor=0\n"
1105
			"file\tid=0,name=\"ppo.s\",size=10,mtime=0x00000000,mod=0\n"
1106
			"seg\tid=0,name=\"PPOTHER\",start=0x000940,size=0x0010,addrsize=absolute,type=ro\n"
1107
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1108
			"line\tid=0,file=0,line=1,span=0\n"
1109
			"sym\tid=0,name=\"RAM_BANK_STORETILEMAP\",addrsize=absolute,size=1,scope=0,def=0,val=0x000005,type=equ\n"
1110
			"mod\tid=0,name=\"ppo.o\",file=0\n";
1111
		static const char *k_pp_own =
1112
			"version\tmajor=2,minor=0\n"
1113
			"file\tid=0,name=\"ppw.s\",size=10,mtime=0x00000000,mod=0\n"
1114
			"seg\tid=0,name=\"STORETILE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1115
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1116
			"line\tid=0,file=0,line=1,span=0\n"
1117
			"sym\tid=0,name=\"RAM_BANK_STORETILESET\",addrsize=absolute,size=1,scope=0,def=0,val=0x000006,type=equ\n"
1118
			"mod\tid=0,name=\"ppw.o\",file=0\n";
1119
		char ppa[512], ppb[512];
1120
		char *t2 = write_temp(k_pp_other, "x16_dbg_info_ppo.dbg");
1121
		bool wrote2 = (t2 != NULL);
1122
		if (wrote2) snprintf(ppa, sizeof ppa, "%s", t2);
1123
		t2 = wrote2 ? write_temp(k_pp_own, "x16_dbg_info_ppw.dbg") : NULL;
1124
		wrote2 = wrote2 && (t2 != NULL);
1125
		if (wrote2) snprintf(ppb, sizeof ppb, "%s", t2);
1126
		if (!wrote2) {
1127
			check(false, "could not write the prefix-ownership fixtures");
1128
		} else {
1129
			check(dbg_info_load(ppa) == 0 && dbg_info_load(ppb) == 0,
1130
			      "loads two modules that both merely resemble the segment");
1131
			const char *f = NULL;
1132
			int         l = 0;
1133
			check(dbg_info_addr_to_source_banked_ex(0xA000, 6, &f, &l)
1134
			          == DBG_BANK_RESOLVED,
1135
			      "and the segment's own module wins among resemblances");
1136
			remove(ppa);
1137
			remove(ppb);
1138
			dbg_info_free();
1139
		}
1140
	}
1141
1142
	// A file that opens but parses to nothing must not take the previous
1143
	// generation with it. Sweeping the old equates before reading meant an
1144
	// empty or corrupt rebuild silently deleted them and still reported
1145
	// success -- the emulator writes these files while a program is running,
1146
	// so a half-written one is a real state, not a hypothetical.
1147
	{
1148
		dbg_info_free();
1149
		static const char *k_tx_good =
1150
			"version\tmajor=2,minor=0\n"
1151
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=2,type=0\n"
1152
			"file\tid=0,name=\"tx.s\",size=10,mtime=0x00000000,mod=0\n"
1153
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1154
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1155
			"line\tid=0,file=0,line=1,span=0\n"
1156
			"sym\tid=0,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
1157
			"sym\tid=1,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
1158
			"mod\tid=0,name=\"tx.o\",file=0\n";
1159
		char txp[512];
1160
		char *t3 = write_temp(k_tx_good, "x16_dbg_info_tx.dbg");
1161
		if (!t3) {
1162
			check(false, "could not write the transactional fixture");
1163
		} else {
1164
			snprintf(txp, sizeof txp, "%s", t3);
1165
			check(dbg_info_load(txp) == 0, "loads a module with equates");
1166
			dbg_addr_t v = 0;
1167
			check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1168
			      "and reads one back");
1169
1170
			// The same path, rebuilt empty -- as a truncated write leaves it.
1171
			if (write_temp("", "x16_dbg_info_tx.dbg")) {
1172
				check(dbg_info_load(txp) != 0,
1173
				      "an empty rebuild is reported as a failed load");
1174
				v = 0;
1175
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1176
				      "an empty rebuild does not delete what still works");
1177
				const char *f = NULL;
1178
				int         l = 0;
1179
				check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
1180
				          == DBG_BANK_RESOLVED,
1181
				      "and the bank it seeded survives too");
1182
			}
1183
1184
			// And a file of the right shape but no recognisable records.
1185
			if (write_temp("nonsense\nmore nonsense\n", "x16_dbg_info_tx.dbg")) {
1186
				check(dbg_info_load(txp) != 0,
1187
				      "an unparseable rebuild is reported as a failed load");
1188
				v = 0;
1189
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1190
				      "nor does an unparseable one");
1191
			}
1192
1193
			// The shape that actually occurs: cut off after the line records
1194
			// but before the symbols. ld65 emits them in that order.
1195
			static const char *k_tx_partial =
1196
				"version\tmajor=2,minor=0\n"
1197
				"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=2,type=0\n"
1198
				"file\tid=0,name=\"tx.s\",size=10,mtime=0x00000000,mod=0\n"
1199
				"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1200
				"span\tid=0,seg=0,start=0,size=16,type=0\n"
1201
				"line\tid=0,file=0,line=1,span=0\n";
1202
			if (write_temp(k_tx_partial, "x16_dbg_info_tx.dbg")) {
1203
				check(dbg_info_load(txp) != 0,
1204
				      "a write cut off before the symbols is a failed load");
1205
				v = 0;
1206
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1207
				      "and a write cut off before the symbols keeps them too");
1208
			}
1209
			remove(txp);
1210
			dbg_info_free();
1211
		}
1212
	}
1213
1214
	// An equate name too long for the reduction buffer must be refused, not
1215
	// copied into it. The name comes straight out of a file sitting next to a
1216
	// loaded program, so its length is not ours to assume.
1217
	//
1218
	// This is a smoke test, not a guard: squash_name() refuses the name either
1219
	// way, so the resolution outcome is the same with or without the length
1220
	// check, and only a sanitizer can see the difference. Removing the check
1221
	// turns this input into a 300-byte write into a 128-byte stack buffer.
1222
	{
1223
		dbg_info_free();
1224
		char huge[400];
1225
		memset(huge, 'B', 300);
1226
		huge[300] = '\0';
1227
		char buf2[2048];
1228
		snprintf(buf2, sizeof buf2,
1229
		         "version\tmajor=2,minor=0\n"
1230
		         "file\tid=0,name=\"ov.s\",size=10,mtime=0x00000000,mod=0\n"
1231
		         "seg\tid=0,name=\"%s\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1232
		         "span\tid=0,seg=0,start=0,size=16,type=0\n"
1233
		         "line\tid=0,file=0,line=1,span=0\n"
1234
		         "sym\tid=0,name=\"RAM_BANK_%s\",addrsize=absolute,size=1,scope=0,def=0,val=0x000008,type=equ\n"
1235
		         "mod\tid=0,name=\"ov.o\",file=0\n",
1236
		         huge, huge);
1237
		char *hp = write_temp(buf2, "x16_dbg_info_hugeequ.dbg");
1238
		if (!hp) {
1239
			check(false, "could not write the long-equate fixture");
1240
		} else {
1241
			char hpath[512];
1242
			snprintf(hpath, sizeof hpath, "%s", hp);
1243
			check(dbg_info_load(hpath) == 0, "loads a module with an oversized equate name");
1244
			const char *f = NULL;
1245
			int         l = 0;
1246
			check(dbg_info_addr_to_source_banked_ex(0xA000, 8, &f, &l)
1247
			          != DBG_BANK_RESOLVED,
1248
			      "and an oversized equate name is handled without incident");
1249
			remove(hpath);
1250
			dbg_info_free();
1251
		}
1252
	}
1253
1254
	// ld65 states how many symbols it wrote. That is what separates a file cut
1255
	// off partway through its symbol block from one that genuinely declares
1256
	// fewer -- the two have the same shape and differ only in the promise.
1257
	{
1258
		dbg_info_free();
1259
		static const char *k_cnt_full =
1260
			"version\tmajor=2,minor=0\n"
1261
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=3,type=0\n"
1262
			"file\tid=0,name=\"cnt.s\",size=10,mtime=0x00000000,mod=0\n"
1263
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1264
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1265
			"line\tid=0,file=0,line=1,span=0\n"
1266
			"sym\tid=0,name=\"_cnt_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x00a000,type=lab\n"
1267
			"sym\tid=1,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
1268
			"sym\tid=2,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
1269
			"mod\tid=0,name=\"cnt.o\",file=0\n";
1270
		// Promises three, delivers one: a write still in progress.
1271
		static const char *k_cnt_cut =
1272
			"version\tmajor=2,minor=0\n"
1273
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=3,type=0\n"
1274
			"file\tid=0,name=\"cnt.s\",size=10,mtime=0x00000000,mod=0\n"
1275
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1276
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1277
			"line\tid=0,file=0,line=1,span=0\n"
1278
			"sym\tid=0,name=\"_cnt_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x00a000,type=lab\n";
1279
		// Same shape, but promises one and delivers it: a real rebuild that
1280
		// dropped the constants.
1281
		static const char *k_cnt_slim =
1282
			"version\tmajor=2,minor=0\n"
1283
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=1,type=0\n"
1284
			"file\tid=0,name=\"cnt.s\",size=10,mtime=0x00000000,mod=0\n"
1285
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1286
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1287
			"line\tid=0,file=0,line=1,span=0\n"
1288
			"sym\tid=0,name=\"_cnt_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x00a000,type=lab\n"
1289
			"mod\tid=0,name=\"cnt.o\",file=0\n";
1290
		char cpath[512];
1291
		char *c1 = write_temp(k_cnt_full, "x16_dbg_info_cnt.dbg");
1292
		if (!c1) {
1293
			check(false, "could not write the record-count fixture");
1294
		} else {
1295
			snprintf(cpath, sizeof cpath, "%s", c1);
1296
			check(dbg_info_load(cpath) == 0, "loads a module that declares three symbols");
1297
			dbg_addr_t v = 0;
1298
			check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1299
			      "and reads a constant from it");
1300
1301
			if (write_temp(k_cnt_cut, "x16_dbg_info_cnt.dbg")) {
1302
				check(dbg_info_load(cpath) != 0,
1303
				      "a file that promised three and gave one is a failed load");
1304
				v = 0;
1305
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1306
				      "a file that promised three and gave one keeps them");
1307
			}
1308
1309
			// Cut inside the symbol block rather than before it. For a real
1310
			// program the symbols are the bulk of the file, so this is where a
1311
			// partial write is most likely to land -- and the shape the
1312
			// previous guard, which only asked whether any symbol was seen,
1313
			// could not tell from a finished file.
1314
			static const char *k_cnt_mid =
1315
				"version\tmajor=2,minor=0\n"
1316
				"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=3,type=0\n"
1317
				"file\tid=0,name=\"cnt.s\",size=10,mtime=0x00000000,mod=0\n"
1318
				"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1319
				"span\tid=0,seg=0,start=0,size=16,type=0\n"
1320
				"line\tid=0,file=0,line=1,span=0\n"
1321
				"sym\tid=0,name=\"_cnt_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x00a000,type=lab\n"
1322
				"sym\tid=1,name=\"SCORE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n";
1323
			if (write_temp(k_cnt_full, "x16_dbg_info_cnt.dbg")) {
1324
				dbg_info_load(cpath);
1325
			}
1326
			if (write_temp(k_cnt_mid, "x16_dbg_info_cnt.dbg")) {
1327
				check(dbg_info_load(cpath) != 0,
1328
				      "a cut inside the symbol block is a failed load too");
1329
				v = 0;
1330
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1331
				      "and the bank equate that follows the cut survives");
1332
				const char *bf = NULL;
1333
				int         bl = 0;
1334
				check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &bf, &bl)
1335
				          == DBG_BANK_RESOLVED,
1336
				      "so the bank it seeded is still known");
1337
			}
1338
1339
			// A symbol line cut off mid-record is not a symbol. Counting it
1340
			// would let a file that stopped halfway look complete.
1341
			static const char *k_cnt_torn =
1342
				"version\tmajor=2,minor=0\n"
1343
				"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=2,type=0\n"
1344
				"file\tid=0,name=\"cnt.s\",size=10,mtime=0x00000000,mod=0\n"
1345
				"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1346
				"span\tid=0,seg=0,start=0,size=16,type=0\n"
1347
				"line\tid=0,file=0,line=1,span=0\n"
1348
				"sym\tid=0,name=\"_cnt_entry\",addrsize=absolute,size=3,scope=0,def=0,val=0x00a000,type=lab\n"
1349
				"sym\tid=1,name=\"SCO\n";
1350
			if (write_temp(k_cnt_full, "x16_dbg_info_cnt.dbg")) {
1351
				dbg_info_load(cpath);
1352
			}
1353
			if (write_temp(k_cnt_torn, "x16_dbg_info_cnt.dbg")) {
1354
				dbg_info_load(cpath);
1355
				v = 0;
1356
				check(dbg_info_equate_to_value("SCORE", &v) && v == 0x1234,
1357
				      "and a torn final symbol does not count towards the promise");
1358
			}
1359
1360
			// Restore, then the same shape with an honest count.
1361
			if (write_temp(k_cnt_full, "x16_dbg_info_cnt.dbg")) {
1362
				dbg_info_load(cpath);
1363
			}
1364
			if (write_temp(k_cnt_slim, "x16_dbg_info_cnt.dbg")) {
1365
				dbg_info_load(cpath);
1366
				v = 0;
1367
				check(!dbg_info_equate_to_value("SCORE", &v),
1368
				      "but one that promised one and gave one drops them");
1369
			}
1370
			remove(cpath);
1371
			dbg_info_free();
1372
		}
1373
	}
1374
1375
	// The same name can be filed as a bank constant or an ordinary one
1376
	// depending on its value, so a redefinition can cross between the two
1377
	// tables. The later definition has to be the only one left.
1378
	{
1379
		dbg_info_free();
1380
		static const char *k_cross =
1381
			"version\tmajor=2,minor=0\n"
1382
			"file\tid=0,name=\"cr.s\",size=10,mtime=0x00000000,mod=0\n"
1383
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1384
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1385
			"line\tid=0,file=0,line=1,span=0\n"
1386
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
1387
			"sym\tid=1,name=\"RAM_BANK_CODE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
1388
			"mod\tid=0,name=\"cr.o\",file=0\n";
1389
		char *xp = write_temp(k_cross, "x16_dbg_info_cross.dbg");
1390
		if (!xp) {
1391
			check(false, "could not write the cross-table fixture");
1392
		} else {
1393
			char xpath[512];
1394
			snprintf(xpath, sizeof xpath, "%s", xp);
1395
			check(dbg_info_load(xpath) == 0, "loads a name defined twice across tables");
1396
			dbg_addr_t v = 0;
1397
			check(dbg_info_equate_to_value("RAM_BANK_CODE", &v) && v == 0x1234,
1398
			      "the later definition answers name lookups");
1399
			const char *f = NULL;
1400
			int         l = 0;
1401
			check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f, &l)
1402
			          != DBG_BANK_RESOLVED,
1403
			      "and the superseded one no longer seeds a bank");
1404
			remove(xpath);
1405
			dbg_info_free();
1406
		}
1407
1408
		// And the same collision in the other order: filed as an ordinary
1409
		// constant first, then redefined as a bank one.
1410
		dbg_info_free();
1411
		static const char *k_cross_rev =
1412
			"version\tmajor=2,minor=0\n"
1413
			"file\tid=0,name=\"cr2.s\",size=10,mtime=0x00000000,mod=0\n"
1414
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1415
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1416
			"line\tid=0,file=0,line=1,span=0\n"
1417
			"sym\tid=0,name=\"RAM_BANK_CODE\",addrsize=absolute,size=2,scope=0,def=0,val=0x001234,type=equ\n"
1418
			"sym\tid=1,name=\"RAM_BANK_CODE\",addrsize=absolute,size=1,scope=0,def=0,val=0x000003,type=equ\n"
1419
			"mod\tid=0,name=\"cr2.o\",file=0\n";
1420
		char *yp = write_temp(k_cross_rev, "x16_dbg_info_cross2.dbg");
1421
		if (!yp) {
1422
			check(false, "could not write the reverse cross-table fixture");
1423
		} else {
1424
			char ypath[512];
1425
			snprintf(ypath, sizeof ypath, "%s", yp);
1426
			check(dbg_info_load(ypath) == 0, "loads the same collision reversed");
1427
			dbg_addr_t v2 = 0;
1428
			check(!dbg_info_equate_to_value("RAM_BANK_CODE", &v2),
1429
			      "the superseded constant no longer answers name lookups");
1430
			const char *f2 = NULL;
1431
			int         l2 = 0;
1432
			check(dbg_info_addr_to_source_banked_ex(0xA000, 3, &f2, &l2)
1433
			          == DBG_BANK_RESOLVED,
1434
			      "and the later definition seeds the bank");
1435
			remove(ypath);
1436
			dbg_info_free();
1437
		}
1438
	}
1439
1440
	// A .dbg with no `info` line gives no record counts to check against, so a
1441
	// short read cannot be detected and the file is merged. It must still not
1442
	// be treated as a statement that the module declares nothing.
1443
	{
1444
		dbg_info_free();
1445
		static const char *k_ni_full =
1446
			"version\tmajor=2,minor=0\n"
1447
			"file\tid=0,name=\"ni.s\",size=10,mtime=0x00000000,mod=0\n"
1448
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1449
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1450
			"line\tid=0,file=0,line=1,span=0\n"
1451
			"sym\tid=0,name=\"NILIMIT\",addrsize=absolute,size=2,scope=0,def=0,val=0x004321,type=equ\n"
1452
			"mod\tid=0,name=\"ni.o\",file=0\n";
1453
		static const char *k_ni_cut =
1454
			"version\tmajor=2,minor=0\n"
1455
			"file\tid=0,name=\"ni.s\",size=10,mtime=0x00000000,mod=0\n"
1456
			"seg\tid=0,name=\"CODE\",start=0x00a000,size=0x0010,addrsize=absolute,type=ro\n"
1457
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1458
			"line\tid=0,file=0,line=1,span=0\n";
1459
		char nipath[512];
1460
		char *n1 = write_temp(k_ni_full, "x16_dbg_info_noinfo.dbg");
1461
		if (!n1) {
1462
			check(false, "could not write the info-less fixture");
1463
		} else {
1464
			snprintf(nipath, sizeof nipath, "%s", n1);
1465
			check(dbg_info_load(nipath) == 0, "loads a .dbg with no info record");
1466
			dbg_addr_t v = 0;
1467
			check(dbg_info_equate_to_value("NILIMIT", &v) && v == 0x4321,
1468
			      "and reads a constant from it");
1469
			if (write_temp(k_ni_cut, "x16_dbg_info_noinfo.dbg")) {
1470
				dbg_info_load(nipath);
1471
				v = 0;
1472
				check(dbg_info_equate_to_value("NILIMIT", &v) && v == 0x4321,
1473
				      "a short read it cannot detect still keeps the constant");
1474
			}
1475
			remove(nipath);
1476
			dbg_info_free();
1477
		}
1478
	}
1479
1480
	// ── Complete records that are simply not ours to keep ───────────────────
1481
	// ld65 counts every `sym` record in its `info` line, so the check for a
1482
	// half-written file has to count the records it SAW, not the ones it chose
1483
	// to store. Two shapes broke that, and either one refused the whole file:
1484
	// an import, which names a symbol another module defines and so carries no
1485
	// `val`; and a constant outside the 24-bit space the tables describe --
1486
	// C's EOF is -1, written as 0xFFFFFFFF, so every program that includes
1487
	// <stdio.h> has one.
1488
	{
1489
		dbg_info_free();
1490
		static const char *k_dbg_imp =
1491
			"version\tmajor=2,minor=0\n"
1492
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=3,type=0\n"
1493
			"file\tid=0,name=\"imp.s\",size=10,mtime=0x00000000,mod=0\n"
1494
			"seg\tid=0,name=\"CODE\",start=0x000801,size=0x0010,addrsize=absolute,type=ro\n"
1495
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1496
			"line\tid=0,file=0,line=3,span=0\n"
1497
			"sym\tid=0,name=\"_start\",addrsize=absolute,size=3,scope=0,def=0,val=0x000801,type=lab\n"
1498
			"sym\tid=1,name=\"_puts\",addrsize=absolute,scope=0,def=0,ref=7,type=imp,exp=4\n"
1499
			"sym\tid=2,name=\"EOF\",addrsize=long,scope=5,def=9,val=0xFFFFFFFF,type=equ\n"
1500
			"mod\tid=0,name=\"imp.o\",file=0\n";
1501
		char *ip = write_temp(k_dbg_imp, "x16_dbg_info_imp.dbg");
1502
		if (!ip) {
1503
			check(false, "could not write the import fixture");
1504
		} else {
1505
			check(dbg_info_load(ip) == 0,
1506
			      "merges a .dbg whose declared symbol count includes imports "
1507
			      "and out-of-range constants");
1508
			const char *f = NULL;
1509
			int         n = 0;
1510
			check(dbg_info_addr_to_source(0x0801, &f, &n) && n == 3,
1511
			      "  ...and its mappings are usable");
1512
			dbg_addr_t a = 0;
1513
			check(dbg_info_label_to_addr("_start", &a) && a == 0x0801,
1514
			      "  ...and its labels survived");
1515
			check(!dbg_info_equate_to_value("EOF", &a),
1516
			      "  ...while the out-of-range constant is counted, not kept");
1517
			remove(ip);
1518
			dbg_info_free();
1519
		}
1520
	}
1521
1522
	// ── cc65 C: high-level lines outrank the generated assembly ─────────────
1523
	// Compiling C goes hello.c -> hello.s -> ca65, and with -g ca65 records BOTH
1524
	// line infos over the same bytes: a type=1 record naming the C statement and
1525
	// a type=0 record per instruction naming the generated .s. The per-
1526
	// instruction assembly spans are always the smaller, so the "innermost span
1527
	// wins" rule that is right for assembly inverts here and would step through
1528
	// a generated file that cl65 deletes on the way out.
1529
	//
1530
	// Layout, all in CODE at $0801:
1531
	//   $0801-$080C  x16hello.c:5   over four assembly spans
1532
	//   $080D-$0814  x16hello.c:6   over two
1533
	//   $0815-$0818  x16hello.c:5   again, later (a loop's second half)
1534
	//   $0819-$081E  x16util.s:10   a macro record over an assembly one
1535
	//   $081F-$0824  x16mixed.c:7   over x16mixed.s
1536
	//   $0825-$082A  x16gen.c:9     over cl65's one-step temporary name
1537
	{
1538
		dbg_info_free();
1539
		static const char *k_dbg_cc65 =
1540
			"version\tmajor=2,minor=0\n"
1541
			"info\tcsym=0,file=7,lib=0,line=16,mod=1,scope=1,seg=1,span=16,sym=2,type=0\n"
1542
			"file\tid=0,name=\"x16hello.c\",size=200,mtime=0x00000000,mod=0\n"
1543
			"file\tid=1,name=\"x16hello.s\",size=900,mtime=0x00000000,mod=0\n"
1544
			"file\tid=2,name=\"x16util.s\",size=100,mtime=0x00000000,mod=0\n"
1545
			"file\tid=3,name=\"x16mixed.c\",size=100,mtime=0x00000000,mod=0\n"
1546
			"file\tid=4,name=\"x16mixed.s\",size=100,mtime=0x00000000,mod=0\n"
1547
			"file\tid=5,name=\"x16gen.c\",size=100,mtime=0x00000000,mod=0\n"
1548
			"file\tid=6,name=\"x16gen.c.81128.0.s\",size=900,mtime=0x00000000,mod=0\n"
1549
			"seg\tid=0,name=\"CODE\",start=0x000801,size=0x0040,addrsize=absolute,type=ro\n"
1550
			"span\tid=0,seg=0,start=0,size=12,type=0\n"
1551
			"span\tid=1,seg=0,start=0,size=3,type=0\n"
1552
			"span\tid=2,seg=0,start=3,size=2,type=0\n"
1553
			"span\tid=3,seg=0,start=5,size=3,type=0\n"
1554
			"span\tid=4,seg=0,start=8,size=4,type=0\n"
1555
			"span\tid=5,seg=0,start=12,size=8,type=0\n"
1556
			"span\tid=6,seg=0,start=12,size=4,type=0\n"
1557
			"span\tid=7,seg=0,start=16,size=4,type=0\n"
1558
			"span\tid=8,seg=0,start=20,size=4,type=0\n"
1559
			"span\tid=9,seg=0,start=20,size=4,type=0\n"
1560
			"span\tid=10,seg=0,start=24,size=6,type=0\n"
1561
			"span\tid=11,seg=0,start=24,size=2,type=0\n"
1562
			"span\tid=12,seg=0,start=30,size=6,type=0\n"
1563
			"span\tid=13,seg=0,start=30,size=3,type=0\n"
1564
			"span\tid=14,seg=0,start=36,size=6,type=0\n"
1565
			"span\tid=15,seg=0,start=36,size=3,type=0\n"
1566
			"line\tid=0,file=0,line=5,type=1,span=0\n"
1567
			"line\tid=1,file=1,line=100,span=1\n"
1568
			"line\tid=2,file=1,line=101,span=2\n"
1569
			"line\tid=3,file=1,line=102,span=3\n"
1570
			"line\tid=4,file=1,line=103,span=4\n"
1571
			"line\tid=5,file=0,line=6,type=1,span=5\n"
1572
			"line\tid=6,file=1,line=104,span=6\n"
1573
			"line\tid=7,file=1,line=105,span=7\n"
1574
			"line\tid=8,file=0,line=5,type=1,span=8\n"
1575
			"line\tid=9,file=1,line=106,span=9\n"
1576
			"line\tid=10,file=2,line=10,type=2,count=1,span=10\n"
1577
			"line\tid=11,file=2,line=20,span=11\n"
1578
			"line\tid=12,file=3,line=7,type=1,span=12\n"
1579
			"line\tid=13,file=4,line=200,span=13\n"
1580
			"line\tid=14,file=5,line=9,type=1,span=14\n"
1581
			"line\tid=15,file=6,line=300,span=15\n"
1582
			"sym\tid=0,name=\"_main\",addrsize=absolute,size=3,scope=0,def=0,val=0x000801,type=lab\n"
1583
			"sym\tid=1,name=\"_printf\",addrsize=absolute,scope=0,def=0,ref=9,type=imp,exp=6\n"
1584
			"mod\tid=0,name=\"x16hello.o\",file=0\n";
1585
1586
		// A hand-written .s that IS on disk must survive the picker filter, so
1587
		// it has to exist next to the .dbg before the load looks for it.
1588
		char *mixed_asm = write_temp("; hand-written\n", "x16mixed.s");
1589
		char *cpath     = write_temp(k_dbg_cc65, "x16_dbg_info_cc65.dbg");
1590
		if (!cpath) {
1591
			check(false, "could not write the cc65 fixture");
1592
		} else {
1593
			check(dbg_info_load(cpath) == 0, "loads a cc65 C .dbg");
1594
1595
			const char *f = NULL;
1596
			int         n = 0;
1597
1598
			// The start of a C statement, where a tiny assembly span starts too.
1599
			check(dbg_info_addr_to_source_nearest(0x0801, &f, &n),
1600
			      "maps the first address of a C statement");
1601
			check(f && strstr(f, "x16hello.c") != NULL, "  ...to the C source");
1602
			check(n == 5, "  ...to the C line");
1603
1604
			// The case an equal-start-only preference cannot reach: an interior
1605
			// instruction boundary. The C statement's span opened at $0801, so
1606
			// nothing that starts at $0804 knows about it -- and $0804 is where
1607
			// stepping actually lands.
1608
			f = NULL; n = 0;
1609
			check(dbg_info_addr_to_source_nearest(0x0804, &f, &n)
1610
			          && f && strstr(f, "x16hello.c") != NULL && n == 5,
1611
			      "an interior instruction boundary still reports the C line");
1612
1613
			// And mid-instruction, which is where a multi-byte operand puts it.
1614
			f = NULL; n = 0;
1615
			check(dbg_info_addr_to_source_nearest(0x0807, &f, &n)
1616
			          && f && strstr(f, "x16hello.c") != NULL && n == 5,
1617
			      "an address inside an instruction reports the C line");
1618
1619
			// The last byte of the statement belongs to it, not to the next.
1620
			f = NULL; n = 0;
1621
			check(dbg_info_addr_to_source_nearest(0x080C, &f, &n)
1622
			          && f && strstr(f, "x16hello.c") != NULL && n == 5,
1623
			      "the last byte of a C statement is still that statement");
1624
1625
			// Stepping on: the next statement, not a smeared-out first one.
1626
			f = NULL; n = 0;
1627
			check(dbg_info_addr_to_source_nearest(0x080D, &f, &n)
1628
			          && f && strstr(f, "x16hello.c") != NULL && n == 6,
1629
			      "the next C statement is reported at its own address");
1630
1631
			// The exact-match form feeds the DAP disassembly view, which must
1632
			// not name a file the picker hides.
1633
			f = NULL; n = 0;
1634
			check(dbg_info_addr_to_source(0x0804, &f, &n)
1635
			          && f && strstr(f, "x16hello.c") != NULL && n == 5,
1636
			      "the exact-address lookup also prefers the C line");
1637
1638
			// A C statement and an assembly span with identical bounds: the
1639
			// type has to decide, since the span size cannot.
1640
			f = NULL; n = 0;
1641
			check(dbg_info_addr_to_source_nearest(0x0815, &f, &n)
1642
			          && f && strstr(f, "x16hello.c") != NULL && n == 5,
1643
			      "a C line wins a same-size tie with the generated assembly");
1644
1645
			// type=1 > type=2 > type=0: a macro record still beats the raw
1646
			// assembly underneath it.
1647
			f = NULL; n = 0;
1648
			check(dbg_info_addr_to_source_nearest(0x0819, &f, &n)
1649
			          && f && strstr(f, "x16util.s") != NULL && n == 10,
1650
			      "a macro record outranks the assembly line inside it");
1651
1652
			// Line breakpoints. This C line generated code twice; the
1653
			// breakpoint belongs at the start of the statement.
1654
			{
1655
				dbg_addr_t a = 0;
1656
				check(dbg_info_source_to_addr("x16hello.c", 5, &a) && a == 0x0801,
1657
				      "a C line breakpoint resolves to the lowest of its spans");
1658
				a = 0;
1659
				check(dbg_info_source_to_addr("x16hello.c", 6, &a) && a == 0x080D,
1660
				      "and each statement resolves to its own start");
1661
			}
1662
1663
			// The per-instruction assembly spans are the anchored
1664
			// disassembler's alignment anchors and must stay in the map, however
1665
			// little the source view wants to display them.
1666
			check(dbg_info_is_span_start(0x0804), "keeps an assembly span start as an anchor");
1667
			check(dbg_info_is_span_start(0x0809), "keeps the later ones too");
1668
			check(!dbg_info_is_span_start(0x0807), "and still rejects a mid-span address");
1669
1670
			// The picker offers what the user wrote. x16hello.s is generated and
1671
			// gone; x16util.s has no C source it could have been generated from;
1672
			// x16mixed.s is paired with a C file but is on disk, so somebody
1673
			// wanted it.
1674
			{
1675
				int  fc      = dbg_info_file_count();
1676
				bool has_c   = false, has_gen = false, has_util = false;
1677
				bool has_mixed_c = false, has_mixed_s = false;
1678
				bool has_gen_c = false, has_gen_tmp = false;
1679
				for (int i = 0; i < fc; i++) {
1680
					const char *nm = NULL;
1681
					if (!dbg_info_file_at(i, &nm) || !nm)
1682
						continue;
1683
					if (!strcmp(nm, "x16hello.c")) has_c = true;
1684
					if (!strcmp(nm, "x16hello.s")) has_gen = true;
1685
					if (!strcmp(nm, "x16util.s")) has_util = true;
1686
					if (!strcmp(nm, "x16mixed.c")) has_mixed_c = true;
1687
					if (!strcmp(nm, "x16mixed.s")) has_mixed_s = true;
1688
					if (!strcmp(nm, "x16gen.c")) has_gen_c = true;
1689
					if (!strcmp(nm, "x16gen.c.81128.0.s")) has_gen_tmp = true;
1690
				}
1691
				check(fc == 5, "the picker drops only the generated intermediates");
1692
				check(has_c, "  ...keeps the C source");
1693
				check(!has_gen, "  ...hides the .s cc65 generated and deleted");
1694
				check(has_util, "  ...keeps a .s with no C source behind it");
1695
				check(has_mixed_c && has_mixed_s,
1696
				      "  ...keeps a hand-written .s that is on disk");
1697
				// cl65 doing compile-and-assemble in one step names the
1698
				// intermediate after the whole C file plus a temporary suffix,
1699
				// which shares no stem with it. This is what a real
1700
				// `cl65 -t cx16 -g` build produces.
1701
				check(has_gen_c, "  ...keeps the C source of a one-step build");
1702
				check(!has_gen_tmp, "  ...hides cl65's temporary intermediate");
1703
1704
				// Indices stay dense: a caller walking 0..count-1 must not hit
1705
				// a gap where the hidden file used to be.
1706
				const char *nm = NULL;
1707
				check(dbg_info_file_at(fc - 1, &nm) && nm, "the last visible index resolves");
1708
				check(!dbg_info_file_at(fc, &nm), "and one past the end does not");
1709
			}
1710
1711
			// Unloading the C module takes the high-level info with it, so
1712
			// nothing stays hidden on account of a module that has gone.
1713
			dbg_info_unload_range(0x0801, 0x0840);
1714
			check(dbg_info_file_count() == 7,
1715
			      "unloading the C module puts every file back on offer");
1716
1717
			remove(cpath);
1718
			if (mixed_asm)
1719
				remove(mixed_asm);
1720
			dbg_info_free();
1721
		}
1722
	}
1723
1724
	// An assembly-only .dbg must be untouched by all of the above: no high-level
1725
	// records, so nothing is preferred and nothing is hidden. cc65's own runtime
1726
	// files (cbm/loadaddr.s and friends) are exactly the ".s referenced only by
1727
	// assembly records and not on disk" shape the filter looks for, and they
1728
	// must keep showing up for an assembly project.
1729
	{
1730
		dbg_info_free();
1731
		char *apath = write_temp(k_dbg, "x16_dbg_info_asmonly.dbg");
1732
		if (!apath) {
1733
			check(false, "could not write the assembly-only fixture");
1734
		} else {
1735
			check(dbg_info_load(apath) == 0, "reloads the assembly-only fixture");
1736
			check(dbg_info_file_count() == 2,
1737
			      "an assembly-only .dbg still offers every file it names");
1738
			const char *f = NULL;
1739
			int         n = 0;
1740
			check(dbg_info_addr_to_source_nearest(0x0805, &f, &n) && n == 10,
1741
			      "and still resolves by innermost span");
1742
			remove(apath);
1743
			dbg_info_free();
1744
		}
1745
	}
1746
1747
	// ── A refused load must not speak for the ones that succeeded ───────────
1748
	// A .dbg caught mid-write is refused, but its records are already in the
1749
	// tables and the address map is rebuilt on the way out. If deciding
1750
	// "this program is written in C" happened there, a half-written C .dbg --
1751
	// which is exactly what the auto-load path finds while cl65 is still
1752
	// writing -- would switch every already-loaded assembly module over to the
1753
	// C lookup, for good: the refused records stay behind, so every later
1754
	// rebuild would keep agreeing with it.
1755
	{
1756
		dbg_info_free();
1757
		// An assembly module with two overlapping spans opening at different
1758
		// addresses, so which one gets reported depends on the lookup taken.
1759
		static const char *k_dbg_asm_two =
1760
			"version\tmajor=2,minor=0\n"
1761
			"file\tid=0,name=\"two.s\",size=100,mtime=0x00000000,mod=0\n"
1762
			"seg\tid=0,name=\"CODE\",start=0x000801,size=0x0020,addrsize=absolute,type=ro\n"
1763
			"span\tid=0,seg=0,start=0,size=8,type=0\n"
1764
			"span\tid=1,seg=0,start=3,size=17,type=0\n"
1765
			"line\tid=0,file=0,line=10,span=0\n"
1766
			"line\tid=1,file=0,line=11,span=1\n"
1767
			"mod\tid=0,name=\"two.o\",file=0\n";
1768
		// A C module cut off mid-write: its `info` line promises two symbols
1769
		// and only one arrived.
1770
		static const char *k_dbg_c_cut =
1771
			"version\tmajor=2,minor=0\n"
1772
			"info\tcsym=0,file=1,lib=0,line=1,mod=1,scope=1,seg=1,span=1,sym=2,type=0\n"
1773
			"file\tid=0,name=\"cut.c\",size=100,mtime=0x00000000,mod=0\n"
1774
			"seg\tid=0,name=\"OTHER\",start=0x002000,size=0x0010,addrsize=absolute,type=ro\n"
1775
			"span\tid=0,seg=0,start=0,size=16,type=0\n"
1776
			"line\tid=0,file=0,line=3,type=1,span=0\n"
1777
			"sym\tid=0,name=\"_cut\",addrsize=absolute,size=3,scope=0,def=0,val=0x002000,type=lab\n";
1778
1779
		char *apath = write_temp(k_dbg_asm_two, "x16_dbg_info_two.dbg");
1780
		char *cpath = write_temp(k_dbg_c_cut, "x16_dbg_info_cut.dbg");
1781
		if (!apath || !cpath) {
1782
			check(false, "could not write the refused-load fixtures");
1783
		} else {
1784
			check(dbg_info_load(apath) == 0, "loads an assembly module");
1785
			const char *f = NULL;
1786
			int         before = 0, after = 0;
1787
			check(dbg_info_addr_to_source_nearest(0x0806, &f, &before),
1788
			      "  ...and resolves an address inside two overlapping spans");
1789
1790
			check(dbg_info_load(cpath) != 0, "refuses a half-written C .dbg");
1791
1792
			f = NULL;
1793
			check(dbg_info_addr_to_source_nearest(0x0806, &f, &after),
1794
			      "the assembly module still resolves afterwards");
1795
			check(before == after,
1796
			      "  ...to the same line: a refused load does not change how "
1797
			      "another module is read");
1798
			check(dbg_info_file_count() == 2,
1799
			      "  ...and hides nothing on a refused load's say-so");
1800
1801
			remove(apath);
1802
			remove(cpath);
1803
			dbg_info_free();
1804
		}
1805
	}
1806
1807
	printf("\n%s (%d failure%s)\n", g_fails ? "FAILED" : "PASSED", g_fails,
1808
	       g_fails == 1 ? "" : "s");
1809
	return g_fails ? 1 : 0;
1810
}