Linking FreeBSD with lld: -dc option missing

Ed Maste via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 16 08:57:08 PST 2016


I'm now looking in more detail at the final two issues preventing us
from fully using lld for building FreeBSD userland and kernel. The
issues have been discussed in sub-threads in the past, but as we're
very close to building FreeBSD with lld "out of the box" I'm focusing
on final resolutions for them. I need to either have lld gain the last
pieces of missing functionality, or commit to a permanent change in
FreeBSD.

One of the outstanding issues is the -d/-dc/-dp option, which converts
common symbols into allocated .bss symbols with -r linking. The option
is used by FreeBSD's rescue utility, which is a Busybox-like tool that
combines many binaries into one - e.g., cat, ls, sh, etc., and is
probably best explained by the comment in the crunchide tool used in
the build process:

 * crunchide.c - tiptoes through a symbol table, hiding all defined
 *      global symbols.  Allows the user to supply a "keep list" of symbols
 *      that are not to be hidden.  This program relies on the use of the
 *      linker's -dc flag to actually put global bss data into the file's
 *      bss segment (rather than leaving it as undefined "common" data).
 *
 *      The point of all this is to allow multiple programs to be linked
 *      together without getting multiple-defined errors.
 *
 *      For example, consider a program "foo.c".  It can be linked with a
 *      small stub routine, called "foostub.c", eg:
 *          int foo_main(int argc, char **argv){ return main(argc, argv); }
 *      like so:
 *          cc -c foo.c foostub.c
 *          ld -dc -r foo.o foostub.o -o foo.combined.o
 *          crunchide -k _foo_main foo.combined.o
 *      at this point, foo.combined.o can be linked with another program
 *      and invoked with "foo_main(argc, argv)".  foo's main() and any
 *      other globals are hidden and will not conflict with other symbols.

In order to make this work with lld I need to do one of three things:

1) compile the object files with -fno-common
2) give crunchide some simple linking ability, so that it can convert
common symbols into allocated .bss symbols
3) add -dc support to lld

Rafael previously suggested #1. It's feasible, but makes the crunch
build system slightly less portable (in that it couldn't be used with
other software that relies on common symbol behaviour). It also
requires that we build a second set of objects for the rescue build,
if -fno-common isn't applied to the "normal" build of the tools.

#2 seems like more effort than is sensible.

I have a slight preference for #3, and I think will be a relatively
small change in lld. However, a mitigating factor is that I've had
trouble finding any other users of this option. I did find (through
Debian's code search) that vim and emacs include
FORCE_COMMON_ALLOCATION, which is the linker script option
corresponding to -dc. It wasn't immediately clear though if it's
actually used in the build on operating systems of interest.

Rafael/Rui, do you think it's reasonable to add -dc to lld?

-Ed


More information about the llvm-commits mailing list