anchor.diff:            (gethash "T" *symbol-table*) t-value
             *undefined-function-address* undef-fn)))
   
  -(defun write-image (name description)
  -  (declare (ignore description))
  -  (with-open-file (s (format nil "~A.image" name)
  +(alexandria:define-constant +kboot-note-name+ #(#x4B #x42 #x6F #x6F #x74 #x00) ; "KBoot\0"
  +  :test 'equalp)
  +(defconstant +kboot-itag-image+ 0)
  +(defconstant +kboot-itag-image-length+ 8)
  +(defconstant +kboot-image-version+ 1)
  +(defconstant +kboot-image-sections+ (ash 1 0))
  +(defconstant +kboot-image-log+ (ash 1 1))
  +(defconstant +kboot-itag-load+ 1)
  +(defconstant +kboot-itag-load-length+ 48)
  +(defconstant +kboot-load-fixed+ (ash 1 0))
  +(defconstant +kboot-itag-option+ 2)
  +(defconstant +kboot-itag-option-length+ 13)
  +(defconstant +kboot-option-boolean+ 0)
  +(defconstant +kboot-option-string+ 1)
  +(defconstant +kboot-option-integer+ 2)
  +(defconstant +kboot-itag-mapping+ 3)
  +(defconstant +kboot-itag-mapping-length+ 24)
  +(defconstant +kboot-itag-video+ 4)
  +(defconstant +kboot-itag-video-length+ 13)
  +(defconstant +kboot-video-vga+ (ash 1 0))
  +(defconstant +kboot-video-lfb+ (ash 1 1))
  +
  +;;; ELF64 ident field.
  +(alexandria:define-constant +elf64-ident+ #(#x7F #x45 #x4C #x46 ; Magic, "\x7FELF".
  +                                            #x02                ; 64-bit class.
  +                                            #x01                ; LSB data.
  +                                            #x01                ; ELF v1.
  +                                            #xFF                ; OS ABI, standalone.
  +                                            #x00                ; ABI version.
  +                                            ;; Padding.
  +                                            #x00 #x00 #x00 #x00 #x00 #x00 #x00)
  +  :test 'equalp
  +  :documentation "ELF e_ident field.")
  +
  +(defconstant +elf-header-size+ 64 "Size of ELF header in octets.")
  +(defconstant +elf-e_ident+ 0 "Offset of e_ident field.")
  +(defconstant +elf-e_type+ 16 "Offset of e_type field.")
  +(defconstant +elf-e_machine+ 18 "Offset of e_machine field.")
  +(defconstant +elf-e_version+ 20 "Offset of e_version field.")
  +(defconstant +elf-e_entry+ 24 "Offset of e_entry field.")
  +(defconstant +elf-e_phoff+ 32 "Offset of e_phoff field.")
  +(defconstant +elf-e_shoff+ 40 "Offset of e_shoff field.")
  +(defconstant +elf-e_flags+ 48 "Offset of e_flags field.")
  +(defconstant +elf-e_ehsize+ 52 "Offset of e_ehsize field.")
  +(defconstant +elf-e_phentsize+ 54 "Offset of e_phentsize field.")
  +(defconstant +elf-e_phnum+ 56 "Offset of e_phnum field.")
  +(defconstant +elf-e_shentsize+ 58 "Offset of e_shentsize field.")
  +(defconstant +elf-e_shnum+ 60 "Offset of e_shnum field.")
  +(defconstant +elf-e_shstrndx+ 62 "Offset of e_shstrndx field.")
  +(defconstant +elf-type-exec+ 2 "ELF e_type field.")
  +(defconstant +elf-machine-x86-64+ 62 "ELF e_machine field.")
  +(defconstant +elf-version+ 1 "ELF e_version field.")
  +
  +(defconstant +elf-phdr-size+ 56 "Size of one ELF program header in octets.")
  +(defconstant +elf-p_type+ 0 "Offset of p_type field.")
  +(defconstant +elf-p_flags+ 4 "Offset of p_flags field.")
  +(defconstant +elf-p_offset+ 8 "Offset of p_offset field.")
  +(defconstant +elf-p_vaddr+ 16 "Offset of p_vaddr field.")
  +(defconstant +elf-p_paddr+ 24 "Offset of p_paddr field.")
  +(defconstant +elf-p_filesz+ 32 "Offset of p_filesz field.")
  +(defconstant +elf-p_memsz+ 40 "Offset of p_memsz field.")
  +(defconstant +elf-p_align+ 48 "Offset of p_align field.")
  +
  +(defconstant +elf-pt-null+ 0)
  +(defconstant +elf-pt-load+ 1)
  +(defconstant +elf-pt-note+ 4)
  +
  +(defun round-up (n boundary)
  +  (if (zerop (rem n boundary))
  +      n
  +      (+ n boundary (- (rem n boundary)))))
  +
  +(defun elf-note (name type data)
  +  "Construct an ELF note."
  +  ;; +0  4B Length of name.
  +  ;; +4  4B Length of data.
  +  ;; +8  4B Type
  +  ;; +12 x  Name, padded up to 4 bytes.
  +  ;; +12+x  Data, padded up to 4 bytes.
  +  (let* ((len (+ 4 4 4 (round-up (length name) 4) (round-up (length data) 4)))
  +         (vec (make-array len
  +                          :element-type '(unsigned-byte 8)
  +                          :initial-element 0)))
  +    (setf (ub32ref/le vec 0) (length name)
  +          (ub32ref/le vec 4) (length data)
  +          (ub32ref/le vec 8) type)
  +    (replace vec name :start1 12)
  +    (replace vec data :start1 (+ 12 (round-up (length name) 4)))
  +    vec))
  +
  +(defun kboot-itag-image ()
  +  (let ((vec (make-array +kboot-itag-image-length+
  +                         :element-type '(unsigned-byte 8)
  +                         :initial-element 0)))
  +    (setf (ub32ref/le vec 0) +kboot-image-version+
  +          (ub32ref/le vec 4) (logior +kboot-image-log+))
  +    (elf-note +kboot-note-name+ +kboot-itag-image+ vec)))
  +
  +(defun kboot-itag-load (physical)
  +  (let ((vec (make-array +kboot-itag-load-length+
  +                         :element-type '(unsigned-byte 8)
  +                         :initial-element 0)))
  +    (setf (ub32ref/le vec 0) (logior +kboot-load-fixed+)
  +          (ub32ref/le vec 24) physical)
  +    (elf-note +kboot-note-name+ +kboot-itag-load+ vec)))
  +
  +(defun kboot-itag-video ()
  +  (let ((vec (make-array +kboot-itag-load-length+
  +                         :element-type '(unsigned-byte 8)
  +                         :initial-element 0)))
  +    (setf (ub32ref/le vec 0) (logior +kboot-video-lfb+ +kboot-video-vga+))
  +    (elf-note +kboot-note-name+ +kboot-itag-video+ vec)))
  +
  +(defun make-elf-header (kboot-entry n-phdrs)
  +  (let ((elf-header (make-array +elf-header-size+
  +                                :element-type '(unsigned-byte 8)
  +                                :initial-element 0)))
  +    (replace elf-header +elf64-ident+)
  +    (setf (ub16ref/le elf-header +elf-e_type+) +elf-type-exec+
  +          (ub16ref/le elf-header +elf-e_machine+) +elf-machine-x86-64+
  +          (ub32ref/le elf-header +elf-e_version+) +elf-version+
  +          (ub64ref/le elf-header +elf-e_entry+) kboot-entry
  +          ;; Program headers come after the ELF header.
  +          (ub64ref/le elf-header +elf-e_phoff+) +elf-header-size+
  +          ;; No section headers.
  +          (ub64ref/le elf-header +elf-e_shoff+) 0
  +          (ub32ref/le elf-header +elf-e_flags+) 0
  +          (ub16ref/le elf-header +elf-e_ehsize+) +elf-header-size+
  +          (ub16ref/le elf-header +elf-e_phentsize+) +elf-phdr-size+
  +          (ub16ref/le elf-header +elf-e_phnum+) n-phdrs
  +          (ub16ref/le elf-header +elf-e_shentsize+) 0
  +          (ub16ref/le elf-header +elf-e_shnum+) 0
  +          (ub16ref/le elf-header +elf-e_shstrndx+) 0)
  +    elf-header))
  +
  +(defun make-elf-phdr (type &key (offset 0) (vaddr 0) (paddr vaddr) (filesz 0) (memsz filesz))
  +  (let ((phdr (make-array +elf-phdr-size+
  +                          :element-type '(unsigned-byte 8)
  +                          :initial-element 0)))
  +    (setf (ub32ref/le phdr +elf-p_type+) type
  +          (ub32ref/le phdr +elf-p_flags+) #b111 ; RWX
  +          (ub64ref/le phdr +elf-p_offset+) offset
  +          (ub64ref/le phdr +elf-p_vaddr+) vaddr
  +          (ub64ref/le phdr +elf-p_paddr+) paddr
  +          (ub64ref/le phdr +elf-p_filesz+) filesz
  +          (ub64ref/le phdr +elf-p_memsz+) memsz
  +          (ub64ref/le phdr +elf-p_align+) 4)
  +    phdr))
  +
  +(defun write-kboot-header (stream kboot-entry)
  +  (let ((notes '())
  +        n-phdrs
  +        file-offset)
  +    ;; Add notes.
  +    (push (list (kboot-itag-image) 0) notes)
  +    (push (list (kboot-itag-load *physical-load-address*) 0) notes)
  +    (push (list (kboot-itag-video) 0) notes)
  +    ;; LOAD phdr plus NOTE phdrs.
  +    (setf n-phdrs (+ 1 (length notes)))
  +    ;; Compute note file offsets.
  +    (setf file-offset (+ +elf-header-size+ (* n-phdrs +elf-phdr-size+)))
  +    (dolist (n notes)
  +      (setf (second n) file-offset)
  +      (incf file-offset (length (first n))))
  +    ;; Write out the ELF header.
  +    (write-sequence (make-elf-header kboot-entry n-phdrs) stream)
  +    ;; Write program headers.
  +    (write-sequence (make-elf-phdr +elf-pt-load+
  +                                   :offset file-offset
  +                                   :vaddr *physical-load-address*
  +                                   :filesz (image-data-size)
  +                                   :memsz (total-image-size))
  +                    stream)
  +    (dolist (n notes)
  +      (write-sequence (make-elf-phdr +elf-pt-note+
  +                                     :offset (second n)
  +                                     :filesz (length (first n)))
  +                      stream))
  +    ;; Write notes.
  +    (dolist (n notes)
  +      (write-sequence (first n) stream))))
  +
  +(defun write-image (name kboot)
  +  (with-open-file (s (make-pathname :type "image" :defaults name)
                        :direction :output
                        :element-type '(unsigned-byte 8)
                        :if-exists :supersede)
  +    (when kboot (write-kboot-header s kboot))
       ;; Must match the page-table code!
       ;; First comes the first 2MB of the static area.
       (write-sequence *static-data* s :end #x200000)
  
positive.code:             *static-area* *static-area-hint*
              *unifont-bmp*
              *unicode-info* *unicode-name-store*
              *unicode-encoding-table* *unicode-name-trie*))
      (setf (cold-symbol-value '*undefined-function-thunk*) (make-value *undefined-function-address* +tag-function+))
      (generate-string-array cl-symbol-names '*cl-symbols*)
      (generate-string-array system-symbol-names '*system-symbols*)
  
negative.code:             (word (+ (truncate *static-area-base* 8) *static-offset* -1)) #b100))
      (apply-fixups *pending-fixups*)
      (write-map-file image-name *function-map*)
      (write-image image-name (when kboot (make-value kboot-entry-fn +tag-function+)))))
  
  (defun load-source-files (files set-fdefinitions)
    (mapc (lambda (f) (load-source-file f set-fdefinitions)) files))
  