Opened 7 years ago

Last modified 2 years ago

#5197 new feature request

Support static linker semantics for archives and weak symbols

Reported by: duncan Owned by:
Priority: low Milestone:
Component: Runtime System (Linker) Version: 7.0.3
Keywords: Cc: nathanhowell@…, simonmar
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


While looking at #5004, I had a go at getting the GHCi linker to load the LLVM libs, as in making this work: ghci -package llvm.

This involves loading a whole bunch of LLVM*.a files and linking them against the C and C++ standard libs. This in turn requires a few more features in the GHCi linker:

  • support for weak symbols
  • search archives only on demand
  • support .oS files in archives

The last is trivial.

The first is not so hard to do. When the linker finds a definition of a weak symbol, if there's already a symbol with that name in the symbol table then we just ignore the new one. When resolving symbols if we find an unresolved weak symbol we just give it the value zero. Doing this is enough to load .e.g libstdc++.a and libc.a (rather than

The next part is a bit more work. When you give system static linker some .a files, it only uses them to provide definitions for unresolved symbols in the main target. In particular it is fine for two .a files to provide definitions of the same symbol because the linker just looks for the first. (This is in contrast to duplicate symbols in the main .o input files). On linux, both libm and libc define some of the same symbols, such as __isinf.

Similarly, there is a problem that the GHCi linker predefines the atexit symbol, but that is also defined by libc.a.

So for the GHCi linker to load both libm and libc then it has to follow the standard semantics for linking archives. Currently it treats an archive as a request to link all the .o files in that archive.

Probably it is not sensible to go as far as implementing the full standard static linking semantics in the GHCi linker. It's of somewhat questionable value since we mainly need it for linking Haskell code, not C/C++ code.

Nevertheless, if we do need this, then the attached Linker.c has a partial implementation. It does the weak symbols and .oS files in archives.

Attachments (1)

Linker.c (200.4 KB) - added by duncan 7 years ago.
Linker.c with support for ELF weak symbols and .oS archive members

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by duncan

Attachment: Linker.c added

Linker.c with support for ELF weak symbols and .oS archive members

comment:1 Changed 7 years ago by igloo

Component: GHCiRuntime System
Milestone: 7.4.1

comment:2 Changed 7 years ago by nathanhowell

Cc: nathanhowell@… added

comment:3 Changed 6 years ago by igloo

Priority: normallow

comment:4 Changed 6 years ago by igloo


comment:5 Changed 6 years ago by igloo

Blocked By: 3658 added

comment:6 Changed 4 years ago by thoughtpolice


Moving to 7.10.1.

comment:7 Changed 4 years ago by thomie

Blocked By: 3658 removed
Cc: simonmar added
difficulty: Unknown

Still a problem with 7.8.3.

$ uname -op
x86_64 GNU/Linux

$ cabal install llvm-general

$ ghci -package llvm-general
GHCi, version 7.8.3
Loading package llvm-general- ... linking ... Segmentation fault (core dumped)

comment:8 Changed 3 years ago by thoughtpolice


Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:9 Changed 3 years ago by ezyang

Component: Runtime SystemRuntime System (Linker)

comment:10 Changed 3 years ago by thoughtpolice


Milestone renamed

comment:11 Changed 2 years ago by thomie

Milestone: 8.0.1
Note: See TracTickets for help on using tickets.