Skip to content

Commit 232e40a

Browse files
author
Noam Preil
committed
Implement relocation tables in KEXC binaries
1 parent 2f65d75 commit 232e40a

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

doc/kexc

+10-9
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ good idea.
3535

3636
The following headers are defined in kernel.inc:
3737

38-
Name Description
38+
Name Description
3939

40-
KEXC_HEADER_END The end of the header list. Value may be omitted.
41-
KEXC_ENTRY_POINT Pointer to executable entry point.
42-
KEXC_STACK_SIZE Bytes of stack required, divided by two.
43-
KEXC_KERNEL_VER Minimum kernel version supported. Major, minor.
44-
KEXC_THREAD_FLAGS Thread flags. Only the upper 8 bits are considered.
45-
KEXC_NAME Pointer to program name.
46-
KEXC_DESCRIPTION Pointer to program description.
40+
KEXC_HEADER_END The end of the header list. Value may be omitted.
41+
KEXC_ENTRY_POINT Pointer to executable entry point.
42+
KEXC_STACK_SIZE Bytes of stack required, divided by two.
43+
KEXC_KERNEL_VER Minimum kernel version supported. Major, minor.
44+
KEXC_THREAD_FLAGS Thread flags. Only the upper 8 bits are considered.
45+
KEXC_NAME Pointer to program name.
46+
KEXC_DESCRIPTION Pointer to program description.
47+
KEXC_RELOCATION_TABLE Pointer to the relocation table.
4748

4849
Header keys are numbered from 0x00-0xFF, inclusive. The 0x00-0x7F range is
49-
reserved for kernel use, and 0x80-0xFF is available for arbitrary use.
50+
reserved for kernel use, and 0x80-0xFF is available for arbituary use.
5051

5152
Executable Programs
5253

include/defines.inc

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ panic_failed_init .equ 4
4242

4343
; KEXC headers
4444
; 0x00-0x7F reserved for kernel use
45-
; 0x80-0xFF available for arbituary use
45+
; 0x80-0xFF available for arbitrary use
4646
KEXC_HEADER_END .equ 0x00
4747
KEXC_ENTRY_POINT .equ 0x01
4848
KEXC_STACK_SIZE .equ 0x02
4949
KEXC_KERNEL_VER .equ 0x03
5050
KEXC_THREAD_FLAGS .equ 0x04
5151
KEXC_NAME .equ 0x05
5252
KEXC_DESCRIPTION .equ 0x06
53+
KEXC_RELOCATION_TABLE .equ 0x07
5354

5455
; Thread flags
5556
THREAD_NON_SUSPENDABLE .equ 2

src/00/thread.asm

+48-5
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ _: pop af
354354
cp a
355355
ret
356356

357+
.echo "lp: 0x{0:X4}" launchProgram
357358
;; launchProgram [Threading]
358359
;; Loads the specified file into memory as a program and starts a
359360
;; new thread for it. The file must be a valid KEXC executable.
@@ -426,12 +427,18 @@ launchProgram:
426427
.unknown_ver:
427428
; no minimum version is specified by the executable
428429
.no_minimum_ver:
430+
; Check for a relocation table
431+
ld b, KEXC_RELOCATION_TABLE
432+
push ix \ call _getThreadHeader \ pop ix
433+
call z, .relocate
434+
429435
; Grab header info
430436
ld b, KEXC_ENTRY_POINT
431437
push ix \ call _getThreadHeader \ pop ix
432438
jr nz, .no_entry_point
433439
push hl
434-
ld b, KEXC_STACK_SIZE
440+
; b still has KEXC_ENTRY_POINT, and KEXC_STACK_SIZE is 1 higher
441+
inc b
435442
push ix \ call _getThreadHeader \ pop ix
436443
ld c, l ; TODO: Error out if H is nonzero?
437444
jr z, _
@@ -459,14 +466,14 @@ _: ld a, b
459466
pop bc
460467
cp a
461468
ret
462-
.kernel_too_low:
463-
ld a, errKernelMismatch
469+
.magic_error:
470+
ld a, errNoMagic
464471
jr .error
465472
.no_entry_point:
466473
ld a, errNoEntryPoint
467474
jr .error
468-
.magic_error:
469-
ld a, errNoMagic
475+
.kernel_too_low:
476+
ld a, errKernelMismatch
470477
jr .error
471478
.error_pop2:
472479
inc sp \ inc sp
@@ -484,6 +491,42 @@ _: or 1
484491
ld a, b
485492
pop bc
486493
ret
494+
; thrashes de, bc, and hl
495+
.relocate:
496+
; ix = executable address
497+
; hl = program-relative relocation table address
498+
push ix \ pop de
499+
add hl, de
500+
; hl = absolute address of relocation table
501+
.relocation_loop:
502+
ld e, (hl)
503+
inc hl
504+
ld d, (hl)
505+
; de = first entry in relocation table
506+
dec hl
507+
; hl: preserved
508+
ld bc, 0
509+
call cpBCDE
510+
ret z
511+
; de contains the program-relative address of a program-relative pointer to relocate
512+
; need to execute, in effect, `add (ix + de), ix`
513+
push ix
514+
add ix, de
515+
push ix \ pop de
516+
pop ix
517+
; de = absolute address of pointer to relocate
518+
519+
; add (de), ix
520+
push ix \ pop bc
521+
ld a, (de)
522+
add a, c
523+
ld (de), a
524+
inc de
525+
ld a, (de)
526+
add a, b
527+
ld (de), a
528+
inc hl \ inc hl
529+
jr .relocation_loop
487530

488531
;; exitThread [Threading]
489532
;; Immediately terminates the running thread.

0 commit comments

Comments
 (0)