Mach-O: GNU linker script support

Hi all,

I’m working on support for Clang, Mach-O, and GNU linker scripts using LLD. Note: I am not targetting ld64 because the project would like to continue to use conventions for gcc / elf targets.

My end-goal is mainly to support the native_posix_64 target on macOS for Zephyr, which is a topic I will be speaking about at ZDS in a few days. Most of Zephyr’s targets are ELF and use gcc or llvm toolchains, and we would like to continue to use llvm for as close as a drop-in-replacement-as-possible for our purposes.

I posted about it a while ago and have some related work on adding -fhash-section-names=N and -fhashed-section-names=outfile on clang / clang++. The compiler frontend changes are working quite well, although there is room for improvement. The related change is here, and an example of the result is here.

The hash is one-way for all practical purposes and follows this (I’m quite flexible if there is a better, reversible alternative)

  • SHA256
  • Base64
  • Truncate to N

For, LLD, my planned changes are:

  1. add a corresponding -hashed-section-names=infile option (to reverse the hash)
  2. ~fork the ELF implementation and brute-force it to support Mach-O, potentially recycling some of the existing Mach-O implementation
  3. ~fork the linker script parser / impl and brute-force it to support with Mach-O binaries
  4. update the relevant config bits to enable parsing GNU linker scripts for the new target

Likely a lot of the brute-forcing will take several iterations to become clear and concise, but eventually, I would like to ensure that the work can either be merged back into the original subproject(s).

Just wondering if any of the LLD developers have any suggestions before I dive in.

Hope you are well,

C

Related discussion: Feedback on feature (and status of lld linker script support?) - #7 by smithp35

I’d hope that your needs can be satisfied without adding linker script support to the Mach-O support.

One possibility is whether you can use the ELF port and then transform the ELF ET_EXEC/ET_DYN file to a Mach-O file. See Plan 9 a.out executables with lld - #4 by smithp35 for a similar suggestion for a.out
While ELF is mostly a super set of a.out, Mach-O has many features not in ELF. You may need some placeholders in the linker script (e.g. . += 16 to reserve 16 bytes) then use a post-processing tool to fill the content.

| MaskRay
June 5 |

  • | - |

Related discussion: Feedback on feature (and status of lld linker script support?) - #7 by smithp35

I’d hope that your needs can be satisfied without adding linker script support to the Mach-O support.

Yes, I have no intention of modifying ld64, since it’s intended behaviour is to mimic that of Apple’s ld. The aim is to masquerade this new linker as if it were working with ELF binaries while it is really doing the work on Mach-O files. Underneath, it will obviously still link into Apple’s system libraries of course.