325 Commits

Author SHA1 Message Date
Jakub Konka
705cd64080 macho: preactively add zerofill sections in correct order
The required order on macOS nowadays is:
* __data
* __thread_vars
* __thread_data
* __thread_bss
* __bss
2021-08-27 10:45:56 +02:00
Jakub Konka
8bd0687713 macho: port mechanism for allocating symbols within atoms
such as entire sections represented as a single atom.
2021-08-26 16:16:34 +02:00
Jakub Konka
570c75cb74 macho: write all atoms in flush so that we can resolve relocs 2021-08-26 14:23:28 +02:00
Jakub Konka
432fb7054d macho: fix stub writing in self-hosted setting 2021-08-26 13:45:28 +02:00
Jakub Konka
d19d3342c2 macho: save lazy binding info as part of the atom 2021-08-26 11:43:11 +02:00
Jakub Konka
c12183b608 macho: fix text atom allocation 2021-08-25 23:00:58 +02:00
Jakub Konka
ee786e5c3c macho: add GOT entries as actual atoms 2021-08-25 17:23:30 +02:00
Jakub Konka
af57ccbe27 macho: generalise free list usage to all sections 2021-08-25 15:11:21 +02:00
Jakub Konka
ea4bd2b879 macho: add routine for creating Got atoms 2021-08-25 10:51:09 +02:00
Jakub Konka
aee6f14bce macho: use existing rebase mechanism to rebase lazy pointers 2021-08-24 20:16:34 +02:00
Jakub Konka
32ce8238a8 macho: rewrite populateLazyBindOffsetsInStubHelper to use atoms
Instead of referencing stub indices since these can now be obtained
in a more generic fashion from the actual linked-list of atoms in
the __stub_helper section.
2021-08-24 15:19:28 +02:00
Jakub Konka
8d30092704 macho: port stub and lazy ptr atoms to stage2 2021-08-24 13:16:43 +02:00
Jakub Konka
91c0552cfc macho: add routine for creating stubs in __stubs section 2021-08-24 00:03:51 +02:00
Jakub Konka
876071b50b macho: fix writing stubs (on x86_64 only) and lazy ptrs 2021-08-23 23:23:00 +02:00
Jakub Konka
9e7b2fb894 macho: add routine for creating lazy pointer for stub 2021-08-23 23:01:14 +02:00
Jakub Konka
799c5bb955 macho: add routine for creating a dynamic stub_helper atom
With this routine, we are now able to freely shift stub_helper
section in memory and in file since the VM addressing is now dynamically
dependent on the positioning of `__stub_helper` preamble and other
sections generated by the linker.
2021-08-23 22:12:04 +02:00
Jakub Konka
ea499203fe macho: incrementally write dyld_private and stub_helper atoms
By incrementally I mean using the incremental linker machinery
and concepts. Currently, lots of repetition but already highlighted
a potential problem with resolving relocations for symbols that
weren't seen yet but wanting to write the atom to file (before
seeing the relevant atoms).
2021-08-21 22:26:02 +02:00
Jakub Konka
d61d85abd2 macho: implement aarch64 prong of createStubHelperAtom 2021-08-21 18:55:15 +02:00
Jakub Konka
d63d8ae1c0 macho: create __stub_helper preamble atom
with relocations to `dyld_private` and `__dyld_stub_binder` symbols
making the routine properly dynamic (i.e., making it possible to call
the routine before VM allocation takes place).
2021-08-21 14:21:55 +02:00
Jakub Konka
2d10c52b3c macho: create an explicit symbol and atom for dyld entry in __data
This way, we will be able to refer to that atom dynamically via
relocations rather than requiring hardcoded addressing upfront.
2021-08-21 11:51:50 +02:00
Jakub Konka
792fd9c4a3 macho: extract logic for creating and tracking atoms into fn 2021-08-20 09:49:21 +02:00
Jakub Konka
153e231774 macho: track unresolved externs globally
this way we share state between incremental and traditional paths.
2021-08-20 09:12:26 +02:00
Jakub Konka
30247fbb6a macho: remove redundant writeStubHelperCommon codepath 2021-08-18 11:46:45 +02:00
Jakub Konka
8167456c58 macho: resolve undefs in incremental properly
Instead of assuming that every undef extern symbol comes from libSystem,
actually perform the check!
2021-08-18 00:17:03 +02:00
Jakub Konka
790633a2a0 macho: use common codepath for resolving dyld_stub_binder 2021-08-17 22:10:07 +02:00
Jakub Konka
8b795fe2ac macho: parse input files and libs in incremental
This converges parsing of linker line in incremental; however,
still doesn't save the parsing state between updates.
2021-08-17 19:49:17 +02:00
Jakub Konka
69f4281774 macho: memorize if dyld_stub_binder was already resolved 2021-08-17 18:33:41 +02:00
Jakub Konka
c6ea181e75 macho: dedup setting entry point logic 2021-08-17 10:54:10 +02:00
Jakub Konka
52f9b283a7 macho: fix writeSymbolTable() function
After merging `populateMetadata` with `populateMissingMetadata`,
it is imperative we clear the number of symbols in `writeSymbolTable`.
However, this is hopefully just a temp measure until the convergence
of incremental with traditional is complete.
2021-08-16 19:58:05 +02:00
Jakub Konka
a51edc978f macho: converge populateMetadata with populateMissingMetadata 2021-08-16 19:15:04 +02:00
Jakub Konka
4c90c1ff63 Merge remote-tracking branch 'origin/master' into zld-incr 2021-08-16 15:24:20 +02:00
Jakub Konka
e2303840de Fix AST and build errors 2021-08-15 23:50:39 +02:00
Jakub Konka
d5f7963331 macho: adhoc code sign binaries targeting aarch64-xxx-simulator 2021-08-15 18:29:19 +02:00
Jakub Konka
f82c26eb04 macho: don't embed codesig unless targeting aarch64-macos
When developing an iOS app for example, the developer is required
to use Apple's codesign utility to generate a valid signature
as done by Xcode.
2021-08-15 18:07:30 +02:00
Jakub Konka
b20b6d7da9 macho: move bit adding rpaths to common codepath 2021-08-14 22:18:18 +02:00
Jakub Konka
819ef52104 macho: merge linkWithZld with flush 2021-08-14 21:56:41 +02:00
Jakub Konka
c53423f8aa macho: don't cache unused link options
This will change incrementally once we support more linker options.
2021-08-13 10:46:53 +02:00
Jakub Konka
f49aa960a9 macho: use ArrayHashMap.popOrNull where applicable 2021-08-12 10:20:57 +02:00
Jakub Konka
f01366e8b3 macho: exclude tentative def before checking for collision
when resolving global symbols.
2021-08-12 10:20:57 +02:00
Jakub Konka
35403d41ce macho: use array hashmaps for quick lookups
as containers for unresolved and tentative definitions when resolving
symbols.
2021-08-12 10:20:57 +02:00
Jakub Konka
da57d6df32 macho: simplify symbol management and resolution
instead of globally storing unresolved and tentative defs,
store indices to actual symbols in the functions that are responsible
for symbol resolution.
2021-08-12 10:20:57 +02:00
Jakub Konka
5d548cc651 macho: move parsing logic for Object, Archive and Dylib into MachO
This way, the functionality is better segregated, and we finally do
not unnecessarily reparse dynamic libraries that were already visited
and parsed.
2021-08-11 19:38:00 +02:00
Jakub Konka
2371a63bd4 macho: allow .simulator ABI when targeting Apple simulator env
For example, in order to run a binary on an iPhone Simulator,
you need to specify that explicitly as part of the target as
`aarch64-ios-simulator` rather than `aarch64-ios-gnu` or
`aarch64-ios` for short.
2021-08-10 13:41:10 +02:00
Jakub Konka
7007684984 macho: swap out VERSION_MIN for BUILD_VERSION
this makes the app successfully run on the iOS simluator!
2021-08-10 13:41:10 +02:00
Jakub Konka
ace9b3de64 macho: fix parsing target string when linking against tbds 2021-08-10 13:41:07 +02:00
Jakub Konka
049ff45430 macho: add basic support for building iOS binaries
* ensure we correctly transfer `-iwithsysroot` and
  `-iframeworkwithsysroot` flags with values from `build.zig` and that
  they are correctly transferred forward to `zig cc`
* try to look for `libSystem.tbd` in the provided syslibroot - one
  caveat that the user will have to specify library search paths too
2021-08-10 13:40:39 +02:00
Jakub Konka
f2bf1390a2 macho: fix linking of dylibs and frameworks
Previously, I have incorrectly assumed that with two-level namespace
we only need to link in dylibs/frameworks that actually export symbols
which are undefined in the linked image. Turns out, regardless of
whether we link with two-level namespace (default on macOS) or a
flat namespace (more common on other platforms), we always need to
put the dylibs/frameworks as specified by the user from the linker
line into the final linked image.
2021-08-10 08:13:07 +02:00
Jakub Konka
41d7787b69 macho: remove obsolete pack/unpack dylib ordinal fns
Remove some unused debugging machinery such as full printing of the
symtab after symbol resolution. It was there only for the time of
rewriting the linker.
2021-08-02 19:49:32 +02:00
Jakub Konka
bf25650974 macho: refactor management of section ordinals
Instead of storing a two-way relation (seg,sect) <=> ordinal
we get the latter with `getIndex((seg, sect))`.
2021-08-02 19:49:32 +02:00
Jakub Konka
f3b328ee8c macho: refactor tracking of referenced dylibs
Now, index in the global referenced array hashmap is equivalent to
the dylib's ordinal in the final linked image.
2021-08-02 19:49:32 +02:00