1c5c6f187SBen Dunbobbin========================================== 2c5c6f187SBen DunbobbinHow to build Windows Itanium applications. 3c5c6f187SBen Dunbobbin========================================== 4c5c6f187SBen Dunbobbin 5c5c6f187SBen DunbobbinIntroduction 6c5c6f187SBen Dunbobbin============ 7c5c6f187SBen Dunbobbin 8c5c6f187SBen DunbobbinThis document contains information describing how to create a Windows Itanium toolchain. 9c5c6f187SBen Dunbobbin 10c5c6f187SBen DunbobbinWindows Itanium allows you to deploy Itanium C++ ABI applications on top of the MS VS CRT. 11c5c6f187SBen DunbobbinThis environment can use the Windows SDK headers directly and does not required additional 12c5c6f187SBen Dunbobbinheaders or additional runtime machinery (such as is used by mingw). 13c5c6f187SBen Dunbobbin 14c5c6f187SBen DunbobbinWindows Itanium Stack: 15c5c6f187SBen Dunbobbin 16c5c6f187SBen Dunbobbin* Uses the Itanium C++ abi. 17c5c6f187SBen Dunbobbin* libc++. 18c5c6f187SBen Dunbobbin* libc++-abi. 19c5c6f187SBen Dunbobbin* libunwind. 20c5c6f187SBen Dunbobbin* The MS VS CRT. 21c5c6f187SBen Dunbobbin* Is compatible with MS Windows SDK include headers. 22c5c6f187SBen Dunbobbin* COFF/PE file format. 23c5c6f187SBen Dunbobbin* LLD 24c5c6f187SBen Dunbobbin 25c5c6f187SBen DunbobbinNote: compiler-rt is not used. This functionality is supplied by the MS VCRT. 26c5c6f187SBen Dunbobbin 27c5c6f187SBen DunbobbinPrerequisites 28c5c6f187SBen Dunbobbin============= 29c5c6f187SBen Dunbobbin 30c5c6f187SBen Dunbobbin* The MS SDK is installed as part of MS Visual Studio. 31c5c6f187SBen Dunbobbin* Clang with support for the windows-itanium triple. 32c5c6f187SBen Dunbobbin* COFF LLD with support for the -autoimport switch. 33c5c6f187SBen Dunbobbin 34c5c6f187SBen DunbobbinKnown issues: 35c5c6f187SBen Dunbobbin============= 36c5c6f187SBen Dunbobbin 37c5c6f187SBen DunbobbinSJLJ exceptions, "-fsjlj-exceptions", are the only currently supported model. 38c5c6f187SBen Dunbobbin 39c5c6f187SBen Dunbobbinlink.exe (the MS linker) is unsuitable as it doesn't support auto-importing which 40c5c6f187SBen Dunbobbinis currently required to link correctly. However, if that limitation is removed 41c5c6f187SBen Dunbobbinthen there are no other known issues with using link.exe. 42c5c6f187SBen Dunbobbin 43c5c6f187SBen DunbobbinCurrently, there is a lack of a usable Windows compiler driver for Windows Itanium. 44c5c6f187SBen DunbobbinA reasonable work-around is to build clang with a windows-msvc default target and 45c5c6f187SBen Dunbobbinthen override the triple with e.g. "-Xclang -triple -Xclang x86_64-unknown-windows-itanium". 46c5c6f187SBen DunbobbinThe linker can be specified with: "-fuse-ld=lld". 47c5c6f187SBen Dunbobbin 48c5c6f187SBen DunbobbinIn the Itanium C++ ABI the first member of an object is a pointer to the vtable 49c5c6f187SBen Dunbobbinfor its class. The vtable is often emitted into the object file with the key function 50c5c6f187SBen Dunbobbinand must be imported for classes marked dllimport. The pointers must be globally 51c5c6f187SBen Dunbobbinunique. Unfortunately, the COFF/PE file format does not provide a mechanism to 52c5c6f187SBen Dunbobbinstore a runtime address from another DLL into this pointer (although runtime 53c5c6f187SBen Dunbobbinaddresses are patched into the IAT). Therefore, the compiler must emit some code, 54c5c6f187SBen Dunbobbinthat runs after IAT patching but before anything that might use the vtable pointers, 55c5c6f187SBen Dunbobbinand sets the vtable pointer to the address from the IAT. For the special case of 56c5c6f187SBen Dunbobbinthe references to vtables for __cxxabiv1::__class_type_info from typeinto objects 57c5c6f187SBen Dunbobbinthere is no declaration available to the compiler so this can't be done. To allow 58c5c6f187SBen Dunbobbinprograms to link we currently rely on the -auto-import switch in LLD to auto-import 59c5c6f187SBen Dunbobbinreferences to __cxxabiv1::__class_type_info pointers (see: https://reviews.llvm.org/D43184 60c5c6f187SBen Dunbobbinfor a related discussion). This allows for linking; but, code that actually uses 61c5c6f187SBen Dunbobbinsuch fields will not work as they these will not be fixed up at runtime. See 62c5c6f187SBen Dunbobbin_pei386_runtime_relocator which handles the runtime component of the autoimporting 63c5c6f187SBen Dunbobbinscheme used for mingw and comments in https://reviews.llvm.org/D43184 and 64c5c6f187SBen Dunbobbinhttps://reviews.llvm.org/D89518 for more. 65c5c6f187SBen Dunbobbin 66c5c6f187SBen DunbobbinAssembling a Toolchain: 67c5c6f187SBen Dunbobbin======================= 68c5c6f187SBen Dunbobbin 69c5c6f187SBen DunbobbinThe procedure is: 70c5c6f187SBen Dunbobbin 71c5c6f187SBen Dunbobbin# Build an LLVM toolchain with support for Windows Itanium. 72c5c6f187SBen Dunbobbin# Use the toolchain from step 1. to build libc++, libc++abi, and libunwind. 73c5c6f187SBen Dunbobbin 74c5c6f187SBen DunbobbinIt is also possible to cross-compile from Linux. 75c5c6f187SBen Dunbobbin 76c5c6f187SBen DunbobbinOne method of building the libraries in step 2. is to build them "stand-alone". 77c5c6f187SBen DunbobbinA stand-alone build doesn't involve the rest of the LLVM tree. The steps are: 78c5c6f187SBen Dunbobbin 79c5c6f187SBen Dunbobbin* ``cd build-dir`` 80c5c6f187SBen Dunbobbin* ``cmake -DLLVM_PATH=<path to llvm checkout e.g. /llvm-project/> -DCMAKE_INSTALL_PREFIX=<install path> <other options> <path to project e.g. /llvm-project/libcxxabi>`` 81c5c6f187SBen Dunbobbin* ``<make program e.g. ninja>`` 82c5c6f187SBen Dunbobbin* ``<make program> install`` 83c5c6f187SBen Dunbobbin 84c5c6f187SBen DunbobbinMore information on standalone builds can be found in the build documentation for 85c5c6f187SBen Dunbobbinthe respective libraries. The next section discuss the salient options and modifications 86c5c6f187SBen Dunbobbinrequired for building and installing the libraries using standalone builds. This assumes 87c5c6f187SBen Dunbobbinthat we are building libunwind and ibc++ as DLLs and statically linking libc++abi into 88c5c6f187SBen Dunbobbinlibc++. Other build configurations are possible, but they are not discussed here. 89c5c6f187SBen Dunbobbin 90c5c6f187SBen DunbobbinCommon CMake configuration options: 91c5c6f187SBen Dunbobbin----------------------------------- 92c5c6f187SBen Dunbobbin 93c5c6f187SBen Dunbobbin* ``-D_LIBCPP_ABI_FORCE_ITANIUM'`` 94c5c6f187SBen Dunbobbin 95c5c6f187SBen DunbobbinTell the libc++ headers that the Itanium C++ ABI is being used. 96c5c6f187SBen Dunbobbin 97c5c6f187SBen Dunbobbin* ``-DCMAKE_C_FLAGS="-lmsvcrt -llegacy_stdio_definitions -D_NO_CRT_STDIO_INLINE"`` 98c5c6f187SBen Dunbobbin 99c5c6f187SBen DunbobbinSupply CRT definitions including stdio definitions that have been removed from the MS VS CRT. 100*5294a0f7SKazu HirataWe don't want the stdio functions declared inline as they will cause multiple definition 101c5c6f187SBen Dunbobbinerrors when the same symbols are pulled in from legacy_stdio_definitions.ib. 102c5c6f187SBen Dunbobbin 103c5c6f187SBen Dunbobbin* ``-DCMAKE_INSTALL_PREFIX=<install path>`` 104c5c6f187SBen Dunbobbin 105c5c6f187SBen DunbobbinWhere to install the library and headers. 106c5c6f187SBen Dunbobbin 107c5c6f187SBen DunbobbinBuilding libunwind: 108c5c6f187SBen Dunbobbin------------------- 109c5c6f187SBen Dunbobbin 110c5c6f187SBen Dunbobbin* ``-DLIBUNWIND_ENABLE_SHARED=ON`` 111c5c6f187SBen Dunbobbin* ``-DLIBUNWIND_ENABLE_STATIC=OFF`` 112c5c6f187SBen Dunbobbin 113c5c6f187SBen Dunbobbinlibunwind can be built as a DLL. It is not dependent on other projects. 114c5c6f187SBen Dunbobbin 115c5c6f187SBen Dunbobbin* ``-DLIBUNWIND_USE_COMPILER_RT=OFF`` 116c5c6f187SBen Dunbobbin 117c5c6f187SBen DunbobbinWe use the MS runtime. 118c5c6f187SBen Dunbobbin 119c5c6f187SBen DunbobbinThe CMake files will need to be edited to prevent them adding GNU specific libraries to the link line. 120c5c6f187SBen Dunbobbin 121c5c6f187SBen DunbobbinBuilding libc++abi: 122c5c6f187SBen Dunbobbin------------------- 123c5c6f187SBen Dunbobbin 124c5c6f187SBen Dunbobbin* ``-DLIBCXXABI_ENABLE_SHARED=OFF`` 125c5c6f187SBen Dunbobbin* ``-DLIBCXXABI_ENABLE_STATIC=ON`` 126c5c6f187SBen Dunbobbin* ``-DLIBCXX_ENABLE_SHARED=ON'`` 127c5c6f187SBen Dunbobbin* ``-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON`` 128c5c6f187SBen Dunbobbin 129c5c6f187SBen DunbobbinTo break the symbol dependency between libc++abi and libc++ we 130c5c6f187SBen Dunbobbinbuild libc++abi as a static library and then statically link it 131c5c6f187SBen Dunbobbininto the libc++ DLL. This necessitates setting the CMake file 132c5c6f187SBen Dunbobbinto ensure that the visibility macros (which expand to dllexport/import) 133c5c6f187SBen Dunbobbinare expanded as they will be needed when creating the final libc++ 134c5c6f187SBen DunbobbinDLL later, see: https://reviews.llvm.org/D90021. 135c5c6f187SBen Dunbobbin 136c5c6f187SBen Dunbobbin* ``-DLIBCXXABI_LIBCXX_INCLUDES=<path to libcxx>/include`` 137c5c6f187SBen Dunbobbin 138c5c6f187SBen DunbobbinWhere to find the libc++ headers 139c5c6f187SBen Dunbobbin 140c5c6f187SBen DunbobbinBuilding libc++: 141c5c6f187SBen Dunbobbin---------------- 142c5c6f187SBen Dunbobbin 143c5c6f187SBen Dunbobbin* ``-DLIBCXX_ENABLE_SHARED=ON`` 144c5c6f187SBen Dunbobbin* ``-DLIBCXX_ENABLE_STATIC=OFF`` 145c5c6f187SBen Dunbobbin 146c5c6f187SBen DunbobbinWe build libc++ as a DLL and statically link libc++abi into it. 147c5c6f187SBen Dunbobbin 148c5c6f187SBen Dunbobbin* ``-DLIBCXX_INSTALL_HEADERS=ON`` 149c5c6f187SBen Dunbobbin 150c5c6f187SBen DunbobbinInstall the headers. 151c5c6f187SBen Dunbobbin 152c5c6f187SBen Dunbobbin* ``-DLIBCXX_USE_COMPILER_RT=OFF`` 153c5c6f187SBen Dunbobbin 154c5c6f187SBen DunbobbinWe use the MS runtime. 155c5c6f187SBen Dunbobbin 156c5c6f187SBen Dunbobbin* ``-DLIBCXX_HAS_WIN32_THREAD_API=ON`` 157c5c6f187SBen Dunbobbin 158c5c6f187SBen DunbobbinWindows Itanium does not offer a POSIX-like layer over WIN32. 159c5c6f187SBen Dunbobbin 160c5c6f187SBen Dunbobbin* ``-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON`` 161c5c6f187SBen Dunbobbin* ``-DLIBCXX_CXX_ABI=libcxxabi`` 162c5c6f187SBen Dunbobbin* ``-DLIBCXX_CXX_ABI_INCLUDE_PATHS=<libcxxabi src path>/include`` 163c5c6f187SBen Dunbobbin* ``-DLIBCXX_CXX_ABI_LIBRARY_PATH=<libcxxabi build path>/lib`` 164c5c6f187SBen Dunbobbin 165c5c6f187SBen DunbobbinUse the static libc++abi library built earlier. 166c5c6f187SBen Dunbobbin 167c5c6f187SBen Dunbobbin* ``-DLIBCXX_NO_VCRUNTIME=ON`` 168c5c6f187SBen Dunbobbin 169c5c6f187SBen DunbobbinRemove any dependency on the VC runtime - we need libc++abi to supply the C++ runtime. 170c5c6f187SBen Dunbobbin 171c5c6f187SBen Dunbobbin* ``-DCMAKE_C_FLAGS=<path to installed unwind.lib>`` 172c5c6f187SBen Dunbobbin 173c5c6f187SBen DunbobbinAs we are statically linking against libcxxabi we need to link 174c5c6f187SBen Dunbobbinagainst the unwind import library to resolve unwind references 175c5c6f187SBen Dunbobbinfrom the libcxxabi objects. 176c5c6f187SBen Dunbobbin 177c5c6f187SBen Dunbobbin* ``-DCMAKE_C_FLAGS+=' -UCLOCK_REALTIME'`` 178c5c6f187SBen Dunbobbin 179c5c6f187SBen DunbobbinPrevent the inclusion of sys/time that MS doesn't provide. 180c5c6f187SBen Dunbobbin 181c5c6f187SBen DunbobbinNotes: 182c5c6f187SBen Dunbobbin------ 183c5c6f187SBen Dunbobbin 184c5c6f187SBen DunbobbinAn example build recipe is available here: https://reviews.llvm.org/D88124 185