Subject: bug#58033: PatchELF can create broken ELF binaries

From: Maxime Devos <[email-masked]>
Date: Sat, 11 Feb 2023 20:43:28 +0100

I think I've found another instance of this bug.
Unlike <https://issues.guix.gnu.org/58033#0>, I'm getting an error 
message from validate-runpath, in the form of a type error.  Regardless 
of whether PatchELF's output is correct, I think there shouldn't be any 
type errors.

$ guix build -f guix.scm
[...]
validating RUNPATH of 1 binaries in 
"/gnu/store/cn7m18zrxp3ba8dxp6hy1w8jdr0kqp43-gearhead2-0.1/bin"...
error: in phase 'validate-runpath': uncaught exception:
wrong-type-arg "struct-vtable" "Wrong type argument in position ~A 
(expecting ~A): ~S" (1 "struct" #f) (#f)
phase `validate-runpath' failed after 0.0 seconds
Backtrace:
           18 (primitive-load "/gnu/store/ygpsgmga4qsp042z5nf9av4fm3y…")
In guix/build/gnu-build-system.scm:
     906:2 17 (gnu-build #:source _ #:outputs _ #:inputs _ #:phases . #)
In ice-9/boot-9.scm:
   1752:10 16 (with-exception-handler _ _ #:unwind? _ # _)
In srfi/srfi-1.scm:
     634:9 15 (for-each #<procedure 7fffeeb52420 at guix/build/gnu-b…> …)
In ice-9/boot-9.scm:
   1752:10 14 (with-exception-handler _ _ #:unwind? _ # _)
In guix/build/gnu-build-system.scm:
    927:23 13 (_)
    567:16 12 (validate-runpath #:validate-runpath? _ # _ #:outputs _)
In guix/build/utils.scm:
    677:23 11 (loop ("/gnu/store/cn7m18zrxp3ba8dxp6hy1w8jdr0kqp43-g…") …)
    677:23 10 (loop ("/gnu/store/cn7m18zrxp3ba8dxp6hy1w8jdr0kqp43-g…") …)
In guix/build/gremlin.scm:
     355:2  9 (validate-needed-in-runpath "/gnu/store/cn7m18zrxp3ba8…" …)
In ice-9/boot-9.scm:
   1752:10  8 (with-exception-handler _ _ #:unwind? _ # _)
In guix/build/gremlin.scm:
    368:20  7 (_)
    228:20  6 (elf-dynamic-info #<<elf> bytes: #vu8(127 69 76 70 2 1 …>)
In srfi/srfi-1.scm:
    586:17  5 (map1 (#<<dynamic-entry> type: 29 value: 905 offset:…> …))
In guix/build/gremlin.scm:
    189:32  4 (interpret-dynamic-entry #<<dynamic-entry> type: 29 val…>)
    166:14  3 (vma->offset #<<elf> bytes: #vu8(127 69 76 70 2 1 1 0 …> …)
In ice-9/boot-9.scm:
   1685:16  2 (raise-exception _ #:continuable? _)
   1683:16  1 (raise-exception _ #:continuable? _)
   1685:16  0 (raise-exception _ #:continuable? _)

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure struct-vtable: Wrong type argument in position 1 (expecting 
struct): #f
builder for 
`/gnu/store/3x0bsdyczsxjhmwynvb3mgh5x75i88s4-gearhead2-0.1.drv' failed 
with exit code 1

The file "guix.scm" contains the following (remember to comment-out 
#:strip-binaries? #false) (while there is a (license #f) line, that's 
just because I didn't fill everything in yet; the software is actually 
free):

(use-modules (guix gexp) (guix packages) (guix build-system gnu) (guix 
utils) (guix hash) (gnu packages pascal)
   (gnu packages sdl) (gnu packages elf))

(define (fpc-compilation-options)
   ;; -P and -T needs to be set for cross-compilation
   #~`(#$(cond ((target-aarch64?) "-Paarch64")
               ((target-ppc32?) "-Ppowerpc")
               ((target-ppc64le?) "-Ppowerpc64")
               ((target-x86-64?) "-Px86_64")
               ((target-x86-32?) "-Pi386")
               ((target-arm32?) "-Parm")
               (#true (error "unrecognised cross-compilation target")))
       #$(cond ((target-linux?) "-Tlinux")
               ((target-hurd?) "-Tlinux") ; untested, hopefully it's 
close enough
               (#true (error "unrecognised cross-compilation target")))
       "-O2" ; do some optimisations
       ;; Let fpc find libraries.
       ,@(append-map (lambda (x)
                       (list (string-append "-Fl" x) ; fpc's equivalent 
of -L
                             (string-append "-Xr" x))) ; equivalent of 
-Wl,rpath
                     (string-split (getenv #$(if (%current-target-system)
                                                 "CROSS_LIBRARY_PATH"
                                                 "LIBRARY_PATH")) #\:))))

(package
   (name "gearhead2")
   (version "0.1") ; TODO
   (build-system gnu-build-system)
   (source (local-file "../gearhead-2" #:recursive? #true
                       #:select? (negate vcs-file?)))
   (arguments
     (list #:modules `((srfi srfi-1) ,@%gnu-build-system-modules)
           ;; #:strip-binaries? #true causes type errors in 
validate-runpath,
           ;; see <https://issues.guix.gnu.org/58033>.
           #:strip-binaries? #false ; strip-binaries? #true causes type 
errors in
           #:phases
           #~(modify-phases %standard-phases
               (delete 'configure)
               (replace 'build
                 (lambda _
                   (apply invoke "fpc" "gearhead2" 
#$(fpc-compilation-options))
                   ;; XXX: somehow the -Xr in fpc-compilation-options has no
                   ;; effect.  Work-around this.
                   (invoke "patchelf" "--set-rpath"
                           (getenv #$(if (%current-target-system)
                                         "CROSS_LIBRARY_PATH"
                                         "LIBRARY_PATH"))
                           "gearhead2")))
               (delete 'check) ; no test suite exists
               (replace 'install
                 (lambda _
                   (install-file "gearhead2" (string-append #$output 
"/bin")))))))
   (native-inputs (list patchelf fpc))
   (inputs (list sdl sdl-image sdl-ttf))
   (synopsis #f)
   (description #f)
   (home-page #f)
   (license #f))