OpenMP linker script is known to cause problems for gold and lld linkers on Linux and it will also cause problems for Windows enabling in future. This is a sufficient justification for eliminating use of OpenMP linker script and replacing it with a portable solution. This patch contains prototype changes which implement such solution.
First a brief explanation how OpenMP linker script is being used in existing implementation. OpenMP linker script is dynamically generated by the clang driver and is added to the host link command to fulfil the following tasks
- Insert device binaries into the host binary at link time as data (makes host binary fat)
- Creates pair of symbols for the start/end address for each device binary
- And creates pair of symbols with start/end addresses around the compiler generated offload entry table
All symbols that are created by the linker script are used by the offload registration code that is added by the compiler to each host object as a comdat group. This compiler generated code consists of a pair of data objects (device binary descriptor) that use those symbols as initializers and two functions. One of those functions registers device binary descriptor at OpenMP runtime at program startup and the other unregisters it. BTW, having offload registration code in each host object is not good because it makes host object dependent on a particular list of targets (device binary descriptor depends on the offload targets).
This patch implements an alternative solution for the above tasks. Device binaries are inserted into the host binary with a help of the wrapper bit-code file which contains device binaries as data as well as the offload registration code for registering device binaries in offload runtime (tasks 1 and 2 in the above list). Wrapper bit-code file is dynamically created by the clang driver with a help of new tool clang-offload-wrapper which takes device binaries as input and produces bit-code file with required contents. Wrapper bit-code is then compiled to an object and resulting object is appended to the host linking by the clang driver.
Start/end symbols around the offload entry table (3 in the list above) are added with a help of a new pair of object files omptargetbegin.o/omptargetend.o that define those symbols in appropriate section. These object files are added before/after user's objects and libraries by the clang driver similar to the other begin/end objects like crti.o/crtn.o or crtbegin.o/crtend.o.
This is a full patch with all required changes. I will split is into smaller pieces for upstreaming.