Skip to content

Link basic MachO executables with stage2 #6476

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 9 commits into from
Oct 4, 2020
Merged

Conversation

kubkon
Copy link
Member

@kubkon kubkon commented Sep 30, 2020

This PR adds enough of MachO linker implementation to pass basic stage2 "Hello, world!" test.

A couple of things left before I'll feel happy about merging this in:

  • refactor free space analysis - currently, it's a little bit hack-ish due to the weird structure of MachO meta info
    - [ ] add basic export trie generation algorithm - currently, this bit is 100% hard-coded
  • move exe-only bits behind a switch/if statement in linker

EDIT: I really want this merged and working out a good algorithm for export trie will take some time (and potentially changes to the current codebase anyhow), so I'm inclined to leave it as is, i.e., it currently assumes that _start symbol is there and is the only exported symbol. In terms of free space analysis, I'm still not 100% happy with how things are done, but after more refactoring, it's definitely better than it was, and in subsequent PRs I'll revisit this bit and see if I can come up with a better alternative to how it's done now. Oh, and this mainly applies to Exes since Objs are a lot simpler in MachO wrt segment/section(s) organisation.

@kubkon kubkon added os-macos frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Sep 30, 2020
@jedisct1
Copy link
Contributor

Exciting stuff! 👍

@kubkon kubkon marked this pull request as ready for review October 2, 2020 19:29
@kubkon
Copy link
Member Author

kubkon commented Oct 2, 2020

OK, so I think this is ready for review.

kubkon added 9 commits October 4, 2020 15:31
* Convert draft to generate all relevant segments and sections in right places
* Do not prealloc space in text blocks until we can NOP
* Write out LC_LOAD_DYLINKER command
* Add LC_LOAD_DYLIB command in order to specify to load libSystem
* Redo update decl exports (similar to Elf globals, globals need to be contiguous in
  memory)
Also, rewrites codegen section to store symbol address in a register
to then later invoke `callq` on the register.
This commit refactors free space analysis (and VM analysis) somewhat.
This is still far from perfect, but at least it's not using any
hardcoded values.

This commit also reorganizes different flushing bits here and there,
so that bit common to Exe and Obj are put in the common path, while
Exe-specific are behind an appropriate switch/if statement.
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.

Nice work! You've really embarked on quite a journey here.

Does it work if you add another case.addCompareOutput here and change the printed text to a new string?

\\}
\\
\\fn print() void {
\\ asm volatile ("syscall"
Copy link
Member

Choose a reason for hiding this comment

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

I'm on board with using this for now in the early stages of development, but the end goal here is to use libSystem for calling write() and exit() right?

Also the plan for the entry point on macos is to expose main() instead of _start:

@export(main, .{ .name = "main", .linkage = .Weak });

Copy link
Member Author

Choose a reason for hiding this comment

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

Excellent point. Currently, this won't work since I've got the export trie hardcoded to only encode the _start symbol; however, I'm working on an actual mechanism for auto-generating it from global exports as we speak.

@andrewrk andrewrk merged commit 6aa668e into ziglang:master Oct 4, 2020
@kubkon
Copy link
Member Author

kubkon commented Oct 5, 2020

Nice work! You've really embarked on quite a journey here.

Does it work if you add another case.addCompareOutput here and change the printed text to a new string?

No, I don't think so. Or put it another way, I haven't tested it yet, plus I'm still working on the incremental linking bit. But soon... 😈

@kubkon kubkon deleted the macho-exe branch October 5, 2020 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. os-macos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants