Opened 14 months ago

Last modified 14 months ago

#9280 new bug

GHCi crash: illegal text-relocation to _ in _ from _ in _ for architecture x86_64; relocation R_X86_64_PC32 against undefined symbol _ can not be used when making a shared object

Reported by: mietek Owned by:
Priority: normal Milestone:
Component: GHCi Version: 7.8.2
Keywords: crash, dynamic linking Cc: hvr
Operating System: Unknown/Multiple Architecture: x86_64 (amd64)
Type of failure: GHCi crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

Given a statically-linked Haskell object file, interactive GHCi 7.8.2 crashes.

$ cat >Foo.hs <<EOF
module Foo where

foo :: IO ()
foo = print "Foo"
EOF

On OS X:

$ ghc -Wall -c -o Foo.o Foo.hs
$ ghci -v Foo.o
GHCi, version 7.8.2: http://www.haskell.org/ghc/  :? for help
Glasgow Haskell Compiler, Version 7.8.2, stage 2 booted by GHC version 7.6.3
Using binary package database: /opt/ghc-7.8.2/lib/ghc-7.8.2/package.conf.d/package.cache
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-948744e1f99cc8bcc7c7d3ba60c7c2d8
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-dc47f6b546fc171f67a7f7d311684a99
wired-in package base mapped to base-4.7.0.0-a333addb6892f3cc2e6baa5ec782bd04
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-ed6ecfb467e6936688bb20f968f702e1
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
*** gcc:
/usr/bin/gcc -m64 -fno-stack-protector -DTABLES_NEXT_TO_CODE -L/opt/ghc-7.8.2/lib/ghc-7.8.2/base-4.7.0.0 --print-file-name libiconv.dylib
Loading package base ... linking ... done.
Loading object (static) Foo.o ... Created temporary directory: /var/folders/26/0tzj1txn0vb_0061l4z4rsmr0000gn/T/ghc41409_0
*** Linker:
/usr/bin/gcc -m64 -fno-stack-protector -DTABLES_NEXT_TO_CODE -m64 -dynamiclib -o /var/folders/26/0tzj1txn0vb_0061l4z4rsmr0000gn/T/ghc41409_0/ghc41409_1.dylib Foo.o -undefined dynamic_lookup -single_module -install_name '@rpath/ghc41409_1.dylib' -L/opt/ghc-7.8.2/lib/ghc-7.8.2/base-4.7.0.0 -Wl,-rpath -Wl,/opt/ghc-7.8.2/lib/ghc-7.8.2/base-4.7.0.0 -L/opt/ghc-7.8.2/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -Wl,-rpath -Wl,/opt/ghc-7.8.2/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -L/opt/ghc-7.8.2/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -Wl,-rpath -Wl,/opt/ghc-7.8.2/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -L/opt/ghc-7.8.2/lib/ghc-7.8.2/rts-1.0 -Wl,-rpath -Wl,/opt/ghc-7.8.2/lib/ghc-7.8.2/rts-1.0 -lHSbase-4.7.0.0-ghc7.8.2 -lHSinteger-gmp-0.5.1.0-ghc7.8.2 -lHSghc-prim-0.3.1.0-ghc7.8.2 -liconv
ld: illegal text-relocation to '_ghczmprim_GHCziCString_unpackCStringzh_closure' in /opt/ghc-7.8.2/lib/ghc-7.8.2/ghc-prim-0.3.1.0/libHSghc-prim-0.3.1.0-ghc7.8.2.dylib from '_sVK_info' in Foo.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** Deleting temp files:
Deleting: /var/folders/26/0tzj1txn0vb_0061l4z4rsmr0000gn/T/ghc41409_0/ghc41409_1.dylib
Warning: deleting non-existent /var/folders/26/0tzj1txn0vb_0061l4z4rsmr0000gn/T/ghc41409_0/ghc41409_1.dylib
*** Deleting temp dirs:
Deleting: /var/folders/26/0tzj1txn0vb_0061l4z4rsmr0000gn/T/ghc41409_0

On Linux, without -fPIC:

$ ghc -Wall -c -o Foo.o Foo.hs
ghci -v Foo.o
GHCi, version 7.8.2: http://www.haskell.org/ghc/  :? for help
Glasgow Haskell Compiler, Version 7.8.2, stage 2 booted by GHC version 7.6.3
Using binary package database: /app/.halcyon/ghc/lib/ghc-7.8.2/package.conf.d/package.cache
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-948744e1f99cc8bcc7c7d3ba60c7c2d8
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-dc47f6b546fc171f67a7f7d311684a99
wired-in package base mapped to base-4.7.0.0-018311399e3b6350d5be3a16b144df9b
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-dcc8c210fb02937e104bc1784d7b0f06
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
Loading package ghc-prim ... linking ... done.
*** gcc:
/usr/bin/gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -L/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 --print-file-name libgmp.so
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading object (static) Foo.o ... Created temporary directory: /tmp/ghc7203_0
*** Linker:
/usr/bin/gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -o /tmp/ghc7203_0/ghc7203_1.so Foo.o -shared -Wl,-Bsymbolic -Wl,-h,ghc7203_1.so -L/app/.halcyon/ghc/lib/ghc-7.8.2/base-4.7.0.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/base-4.7.0.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/rts-1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/rts-1.0 -lHSbase-4.7.0.0-ghc7.8.2 -lHSinteger-gmp-0.5.1.0-ghc7.8.2 -lHSghc-prim-0.3.1.0-ghc7.8.2 -lgmp '-Wl,--hash-size=31' -Wl,--reduce-memory-overheads
/usr/bin/ld: Foo.o: relocation R_X86_64_32S against `stg_bh_upd_frame_info' can not be used when making a shared object; recompile with -fPIC
Foo.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
*** Deleting temp files:
Deleting: /tmp/ghc7203_0/ghc7203_1.so
Warning: deleting non-existent /tmp/ghc7203_0/ghc7203_1.so
*** Deleting temp dirs:
Deleting: /tmp/ghc7203_0

On Linux, with -fPIC:

$ ghc -Wall -c -fPIC -o Foo.o Foo.hs
$ ghci -v Foo.o
GHCi, version 7.8.2: http://www.haskell.org/ghc/  :? for help
Glasgow Haskell Compiler, Version 7.8.2, stage 2 booted by GHC version 7.6.3
Using binary package database: /app/.halcyon/ghc/lib/ghc-7.8.2/package.conf.d/package.cache
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-948744e1f99cc8bcc7c7d3ba60c7c2d8
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-dc47f6b546fc171f67a7f7d311684a99
wired-in package base mapped to base-4.7.0.0-018311399e3b6350d5be3a16b144df9b
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-dcc8c210fb02937e104bc1784d7b0f06
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
Loading package ghc-prim ... linking ... done.
*** gcc:
/usr/bin/gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -L/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 --print-file-name libgmp.so
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading object (static) Foo.o ... Created temporary directory: /tmp/ghc7223_0
*** Linker:
/usr/bin/gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -o /tmp/ghc7223_0/ghc7223_1.so Foo.o -shared -Wl,-Bsymbolic -Wl,-h,ghc7223_1.so -L/app/.halcyon/ghc/lib/ghc-7.8.2/base-4.7.0.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/base-4.7.0.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/integer-gmp-0.5.1.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/ghc-prim-0.3.1.0 -L/app/.halcyon/ghc/lib/ghc-7.8.2/rts-1.0 -Wl,-rpath -Wl,/app/.halcyon/ghc/lib/ghc-7.8.2/rts-1.0 -lHSbase-4.7.0.0-ghc7.8.2 -lHSinteger-gmp-0.5.1.0-ghc7.8.2 -lHSghc-prim-0.3.1.0-ghc7.8.2 -lgmp '-Wl,--hash-size=31' -Wl,--reduce-memory-overheads
/usr/bin/ld: Foo.o: relocation R_X86_64_PC32 against undefined symbol `ghczmprim_GHCziCString_unpackCStringzh_closure' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
*** Deleting temp files:
Deleting: /tmp/ghc7223_0/ghc7223_1.so
Warning: deleting non-existent /tmp/ghc7223_0/ghc7223_1.so
*** Deleting temp dirs:
Deleting: /tmp/ghc7223_0

Change History (4)

comment:1 Changed 14 months ago by mietek

  • Summary changed from GHCi crash: to GHCi crash: illegal text-relocation

comment:2 Changed 14 months ago by mietek

  • Summary changed from GHCi crash: illegal text-relocation to GHCi crash: illegal text-relocation; relocation R_X86_64_PC32 against undefined symbol _ can not be used when making a shared object

comment:3 Changed 14 months ago by mietek

As discussed on IRC with rwbarton, it's questionable whether this is an issue besides "you're not supposed to do this".

On one hand, GHCi claims it can load object files, and GHCi 7.6.3 does not crash when fed with an .o file produced by itself.

On the other hand, feeding an .o file to GHCi 7.6.3 is not useful, as there is apparently no way to bring the functions declared in the corresponding .hi file into scope, in the absence of an .hs file.

I see two solutions — either adjust user expectations, or allow feeding GHCi explicitly with .o/.hi pairs.

Last edited 14 months ago by mietek (previous) (diff)

comment:4 Changed 14 months ago by mietek

  • Summary changed from GHCi crash: illegal text-relocation; relocation R_X86_64_PC32 against undefined symbol _ can not be used when making a shared object to GHCi crash: illegal text-relocation to _ in _ from _ in _ for architecture x86_64; relocation R_X86_64_PC32 against undefined symbol _ can not be used when making a shared object
Note: See TracTickets for help on using tickets.