    (error "reloc not in 32-bit units!"))
                        (bytevector-u32-native-set! buf dst
                                                    (pack-u8-s24 (logand u32 #xff)
                                                                 (ash rel -2)))))
                     (else (error "bad relocation kind" reloc)))))))
            relocs))

(define (process-labels labels)
  "Define linker symbols for the label-offset map in @var{labels}.
The offsets are expected to be expressed in words."
  (hash-map->list (lambda (label loc)
                    (make-linker-symbol label loc))
                  labels))

(define (link-text-object asm)
  "Link the .rtl-text section, swapping the endianness of the bytes if
needed."
  (let ((size (asm-pos asm)))
    (make-object asm '.rtl-text size
                 (lambda (buf)
                   (bytevector-copy! (asm-buf asm) 0 buf 0 size)
                   (unless (eq? (asm-endianness asm) (native-endianness))
                     (byte-swap/4! buf))
                   (patch-relocs! buf (asm-relocs asm) (asm-labels asm)))
                 (process-relocs (asm-relocs asm)
                                 (asm-labels asm))
                 (process-labels (asm-labels asm)))))


