1
0
mirror of https://git.savannah.gnu.org/git/guix.git synced 2026-04-06 21:20:33 +02:00

Revert "Use mmap for the elf parser, reducing memory usage."

This reverts commit 2c1fe0df11.
This commit is contained in:
Maxim Cournoyer
2025-10-30 16:19:50 +09:00
parent 9d60fdf6a2
commit 0f39db9c19
7 changed files with 164 additions and 147 deletions

View File

@@ -1,6 +1,5 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2025 Maxim Cournoyer <maxim@guixotic.coop>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -21,15 +20,12 @@
#:use-module (guix elf)
#:use-module (guix build utils)
#:use-module (guix build debug-link)
#:use-module (guix build io)
#:use-module (guix gexp)
#:use-module (guix modules)
#:use-module (guix store)
#:use-module (guix tests)
#:use-module (guix monads)
#:use-module (guix derivations)
#:use-module (gnu packages bootstrap)
#:use-module ((gnu packages guile) #:select (guile-3.0))
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-64)
@@ -44,12 +40,15 @@
(_
#f)))
(define read-elf
(compose parse-elf get-bytevector-all))
(test-begin "debug-link")
(unless %guile-executable (test-skip 1))
(test-assert "elf-debuglink, no .gnu_debuglink section"
(let ((elf (parse-elf (file->bytevector %guile-executable))))
(test-assert "elf-debuglink"
(let ((elf (call-with-input-file %guile-executable read-elf)))
(match (call-with-values (lambda () (elf-debuglink elf)) list)
((#f #f) ;no '.gnu_debuglink' section
(pk 'no-debuglink #t))
@@ -57,101 +56,95 @@
(string-suffix? ".debug" file)))))
;; Since we need %BOOTSTRAP-GCC and co., we have to skip the following tests
;; when networking is unreachable because we'd fail to download it. Since
;; using mmap to load ELF more efficiently, we also need the regular Guile
;; package, as guile-bootstrap cannot resolve dynamic symbols.
(with-external-store store
(unless (and (network-reachable?) store) (test-skip 1))
(test-assertm "elf-debuglink"
;; Check whether we can compute the CRC just like objcopy, and whether we
;; can retrieve it.
(let* ((code (plain-file "test.c" "int main () { return 42; }"))
(exp (with-imported-modules (source-module-closure
'((guix build io)
(guix build utils)
(guix build debug-link)
(guix elf)))
#~(begin
(use-modules (guix build io)
(guix build utils)
(guix build debug-link)
(guix elf)
(rnrs io ports))
;; when networking is unreachable because we'd fail to download it.
(unless (network-reachable?) (test-skip 1))
(test-assertm "elf-debuglink"
;; Check whether we can compute the CRC just like objcopy, and whether we
;; can retrieve it.
(let* ((code (plain-file "test.c" "int main () { return 42; }"))
(exp (with-imported-modules '((guix build utils)
(guix build debug-link)
(guix elf))
#~(begin
(use-modules (guix build utils)
(guix build debug-link)
(guix elf)
(rnrs io ports))
(define read-elf
(compose parse-elf file->bytevector))
(define read-elf
(compose parse-elf get-bytevector-all))
(setenv "PATH" (string-join '(#$%bootstrap-gcc
#$%bootstrap-binutils)
"/bin:" 'suffix))
(invoke "gcc" "-O0" "-g" #$code "-o" "exe")
(copy-file "exe" "exe.debug")
(invoke "strip" "--only-keep-debug" "exe.debug")
(invoke "strip" "--strip-debug" "exe")
(invoke "objcopy" "--add-gnu-debuglink=exe.debug"
"exe")
(call-with-values (lambda ()
(elf-debuglink (read-elf "exe")))
(lambda (file crc)
(call-with-output-file #$output
(lambda (port)
(let ((expected (call-with-input-file "exe.debug"
debuglink-crc32)))
(write (list file (= crc expected))
port))))))))))
(mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
(x (built-derivations (list drv))))
(call-with-input-file (derivation->output-path drv)
(lambda (port)
(return (match (read port)
(("exe.debug" #t) #t)
(x (pk 'fail x #f)))))))))
(setenv "PATH" (string-join '(#$%bootstrap-gcc
#$%bootstrap-binutils)
"/bin:" 'suffix))
(invoke "gcc" "-O0" "-g" #$code "-o" "exe")
(copy-file "exe" "exe.debug")
(invoke "strip" "--only-keep-debug" "exe.debug")
(invoke "strip" "--strip-debug" "exe")
(invoke "objcopy" "--add-gnu-debuglink=exe.debug"
"exe")
(call-with-values (lambda ()
(elf-debuglink
(call-with-input-file "exe"
read-elf)))
(lambda (file crc)
(call-with-output-file #$output
(lambda (port)
(let ((expected (call-with-input-file "exe.debug"
debuglink-crc32)))
(write (list file (= crc expected))
port))))))))))
(mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
(x (built-derivations (list drv))))
(call-with-input-file (derivation->output-path drv)
(lambda (port)
(return (match (read port)
(("exe.debug" #t) #t)
(x (pk 'fail x #f)))))))))
(unless (and (network-reachable?) store) (test-skip 1))
(test-assertm "set-debuglink-crc"
;; Check whether 'set-debuglink-crc' successfully updates the CRC.
(let* ((code (plain-file "test.c" "int main () { return 42; }"))
(debug (plain-file "exe.debug" "a"))
(exp (with-imported-modules (source-module-closure
'((guix build io)
(guix build utils)
(guix build debug-link)
(guix elf)))
#~(begin
(use-modules (guix build io)
(guix build utils)
(guix build debug-link)
(guix elf)
(rnrs io ports))
(unless (network-reachable?) (test-skip 1))
(test-assertm "set-debuglink-crc"
;; Check whether 'set-debuglink-crc' successfully updates the CRC.
(let* ((code (plain-file "test.c" "int main () { return 42; }"))
(debug (plain-file "exe.debug" "a"))
(exp (with-imported-modules '((guix build utils)
(guix build debug-link)
(guix elf))
#~(begin
(use-modules (guix build utils)
(guix build debug-link)
(guix elf)
(rnrs io ports))
(define read-elf
(compose parse-elf file->bytevector))
(define read-elf
(compose parse-elf get-bytevector-all))
(setenv "PATH" (string-join '(#$%bootstrap-gcc
#$%bootstrap-binutils)
"/bin:" 'suffix))
(invoke "gcc" "-O0" "-g" #$code "-o" "exe")
(copy-file "exe" "exe.debug")
(invoke "strip" "--only-keep-debug" "exe.debug")
(invoke "strip" "--strip-debug" "exe")
(invoke "objcopy" "--add-gnu-debuglink=exe.debug"
"exe")
(set-debuglink-crc "exe" #$debug)
(call-with-values (lambda ()
(elf-debuglink
(read-elf "exe")))
(lambda (file crc)
(call-with-output-file #$output
(lambda (port)
(write (list file crc) port)))))))))
(mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
(x (built-derivations (list drv))))
(call-with-input-file (derivation->output-path drv)
(lambda (port)
(return (match (read port)
(("exe.debug" crc)
(= crc (debuglink-crc32 (open-input-string "a"))))
(x
(pk 'fail x #f))))))))))
(setenv "PATH" (string-join '(#$%bootstrap-gcc
#$%bootstrap-binutils)
"/bin:" 'suffix))
(invoke "gcc" "-O0" "-g" #$code "-o" "exe")
(copy-file "exe" "exe.debug")
(invoke "strip" "--only-keep-debug" "exe.debug")
(invoke "strip" "--strip-debug" "exe")
(invoke "objcopy" "--add-gnu-debuglink=exe.debug"
"exe")
(set-debuglink-crc "exe" #$debug)
(call-with-values (lambda ()
(elf-debuglink
(call-with-input-file "exe"
read-elf)))
(lambda (file crc)
(call-with-output-file #$output
(lambda (port)
(write (list file crc) port)))))))))
(mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
(x (built-derivations (list drv))))
(call-with-input-file (derivation->output-path drv)
(lambda (port)
(return (match (read port)
(("exe.debug" crc)
(= crc (debuglink-crc32 (open-input-string "a"))))
(x
(pk 'fail x #f)))))))))
(test-end "debug-link")

View File

@@ -23,7 +23,6 @@
#:use-module (guix tests)
#:use-module ((guix utils) #:select (call-with-temporary-directory
target-aarch64?))
#:use-module (guix build io)
#:use-module (guix build utils)
#:use-module (guix build gremlin)
#:use-module (gnu packages bootstrap)
@@ -45,6 +44,9 @@
(_
#f)))
(define read-elf
(compose parse-elf get-bytevector-all))
(define c-compiler
(or (which "gcc") (which "cc") (which "g++")))
@@ -53,7 +55,8 @@
(unless %guile-executable (test-skip 1))
(test-assert "elf-dynamic-info-needed, executable"
(let ((dyninfo (file-dynamic-info %guile-executable)))
(let* ((elf (call-with-input-file %guile-executable read-elf))
(dyninfo (elf-dynamic-info elf)))
(or (not dyninfo) ;static executable
(lset<= string=?
(list (string-append "libguile-" (effective-version))
@@ -137,7 +140,9 @@
(display "int main () { puts(\"hello\"); }" port)))
(invoke c-compiler "t.c"
"-Wl,--enable-new-dtags" "-Wl,-rpath=/foo" "-Wl,-rpath=/bar")
(let* ((dyninfo (file-dynamic-info "a.out"))
(let* ((dyninfo (elf-dynamic-info
(parse-elf (call-with-input-file "a.out"
get-bytevector-all))))
(old (elf-dynamic-info-runpath dyninfo))
(new (strip-runpath "a.out"))
(new* (strip-runpath "a.out")))
@@ -191,7 +196,10 @@
(display "// empty file" port)))
(invoke c-compiler "t.c"
"-shared" "-Wl,-soname,libfoo.so.2")
(let ((dyninfo (file-dynamic-info "a.out")))
(elf-dynamic-info-soname dyninfo))))))
(let* ((dyninfo (elf-dynamic-info
(parse-elf (call-with-input-file "a.out"
get-bytevector-all))))
(soname (elf-dynamic-info-soname dyninfo)))
soname)))))
(test-end "gremlin")