Skip to content

macho: big-ish refactor and report errors to the user using Zig's API #17020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Aug 31, 2023

Conversation

kubkon
Copy link
Member

@kubkon kubkon commented Aug 30, 2023

In summary, this PR implements some long-overdue updates. It simplifies input file parsing - we now do a pre-check read of the input object header to work out what we are dealing with, and then parse in full. This greatly simplified the parsing decision logic, although there is still lots of room for improvement!

State and logic that should be unified between drivers is now unified. This includes input file parsing and symbol resolution which do not differ almost at all between incremental and zld mode, but excludes things like section handling where in incremental mode we preallocate them upfront and in zld mode we don't bother with size calculation until after we resolve symbols, dead strip, etc.

The zld mode now also uses TableSection abstraction to track synthetics such as GOT cells, TLV pointers, stub routines, etc. This has cleaned up committing zld linker state to file significantly since we can now handle writing of the synthetics separately.

Logic responsible for committing __LINKEDIT data has been unified since it is also shared between drivers in 100% - this includes things like dyld opcodes, export trie, symtabs, etc.

In order to make error handling practical, I have removed Zld struct, pruned it and merged what was left with MachO struct. This way, we also refer to an instance of MachO as context, and we can use across drivers to report user errors with as little hassle as possible.

And yes, MachO now reports simplistic linker errors to the user via the Zig's error API. Here's a few screenshots of what it now looks like:

image
image
image

While working on the error handling, I thought prudent to teach the linker how to parse LC_BUILD_VERSION and LC_VERSION_MIN_* load commands so that we can flag any target mismatch to the user. Oh, and I have ported logic for working out which load command to emit based on the min OS version from Apple's ld64. Perhaps won't be of great use to us just yet, but opens the door, however slightly, for older macOS versions support.

kubkon added 30 commits August 29, 2023 11:39
Write thunks separately from other atoms - this can still be improved
by not using atoms at all, but one thing at a time.
kubkon added 26 commits August 29, 2023 11:40
Eventually, we will validate DWARF info upfront and report errors
to the user but this will require a rewrite of several parts of
the linker so leaving as a TODO for the near future.
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work. I'm very pleased with this.

@kubkon kubkon merged commit f4c9e19 into master Aug 31, 2023
@kubkon kubkon deleted the macho-versions branch August 31, 2023 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants