doc: explain why passing -mlinker-version is required when cross-compiling #19407

pull fanquake wants to merge 1 commits into bitcoin:master from fanquake:explain_mlinker_version_required changing 1 files +3 −0
  1. fanquake commented at 12:21 PM on June 29, 2020: member

    I have been down a 🐇 hole. Closes #19359.

    When Clang is compiled, a check is run to define HOST_LINK_VERSION as the output of $CMAKE_LINKER -v. Note the this is the version of the linker being used to compile Clang itself.. and this check is only run when compiling Clang for macOS.

    In the Clang driver, if HOST_LINK_VERSION has been defined, there is some additional runtime functionality. An -mlinker-version argument, with the value of HOST_LINK_VERSION will be added to the linker arguments, if -mlinker-version has not been passed in by the user.

    This is a bit weird, as by default, you are setting -mlinker-version to the version of the linker that was used to build the Clang binary, not the linker which will be used when compiling. The commit which introduced the functionality, https://github.com/llvm/llvm-project/commit/628fcf4e3b79ba9f41dd1b49b1aba7a20b68fc0e, described it as a "hack", that should be replaced. However, that was 10 years ago, and the behaviour is still here.

    In the Darwin driver, a check is done for the -mlinker-version argument. If there is no argument, the version will default to 0. Given the above, this should never happen when using Clang for macOS. A series of comparisons are then performed, to check whether the linker version is modern enough to enable certain features, like -demangle.

    What this means

    macOS

    A Clang compiled for macOS, i.e clang+llvm-8.0.0-x86_64-apple-darwin, will have HOST_LINKER_VERSION set to the version of the linker used to compile Clang itself.

    At runtime, -mlinker-version=HOST_LINKER_VERSION will be added to the linker args, if -mlinker-version wasn't passed in. In the Darwin driver, additional arguments, like -demangle, will be added to the linker arguments, because HOST_LINKER_VERSION was likely some very modern version of lld or ld64.

    Linux (cross compilation in depends)

    A Clang compiled for Linux, i.e clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04, which we now use for macOS builds in depends, will behave differently. As it's built for Linux, HOST_LINKER_VERSION was not defined at compile time, and there will be no default behaviour of appending -mlinker-version=HOST_LINKER_VERSION to the linker args. Thus, unless you pass in -mlinker-version yourself, when the version checks are done in the Darwin driver, no modern linker features will be enabled, as the version will have defaulted to 0.

    Therefore, it's important that we continue to pass -mlinker-version="our LD64 version" as part of our compilation flags, if we want to have "modern" linker features enabled for our macOS builds.

    Summary

    Clang 8. Building a macOS binary. Link line with path arguments trimmed.

    default behaviour -mlinker-version=100 (-demangle threshold) -mlinker-version=530
    macOS Clang -demangle -lto_library ../libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.15.0 -o a.out ../test-b8b9b3.o -lc++ -lSystem ../libclang_rt.osx.a -demangle -dynamic -arch x86_64 -macosx_version_min 10.15.0 -o a.out ../test-a66966.o -lc++ -lSystem ../libclang_rt.osx.a same as default
    Linux Clang -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out ../test-bfce57.o -lc++ -lSystem -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out ../test-a846a3.o -lc++ -lSystem -demangle -lto_library ../libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out ../test-de0280.o -lc++ -lSystem

    Note: Most links here are pointing to the 8.x branch of LLVM/Clang, as we are using that version in depends.

    Note: To add a little more confusion, you wont see -mlinker-version X in your compile flags, you'll see -target-linker-version X.

  2. doc: explain why passing -mlinker-version is required a8d39b8840
  3. fanquake added the label Docs on Jun 29, 2020
  4. fanquake added the label Build system on Jun 29, 2020
  5. laanwj commented at 2:31 PM on June 29, 2020: member

    ACK a8d39b88406e2047746366355666b5f603105a2e

    I guess this option isn't mentioned in the MacOS build doc as that doesn't deal with cross-compilation? But, it seems this information would be useful in some documentation. I expect no one to look in that ,mk file unless they've already descended into a another warren of rabbit holes.

  6. DrahtBot commented at 7:06 PM on June 29, 2020: member

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #17919 ([DO NOT MERGE] depends: Allow building with system clang by dongcarl)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  7. fanquake commented at 5:32 AM on June 30, 2020: member

    I guess this option isn't mentioned in the MacOS build doc as that doesn't deal with cross-compilation?

    Correct. When you're building on macOS, for macOS, Apples Clang etc takes care of wiring things up correctly. As I mentioned in my comment here, I think I'll try and combine a more concise version of these notes into some depends documentation.

  8. fanquake merged this on Jul 3, 2020
  9. fanquake closed this on Jul 3, 2020

  10. fanquake deleted the branch on Jul 3, 2020
  11. zkbot referenced this in commit 2a39656e6d on Jul 30, 2020
  12. zkbot referenced this in commit e0692ed4df on Aug 7, 2020
  13. jasonbcox referenced this in commit 07bfa43011 on Oct 2, 2020
  14. deadalnix referenced this in commit bead2c8c43 on Oct 3, 2020
  15. furszy referenced this in commit 816f42d7ac on May 25, 2021
  16. kittywhiskers referenced this in commit b22e3b9e79 on Jul 15, 2021
  17. kittywhiskers referenced this in commit 5e217cb6e8 on Jul 15, 2021
  18. kittywhiskers referenced this in commit 8a3a6f0a90 on Jul 20, 2021
  19. kittywhiskers referenced this in commit bee08fe1b0 on Jul 20, 2021
  20. kittywhiskers referenced this in commit 00bcabfd8d on Jul 20, 2021
  21. kittywhiskers referenced this in commit d84dbb57d5 on Jul 20, 2021
  22. kittywhiskers referenced this in commit ef4a6efc16 on Aug 1, 2021
  23. kittywhiskers referenced this in commit e30f5bdfca on Aug 24, 2021
  24. kittywhiskers referenced this in commit 0a6280fa5f on Aug 24, 2021
  25. kittywhiskers referenced this in commit 6c4d0e17c8 on Aug 25, 2021
  26. kittywhiskers referenced this in commit 8465089eb4 on Aug 25, 2021
  27. kittywhiskers referenced this in commit 35f2f7530f on Aug 26, 2021
  28. kittywhiskers referenced this in commit e499e13282 on Aug 26, 2021
  29. kittywhiskers referenced this in commit 1b53cdaf3c on Aug 26, 2021
  30. kittywhiskers referenced this in commit 346ad72c40 on Aug 27, 2021
  31. kittywhiskers referenced this in commit 7913713c96 on Aug 30, 2021
  32. kittywhiskers referenced this in commit 64477a7d25 on Sep 1, 2021
  33. kittywhiskers referenced this in commit f93e373b41 on Sep 1, 2021
  34. kittywhiskers referenced this in commit fc02611ed1 on Sep 1, 2021
  35. kittywhiskers referenced this in commit 37319e4696 on Sep 1, 2021
  36. kittywhiskers referenced this in commit f2797da1a2 on Sep 2, 2021
  37. kittywhiskers referenced this in commit 3423679cdb on Sep 3, 2021
  38. kittywhiskers referenced this in commit e53f63ce30 on Sep 3, 2021
  39. kittywhiskers referenced this in commit 3fefde95da on Sep 3, 2021
  40. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me