D-Programming-Language/dmd — pull request #4140 by JinShil (2014-11-15 02:47:08+00:00)
Title: fix issue 6952 - Pass linker switches to gcc instead of to ld

This provides a fix for https://issues.dlang.org/show_bug.cgi?id=6952.

`dmd` uses `gcc` for its linker, not `ld`. Currently `dmd` appends `-Xlinker` to linker switches (i.e. switches that begin with `-L`) so they bypass `gcc` and are passed directly to `ld`.

In GCC, some options are passed transparently to `ld` (e.g. -lmylib), some are recognized only by `gcc` but not by `ld`, and some seem to have a different affect if passed to `gcc` than if passed to `ld`. (See https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html for a description of GCC's linker options).

By passing `dmd`'s linker options directly to `ld`, it prevents users from leveraging options that are only recognized by `gcc` but affect the linking process, or options that have different effect in `gcc` than in `ld`. Such switches include `-static`, `-nodefaultlibs` and `-nostdlib`. (There may be others).

This pull request modifies the way `dmd` passes linker options to `gcc`. It removes the `-Xlinker` prefix for any `dmd` switch prefixed with `-L`. If users still wish to bypass `gcc` and pass switches directly to `ld`, they can use `-L-Xlinker -L-ldswitch` to their `dmd` command. This commit modifies modifies dmd.conf to pass `--export-dynamic` directly to `ld`, since `--export-dynamic` is not recognized by `gcc`.

This change has the potential to break users' build scripts if they are depending on `ld`-specific options to be passed directly to `ld` using the `-L` prefix. But, I think this is the best way forward, as it allows users to control all aspects of the linking process. That is users can choose which options go to `gcc` and which options bypass `gcc` and go directly to `ld`. Currently, users don't have this capability.

I also looked into using `ld`, instead of `gcc`, as the linker for `dmd`, but that would involve adding quite a bit of platform specific logic to DMD. So, I think using `gcc` as the linker is appropriate.