Rev 14 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
# QNX 8.0 `gcc` linker driver drop-in replacement
```
$ uname -a
QNX XPS64-df54 8.0.0 2024/06/13-12:41:48EDT x86pc x86_64
$ gcc hello.c
$ ./a.out
Hello world!
```
## Rationale
Until a proper QNXToolchain class is implemented in the Clang driver, Clang relies on GNU GCC to drive the system linker when it hasn't enough knowledge of the underlying platform.
So, if we are to provide a LLVM/Clang-based native toolchain for QNX, we *HAVE* to provide a `gcc` executable for Clang to be able to link QNX files.
This is an either/or situation and it was decided that reimplementing an external linker driver would be faster than doing it in the LLVM codebase (mostly because of the very long iteration times that rebuilding LLVM causes, even when using a compiler cache.)
### Why would Clang chain-call `gcc` with the intent of linking?
Because GNU GCC is itself a 'driver' which can also invoke either a compiler or a linker (`gcc` itself invokes `cc1` or `cc1plus`, the actual compiler, depending on the language, then runs `collect2` which in turn invokes `ld`, the UNIX linker).
This makes GCC the unique entrypoint for any platform- and target-specific compilation and linking task, and foreign compilers such as Clang just leverage that feature when support of the target system isn't built-in.
This implies a drop-in GCC replacement needs to either chain-call `clang` when it is called with the intent of *compiling* and chain-call `ld` when it is called with the intent of *linking*.
This can be decided by looking at its command-line arguments.
### How does this drop-in `gcc` replacement know how to call `ld` correctly?
Because when proper support is added to it, GNU GCC contains a 'linker specification' for the system (a string of symbols describing a sort of decision tree) which it uses to construct the linker command line.
The `ld` linker specifications for QNX 8.0 are public (because GCC is) and can be extracted from `gcc -dumpspecs` from the Win64->QNX and Linux->QNX cross-compilers provided by Blackberry, and have been reimplemented in this file.
The GNU specs language is documented [on the GNU website here](https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html).
## Driver behaviour
The following environment variables control the behaviour of the driver:
- `DEBUG_LINKER_DRIVER` - when defined to any value (including an empty string), the constructed ld command line is printed to the standard error before calling the linker.
## How to build
Build with:
qnxsdp_env
ntox86_64-gcc linkerdriver.c -o gcc
or just `make` on Win32.
--
Pierre-Marie Baty <pm@pmbaty.com>